public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Sunil Pandey <skpgkp2@gmail.com>
To: James Tirta Halim <tirtajames45@gmail.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] sysdeps/memmem-avx2.c: add memmem-avx2.c
Date: Mon, 11 Dec 2023 09:44:32 -0800	[thread overview]
Message-ID: <CAMAf5_edePM69BxZ1tSFRBc6024O6KBaoFopjUj2qF0GnSpDDA@mail.gmail.com> (raw)
In-Reply-To: <20231211173320.702246-1-tirtajames45@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2610 bytes --]

Can you please provide some context/comment on this patch?

--Sunil

On Mon, Dec 11, 2023 at 9:37 AM James Tirta Halim <tirtajames45@gmail.com>
wrote:

> ---
>  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
>
>

  reply	other threads:[~2023-12-11 17:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-11 17:33 James Tirta Halim
2023-12-11 17:44 ` Sunil Pandey [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMAf5_edePM69BxZ1tSFRBc6024O6KBaoFopjUj2qF0GnSpDDA@mail.gmail.com \
    --to=skpgkp2@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=tirtajames45@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).