mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] add memcmpeq: memcmp that returns length of first mismatch
@ 2024-02-27 14:07 James Tirta Halim
  2024-02-27 14:49 ` Rich Felker
  0 siblings, 1 reply; 10+ messages in thread
From: James Tirta Halim @ 2024-02-27 14:07 UTC (permalink / raw)
  To: musl; +Cc: James Tirta Halim

(unaligned access must be supported for mempcmpeq if using word access)

mempcmpeq returns the length of the first mismatched byte. If S1 and S2
are equal, n is returned.

The function is meant to be used internally in strstr and memmem:
https://inbox.vuxu.org/musl/20181108193451.GD5150@brightrain.aerifal.cx/

glibc bench-memcmpeq timings (Core i3-1115G4):
mempcmpeq memcmp_musl memcmp __memcmpeq_evex __memcmpeq_avx2 __memcmpeq_sse2
Average:
62.3978 269.813 16.0426 14.9072 14.1125 20.6527
Total:
30637.3 132478 7876.92 7319.43 6929.23 10140.5

Passes glibc test-memcmpeq.

Return value in testing and benchmarking is changed to suit the
behavior of memcmpeq.

---
 src/string/mempcmpeq.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 src/string/mempcmpeq.c

diff --git a/src/string/mempcmpeq.c b/src/string/mempcmpeq.c
new file mode 100644
index 00000000..bc17bc58
--- /dev/null
+++ b/src/string/mempcmpeq.c
@@ -0,0 +1,19 @@
+#include <stddef.h>
+
+size_t
+mempcmpeq(const void *s1,
+          const void *s2,
+          size_t n)
+{
+	const size_t length = n;
+#ifdef __GNUC__
+	typedef size_t __attribute__((__may_alias__)) word;
+	const unsigned char *p1 = (const unsigned char *)s1;
+	const unsigned char *p2 = (const unsigned char *)s2;
+	for (; n >= sizeof(word) && *(word *)p1 == *(word *)p2; p1+=sizeof(word), p2+=sizeof(word), n-=sizeof(word));
+#endif
+	for (; n; --n)
+		if (*p1++ != *p2++)
+			return (size_t)((p1 - 1 - (const unsigned char *)s1));
+	return length;
+}
-- 
2.44.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-02-29  4:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-27 14:07 [musl] [PATCH] add memcmpeq: memcmp that returns length of first mismatch James Tirta Halim
2024-02-27 14:49 ` Rich Felker
2024-02-27 14:51   ` Rich Felker
2024-02-28  4:40     ` Markus Wichmann
2024-02-28 23:14       ` Thorsten Glaser
2024-02-29  0:02         ` Pedro Falcato
2024-02-29  0:10           ` Thorsten Glaser
2024-02-29  0:57             ` Robert Clausecker
2024-02-29  1:13               ` Pedro Falcato
2024-02-29  4:19         ` Markus Wichmann

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).