sq_mini_racer 0.2.4.sqreen1 → 0.2.4.sqreen2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,463 @@
1
+
2
+ #ifndef SIMDUTF8CHECK_H
3
+ #define SIMDUTF8CHECK_H
4
+ #include <stdbool.h>
5
+ #include <stddef.h>
6
+ #include <stdint.h>
7
+ #include <string.h>
8
+ #include <x86intrin.h>
9
+ /*
10
+ * legal utf-8 byte sequence
11
+ * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94
12
+ *
13
+ * Code Points 1st 2s 3s 4s
14
+ * U+0000..U+007F 00..7F
15
+ * U+0080..U+07FF C2..DF 80..BF
16
+ * U+0800..U+0FFF E0 A0..BF 80..BF
17
+ * U+1000..U+CFFF E1..EC 80..BF 80..BF
18
+ * U+D000..U+D7FF ED 80..9F 80..BF
19
+ * U+E000..U+FFFF EE..EF 80..BF 80..BF
20
+ * U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
21
+ * U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
22
+ * U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
23
+ *
24
+ */
25
+
26
+ // all byte values must be no larger than 0xF4
27
+ static inline void checkSmallerThan0xF4(__m128i current_bytes,
28
+ __m128i *has_error) {
29
+ // unsigned, saturates to 0 below max
30
+ *has_error = _mm_or_si128(*has_error,
31
+ _mm_subs_epu8(current_bytes, _mm_set1_epi8(0xF4)));
32
+ }
33
+
34
+ static inline __m128i continuationLengths(__m128i high_nibbles) {
35
+ return _mm_shuffle_epi8(
36
+ _mm_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
37
+ 0, 0, 0, 0, // 10xx (continuation)
38
+ 2, 2, // 110x
39
+ 3, // 1110
40
+ 4), // 1111, next should be 0 (not checked here)
41
+ high_nibbles);
42
+ }
43
+
44
+ static inline __m128i carryContinuations(__m128i initial_lengths,
45
+ __m128i previous_carries) {
46
+
47
+ __m128i right1 =
48
+ _mm_subs_epu8(_mm_alignr_epi8(initial_lengths, previous_carries, 16 - 1),
49
+ _mm_set1_epi8(1));
50
+ __m128i sum = _mm_add_epi8(initial_lengths, right1);
51
+
52
+ __m128i right2 = _mm_subs_epu8(_mm_alignr_epi8(sum, previous_carries, 16 - 2),
53
+ _mm_set1_epi8(2));
54
+ return _mm_add_epi8(sum, right2);
55
+ }
56
+
57
+ static inline void checkContinuations(__m128i initial_lengths, __m128i carries,
58
+ __m128i *has_error) {
59
+
60
+ // overlap || underlap
61
+ // carry > length && length > 0 || !(carry > length) && !(length > 0)
62
+ // (carries > length) == (lengths > 0)
63
+ __m128i overunder =
64
+ _mm_cmpeq_epi8(_mm_cmpgt_epi8(carries, initial_lengths),
65
+ _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128()));
66
+
67
+ *has_error = _mm_or_si128(*has_error, overunder);
68
+ }
69
+
70
+ // when 0xED is found, next byte must be no larger than 0x9F
71
+ // when 0xF4 is found, next byte must be no larger than 0x8F
72
+ // next byte must be continuation, ie sign bit is set, so signed < is ok
73
+ static inline void checkFirstContinuationMax(__m128i current_bytes,
74
+ __m128i off1_current_bytes,
75
+ __m128i *has_error) {
76
+ __m128i maskED = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xED));
77
+ __m128i maskF4 = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xF4));
78
+
79
+ __m128i badfollowED =
80
+ _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x9F)), maskED);
81
+ __m128i badfollowF4 =
82
+ _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x8F)), maskF4);
83
+
84
+ *has_error = _mm_or_si128(*has_error, _mm_or_si128(badfollowED, badfollowF4));
85
+ }
86
+
87
+ // map off1_hibits => error condition
88
+ // hibits off1 cur
89
+ // C => < C2 && true
90
+ // E => < E1 && < A0
91
+ // F => < F1 && < 90
92
+ // else false && false
93
+ static inline void checkOverlong(__m128i current_bytes,
94
+ __m128i off1_current_bytes, __m128i hibits,
95
+ __m128i previous_hibits, __m128i *has_error) {
96
+ __m128i off1_hibits = _mm_alignr_epi8(hibits, previous_hibits, 16 - 1);
97
+ __m128i initial_mins = _mm_shuffle_epi8(
98
+ _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
99
+ -128, -128, // 10xx => false
100
+ 0xC2, -128, // 110x
101
+ 0xE1, // 1110
102
+ 0xF1),
103
+ off1_hibits);
104
+
105
+ __m128i initial_under = _mm_cmpgt_epi8(initial_mins, off1_current_bytes);
106
+
107
+ __m128i second_mins = _mm_shuffle_epi8(
108
+ _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
109
+ -128, -128, // 10xx => false
110
+ 127, 127, // 110x => true
111
+ 0xA0, // 1110
112
+ 0x90),
113
+ off1_hibits);
114
+ __m128i second_under = _mm_cmpgt_epi8(second_mins, current_bytes);
115
+ *has_error =
116
+ _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under));
117
+ }
118
+
119
+ struct processed_utf_bytes {
120
+ __m128i rawbytes;
121
+ __m128i high_nibbles;
122
+ __m128i carried_continuations;
123
+ };
124
+
125
+ static inline void count_nibbles(__m128i bytes,
126
+ struct processed_utf_bytes *answer) {
127
+ answer->rawbytes = bytes;
128
+ answer->high_nibbles =
129
+ _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F));
130
+ }
131
+
132
+ // check whether the current bytes are valid UTF-8
133
+ // at the end of the function, previous gets updated
134
+ static struct processed_utf_bytes
135
+ checkUTF8Bytes(__m128i current_bytes, struct processed_utf_bytes *previous,
136
+ __m128i *has_error) {
137
+ struct processed_utf_bytes pb;
138
+ count_nibbles(current_bytes, &pb);
139
+
140
+ checkSmallerThan0xF4(current_bytes, has_error);
141
+
142
+ __m128i initial_lengths = continuationLengths(pb.high_nibbles);
143
+
144
+ pb.carried_continuations =
145
+ carryContinuations(initial_lengths, previous->carried_continuations);
146
+
147
+ checkContinuations(initial_lengths, pb.carried_continuations, has_error);
148
+
149
+ __m128i off1_current_bytes =
150
+ _mm_alignr_epi8(pb.rawbytes, previous->rawbytes, 16 - 1);
151
+ checkFirstContinuationMax(current_bytes, off1_current_bytes, has_error);
152
+
153
+ checkOverlong(current_bytes, off1_current_bytes, pb.high_nibbles,
154
+ previous->high_nibbles, has_error);
155
+ return pb;
156
+ }
157
+
158
+ // alternative to _mm_testz_si128(x, x) (which requires SSE 4.1)
159
+ static inline bool is_all_zeros(__m128i xmm) {
160
+ return _mm_movemask_epi8(_mm_cmpeq_epi8(xmm, _mm_setzero_si128())) == 0xFFFF;
161
+ }
162
+
163
+ static bool validate_utf8_fast(const char *src, size_t len) {
164
+ size_t i = 0;
165
+ __m128i has_error = _mm_setzero_si128();
166
+ struct processed_utf_bytes previous = {.rawbytes = _mm_setzero_si128(),
167
+ .high_nibbles = _mm_setzero_si128(),
168
+ .carried_continuations =
169
+ _mm_setzero_si128()};
170
+ if (len >= 16) {
171
+ for (; i <= len - 16; i += 16) {
172
+ __m128i current_bytes = _mm_loadu_si128((const __m128i *)(src + i));
173
+ previous = checkUTF8Bytes(current_bytes, &previous, &has_error);
174
+ }
175
+ }
176
+
177
+ // last part
178
+ if (i < len) {
179
+ char buffer[16];
180
+ memset(buffer, 0, 16);
181
+ memcpy(buffer, src + i, len - i);
182
+ __m128i current_bytes = _mm_loadu_si128((const __m128i *)(buffer));
183
+ previous = checkUTF8Bytes(current_bytes, &previous, &has_error);
184
+ } else {
185
+ has_error =
186
+ _mm_or_si128(_mm_cmpgt_epi8(previous.carried_continuations,
187
+ _mm_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
188
+ 9, 9, 9, 9, 9, 1)),
189
+ has_error);
190
+ }
191
+
192
+ return is_all_zeros(has_error);
193
+ }
194
+
195
+ #ifdef __AVX2__
196
+
197
+ /*****************************/
198
+ static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b) {
199
+ return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15);
200
+ }
201
+
202
+ static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b) {
203
+ return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14);
204
+ }
205
+
206
+ // all byte values must be no larger than 0xF4
207
+ static inline void avxcheckSmallerThan0xF4(__m256i current_bytes,
208
+ __m256i *has_error) {
209
+ // unsigned, saturates to 0 below max
210
+ *has_error = _mm256_or_si256(
211
+ *has_error, _mm256_subs_epu8(current_bytes, _mm256_set1_epi8(0xF4)));
212
+ }
213
+
214
+ static inline __m256i avxcontinuationLengths(__m256i high_nibbles) {
215
+ return _mm256_shuffle_epi8(
216
+ _mm256_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
217
+ 0, 0, 0, 0, // 10xx (continuation)
218
+ 2, 2, // 110x
219
+ 3, // 1110
220
+ 4, // 1111, next should be 0 (not checked here)
221
+ 1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
222
+ 0, 0, 0, 0, // 10xx (continuation)
223
+ 2, 2, // 110x
224
+ 3, // 1110
225
+ 4 // 1111, next should be 0 (not checked here)
226
+ ),
227
+ high_nibbles);
228
+ }
229
+
230
+ static inline __m256i avxcarryContinuations(__m256i initial_lengths,
231
+ __m256i previous_carries) {
232
+
233
+ __m256i right1 = _mm256_subs_epu8(
234
+ push_last_byte_of_a_to_b(previous_carries, initial_lengths),
235
+ _mm256_set1_epi8(1));
236
+ __m256i sum = _mm256_add_epi8(initial_lengths, right1);
237
+
238
+ __m256i right2 = _mm256_subs_epu8(
239
+ push_last_2bytes_of_a_to_b(previous_carries, sum), _mm256_set1_epi8(2));
240
+ return _mm256_add_epi8(sum, right2);
241
+ }
242
+
243
+ static inline void avxcheckContinuations(__m256i initial_lengths,
244
+ __m256i carries, __m256i *has_error) {
245
+
246
+ // overlap || underlap
247
+ // carry > length && length > 0 || !(carry > length) && !(length > 0)
248
+ // (carries > length) == (lengths > 0)
249
+ __m256i overunder = _mm256_cmpeq_epi8(
250
+ _mm256_cmpgt_epi8(carries, initial_lengths),
251
+ _mm256_cmpgt_epi8(initial_lengths, _mm256_setzero_si256()));
252
+
253
+ *has_error = _mm256_or_si256(*has_error, overunder);
254
+ }
255
+
256
+ // when 0xED is found, next byte must be no larger than 0x9F
257
+ // when 0xF4 is found, next byte must be no larger than 0x8F
258
+ // next byte must be continuation, ie sign bit is set, so signed < is ok
259
+ static inline void avxcheckFirstContinuationMax(__m256i current_bytes,
260
+ __m256i off1_current_bytes,
261
+ __m256i *has_error) {
262
+ __m256i maskED =
263
+ _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xED));
264
+ __m256i maskF4 =
265
+ _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xF4));
266
+
267
+ __m256i badfollowED = _mm256_and_si256(
268
+ _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x9F)), maskED);
269
+ __m256i badfollowF4 = _mm256_and_si256(
270
+ _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x8F)), maskF4);
271
+
272
+ *has_error =
273
+ _mm256_or_si256(*has_error, _mm256_or_si256(badfollowED, badfollowF4));
274
+ }
275
+
276
+ // map off1_hibits => error condition
277
+ // hibits off1 cur
278
+ // C => < C2 && true
279
+ // E => < E1 && < A0
280
+ // F => < F1 && < 90
281
+ // else false && false
282
+ static inline void avxcheckOverlong(__m256i current_bytes,
283
+ __m256i off1_current_bytes, __m256i hibits,
284
+ __m256i previous_hibits,
285
+ __m256i *has_error) {
286
+ __m256i off1_hibits = push_last_byte_of_a_to_b(previous_hibits, hibits);
287
+ __m256i initial_mins = _mm256_shuffle_epi8(
288
+ _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128,
289
+ -128, -128, -128, // 10xx => false
290
+ 0xC2, -128, // 110x
291
+ 0xE1, // 1110
292
+ 0xF1, -128, -128, -128, -128, -128, -128, -128, -128,
293
+ -128, -128, -128, -128, // 10xx => false
294
+ 0xC2, -128, // 110x
295
+ 0xE1, // 1110
296
+ 0xF1),
297
+ off1_hibits);
298
+
299
+ __m256i initial_under = _mm256_cmpgt_epi8(initial_mins, off1_current_bytes);
300
+
301
+ __m256i second_mins = _mm256_shuffle_epi8(
302
+ _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128,
303
+ -128, -128, -128, // 10xx => false
304
+ 127, 127, // 110x => true
305
+ 0xA0, // 1110
306
+ 0x90, -128, -128, -128, -128, -128, -128, -128, -128,
307
+ -128, -128, -128, -128, // 10xx => false
308
+ 127, 127, // 110x => true
309
+ 0xA0, // 1110
310
+ 0x90),
311
+ off1_hibits);
312
+ __m256i second_under = _mm256_cmpgt_epi8(second_mins, current_bytes);
313
+ *has_error = _mm256_or_si256(*has_error,
314
+ _mm256_and_si256(initial_under, second_under));
315
+ }
316
+
317
+ struct avx_processed_utf_bytes {
318
+ __m256i rawbytes;
319
+ __m256i high_nibbles;
320
+ __m256i carried_continuations;
321
+ };
322
+
323
+ static inline void avx_count_nibbles(__m256i bytes,
324
+ struct avx_processed_utf_bytes *answer) {
325
+ answer->rawbytes = bytes;
326
+ answer->high_nibbles =
327
+ _mm256_and_si256(_mm256_srli_epi16(bytes, 4), _mm256_set1_epi8(0x0F));
328
+ }
329
+
330
+ // check whether the current bytes are valid UTF-8
331
+ // at the end of the function, previous gets updated
332
+ static struct avx_processed_utf_bytes
333
+ avxcheckUTF8Bytes(__m256i current_bytes,
334
+ struct avx_processed_utf_bytes *previous,
335
+ __m256i *has_error) {
336
+ struct avx_processed_utf_bytes pb;
337
+ avx_count_nibbles(current_bytes, &pb);
338
+
339
+ avxcheckSmallerThan0xF4(current_bytes, has_error);
340
+
341
+ __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles);
342
+
343
+ pb.carried_continuations =
344
+ avxcarryContinuations(initial_lengths, previous->carried_continuations);
345
+
346
+ avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error);
347
+
348
+ __m256i off1_current_bytes =
349
+ push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes);
350
+ avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error);
351
+
352
+ avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles,
353
+ previous->high_nibbles, has_error);
354
+ return pb;
355
+ }
356
+
357
+ // check whether the current bytes are valid UTF-8
358
+ // at the end of the function, previous gets updated
359
+ static struct avx_processed_utf_bytes
360
+ avxcheckUTF8Bytes_asciipath(__m256i current_bytes,
361
+ struct avx_processed_utf_bytes *previous,
362
+ __m256i *has_error) {
363
+ if (_mm256_testz_si256(current_bytes,
364
+ _mm256_set1_epi8(0x80))) { // fast ascii path
365
+ *has_error = _mm256_or_si256(
366
+ _mm256_cmpgt_epi8(previous->carried_continuations,
367
+ _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
368
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
369
+ 9, 9, 9, 9, 9, 9, 9, 1)),
370
+ *has_error);
371
+ return *previous;
372
+ }
373
+
374
+ struct avx_processed_utf_bytes pb;
375
+ avx_count_nibbles(current_bytes, &pb);
376
+
377
+ avxcheckSmallerThan0xF4(current_bytes, has_error);
378
+
379
+ __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles);
380
+
381
+ pb.carried_continuations =
382
+ avxcarryContinuations(initial_lengths, previous->carried_continuations);
383
+
384
+ avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error);
385
+
386
+ __m256i off1_current_bytes =
387
+ push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes);
388
+ avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error);
389
+
390
+ avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles,
391
+ previous->high_nibbles, has_error);
392
+ return pb;
393
+ }
394
+
395
+ static bool validate_utf8_fast_avx_asciipath(const char *src, size_t len) {
396
+ size_t i = 0;
397
+ __m256i has_error = _mm256_setzero_si256();
398
+ struct avx_processed_utf_bytes previous = {
399
+ .rawbytes = _mm256_setzero_si256(),
400
+ .high_nibbles = _mm256_setzero_si256(),
401
+ .carried_continuations = _mm256_setzero_si256()};
402
+ if (len >= 32) {
403
+ for (; i <= len - 32; i += 32) {
404
+ __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i));
405
+ previous =
406
+ avxcheckUTF8Bytes_asciipath(current_bytes, &previous, &has_error);
407
+ }
408
+ }
409
+
410
+ // last part
411
+ if (i < len) {
412
+ char buffer[32];
413
+ memset(buffer, 0, 32);
414
+ memcpy(buffer, src + i, len - i);
415
+ __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer));
416
+ previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error);
417
+ } else {
418
+ has_error = _mm256_or_si256(
419
+ _mm256_cmpgt_epi8(previous.carried_continuations,
420
+ _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
421
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
422
+ 9, 9, 9, 9, 9, 9, 9, 1)),
423
+ has_error);
424
+ }
425
+
426
+ return _mm256_testz_si256(has_error, has_error);
427
+ }
428
+
429
+ static bool validate_utf8_fast_avx(const char *src, size_t len) {
430
+ size_t i = 0;
431
+ __m256i has_error = _mm256_setzero_si256();
432
+ struct avx_processed_utf_bytes previous = {
433
+ .rawbytes = _mm256_setzero_si256(),
434
+ .high_nibbles = _mm256_setzero_si256(),
435
+ .carried_continuations = _mm256_setzero_si256()};
436
+ if (len >= 32) {
437
+ for (; i <= len - 32; i += 32) {
438
+ __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i));
439
+ previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error);
440
+ }
441
+ }
442
+
443
+ // last part
444
+ if (i < len) {
445
+ char buffer[32];
446
+ memset(buffer, 0, 32);
447
+ memcpy(buffer, src + i, len - i);
448
+ __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer));
449
+ previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error);
450
+ } else {
451
+ has_error = _mm256_or_si256(
452
+ _mm256_cmpgt_epi8(previous.carried_continuations,
453
+ _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
454
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
455
+ 9, 9, 9, 9, 9, 9, 9, 1)),
456
+ has_error);
457
+ }
458
+
459
+ return _mm256_testz_si256(has_error, has_error);
460
+ }
461
+
462
+ #endif // __AVX2__
463
+ #endif