1 | /* strspn with SSE4.2 intrinsics |
2 | Copyright (C) 2009-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <nmmintrin.h> |
20 | #include <string.h> |
21 | #include "varshift.h" |
22 | |
23 | /* We use 0x12: |
24 | _SIDD_SBYTE_OPS |
25 | | _SIDD_CMP_EQUAL_ANY |
26 | | _SIDD_NEGATIVE_POLARITY |
27 | | _SIDD_LEAST_SIGNIFICANT |
28 | on pcmpistri to compare xmm/mem128 |
29 | |
30 | 0 1 2 3 4 5 6 7 8 9 A B C D E F |
31 | X X X X X X X X X X X X X X X X |
32 | |
33 | against xmm |
34 | |
35 | 0 1 2 3 4 5 6 7 8 9 A B C D E F |
36 | A A A A A A A A A A A A A A A A |
37 | |
38 | to find out if the first 16byte data element has any non-A byte and |
39 | the offset of the first byte. There are 2 cases: |
40 | |
41 | 1. The first 16byte data element has the non-A byte, including |
42 | EOS, at the offset X. |
43 | 2. The first 16byte data element is valid and doesn't have the non-A |
44 | byte. |
45 | |
46 | Here is the table of ECX, CFlag, ZFlag and SFlag for 2 cases: |
47 | |
48 | case ECX CFlag ZFlag SFlag |
49 | 1 X 1 0/1 0 |
50 | 2 16 0 0 0 |
51 | |
52 | We exit from the loop for case 1. */ |
53 | |
54 | extern size_t __strspn_sse2 (const char *, const char *) attribute_hidden; |
55 | |
56 | |
57 | size_t |
58 | __attribute__ ((section (".text.sse4.2" ))) |
59 | __strspn_sse42 (const char *s, const char *a) |
60 | { |
61 | if (*a == 0) |
62 | return 0; |
63 | |
64 | const char *aligned; |
65 | __m128i mask, maskz, zero; |
66 | unsigned int maskz_bits; |
67 | unsigned int offset = (int) ((size_t) a & 15); |
68 | zero = _mm_set1_epi8 (b: 0); |
69 | if (offset != 0) |
70 | { |
71 | /* Load masks. */ |
72 | aligned = (const char *) ((size_t) a & -16L); |
73 | __m128i mask0 = _mm_load_si128 (p: (__m128i *) aligned); |
74 | maskz = _mm_cmpeq_epi8 (a: mask0, b: zero); |
75 | |
76 | /* Find where the NULL terminator is. */ |
77 | maskz_bits = _mm_movemask_epi8 (a: maskz) >> offset; |
78 | if (maskz_bits != 0) |
79 | { |
80 | mask = __m128i_shift_right (value: mask0, offset); |
81 | offset = (unsigned int) ((size_t) s & 15); |
82 | if (offset) |
83 | goto start_unaligned; |
84 | |
85 | aligned = s; |
86 | goto start_loop; |
87 | } |
88 | } |
89 | |
90 | /* A is aligned. */ |
91 | mask = _mm_loadu_si128 (p: (__m128i *) a); |
92 | |
93 | /* Find where the NULL terminator is. */ |
94 | maskz = _mm_cmpeq_epi8 (a: mask, b: zero); |
95 | maskz_bits = _mm_movemask_epi8 (a: maskz); |
96 | if (maskz_bits == 0) |
97 | { |
98 | /* There is no NULL terminator. Don't use SSE4.2 if the length |
99 | of A > 16. */ |
100 | if (a[16] != 0) |
101 | return __strspn_sse2 (s, a); |
102 | } |
103 | aligned = s; |
104 | offset = (unsigned int) ((size_t) s & 15); |
105 | |
106 | if (offset != 0) |
107 | { |
108 | start_unaligned: |
109 | /* Check partial string. */ |
110 | aligned = (const char *) ((size_t) s & -16L); |
111 | __m128i value = _mm_load_si128 (p: (__m128i *) aligned); |
112 | __m128i adj_value = __m128i_shift_right (value, offset); |
113 | |
114 | unsigned int length = _mm_cmpistri (mask, adj_value, 0x12); |
115 | /* No need to check CFlag since it is always 1. */ |
116 | if (length < 16 - offset) |
117 | return length; |
118 | /* Find where the NULL terminator is. */ |
119 | maskz = _mm_cmpeq_epi8 (a: value, b: zero); |
120 | maskz_bits = _mm_movemask_epi8 (a: maskz) >> offset; |
121 | if (maskz_bits != 0) |
122 | return length; |
123 | aligned += 16; |
124 | } |
125 | |
126 | start_loop: |
127 | while (1) |
128 | { |
129 | __m128i value = _mm_load_si128 (p: (__m128i *) aligned); |
130 | unsigned int index = _mm_cmpistri (mask, value, 0x12); |
131 | unsigned int cflag = _mm_cmpistrc (mask, value, 0x12); |
132 | if (cflag) |
133 | return (size_t) (aligned + index - s); |
134 | aligned += 16; |
135 | } |
136 | } |
137 | |