2009-07-03 09:48:56 +00:00
|
|
|
/* strcspn with SSE4.2 intrinsics
|
2022-01-01 18:54:23 +00:00
|
|
|
Copyright (C) 2009-2022 Free Software Foundation, Inc.
|
2009-07-03 09:48:56 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2009-07-03 09:48:56 +00:00
|
|
|
|
|
|
|
#include <nmmintrin.h>
|
|
|
|
#include <string.h>
|
2010-08-24 18:35:01 +00:00
|
|
|
#include "varshift.h"
|
2009-07-03 09:48:56 +00:00
|
|
|
|
|
|
|
/* We use 0x2:
|
|
|
|
_SIDD_SBYTE_OPS
|
|
|
|
| _SIDD_CMP_EQUAL_ANY
|
|
|
|
| _SIDD_POSITIVE_POLARITY
|
|
|
|
| _SIDD_LEAST_SIGNIFICANT
|
|
|
|
on pcmpistri to compare xmm/mem128
|
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
X X X X X X X X X X X X X X X X
|
|
|
|
|
|
|
|
against xmm
|
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
A A A A A A A A A A A A A A A A
|
|
|
|
|
|
|
|
to find out if the first 16byte data element has any byte A and
|
|
|
|
the offset of the first byte. There are 3 cases:
|
|
|
|
|
|
|
|
1. The first 16byte data element has the byte A at the offset X.
|
|
|
|
2. The first 16byte data element has EOS and doesn't have the byte A.
|
|
|
|
3. The first 16byte data element is valid and doesn't have the byte A.
|
|
|
|
|
|
|
|
Here is the table of ECX, CFlag, ZFlag and SFlag for 2 cases:
|
|
|
|
|
|
|
|
1 X 1 0/1 0
|
|
|
|
2 16 0 1 0
|
|
|
|
3 16 0 0 0
|
|
|
|
|
|
|
|
We exit from the loop for cases 1 and 2 with jbe which branches
|
|
|
|
when either CFlag or ZFlag is 1. If CFlag == 1, ECX has the offset
|
|
|
|
X for case 1. */
|
|
|
|
|
|
|
|
#ifndef STRCSPN_SSE2
|
2009-07-03 10:23:01 +00:00
|
|
|
# define STRCSPN_SSE2 __strcspn_sse2
|
|
|
|
# define STRCSPN_SSE42 __strcspn_sse42
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_AS_STRPBRK
|
|
|
|
# define RETURN(val1, val2) return val1
|
|
|
|
#else
|
|
|
|
# define RETURN(val1, val2) return val2
|
2009-07-03 09:48:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern
|
|
|
|
#ifdef USE_AS_STRPBRK
|
|
|
|
char *
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
#endif
|
2017-08-19 23:46:33 +00:00
|
|
|
STRCSPN_SSE2 (const char *, const char *) attribute_hidden;
|
2009-07-03 09:48:56 +00:00
|
|
|
|
2009-07-03 10:23:01 +00:00
|
|
|
|
2009-07-03 09:48:56 +00:00
|
|
|
#ifdef USE_AS_STRPBRK
|
|
|
|
char *
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
#endif
|
|
|
|
__attribute__ ((section (".text.sse4.2")))
|
|
|
|
STRCSPN_SSE42 (const char *s, const char *a)
|
|
|
|
{
|
|
|
|
if (*a == 0)
|
2009-07-03 10:23:01 +00:00
|
|
|
RETURN (NULL, strlen (s));
|
2009-07-03 09:48:56 +00:00
|
|
|
|
2009-07-03 10:23:01 +00:00
|
|
|
const char *aligned;
|
2022-03-23 21:57:22 +00:00
|
|
|
__m128i mask, maskz, zero;
|
|
|
|
unsigned int maskz_bits;
|
|
|
|
unsigned int offset = (unsigned int) ((size_t) a & 15);
|
|
|
|
zero = _mm_set1_epi8 (0);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (offset != 0)
|
|
|
|
{
|
|
|
|
/* Load masks. */
|
2009-08-04 19:13:43 +00:00
|
|
|
aligned = (const char *) ((size_t) a & -16L);
|
2009-07-03 10:23:01 +00:00
|
|
|
__m128i mask0 = _mm_load_si128 ((__m128i *) aligned);
|
2022-03-23 21:57:22 +00:00
|
|
|
maskz = _mm_cmpeq_epi8 (mask0, zero);
|
2009-07-03 09:48:56 +00:00
|
|
|
|
|
|
|
/* Find where the NULL terminator is. */
|
2022-03-23 21:57:22 +00:00
|
|
|
maskz_bits = _mm_movemask_epi8 (maskz) >> offset;
|
|
|
|
if (maskz_bits != 0)
|
|
|
|
{
|
|
|
|
mask = __m128i_shift_right (mask0, offset);
|
|
|
|
offset = (unsigned int) ((size_t) s & 15);
|
|
|
|
if (offset)
|
|
|
|
goto start_unaligned;
|
|
|
|
|
|
|
|
aligned = s;
|
|
|
|
goto start_loop;
|
|
|
|
}
|
2009-07-03 09:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 21:57:22 +00:00
|
|
|
/* A is aligned. */
|
|
|
|
mask = _mm_loadu_si128 ((__m128i *) a);
|
|
|
|
/* Find where the NULL terminator is. */
|
|
|
|
maskz = _mm_cmpeq_epi8 (mask, zero);
|
|
|
|
maskz_bits = _mm_movemask_epi8 (maskz);
|
|
|
|
if (maskz_bits == 0)
|
|
|
|
{
|
|
|
|
/* There is no NULL terminator. Don't use SSE4.2 if the length
|
|
|
|
of A > 16. */
|
|
|
|
if (a[16] != 0)
|
|
|
|
return STRCSPN_SSE2 (s, a);
|
2009-07-03 09:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 21:57:22 +00:00
|
|
|
aligned = s;
|
|
|
|
offset = (unsigned int) ((size_t) s & 15);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (offset != 0)
|
|
|
|
{
|
2022-03-23 21:57:22 +00:00
|
|
|
start_unaligned:
|
2009-07-03 09:48:56 +00:00
|
|
|
/* Check partial string. */
|
2009-08-04 19:13:43 +00:00
|
|
|
aligned = (const char *) ((size_t) s & -16L);
|
2009-07-03 10:23:01 +00:00
|
|
|
__m128i value = _mm_load_si128 ((__m128i *) aligned);
|
2009-07-03 09:48:56 +00:00
|
|
|
|
2010-08-24 18:35:01 +00:00
|
|
|
value = __m128i_shift_right (value, offset);
|
2009-07-03 09:48:56 +00:00
|
|
|
|
2022-03-23 21:57:22 +00:00
|
|
|
unsigned int length = _mm_cmpistri (mask, value, 0x2);
|
2009-07-03 09:48:56 +00:00
|
|
|
/* No need to check ZFlag since ZFlag is always 1. */
|
2022-03-23 21:57:22 +00:00
|
|
|
unsigned int cflag = _mm_cmpistrc (mask, value, 0x2);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (cflag)
|
2009-07-03 10:23:01 +00:00
|
|
|
RETURN ((char *) (s + length), length);
|
2009-07-03 09:48:56 +00:00
|
|
|
/* Find where the NULL terminator is. */
|
2022-03-23 21:57:22 +00:00
|
|
|
unsigned int index = _mm_cmpistri (value, value, 0x3a);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (index < 16 - offset)
|
2009-07-03 10:23:01 +00:00
|
|
|
RETURN (NULL, index);
|
2009-07-03 09:48:56 +00:00
|
|
|
aligned += 16;
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:57:22 +00:00
|
|
|
start_loop:
|
2009-07-03 10:23:01 +00:00
|
|
|
while (1)
|
2009-07-03 09:48:56 +00:00
|
|
|
{
|
2009-07-03 10:23:01 +00:00
|
|
|
__m128i value = _mm_load_si128 ((__m128i *) aligned);
|
2022-03-23 21:57:22 +00:00
|
|
|
unsigned int index = _mm_cmpistri (mask, value, 0x2);
|
|
|
|
unsigned int cflag = _mm_cmpistrc (mask, value, 0x2);
|
|
|
|
unsigned int zflag = _mm_cmpistrz (mask, value, 0x2);
|
2009-07-03 10:23:01 +00:00
|
|
|
if (cflag)
|
|
|
|
RETURN ((char *) (aligned + index), (size_t) (aligned + index - s));
|
|
|
|
if (zflag)
|
|
|
|
RETURN (NULL,
|
|
|
|
/* Find where the NULL terminator is. */
|
|
|
|
(size_t) (aligned + _mm_cmpistri (value, value, 0x3a) - s));
|
|
|
|
aligned += 16;
|
2009-07-03 09:48:56 +00:00
|
|
|
}
|
|
|
|
}
|