1 /* strcspn with SSE4.2 intrinsics
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
3 Contributed by Intel Corporation.
4 This file is part of the GNU C Library.
6 SPDX-License-Identifier: LGPL-2.1-or-later
18 #include <tmmintrin.h>
21 #include <nmmintrin.h>
23 #include "ws_mempbrk.h"
24 #include "ws_mempbrk_int.h"
26 /* __has_feature(address_sanitizer) is used later for Clang, this is for
27 * compatibility with other compilers (such as GCC and MSVC) */
29 # define __has_feature(x) 0
32 #define cast_128aligned__m128i(p) ((const __m128i *) (const void *) (p))
34 /* Helper for variable shifts of SSE registers.
35 Copyright (C) 2010 Free Software Foundation, Inc.
38 static const int8_t ___m128i_shift_right
[31] =
40 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
45 __m128i_shift_right (__m128i value
, unsigned long int offset
)
47 /* _mm_loadu_si128() works with unaligned data, cast safe */
48 return _mm_shuffle_epi8 (value
,
49 _mm_loadu_si128 (cast_128aligned__m128i(___m128i_shift_right
+ offset
)));
54 ws_mempbrk_sse42_compile(ws_mempbrk_pattern
* pattern
, const char *needles
)
56 size_t length
= strlen(needles
);
58 pattern
->use_sse42
= ws_cpuid_sse42() && (length
<= 16);
60 if (pattern
->use_sse42
) {
61 pattern
->mask
= _mm_setzero_si128();
62 memcpy(&(pattern
->mask
), needles
, length
);
69 | _SIDD_POSITIVE_POLARITY
70 | _SIDD_LEAST_SIGNIFICANT
71 on pcmpistri to compare xmm/mem128
73 0 1 2 3 4 5 6 7 8 9 A B C D E F
74 X X X X X X X X X X X X X X X X
78 0 1 2 3 4 5 6 7 8 9 A B C D E F
79 A A A A A A A A A A A A A A A A
81 to find out if the first 16byte data element has any byte A and
82 the offset of the first byte. There are 3 cases:
84 1. The first 16byte data element has the byte A at the offset X.
85 2. The first 16byte data element has EOS and doesn't have the byte A.
86 3. The first 16byte data element is valid and doesn't have the byte A.
88 Here is the table of ECX, CFlag, ZFlag and SFlag for 2 cases:
94 We exit from the loop for cases 1 and 2 with jbe which branches
95 when either CFlag or ZFlag is 1. If CFlag == 1, ECX has the offset
99 ws_mempbrk_sse42_exec(const char *haystack
, size_t haystacklen
, const ws_mempbrk_pattern
* pattern
, unsigned char *found_needle
)
104 offset
= (int) ((size_t) haystack
& 15);
105 aligned
= (const char *) ((size_t) haystack
& -16L);
108 /* Check partial string. cast safe it's 16B aligned */
109 __m128i value
= __m128i_shift_right (_mm_load_si128 (cast_128aligned__m128i(aligned
)), offset
);
111 int length
= _mm_cmpistri (pattern
->mask
, value
, 0x2);
112 /* No need to check ZFlag since ZFlag is always 1. */
113 int cflag
= _mm_cmpistrc (pattern
->mask
, value
, 0x2);
114 /* XXX: why does this compare value with value? */
115 int idx
= _mm_cmpistri (value
, value
, 0x3a);
119 *found_needle
= *(haystack
+ length
);
120 return haystack
+ length
;
123 /* Find where the NULL terminator is. */
124 if (idx
< 16 - offset
)
126 /* found NUL @ 'idx', need to switch to slower mempbrk */
127 return ws_mempbrk_portable_exec(haystack
+ idx
+ 1, haystacklen
- idx
- 1, pattern
, found_needle
); /* haystacklen is bigger than 16 & idx < 16 so no underflow here */
130 haystacklen
-= (16 - offset
);
135 while (haystacklen
>= 16)
137 __m128i value
= _mm_load_si128 (cast_128aligned__m128i(aligned
));
138 int idx
= _mm_cmpistri (pattern
->mask
, value
, 0x2);
139 int cflag
= _mm_cmpistrc (pattern
->mask
, value
, 0x2);
140 int zflag
= _mm_cmpistrz (pattern
->mask
, value
, 0x2);
144 *found_needle
= *(aligned
+ idx
);
145 return aligned
+ idx
;
150 /* found NUL, need to switch to slower mempbrk */
151 return ws_mempbrk_portable_exec(aligned
, haystacklen
, pattern
, found_needle
);
157 /* XXX, use mempbrk_slow here? */
158 return ws_mempbrk_portable_exec(aligned
, haystacklen
, pattern
, found_needle
);
161 #endif /* HAVE_SSE4_2 */
168 * indent-tabs-mode: nil
171 * ex: set shiftwidth=2 tabstop=8 expandtab:
172 * :indentSize=2:tabSize=8:noTabs=true: