public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sysdeps/memmem-avx2.c: add memmem-avx2.c
@ 2023-12-11 17:33 James Tirta Halim
  2023-12-11 17:44 ` Sunil Pandey
  0 siblings, 1 reply; 9+ messages in thread
From: James Tirta Halim @ 2023-12-11 17:33 UTC (permalink / raw)
  To: libc-alpha; +Cc: James Tirta Halim

---
 sysdeps/x86_64/multiarch/memmem-avx2.c | 55 ++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 sysdeps/x86_64/multiarch/memmem-avx2.c

diff --git a/sysdeps/x86_64/multiarch/memmem-avx2.c b/sysdeps/x86_64/multiarch/memmem-avx2.c
new file mode 100644
index 0000000000..b0cced73aa
--- /dev/null
+++ b/sysdeps/x86_64/multiarch/memmem-avx2.c
@@ -0,0 +1,55 @@
+#include <immintrin.h>
+#include <string.h>
+#include <inttypes.h>
+#include <libc-pointer-arith.h>
+
+void *
+__memmem_avx2 (const void *hs, size_t hs_len, const void *ne, size_t ne_len)
+{
+  if (ne_len == 1)
+    return (void *) memchr (hs, *(unsigned char *) ne, hs_len);
+  if (__glibc_unlikely (ne_len == 0))
+    return (void *) hs;
+  if (__glibc_unlikely (hs_len == ne_len))
+    return !memcmp (hs, ne, ne_len) ? (void *) hs : NULL;
+  if (__glibc_unlikely (hs_len < ne_len))
+    return NULL;
+  const __m256i nv = _mm256_set1_epi8 (*(char *) ne);
+  const unsigned char *h = (const unsigned char *) hs;
+  const unsigned char *n = (const unsigned char *) ne;
+  const unsigned char *const end = h + hs_len - ne_len;
+  const int c1 = *(n + 1);
+  n += 2, ne_len -= 2;
+  __m256i hv;
+  uint32_t i, m;
+  if (!PTR_IS_ALIGNED (h)) {
+    hv = _mm256_loadu_si256 ((const __m256i *) h);
+    m = (uint32_t) _mm256_movemask_epi8 (_mm256_cmpeq_epi8 (hv, nv));
+    for (; m; m = _blsr_u32 (m)) {
+      i = _tzcnt_u32 (m);
+      if (__glibc_unlikely (h + i > end))
+        return NULL;
+      if (*(h + i + 1) == c1 && !memcmp (h + i + 2, n, ne_len))
+        return (char *) h + i;
+    }
+    h += sizeof (__m256i);
+    if (__glibc_unlikely (h > end))
+      return NULL;
+    h = (const unsigned char *) PTR_ALIGN_UP (h, sizeof (__m256i));
+  }
+  for (;;) {
+    hv = _mm256_load_si256 ((const __m256i *) h);
+    m = (uint32_t) _mm256_movemask_epi8 (_mm256_cmpeq_epi8 (hv, nv));
+    for (; m; m = _blsr_u32 (m)) {
+      i = _tzcnt_u32 (m);
+      if (__glibc_unlikely (h + i > end))
+        return NULL;
+      if (*(h + i + 1) == c1 && !memcmp (h + i + 2, n, ne_len))
+        return (char *) h + i;
+    }
+    h += sizeof (__m256i);
+    if (__glibc_unlikely (h > end))
+      return NULL;
+  }
+  return NULL;
+}
-- 
2.43.0


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

end of thread, other threads:[~2023-12-18 23:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-11 17:33 [PATCH] sysdeps/memmem-avx2.c: add memmem-avx2.c James Tirta Halim
2023-12-11 17:44 ` Sunil Pandey
2023-12-11 18:11   ` Noah Goldstein
2023-12-15 17:03     ` James Tirta Halim
2023-12-15 19:53       ` Carlos O'Donell
2023-12-16  4:33         ` [PATCH] sysdeps/x86_64/multiarch/memmem-avx2.c: " James Tirta Halim
2023-12-18 14:12           ` Carlos O'Donell
2023-12-18 17:48           ` Noah Goldstein
2023-12-18 23:08             ` James

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 read-only IMAP folder(s) and NNTP newsgroup(s).