2009-07-03 17:48:56 +08:00
|
|
|
/* strcspn with SSE4.2 intrinsics
|
|
|
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
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
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
|
|
|
|
|
|
|
#include <nmmintrin.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* 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 18:23:01 +08: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 17:48:56 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern
|
|
|
|
#ifdef USE_AS_STRPBRK
|
|
|
|
char *
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
#endif
|
|
|
|
STRCSPN_SSE2 (const char *, const char *);
|
|
|
|
|
2009-07-03 18:23:01 +08:00
|
|
|
|
2009-07-03 17:48:56 +08: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 18:23:01 +08:00
|
|
|
RETURN (NULL, strlen (s));
|
2009-07-03 17:48:56 +08:00
|
|
|
|
2009-07-03 18:23:01 +08:00
|
|
|
const char *aligned;
|
|
|
|
__m128i mask;
|
2009-08-08 07:19:54 +08:00
|
|
|
/* Fake initialization. gcc otherwise will warn. */
|
|
|
|
asm ("" : "=xm" (mask));
|
2009-07-03 18:23:01 +08:00
|
|
|
int offset = (int) ((size_t) a & 15);
|
2009-07-03 17:48:56 +08:00
|
|
|
if (offset != 0)
|
|
|
|
{
|
|
|
|
/* Load masks. */
|
2009-08-05 03:13:43 +08:00
|
|
|
aligned = (const char *) ((size_t) a & -16L);
|
2009-07-03 18:23:01 +08:00
|
|
|
__m128i mask0 = _mm_load_si128 ((__m128i *) aligned);
|
2009-07-03 17:48:56 +08:00
|
|
|
|
|
|
|
switch (offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
mask = _mm_srli_si128 (mask0, 1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mask = _mm_srli_si128 (mask0, 2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mask = _mm_srli_si128 (mask0, 3);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mask = _mm_srli_si128 (mask0, 4);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
mask = _mm_srli_si128 (mask0, 5);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
mask = _mm_srli_si128 (mask0, 6);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
mask = _mm_srli_si128 (mask0, 7);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
mask = _mm_srli_si128 (mask0, 8);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
mask = _mm_srli_si128 (mask0, 9);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
mask = _mm_srli_si128 (mask0, 10);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
mask = _mm_srli_si128 (mask0, 11);
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
mask = _mm_srli_si128 (mask0, 12);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
mask = _mm_srli_si128 (mask0, 13);
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
mask = _mm_srli_si128 (mask0, 14);
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
mask = _mm_srli_si128 (mask0, 15);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find where the NULL terminator is. */
|
2009-07-03 18:23:01 +08:00
|
|
|
int length = _mm_cmpistri (mask, mask, 0x3a);
|
2009-07-03 17:48:56 +08:00
|
|
|
if (length == 16 - offset)
|
|
|
|
{
|
|
|
|
/* There is no NULL terminator. */
|
2009-07-03 18:23:01 +08:00
|
|
|
__m128i mask1 = _mm_load_si128 ((__m128i *) (aligned + 16));
|
|
|
|
int index = _mm_cmpistri (mask1, mask1, 0x3a);
|
2009-07-03 17:48:56 +08:00
|
|
|
length += index;
|
|
|
|
|
|
|
|
/* Don't use SSE4.2 if the length of A > 16. */
|
|
|
|
if (length > 16)
|
|
|
|
return STRCSPN_SSE2 (s, a);
|
|
|
|
|
|
|
|
if (index != 0)
|
|
|
|
{
|
|
|
|
/* Combine mask0 and mask1. */
|
|
|
|
switch (offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 3);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 4);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 5);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 6);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 7);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 8);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 9);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 10);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 11);
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 12);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 13);
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 14);
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
mask = _mm_alignr_epi8 (mask1, mask0, 15);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A is aligned. */
|
|
|
|
mask = _mm_load_si128 ((__m128i *) a);
|
|
|
|
|
|
|
|
/* Find where the NULL terminator is. */
|
2009-07-03 18:23:01 +08:00
|
|
|
int length = _mm_cmpistri (mask, mask, 0x3a);
|
2009-07-03 17:48:56 +08: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 STRCSPN_SSE2 (s, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = (int) ((size_t) s & 15);
|
|
|
|
if (offset != 0)
|
|
|
|
{
|
|
|
|
/* Check partial string. */
|
2009-08-05 03:13:43 +08:00
|
|
|
aligned = (const char *) ((size_t) s & -16L);
|
2009-07-03 18:23:01 +08:00
|
|
|
__m128i value = _mm_load_si128 ((__m128i *) aligned);
|
2009-07-03 17:48:56 +08:00
|
|
|
|
|
|
|
switch (offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
value = _mm_srli_si128 (value, 1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
value = _mm_srli_si128 (value, 2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
value = _mm_srli_si128 (value, 3);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
value = _mm_srli_si128 (value, 4);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
value = _mm_srli_si128 (value, 5);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
value = _mm_srli_si128 (value, 6);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
value = _mm_srli_si128 (value, 7);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
value = _mm_srli_si128 (value, 8);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
value = _mm_srli_si128 (value, 9);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
value = _mm_srli_si128 (value, 10);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
value = _mm_srli_si128 (value, 11);
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
value = _mm_srli_si128 (value, 12);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
value = _mm_srli_si128 (value, 13);
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
value = _mm_srli_si128 (value, 14);
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
value = _mm_srli_si128 (value, 15);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-03 18:23:01 +08:00
|
|
|
int length = _mm_cmpistri (mask, value, 0x2);
|
2009-07-03 17:48:56 +08:00
|
|
|
/* No need to check ZFlag since ZFlag is always 1. */
|
2009-07-03 18:23:01 +08:00
|
|
|
int cflag = _mm_cmpistrc (mask, value, 0x2);
|
2009-07-03 17:48:56 +08:00
|
|
|
if (cflag)
|
2009-07-03 18:23:01 +08:00
|
|
|
RETURN ((char *) (s + length), length);
|
2009-07-03 17:48:56 +08:00
|
|
|
/* Find where the NULL terminator is. */
|
2009-07-03 18:23:01 +08:00
|
|
|
int index = _mm_cmpistri (value, value, 0x3a);
|
2009-07-03 17:48:56 +08:00
|
|
|
if (index < 16 - offset)
|
2009-07-03 18:23:01 +08:00
|
|
|
RETURN (NULL, index);
|
2009-07-03 17:48:56 +08:00
|
|
|
aligned += 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aligned = s;
|
|
|
|
|
2009-07-03 18:23:01 +08:00
|
|
|
while (1)
|
2009-07-03 17:48:56 +08:00
|
|
|
{
|
2009-07-03 18:23:01 +08:00
|
|
|
__m128i value = _mm_load_si128 ((__m128i *) aligned);
|
|
|
|
int index = _mm_cmpistri (mask, value, 0x2);
|
|
|
|
int cflag = _mm_cmpistrc (mask, value, 0x2);
|
|
|
|
int zflag = _mm_cmpistrz (mask, value, 0x2);
|
|
|
|
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 17:48:56 +08:00
|
|
|
}
|
|
|
|
}
|