1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/* 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
#define STRCSPN_SSE2 __strcspn_sse2
#define STRCSPN_SSE42 __strcspn_sse42
#endif
extern
#ifdef USE_AS_STRPBRK
char *
#else
size_t
#endif
STRCSPN_SSE2 (const char *, const char *);
#ifdef USE_AS_STRPBRK
char *
#else
size_t
#endif
__attribute__ ((section (".text.sse4.2")))
STRCSPN_SSE42 (const char *s, const char *a)
{
int offset;
const char *aligned;
__m128i mask, mask0, mask1;
__m128i value;
int index, length;
int cflag, zflag;
if (*a == 0)
#ifdef USE_AS_STRPBRK
return NULL;
#else
return strlen (s);
#endif
offset = (int) ((size_t) a & 15);
if (offset != 0)
{
/* Load masks. */
aligned = (const char *) ((size_t) a & 0xfffffffffffffff0L);
mask0 = _mm_load_si128 ((__m128i *) aligned);
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. */
length = _mm_cmpistri (mask, mask, 0x3a);
if (length == 16 - offset)
{
/* There is no NULL terminator. */
mask1 = _mm_load_si128 ((__m128i *) (aligned + 16));
index = _mm_cmpistri (mask1, mask1, 0x3a);
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. */
length = _mm_cmpistri (mask, mask, 0x3a);
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. */
aligned = (const char *) ((size_t) s & 0xfffffffffffffff0L);
value = _mm_load_si128 ((__m128i *) aligned);
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;
}
length = _mm_cmpistri (mask, value, 0x2);
/* No need to check ZFlag since ZFlag is always 1. */
cflag = _mm_cmpistrc (mask, value, 0x2);
if (cflag)
#ifdef USE_AS_STRPBRK
return (char *) (s + length);
#else
return length;
#endif
/* Find where the NULL terminator is. */
index = _mm_cmpistri (value, value, 0x3a);
if (index < 16 - offset)
#ifdef USE_AS_STRPBRK
return NULL;
#else
return index;
#endif
aligned += 16;
}
else
aligned = s;
loop:
value = _mm_load_si128 ((__m128i *) aligned);
index = _mm_cmpistri (mask, value, 0x2);
cflag = _mm_cmpistrc (mask, value, 0x2);
zflag = _mm_cmpistrz (mask, value, 0x2);
if (cflag)
#ifdef USE_AS_STRPBRK
return (char *) (aligned + index);
#else
return (size_t) (aligned + index - s);
#endif
if (zflag)
#ifdef USE_AS_STRPBRK
return NULL;
#else
{
/* Find where the NULL terminator is. */
index = _mm_cmpistri (value, value, 0x3a);
return (size_t) (aligned + index - s);
}
#endif
aligned += 16;
goto loop;
}
|