1 /* strcspn with SSE4.2 intrinsics
2 Copyright (C) 2009 Free Software Foundation, Inc.
3 Contributed by Intel Corporation.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <nmmintrin.h>
27 | _SIDD_POSITIVE_POLARITY
28 | _SIDD_LEAST_SIGNIFICANT
29 on pcmpistri to compare xmm/mem128
31 0 1 2 3 4 5 6 7 8 9 A B C D E F
32 X X X X X X X X X X X X X X X X
36 0 1 2 3 4 5 6 7 8 9 A B C D E F
37 A A A A A A A A A A A A A A A A
39 to find out if the first 16byte data element has any byte A and
40 the offset of the first byte. There are 3 cases:
42 1. The first 16byte data element has the byte A at the offset X.
43 2. The first 16byte data element has EOS and doesn't have the byte A.
44 3. The first 16byte data element is valid and doesn't have the byte A.
46 Here is the table of ECX, CFlag, ZFlag and SFlag for 2 cases:
52 We exit from the loop for cases 1 and 2 with jbe which branches
53 when either CFlag or ZFlag is 1. If CFlag == 1, ECX has the offset
57 # define STRCSPN_SSE2 __strcspn_sse2
58 # define STRCSPN_SSE42 __strcspn_sse42
62 # define RETURN(val1, val2) return val1
64 # define RETURN(val1, val2) return val2
73 STRCSPN_SSE2 (const char *, const char *);
81 __attribute__ ((section (".text.sse4.2")))
82 STRCSPN_SSE42 (const char *s
, const char *a
)
85 RETURN (NULL
, strlen (s
));
89 int offset
= (int) ((size_t) a
& 15);
93 aligned
= (const char *) ((size_t) a
& -16L);
94 __m128i mask0
= _mm_load_si128 ((__m128i
*) aligned
);
99 mask
= _mm_srli_si128 (mask0
, 1);
102 mask
= _mm_srli_si128 (mask0
, 2);
105 mask
= _mm_srli_si128 (mask0
, 3);
108 mask
= _mm_srli_si128 (mask0
, 4);
111 mask
= _mm_srli_si128 (mask0
, 5);
114 mask
= _mm_srli_si128 (mask0
, 6);
117 mask
= _mm_srli_si128 (mask0
, 7);
120 mask
= _mm_srli_si128 (mask0
, 8);
123 mask
= _mm_srli_si128 (mask0
, 9);
126 mask
= _mm_srli_si128 (mask0
, 10);
129 mask
= _mm_srli_si128 (mask0
, 11);
132 mask
= _mm_srli_si128 (mask0
, 12);
135 mask
= _mm_srli_si128 (mask0
, 13);
138 mask
= _mm_srli_si128 (mask0
, 14);
141 mask
= _mm_srli_si128 (mask0
, 15);
145 /* Find where the NULL terminator is. */
146 int length
= _mm_cmpistri (mask
, mask
, 0x3a);
147 if (length
== 16 - offset
)
149 /* There is no NULL terminator. */
150 __m128i mask1
= _mm_load_si128 ((__m128i
*) (aligned
+ 16));
151 int index
= _mm_cmpistri (mask1
, mask1
, 0x3a);
154 /* Don't use SSE4.2 if the length of A > 16. */
156 return STRCSPN_SSE2 (s
, a
);
160 /* Combine mask0 and mask1. */
164 mask
= _mm_alignr_epi8 (mask1
, mask0
, 1);
167 mask
= _mm_alignr_epi8 (mask1
, mask0
, 2);
170 mask
= _mm_alignr_epi8 (mask1
, mask0
, 3);
173 mask
= _mm_alignr_epi8 (mask1
, mask0
, 4);
176 mask
= _mm_alignr_epi8 (mask1
, mask0
, 5);
179 mask
= _mm_alignr_epi8 (mask1
, mask0
, 6);
182 mask
= _mm_alignr_epi8 (mask1
, mask0
, 7);
185 mask
= _mm_alignr_epi8 (mask1
, mask0
, 8);
188 mask
= _mm_alignr_epi8 (mask1
, mask0
, 9);
191 mask
= _mm_alignr_epi8 (mask1
, mask0
, 10);
194 mask
= _mm_alignr_epi8 (mask1
, mask0
, 11);
197 mask
= _mm_alignr_epi8 (mask1
, mask0
, 12);
200 mask
= _mm_alignr_epi8 (mask1
, mask0
, 13);
203 mask
= _mm_alignr_epi8 (mask1
, mask0
, 14);
206 mask
= _mm_alignr_epi8 (mask1
, mask0
, 15);
215 mask
= _mm_load_si128 ((__m128i
*) a
);
217 /* Find where the NULL terminator is. */
218 int length
= _mm_cmpistri (mask
, mask
, 0x3a);
221 /* There is no NULL terminator. Don't use SSE4.2 if the length
224 return STRCSPN_SSE2 (s
, a
);
228 offset
= (int) ((size_t) s
& 15);
231 /* Check partial string. */
232 aligned
= (const char *) ((size_t) s
& -16L);
233 __m128i value
= _mm_load_si128 ((__m128i
*) aligned
);
238 value
= _mm_srli_si128 (value
, 1);
241 value
= _mm_srli_si128 (value
, 2);
244 value
= _mm_srli_si128 (value
, 3);
247 value
= _mm_srli_si128 (value
, 4);
250 value
= _mm_srli_si128 (value
, 5);
253 value
= _mm_srli_si128 (value
, 6);
256 value
= _mm_srli_si128 (value
, 7);
259 value
= _mm_srli_si128 (value
, 8);
262 value
= _mm_srli_si128 (value
, 9);
265 value
= _mm_srli_si128 (value
, 10);
268 value
= _mm_srli_si128 (value
, 11);
271 value
= _mm_srli_si128 (value
, 12);
274 value
= _mm_srli_si128 (value
, 13);
277 value
= _mm_srli_si128 (value
, 14);
280 value
= _mm_srli_si128 (value
, 15);
284 int length
= _mm_cmpistri (mask
, value
, 0x2);
285 /* No need to check ZFlag since ZFlag is always 1. */
286 int cflag
= _mm_cmpistrc (mask
, value
, 0x2);
288 RETURN ((char *) (s
+ length
), length
);
289 /* Find where the NULL terminator is. */
290 int index
= _mm_cmpistri (value
, value
, 0x3a);
291 if (index
< 16 - offset
)
292 RETURN (NULL
, index
);
300 __m128i value
= _mm_load_si128 ((__m128i
*) aligned
);
301 int index
= _mm_cmpistri (mask
, value
, 0x2);
302 int cflag
= _mm_cmpistrc (mask
, value
, 0x2);
303 int zflag
= _mm_cmpistrz (mask
, value
, 0x2);
305 RETURN ((char *) (aligned
+ index
), (size_t) (aligned
+ index
- s
));
308 /* Find where the NULL terminator is. */
309 (size_t) (aligned
+ _mm_cmpistri (value
, value
, 0x3a) - s
));