2009-07-03 09:48:56 +00:00
|
|
|
/* strspn with SSE4.2 intrinsics
|
2018-01-01 00:32:25 +00:00
|
|
|
Copyright (C) 2009-2018 Free Software Foundation, Inc.
|
2009-07-03 09:48:56 +00:00
|
|
|
Contributed by Intel Corporation.
|
|
|
|
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
|
|
|
|
<http://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 0x12:
|
|
|
|
_SIDD_SBYTE_OPS
|
|
|
|
| _SIDD_CMP_EQUAL_ANY
|
|
|
|
| _SIDD_NEGATIVE_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 non-A byte and
|
|
|
|
the offset of the first byte. There are 2 cases:
|
|
|
|
|
|
|
|
1. The first 16byte data element has the non-A byte, including
|
|
|
|
EOS, at the offset X.
|
|
|
|
2. The first 16byte data element is valid and doesn't have the non-A
|
|
|
|
byte.
|
|
|
|
|
|
|
|
Here is the table of ECX, CFlag, ZFlag and SFlag for 2 cases:
|
|
|
|
|
|
|
|
case ECX CFlag ZFlag SFlag
|
|
|
|
1 X 1 0/1 0
|
|
|
|
2 16 0 0 0
|
|
|
|
|
|
|
|
We exit from the loop for case 1. */
|
|
|
|
|
2017-08-19 23:46:33 +00:00
|
|
|
extern size_t __strspn_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
|
|
|
size_t
|
|
|
|
__attribute__ ((section (".text.sse4.2")))
|
|
|
|
__strspn_sse42 (const char *s, const char *a)
|
|
|
|
{
|
|
|
|
if (*a == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-07-03 10:23:01 +00:00
|
|
|
const char *aligned;
|
|
|
|
__m128i mask;
|
|
|
|
int offset = (int) ((size_t) a & 15);
|
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);
|
2009-07-03 09:48:56 +00:00
|
|
|
|
2010-08-24 18:35:01 +00:00
|
|
|
mask = __m128i_shift_right (mask0, offset);
|
2009-07-03 09:48:56 +00:00
|
|
|
|
|
|
|
/* Find where the NULL terminator is. */
|
2009-07-03 10:23:01 +00:00
|
|
|
int length = _mm_cmpistri (mask, mask, 0x3a);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (length == 16 - offset)
|
|
|
|
{
|
|
|
|
/* There is no NULL terminator. */
|
2009-07-03 10:23:01 +00:00
|
|
|
__m128i mask1 = _mm_load_si128 ((__m128i *) (aligned + 16));
|
|
|
|
int index = _mm_cmpistri (mask1, mask1, 0x3a);
|
2009-07-03 09:48:56 +00:00
|
|
|
length += index;
|
|
|
|
|
|
|
|
/* Don't use SSE4.2 if the length of A > 16. */
|
|
|
|
if (length > 16)
|
|
|
|
return __strspn_sse2 (s, a);
|
|
|
|
|
|
|
|
if (index != 0)
|
|
|
|
{
|
2010-08-24 18:35:01 +00:00
|
|
|
/* Combine mask0 and mask1. We could play games with
|
|
|
|
palignr, but frankly this data should be in L1 now
|
|
|
|
so do the merge via an unaligned load. */
|
|
|
|
mask = _mm_loadu_si128 ((__m128i *) a);
|
2009-07-03 09:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A is aligned. */
|
|
|
|
mask = _mm_load_si128 ((__m128i *) a);
|
|
|
|
|
|
|
|
/* Find where the NULL terminator is. */
|
2009-07-03 10:23:01 +00:00
|
|
|
int length = _mm_cmpistri (mask, mask, 0x3a);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (length == 16)
|
|
|
|
{
|
|
|
|
/* There is no NULL terminator. Don't use SSE4.2 if the length
|
|
|
|
of A > 16. */
|
|
|
|
if (a[16] != 0)
|
|
|
|
return __strspn_sse2 (s, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = (int) ((size_t) s & 15);
|
|
|
|
if (offset != 0)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
2009-07-03 10:23:01 +00:00
|
|
|
int length = _mm_cmpistri (mask, value, 0x12);
|
2009-07-03 09:48:56 +00:00
|
|
|
/* No need to check CFlag since it is always 1. */
|
|
|
|
if (length < 16 - offset)
|
|
|
|
return length;
|
|
|
|
/* Find where the NULL terminator is. */
|
2009-07-03 10:23:01 +00:00
|
|
|
int index = _mm_cmpistri (value, value, 0x3a);
|
2009-07-03 09:48:56 +00:00
|
|
|
if (index < 16 - offset)
|
|
|
|
return length;
|
|
|
|
aligned += 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aligned = s;
|
|
|
|
|
2009-07-03 10:23:01 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
__m128i value = _mm_load_si128 ((__m128i *) aligned);
|
|
|
|
int index = _mm_cmpistri (mask, value, 0x12);
|
|
|
|
int cflag = _mm_cmpistrc (mask, value, 0x12);
|
|
|
|
if (cflag)
|
|
|
|
return (size_t) (aligned + index - s);
|
|
|
|
aligned += 16;
|
|
|
|
}
|
2009-07-03 09:48:56 +00:00
|
|
|
}
|