Debian Codegraph @

rpython/rlib/fastutf8/src/utf8-avx.c

raw symbols SCIP
1 #include "utf8.h"
2
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <immintrin.h>
7
8 #define BIT(B,P) ((B >> (P-1)) & 0x1)
9
10 void _print_mmy(const char * msg, __m256i chunk)
11 {
12 printf("%s:", msg);
13 // unpack the first 8 bytes, padding with zeros
14 uint64_t a = _mm256_extract_epi64(chunk, 0);
15 uint64_t b = _mm256_extract_epi64(chunk, 1);
16 uint64_t c = _mm256_extract_epi64(chunk, 2);
17 uint64_t d = _mm256_extract_epi64(chunk, 3);
18 printf("%.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x "
19 "%.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x %.2x%.2x%.2x%.2x",
20 (unsigned char)((a >> 0) & 0xff),
21 (unsigned char)((a >> 8) & 0xff),
22 (unsigned char)((a >> 16) & 0xff),
23 (unsigned char)((a >> 24) & 0xff),
24
25 (unsigned char)((a >> 32) & 0xff),
26 (unsigned char)((a >> 40) & 0xff),
27 (unsigned char)((a >> 48) & 0xff),
28 (unsigned char)((a >> 56) & 0xff),
29
30 (unsigned char)((b >> 0) & 0xff),
31 (unsigned char)((b >> 8) & 0xff),
32 (unsigned char)((b >> 16) & 0xff),
33 (unsigned char)((b >> 24) & 0xff),
34
35 (unsigned char)((b >> 32) & 0xff),
36 (unsigned char)((b >> 40) & 0xff),
37 (unsigned char)((b >> 48) & 0xff),
38 (unsigned char)((b >> 56) & 0xff),
39
40 (unsigned char)((c >> 0) & 0xff),
41 (unsigned char)((c >> 8) & 0xff),
42 (unsigned char)((c >> 16) & 0xff),
43 (unsigned char)((c >> 24) & 0xff),
44
45 (unsigned char)((c >> 32) & 0xff),
46 (unsigned char)((c >> 40) & 0xff),
47 (unsigned char)((c >> 48) & 0xff),
48 (unsigned char)((c >> 56) & 0xff),
49
50 (unsigned char)((d >> 0) & 0xff),
51 (unsigned char)((d >> 8) & 0xff),
52 (unsigned char)((d >> 16) & 0xff),
53 (unsigned char)((d >> 24) & 0xff),
54
55 (unsigned char)((d >> 32) & 0xff),
56 (unsigned char)((d >> 40) & 0xff),
57 (unsigned char)((d >> 48) & 0xff),
58 (unsigned char)((d >> 56) & 0xff)
59 );
60
61 printf("\n");
62 }
63
64 ssize_t fu8_count_utf8_codepoints_avx(const char * utf8, size_t len)
65 {
66 const uint8_t * encoded = (const uint8_t*)utf8;
67 __builtin_prefetch(encoded, 0, 0);
68 size_t num_codepoints = 0;
69 __m256i chunk;
70
71 if (len == 0) {
72 return 0;
73 }
74 __m256i zero = _mm256_set1_epi8(0x00);
75 while (len >= 32) {
76 chunk = _mm256_loadu_si256((__m256i*)encoded);
77 if (_mm256_movemask_epi8(chunk) == 0) {
78 // valid ascii chars!
79 len -= 32;
80 encoded += 32;
81 num_codepoints += 32;
82 continue;
83 }
84 __builtin_prefetch(encoded+32, 0, 0);
85
86 __m256i count = _mm256_set1_epi8(0x1);
87 //_print_mm256x("chunk", chunk);
88 // fight against the fact that there is no comparison on unsigned values
89 __m256i chunk_signed = _mm256_add_epi8(chunk, _mm256_set1_epi8(0x80));
90 //_print_mm256x("shunk", chunk_signed);
91
92 // ERROR checking
93 // checking procedure works the following way:
94 //
95 // 1) mark all continuation bytes with either 0x1, 0x3, 0x7 (one, two or three bytes continuation)
96 // 2) then check that there is no byte that has an invalid continuation
97 __m256i twobytemarker = _mm256_cmpgt_epi8( chunk_signed, _mm256_set1_epi8(0xc0-1-0x80));
98 __m256i threebytemarker = _mm256_cmpgt_epi8(chunk_signed, _mm256_set1_epi8(0xe0-1-0x80));
99 __m256i fourbytemarker = _mm256_cmpgt_epi8( chunk_signed, _mm256_set1_epi8(0xf0-1-0x80));
100
101 // the general idea of the following code collects 0xff for each byte position
102 // in the variable contbytes.
103 // at the end check if each position in contbytes set to 0xff is a valid continuation byte
104
105 // check that 0xc0 > 0xc2
106 __m256i validtwobm = _mm256_cmpgt_epi8(chunk_signed, _mm256_set1_epi8(0xc2-1-0x80));
107 if (_mm256_movemask_epi8(_mm256_xor_si256(validtwobm, twobytemarker)) != 0) {
108 // two byte marker should not be in range [0xc0-0xc2)
109 return -1;
110 }
111
112 __m256i state2 = _mm256_andnot_si256(threebytemarker, twobytemarker);
113 __m256i contbytes = _mm256_slli_si256(_mm256_blendv_epi8(state2, _mm256_set1_epi8(0x1), twobytemarker), 1);
114
115 if (_mm256_movemask_epi8(threebytemarker) != 0) {
116 // contains at least one 3 byte marker
117 __m256i istate3 = _mm256_andnot_si256(fourbytemarker, threebytemarker);
118 __m256i state3 = _mm256_slli_si256(_mm256_blendv_epi8(zero, _mm256_set1_epi8(0x3), istate3), 1);
119 state3 = _mm256_or_si256(state3, _mm256_slli_si256(state3, 1));
120
121 contbytes = _mm256_or_si256(contbytes, state3);
122
123 // range check
124 __m256i equal_e0 = _mm256_cmpeq_epi8(_mm256_blendv_epi8(zero, chunk_signed, istate3),
125 _mm256_set1_epi8(0xe0-0x80));
126 if (_mm256_movemask_epi8(equal_e0) != 0) {
127 __m256i mask = _mm256_blendv_epi8(_mm256_set1_epi8(0x7f), chunk_signed, _mm256_slli_si256(equal_e0, 1));
128 __m256i check_surrogate = _mm256_cmpgt_epi8(_mm256_set1_epi8(0xa0-0x80), mask); // lt
129 if (_mm256_movemask_epi8(check_surrogate) != 0) {
130 // invalid surrograte character!!!
131 return -1;
132 }
133 }
134
135 // verify that there are now surrogates
136 if (!ALLOW_SURROGATES) {
137 __m256i equal_ed = _mm256_cmpeq_epi8(_mm256_blendv_epi8(zero, chunk_signed, istate3),
138 _mm256_set1_epi8(0xed-0x80));
139 if (_mm256_movemask_epi8(equal_ed) != 0) {
140 __m256i mask = _mm256_blendv_epi8(_mm256_set1_epi8(0x80), chunk_signed, _mm256_slli_si256(equal_ed, 1));
141 __m256i check_surrogate = _mm256_cmpgt_epi8(mask, _mm256_set1_epi8(0xa0-1-0x80));
142 if (_mm256_movemask_epi8(check_surrogate) != 0) {
143 // invalid surrograte character!!!
144 return -1;
145 }
146 }
147 }
148 }
149
150 if (_mm256_movemask_epi8(fourbytemarker) != 0) {
151 // contain a 4 byte marker
152 __m256i istate4 = _mm256_slli_si256(_mm256_blendv_epi8(zero, _mm256_set1_epi8(0x7), fourbytemarker), 1);
153 __m256i state4 =_mm256_or_si256(istate4, _mm256_slli_si256(istate4, 1));
154 state4 =_mm256_or_si256(state4, _mm256_slli_si256(istate4, 2));
155
156 contbytes = _mm256_or_si256(contbytes, state4);
157
158 // range check, filter out f0 and
159 __m256i equal_f0 = _mm256_cmpeq_epi8(_mm256_blendv_epi8(zero, chunk_signed, fourbytemarker),
160 _mm256_set1_epi8(0xf0-0x80));
161 if (_mm256_movemask_epi8(equal_f0) != 0) {
162 __m256i mask = _mm256_blendv_epi8(_mm256_set1_epi8(0x7f), chunk_signed, _mm256_slli_si256(equal_f0, 1));
163 __m256i check_surrogate = _mm256_cmpgt_epi8(_mm256_set1_epi8(0x90-0x80), mask);
164 if (_mm256_movemask_epi8(check_surrogate) != 0) {
165 return -1;
166 }
167 }
168
169 __m256i equal_f4 = _mm256_cmpeq_epi8(_mm256_blendv_epi8(zero, chunk_signed, fourbytemarker),
170 _mm256_set1_epi8(0xf4-0x80));
171 if (_mm256_movemask_epi8(equal_f4) != 0) {
172 __m256i mask = _mm256_blendv_epi8(_mm256_set1_epi8(0x80), chunk_signed, _mm256_slli_si256(equal_f4, 1));
173 __m256i check_surrogate = _mm256_cmpgt_epi8(mask, _mm256_set1_epi8(0x90-1-0x80));
174 if (_mm256_movemask_epi8(check_surrogate) != 0) {
175 return -1;
176 }
177 }
178
179 __m256i equal_f5_gt = _mm256_cmpgt_epi8(_mm256_blendv_epi8(zero, chunk_signed, fourbytemarker),
180 _mm256_set1_epi8(0xf4-0x80));
181 if (_mm256_movemask_epi8(equal_f5_gt) != 0) {
182 return -1;
183 }
184 }
185
186 // now check that contbytes and the actual byte values have a valid
187 // continuation at each position the marker indicates to have one
188 __m256i check_cont = _mm256_cmpgt_epi8(contbytes, zero);
189 __m256i contpos = _mm256_and_si256(_mm256_set1_epi8(0xc0), chunk);
190 contpos = _mm256_cmpeq_epi8(_mm256_set1_epi8(0x80), contpos);
191 __m256i validcont = _mm256_xor_si256(check_cont, contpos);
192 if (_mm256_movemask_epi8(validcont) != 0) {
193 // uff, nope, that is really not utf8
194 return -1;
195 }
196
197 // CORRECT, calculate the length
198 // copy 0x00 over to each place which is a continuation byte
199 count = _mm256_blendv_epi8(count, zero, contpos);
200
201 // count the code points using 2x 32 bit hadd and one last 16 hadd
202 // the result will end up at the lowest position
203 count = _mm256_hadd_epi32(count, zero);
204 count = _mm256_hadd_epi32(count, zero);
205 count = _mm256_hadd_epi16(count, zero);
206 uint16_t c = _mm256_extract_epi16(count, 0);
207 uint16_t c2 = _mm256_extract_epi16(count, 8);
208 uint16_t points = (c & 0xff) + ((c >> 8) & 0xff) + (c2 & 0xff) + ((c2 >> 8) & 0xff);
209
210 // these cases need to be handled:
211 // 16 byte boundary -> | <- 16 byte boundary
212 // -----------------------------------------+--------------------
213 // 1) 2 byte code point. e.g. ... c2 | 80 ...
214 // 2) 3 byte code point. e.g. ... e6 | 80 80 ...
215 // 3) 3 byte code point. e.g. ... e6 80 | 80 ...
216 // 4) 4 byte code point. e.g. ... f2 | 80 80 80 ...
217 // 5) 4 byte code point. e.g. ... f2 80 | 80 80 ...
218 // 6) 4 byte code point. e.g. ... f2 80 80 | 80 ...
219 //
220 int mask_chunk = _mm256_movemask_epi8(chunk);
221 int mask_conti = _mm256_movemask_epi8(contpos);
222
223 // little endian
224 int lenoff = 32;
225 int minus_codepoints = 0;
226 if (BIT(mask_chunk, 32) != 0 && BIT(mask_conti, 32) == 0) { // 1), 2), 4)
227 minus_codepoints = 1;
228 lenoff -= 1;
229 } else if (BIT(mask_chunk, 31) != 0 && BIT(mask_conti, 31) == 0 &&
230 BIT(mask_conti, 32) == 1) { // 3), 5)
231 minus_codepoints = 1;
232 lenoff -= 2;
233 } else if (BIT(mask_chunk, 30) != 0 && BIT(mask_conti, 30) == 0 &&
234 BIT(mask_conti, 31) == 1 && BIT(mask_conti, 32) == 1) { // 6)
235 minus_codepoints = 1;
236 lenoff -= 3;
237 }
238
239 num_codepoints += points - minus_codepoints;
240 len -= lenoff;
241 encoded += lenoff;
242 }
243
244 if (len == 0) {
245 return num_codepoints;
246 }
247
248 ssize_t result = fu8_count_utf8_codepoints_seq(encoded, len);
249 if (result == -1) {
250 return -1;
251 }
252
253 return num_codepoints + result;
254 return -1;
255 }