utf8_range.c

filter: Statementstmt 58.1% 118 / 203 Branchbran 47.4% 55 / 116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 54.2% 173 / 319 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Cpanel/JSON/XS/Boolean.pm ↑ Summary XS.xs »
1 /* utf8_range.c -- SIMD UTF-8 validation, vendored as explained in
2 * utf8_range.h. Compiled as its own object (see Makefile.PL) so the SIMD
3 * bodies stay out of the parser's translation unit.
4 */
5 #include "utf8_range.h"
6  
7 #include <stdint.h>
8 #include <limits.h>
9  
10 /* x86 needs the target attribute plus __builtin_cpu_supports(), i.e. GCC 4.9
11 * or clang 4; MSVC and older compilers fall back to the scalar path. NEON is
12 * baseline on aarch64, so no dispatch is needed there. */
13 #if defined(__x86_64__) || defined(__i386__)
14 # if defined(__clang__)
15 # if __clang_major__ >= 4 && (!defined __apple_build_version__ || __apple_build_version__ > 5030040)
16 # define CJSON_HAVE_X86_SIMD 1
17 # endif
18 # elif defined(__GNUC__)
19 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
20 # define CJSON_HAVE_X86_SIMD 1
21 # endif
22 # endif
23 #endif
24  
25 #if defined(__aarch64__) && (defined(__GNUC__) || defined(__clang__))
26 # define CJSON_HAVE_NEON 1
27 #endif
28  
29 /* Portable scalar validation. Returns 0 if valid, else the 1-based index of
30 * the first offending character (only used as a boolean here). */
31  
32  
33 /*
34 * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94
35 *
36 * Table 3-7. Well-Formed UTF-8 Byte Sequences
37 *
38 * +--------------------+------------+-------------+------------+-------------+
39 * | Code Points | First Byte | Second Byte | Third Byte | Fourth Byte |
40 * +--------------------+------------+-------------+------------+-------------+
41 * | U+0000..U+007F | 00..7F | | | |
42 * +--------------------+------------+-------------+------------+-------------+
43 * | U+0080..U+07FF | C2..DF | 80..BF | | |
44 * +--------------------+------------+-------------+------------+-------------+
45 * | U+0800..U+0FFF | E0 | A0..BF | 80..BF | |
46 * +--------------------+------------+-------------+------------+-------------+
47 * | U+1000..U+CFFF | E1..EC | 80..BF | 80..BF | |
48 * +--------------------+------------+-------------+------------+-------------+
49 * | U+D000..U+D7FF | ED | 80..9F | 80..BF | |
50 * +--------------------+------------+-------------+------------+-------------+
51 * | U+E000..U+FFFF | EE..EF | 80..BF | 80..BF | |
52 * +--------------------+------------+-------------+------------+-------------+
53 * | U+10000..U+3FFFF | F0 | 90..BF | 80..BF | 80..BF |
54 * +--------------------+------------+-------------+------------+-------------+
55 * | U+40000..U+FFFFF | F1..F3 | 80..BF | 80..BF | 80..BF |
56 * +--------------------+------------+-------------+------------+-------------+
57 * | U+100000..U+10FFFF | F4 | 80..8F | 80..BF | 80..BF |
58 * +--------------------+------------+-------------+------------+-------------+
59 */
60  
61 /* Return 0 - success, >0 - index(1 based) of first error char */
62 70 static int cjson_utf8_naive(const unsigned char *data, int len)
63 {
64 70 int err_pos = 1;
65  
66 690 ▶ while (len) {
Branch: 1 branch
BranchTrueFalseTotal
while (len) { 621 69 690
67 int bytes;
68 621 const unsigned char byte1 = data[0];
69  
70 /* 00..7F */
71 621 ▶ if (byte1 <= 0x7F) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte1 <= 0x7F) { 317 304 621
72 317 bytes = 1;
73 /* C2..DF, 80..BF */
74 304 ▶ } else if (len >= 2 && byte1 >= 0xC2 && byte1 <= 0xDF &&
Branch: 3 branches
BranchTrueFalseTotal
} else if (len >= 2 && byte1 >= 0xC2 && byte1 <= 0xDF && 304 0 304
} else if (len >= 2 && byte1 >= 0xC2 && byte1 <= 0xDF && 304 0 304
} else if (len >= 2 && byte1 >= 0xC2 && byte1 <= 0xDF && 206 98 304
75 206 ▶ (signed char)data[1] <= (signed char)0xBF) {
Branch: 1 branch
BranchTrueFalseTotal
(signed char)data[1] <= (signed char)0xBF) { 206 0 206
76 206 bytes = 2;
77 98 ▶ } else if (len >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len >= 3) { 98 0 98
78 98 const unsigned char byte2 = data[1];
79  
80 /* Is byte2, byte3 between 0x80 ~ 0xBF */
81 98 const int byte2_ok = (signed char)byte2 <= (signed char)0xBF;
82 98 const int byte3_ok = (signed char)data[2] <= (signed char)0xBF;
83  
84 98 ▶ if (byte2_ok && byte3_ok &&
Branch: 3 branches
BranchTrueFalseTotal
if (byte2_ok && byte3_ok && 97 1 98
if (byte2_ok && byte3_ok && 97 0 97
if (byte2_ok && byte3_ok && 10 87 97
85 /* E0, A0..BF, 80..BF */
86 97 ▶ ((byte1 == 0xE0 && byte2 >= 0xA0) ||
Branch: 2 branches
BranchTrueFalseTotal
((byte1 == 0xE0 && byte2 >= 0xA0) || 0 10 10
((byte1 == 0xE0 && byte2 >= 0xA0) || 87 0 87
87 /* E1..EC, 80..BF, 80..BF */
88 87 ▶ (byte1 >= 0xE1 && byte1 <= 0xEC) ||
Branch: 2 branches
BranchTrueFalseTotal
(byte1 >= 0xE1 && byte1 <= 0xEC) || 79 8 87
(byte1 >= 0xE1 && byte1 <= 0xEC) || 0 79 79
89 /* ED, 80..9F, 80..BF */
90 79 ▶ (byte1 == 0xED && byte2 <= 0x9F) ||
Branch: 2 branches
BranchTrueFalseTotal
(byte1 == 0xED && byte2 <= 0x9F) || 0 0 0
(byte1 == 0xED && byte2 <= 0x9F) || 79 0 79
91 /* EE..EF, 80..BF, 80..BF */
92 79 ▶ (byte1 >= 0xEE && byte1 <= 0xEF))) {
Branch: 1 branch
BranchTrueFalseTotal
(byte1 >= 0xEE && byte1 <= 0xEF))) { 0 79 79
93 18 bytes = 3;
94 80 ▶ } else if (len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len >= 4) { 80 0 80
95 /* Is byte4 between 0x80 ~ 0xBF */
96 80 const int byte4_ok = (signed char)data[3] <= (signed char)0xBF;
97  
98 80 ▶ if (byte2_ok && byte3_ok && byte4_ok &&
Branch: 4 branches
BranchTrueFalseTotal
if (byte2_ok && byte3_ok && byte4_ok && 79 1 80
if (byte2_ok && byte3_ok && byte4_ok && 79 0 79
if (byte2_ok && byte3_ok && byte4_ok && 79 0 79
if (byte2_ok && byte3_ok && byte4_ok && 18 61 79
99 /* F0, 90..BF, 80..BF, 80..BF */
100 79 ▶ ((byte1 == 0xF0 && byte2 >= 0x90) ||
Branch: 2 branches
BranchTrueFalseTotal
((byte1 == 0xF0 && byte2 >= 0x90) || 0 18 18
((byte1 == 0xF0 && byte2 >= 0x90) || 61 0 61
101 /* F1..F3, 80..BF, 80..BF, 80..BF */
102 61 ▶ (byte1 >= 0xF1 && byte1 <= 0xF3) ||
Branch: 2 branches
BranchTrueFalseTotal
(byte1 >= 0xF1 && byte1 <= 0xF3) || 0 61 61
(byte1 >= 0xF1 && byte1 <= 0xF3) || 0 0 0
103 /* F4, 80..8F, 80..BF, 80..BF */
104 0 ▶ (byte1 == 0xF4 && byte2 <= 0x8F))) {
Branch: 1 branch
BranchTrueFalseTotal
(byte1 == 0xF4 && byte2 <= 0x8F))) { 0 0 0
105 79 bytes = 4;
106 } else {
107 1 return err_pos;
108 }
109 } else {
110 0 return err_pos;
111 }
112 } else {
113 0 return err_pos;
114 }
115  
116 620 len -= bytes;
117 620 err_pos += bytes;
118 620 data += bytes;
119 }
120  
121 69 return 0;
122 }
123  
124 /* ------------------------------------------------------------------ x86 --- */
125 #ifdef CJSON_HAVE_X86_SIMD
126 # include <immintrin.h>
127 # define CJSON_SIMD_FN(opts) __attribute__((target(opts)))
128 #endif
129  
130 #ifdef CJSON_HAVE_X86_SIMD
131  
132  
133  
134  
135 /*
136 * Map high nibble of "First Byte" to legal character length minus 1
137 * 0x00 ~ 0xBF --> 0
138 * 0xC0 ~ 0xDF --> 1
139 * 0xE0 ~ 0xEF --> 2
140 * 0xF0 ~ 0xFF --> 3
141 */
142 static const int8_t cjson_sse_first_len_tbl[] = {
143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
144 };
145  
146 /* Map "First Byte" to 8-th item of range table (0xC2 ~ 0xF4) */
147 static const int8_t cjson_sse_first_range_tbl[] = {
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
149 };
150  
151 /*
152 * Range table, map range index to min and max values
153 * Index 0 : 00 ~ 7F (First Byte, ascii)
154 * Index 1,2,3: 80 ~ BF (Second, Third, Fourth Byte)
155 * Index 4 : A0 ~ BF (Second Byte after E0)
156 * Index 5 : 80 ~ 9F (Second Byte after ED)
157 * Index 6 : 90 ~ BF (Second Byte after F0)
158 * Index 7 : 80 ~ 8F (Second Byte after F4)
159 * Index 8 : C2 ~ F4 (First Byte, non ascii)
160 * Index 9~15 : illegal: i >= 127 && i <= -128
161 */
162 static const int8_t cjson_sse_range_min_tbl[] = {
163 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
164 0xC2, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
165 };
166 static const int8_t cjson_sse_range_max_tbl[] = {
167 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
168 0xF4, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
169 };
170  
171 /*
172 * Tables for fast handling of four special First Bytes(E0,ED,F0,F4), after
173 * which the Second Byte are not 80~BF. It contains "range index adjustment".
174 * +------------+---------------+------------------+----------------+
175 * | First Byte | original range| range adjustment | adjusted range |
176 * +------------+---------------+------------------+----------------+
177 * | E0 | 2 | 2 | 4 |
178 * +------------+---------------+------------------+----------------+
179 * | ED | 2 | 3 | 5 |
180 * +------------+---------------+------------------+----------------+
181 * | F0 | 3 | 3 | 6 |
182 * +------------+---------------+------------------+----------------+
183 * | F4 | 4 | 4 | 8 |
184 * +------------+---------------+------------------+----------------+
185 */
186 /* index1 -> E0, index14 -> ED */
187 static const int8_t cjson_sse_df_ee_tbl[] = {
188 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
189 };
190 /* index1 -> F0, index5 -> F4 */
191 static const int8_t cjson_sse_ef_fe_tbl[] = {
192 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193 };
194  
195 #define RET_ERR_IDX 0 /* Define 1 to return index of first error char */
196  
197 /* 5x faster than naive method */
198 /* Return 0 - success, -1 - error, >0 - first error char(if RET_ERR_IDX = 1) */
199 CJSON_SIMD_FN("ssse3,sse4.1")
200 0 static int cjson_utf8_range_sse(const unsigned char *data, int len)
201 {
202 #if RET_ERR_IDX
203 int err_pos = 1;
204 #endif
205  
206 0 ▶ if (len >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 16) { 0 0 0
207 0 __m128i prev_input = _mm_set1_epi8(0);
208 0 __m128i prev_first_len = _mm_set1_epi8(0);
209  
210 /* Cached tables */
211 const __m128i first_len_tbl =
212 0 _mm_loadu_si128((const __m128i *)cjson_sse_first_len_tbl);
213 const __m128i first_range_tbl =
214 0 _mm_loadu_si128((const __m128i *)cjson_sse_first_range_tbl);
215 const __m128i range_min_tbl =
216 0 _mm_loadu_si128((const __m128i *)cjson_sse_range_min_tbl);
217 const __m128i range_max_tbl =
218 0 _mm_loadu_si128((const __m128i *)cjson_sse_range_max_tbl);
219 const __m128i df_ee_tbl =
220 0 _mm_loadu_si128((const __m128i *)cjson_sse_df_ee_tbl);
221 const __m128i ef_fe_tbl =
222 0 _mm_loadu_si128((const __m128i *)cjson_sse_ef_fe_tbl);
223  
224 0 __m128i error = _mm_set1_epi8(0);
225  
226 0 ▶ while (len >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 16) { 0 0 0
227 /* declarations hoisted to the block top: perl built before 5.36
228 passes -Werror=declaration-after-statement */
229 __m128i tmp, shift1, pos, range2, minv, maxv;
230 0 const __m128i input = _mm_loadu_si128((const __m128i *)data);
231  
232 /* high_nibbles = input >> 4 */
233 const __m128i high_nibbles =
234 0 _mm_and_si128(_mm_srli_epi16(input, 4), _mm_set1_epi8(0x0F));
235  
236 /* first_len = legal character length minus 1 */
237 /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
238 /* first_len = first_len_tbl[high_nibbles] */
239 0 __m128i first_len = _mm_shuffle_epi8(first_len_tbl, high_nibbles);
240  
241 /* First Byte: set range index to 8 for bytes within 0xC0 ~ 0xFF */
242 /* range = first_range_tbl[high_nibbles] */
243 0 __m128i range = _mm_shuffle_epi8(first_range_tbl, high_nibbles);
244  
245 /* Second Byte: set range index to first_len */
246 /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
247 /* range |= (first_len, prev_first_len) << 1 byte */
248 0 range = _mm_or_si128(
249 0 range, _mm_alignr_epi8(first_len, prev_first_len, 15));
250  
251 /* Third Byte: set range index to saturate_sub(first_len, 1) */
252 /* 0 for 00~7F, 0 for C0~DF, 1 for E0~EF, 2 for F0~FF */
253 /* tmp = (first_len, prev_first_len) << 2 bytes */
254 0 tmp = _mm_alignr_epi8(first_len, prev_first_len, 14);
255 /* tmp = saturate_sub(tmp, 1) */
256 0 tmp = _mm_subs_epu8(tmp, _mm_set1_epi8(1));
257 /* range |= tmp */
258 0 range = _mm_or_si128(range, tmp);
259  
260 /* Fourth Byte: set range index to saturate_sub(first_len, 2) */
261 /* 0 for 00~7F, 0 for C0~DF, 0 for E0~EF, 1 for F0~FF */
262 /* tmp = (first_len, prev_first_len) << 3 bytes */
263 0 tmp = _mm_alignr_epi8(first_len, prev_first_len, 13);
264 /* tmp = saturate_sub(tmp, 2) */
265 0 tmp = _mm_subs_epu8(tmp, _mm_set1_epi8(2));
266 /* range |= tmp */
267 0 range = _mm_or_si128(range, tmp);
268  
269 /*
270 * Now we have below range indices caluclated
271 * Correct cases:
272 * - 8 for C0~FF
273 * - 3 for 1st byte after F0~FF
274 * - 2 for 1st byte after E0~EF or 2nd byte after F0~FF
275 * - 1 for 1st byte after C0~DF or 2nd byte after E0~EF or
276 * 3rd byte after F0~FF
277 * - 0 for others
278 * Error cases:
279 * 9,10,11 if non ascii First Byte overlaps
280 * E.g., F1 80 C2 90 --> 8 3 10 2, where 10 indicates error
281 */
282  
283 /* Adjust Second Byte range for special First Bytes(E0,ED,F0,F4) */
284 /* Overlaps lead to index 9~15, which are illegal in range table */
285 /* shift1 = (input, prev_input) << 1 byte */
286 0 shift1 = _mm_alignr_epi8(input, prev_input, 15);
287 0 pos = _mm_sub_epi8(shift1, _mm_set1_epi8(0xEF));
288 /*
289 * shift1: | EF F0 ... FE | FF 00 ... ... DE | DF E0 ... EE |
290 * pos: | 0 1 15 | 16 17 239| 240 241 255|
291 * pos-240: | 0 0 0 | 0 0 0 | 0 1 15 |
292 * pos+112: | 112 113 127| >= 128 | >= 128 |
293 */
294 0 tmp = _mm_subs_epu8(pos, _mm_set1_epi8(0xF0));
295 0 range2 = _mm_shuffle_epi8(df_ee_tbl, tmp);
296 0 tmp = _mm_adds_epu8(pos, _mm_set1_epi8(0x70));
297 0 range2 = _mm_add_epi8(range2, _mm_shuffle_epi8(ef_fe_tbl, tmp));
298  
299 0 range = _mm_add_epi8(range, range2);
300  
301 /* Load min and max values per calculated range index */
302 0 minv = _mm_shuffle_epi8(range_min_tbl, range);
303 0 maxv = _mm_shuffle_epi8(range_max_tbl, range);
304  
305 /* Check value range */
306 #if RET_ERR_IDX
307 error = _mm_cmplt_epi8(input, minv);
308 error = _mm_or_si128(error, _mm_cmpgt_epi8(input, maxv));
309 /* 5% performance drop from this conditional branch */
310 if (!_mm_testz_si128(error, error))
311 break;
312 #else
313 /* error |= (input < minv) | (input > maxv) */
314 0 tmp = _mm_or_si128(
315 _mm_cmplt_epi8(input, minv),
316 _mm_cmpgt_epi8(input, maxv)
317 );
318 0 error = _mm_or_si128(error, tmp);
319 #endif
320  
321 0 prev_input = input;
322 0 prev_first_len = first_len;
323  
324 0 data += 16;
325 0 len -= 16;
326 #if RET_ERR_IDX
327 err_pos += 16;
328 #endif
329 }
330  
331 #if RET_ERR_IDX
332 /* Error in first 16 bytes */
333 if (err_pos == 1)
334 goto do_naive;
335 #else
336 0 ▶ if (!_mm_testz_si128(error, error))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mm_testz_si128(error, error)) 0 0 0
337 0 return -1;
338 #endif
339  
340 /* Find previous token (not 80~BF) */
341 {
342 0 int32_t token4 = _mm_extract_epi32(prev_input, 3);
343 0 const int8_t *token = (const int8_t *)&token4;
344 0 int lookahead = 0;
345 0 ▶ if (token[3] > (int8_t)0xBF)
Branch: 1 branch
BranchTrueFalseTotal
if (token[3] > (int8_t)0xBF) 0 0 0
346 0 lookahead = 1;
347 0 ▶ else if (token[2] > (int8_t)0xBF)
Branch: 1 branch
BranchTrueFalseTotal
else if (token[2] > (int8_t)0xBF) 0 0 0
348 0 lookahead = 2;
349 0 ▶ else if (token[1] > (int8_t)0xBF)
Branch: 1 branch
BranchTrueFalseTotal
else if (token[1] > (int8_t)0xBF) 0 0 0
350 0 lookahead = 3;
351  
352 0 data -= lookahead;
353 0 len += lookahead;
354 #if RET_ERR_IDX
355 err_pos -= lookahead;
356 #endif
357 }
358 }
359  
360 /* Check remaining bytes with naive method */
361 #if RET_ERR_IDX
362 int err_pos2;
363 do_naive:
364 err_pos2 = cjson_utf8_naive(data, len);
365 if (err_pos2)
366 return err_pos + err_pos2 - 1;
367 return 0;
368 #else
369 0 return cjson_utf8_naive(data, len);
370 #endif
371 }
372 #endif /* CJSON_HAVE_X86_SIMD */
373 #ifdef CJSON_HAVE_X86_SIMD
374  
375  
376  
377  
378 /*
379 * Map high nibble of "First Byte" to legal character length minus 1
380 * 0x00 ~ 0xBF --> 0
381 * 0xC0 ~ 0xDF --> 1
382 * 0xE0 ~ 0xEF --> 2
383 * 0xF0 ~ 0xFF --> 3
384 */
385 static const int8_t cjson_avx2_first_len_tbl[] = {
386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
388 };
389  
390 /* Map "First Byte" to 8-th item of range table (0xC2 ~ 0xF4) */
391 static const int8_t cjson_avx2_first_range_tbl[] = {
392 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
394 };
395  
396 /*
397 * Range table, map range index to min and max values
398 * Index 0 : 00 ~ 7F (First Byte, ascii)
399 * Index 1,2,3: 80 ~ BF (Second, Third, Fourth Byte)
400 * Index 4 : A0 ~ BF (Second Byte after E0)
401 * Index 5 : 80 ~ 9F (Second Byte after ED)
402 * Index 6 : 90 ~ BF (Second Byte after F0)
403 * Index 7 : 80 ~ 8F (Second Byte after F4)
404 * Index 8 : C2 ~ F4 (First Byte, non ascii)
405 * Index 9~15 : illegal: i >= 127 && i <= -128
406 */
407 static const int8_t cjson_avx2_range_min_tbl[] = {
408 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
409 0xC2, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
410 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
411 0xC2, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
412 };
413 static const int8_t cjson_avx2_range_max_tbl[] = {
414 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
415 0xF4, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
416 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
417 0xF4, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
418 };
419  
420 /*
421 * Tables for fast handling of four special First Bytes(E0,ED,F0,F4), after
422 * which the Second Byte are not 80~BF. It contains "range index adjustment".
423 * +------------+---------------+------------------+----------------+
424 * | First Byte | original range| range adjustment | adjusted range |
425 * +------------+---------------+------------------+----------------+
426 * | E0 | 2 | 2 | 4 |
427 * +------------+---------------+------------------+----------------+
428 * | ED | 2 | 3 | 5 |
429 * +------------+---------------+------------------+----------------+
430 * | F0 | 3 | 3 | 6 |
431 * +------------+---------------+------------------+----------------+
432 * | F4 | 4 | 4 | 8 |
433 * +------------+---------------+------------------+----------------+
434 */
435 /* index1 -> E0, index14 -> ED */
436 static const int8_t cjson_avx2_df_ee_tbl[] = {
437 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
438 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
439 };
440 /* index1 -> F0, index5 -> F4 */
441 static const int8_t cjson_avx2_ef_fe_tbl[] = {
442 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
443 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
444 };
445  
446 #define RET_ERR_IDX 0 /* Define 1 to return index of first error char */
447  
448 CJSON_SIMD_FN("avx2")
449 210 static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b) {
450 210 return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15);
451 }
452  
453 CJSON_SIMD_FN("avx2")
454 105 static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b) {
455 105 return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14);
456 }
457  
458 CJSON_SIMD_FN("avx2")
459 105 static inline __m256i push_last_3bytes_of_a_to_b(__m256i a, __m256i b) {
460 105 return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 13);
461 }
462  
463 /* 5x faster than naive method */
464 /* Return 0 - success, -1 - error, >0 - first error char(if RET_ERR_IDX = 1) */
465 CJSON_SIMD_FN("avx2")
466 74 static int cjson_utf8_range_avx2(const unsigned char *data, int len)
467 {
468 #if RET_ERR_IDX
469 int err_pos = 1;
470 #endif
471  
472 74 ▶ if (len >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 32) { 53 21 74
473 53 __m256i prev_input = _mm256_set1_epi8(0);
474 53 __m256i prev_first_len = _mm256_set1_epi8(0);
475  
476 /* Cached tables */
477 const __m256i first_len_tbl =
478 53 _mm256_loadu_si256((const __m256i *)cjson_avx2_first_len_tbl);
479 const __m256i first_range_tbl =
480 53 _mm256_loadu_si256((const __m256i *)cjson_avx2_first_range_tbl);
481 const __m256i range_min_tbl =
482 53 _mm256_loadu_si256((const __m256i *)cjson_avx2_range_min_tbl);
483 const __m256i range_max_tbl =
484 53 _mm256_loadu_si256((const __m256i *)cjson_avx2_range_max_tbl);
485 const __m256i df_ee_tbl =
486 53 _mm256_loadu_si256((const __m256i *)cjson_avx2_df_ee_tbl);
487 const __m256i ef_fe_tbl =
488 53 _mm256_loadu_si256((const __m256i *)cjson_avx2_ef_fe_tbl);
489  
490 #if !RET_ERR_IDX
491 53 __m256i error1 = _mm256_set1_epi8(0);
492 53 __m256i error2 = _mm256_set1_epi8(0);
493 __m256i error;
494 #endif
495  
496 158 ▶ while (len >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 32) { 105 53 158
497 /* declarations hoisted to the block top, see the SSE version */
498 __m256i tmp1, tmp2, shift1, pos, range2, minv, maxv;
499 105 const __m256i input = _mm256_loadu_si256((const __m256i *)data);
500  
501 /* high_nibbles = input >> 4 */
502 const __m256i high_nibbles =
503 315 _mm256_and_si256(_mm256_srli_epi16(input, 4), _mm256_set1_epi8(0x0F));
504  
505 /* first_len = legal character length minus 1 */
506 /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
507 /* first_len = first_len_tbl[high_nibbles] */
508 105 __m256i first_len = _mm256_shuffle_epi8(first_len_tbl, high_nibbles);
509  
510 /* First Byte: set range index to 8 for bytes within 0xC0 ~ 0xFF */
511 /* range = first_range_tbl[high_nibbles] */
512 105 __m256i range = _mm256_shuffle_epi8(first_range_tbl, high_nibbles);
513  
514 /* Second Byte: set range index to first_len */
515 /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
516 /* range |= (first_len, prev_first_len) << 1 byte */
517 105 range = _mm256_or_si256(
518 range, push_last_byte_of_a_to_b(prev_first_len, first_len));
519  
520 /* Third Byte: set range index to saturate_sub(first_len, 1) */
521 /* 0 for 00~7F, 0 for C0~DF, 1 for E0~EF, 2 for F0~FF */
522  
523 /* tmp1 = (first_len, prev_first_len) << 2 bytes */
524 105 tmp1 = push_last_2bytes_of_a_to_b(prev_first_len, first_len);
525 /* tmp2 = saturate_sub(tmp1, 1) */
526 210 tmp2 = _mm256_subs_epu8(tmp1, _mm256_set1_epi8(1));
527  
528 /* range |= tmp2 */
529 105 range = _mm256_or_si256(range, tmp2);
530  
531 /* Fourth Byte: set range index to saturate_sub(first_len, 2) */
532 /* 0 for 00~7F, 0 for C0~DF, 0 for E0~EF, 1 for F0~FF */
533 /* tmp1 = (first_len, prev_first_len) << 3 bytes */
534 105 tmp1 = push_last_3bytes_of_a_to_b(prev_first_len, first_len);
535 /* tmp2 = saturate_sub(tmp1, 2) */
536 210 tmp2 = _mm256_subs_epu8(tmp1, _mm256_set1_epi8(2));
537 /* range |= tmp2 */
538 105 range = _mm256_or_si256(range, tmp2);
539  
540 /*
541 * Now we have below range indices caluclated
542 * Correct cases:
543 * - 8 for C0~FF
544 * - 3 for 1st byte after F0~FF
545 * - 2 for 1st byte after E0~EF or 2nd byte after F0~FF
546 * - 1 for 1st byte after C0~DF or 2nd byte after E0~EF or
547 * 3rd byte after F0~FF
548 * - 0 for others
549 * Error cases:
550 * 9,10,11 if non ascii First Byte overlaps
551 * E.g., F1 80 C2 90 --> 8 3 10 2, where 10 indicates error
552 */
553  
554 /* Adjust Second Byte range for special First Bytes(E0,ED,F0,F4) */
555 /* Overlaps lead to index 9~15, which are illegal in range table */
556 /* shift1 = (input, prev_input) << 1 byte */
557 105 shift1 = push_last_byte_of_a_to_b(prev_input, input);
558 210 pos = _mm256_sub_epi8(shift1, _mm256_set1_epi8(0xEF));
559 /*
560 * shift1: | EF F0 ... FE | FF 00 ... ... DE | DF E0 ... EE |
561 * pos: | 0 1 15 | 16 17 239| 240 241 255|
562 * pos-240: | 0 0 0 | 0 0 0 | 0 1 15 |
563 * pos+112: | 112 113 127| >= 128 | >= 128 |
564 */
565 210 tmp1 = _mm256_subs_epu8(pos, _mm256_set1_epi8(240));
566 105 range2 = _mm256_shuffle_epi8(df_ee_tbl, tmp1);
567 210 tmp2 = _mm256_adds_epu8(pos, _mm256_set1_epi8(112));
568 210 range2 = _mm256_add_epi8(range2, _mm256_shuffle_epi8(ef_fe_tbl, tmp2));
569  
570 105 range = _mm256_add_epi8(range, range2);
571  
572 /* Load min and max values per calculated range index */
573 105 minv = _mm256_shuffle_epi8(range_min_tbl, range);
574 105 maxv = _mm256_shuffle_epi8(range_max_tbl, range);
575  
576 /* Check value range */
577 #if RET_ERR_IDX
578 __m256i error = _mm256_cmpgt_epi8(minv, input);
579 error = _mm256_or_si256(error, _mm256_cmpgt_epi8(input, maxv));
580 /* 5% performance drop from this conditional branch */
581 if (!_mm256_testz_si256(error, error))
582 break;
583 #else
584 210 error1 = _mm256_or_si256(error1, _mm256_cmpgt_epi8(minv, input));
585 105 error2 = _mm256_or_si256(error2, _mm256_cmpgt_epi8(input, maxv));
586 #endif
587  
588 105 prev_input = input;
589 105 prev_first_len = first_len;
590  
591 105 data += 32;
592 105 len -= 32;
593 #if RET_ERR_IDX
594 err_pos += 32;
595 #endif
596 }
597  
598 #if RET_ERR_IDX
599 /* Error in first 16 bytes */
600 if (err_pos == 1)
601 goto do_naive;
602 #else
603 53 error = _mm256_or_si256(error1, error2);
604 53 ▶ if (!_mm256_testz_si256(error, error))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mm256_testz_si256(error, error)) 4 49 53
605 4 return -1;
606 #endif
607  
608 /* Find previous token (not 80~BF) */
609 {
610 49 int32_t token4 = _mm256_extract_epi32(prev_input, 7);
611 49 const int8_t *token = (const int8_t *)&token4;
612 49 int lookahead = 0;
613 49 ▶ if (token[3] > (int8_t)0xBF)
Branch: 1 branch
BranchTrueFalseTotal
if (token[3] > (int8_t)0xBF) 18 31 49
614 18 lookahead = 1;
615 31 ▶ else if (token[2] > (int8_t)0xBF)
Branch: 1 branch
BranchTrueFalseTotal
else if (token[2] > (int8_t)0xBF) 20 11 31
616 20 lookahead = 2;
617 11 ▶ else if (token[1] > (int8_t)0xBF)
Branch: 1 branch
BranchTrueFalseTotal
else if (token[1] > (int8_t)0xBF) 2 9 11
618 2 lookahead = 3;
619  
620 49 data -= lookahead;
621 49 len += lookahead;
622 #if RET_ERR_IDX
623 err_pos -= lookahead;
624 #endif
625 }
626 }
627  
628 /* Check remaining bytes with naive method */
629 #if RET_ERR_IDX
630 int err_pos2;
631 do_naive:
632 err_pos2 = cjson_utf8_naive(data, len);
633 if (err_pos2)
634 return err_pos + err_pos2 - 1;
635 return 0;
636 #else
637 70 return cjson_utf8_naive(data, len);
638 #endif
639 }
640 #endif /* CJSON_HAVE_X86_SIMD */
641  
642 /* ------------------------------------------------------------------ ARM --- */
643 #ifdef CJSON_HAVE_NEON
644 # include <arm_neon.h>
645 # define CJSON_SIMD_FN(opts)
646 #endif
647  
648 #ifdef CJSON_HAVE_NEON
649  
650  
651  
652  
653 /*
654 * Map high nibble of "First Byte" to legal character length minus 1
655 * 0x00 ~ 0xBF --> 0
656 * 0xC0 ~ 0xDF --> 1
657 * 0xE0 ~ 0xEF --> 2
658 * 0xF0 ~ 0xFF --> 3
659 */
660 static const uint8_t cjson_neon_first_len_tbl[] = {
661 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3,
662 };
663  
664 /* Map "First Byte" to 8-th item of range table (0xC2 ~ 0xF4) */
665 static const uint8_t cjson_neon_first_range_tbl[] = {
666 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8,
667 };
668  
669 /*
670 * Range table, map range index to min and max values
671 * Index 0 : 00 ~ 7F (First Byte, ascii)
672 * Index 1,2,3: 80 ~ BF (Second, Third, Fourth Byte)
673 * Index 4 : A0 ~ BF (Second Byte after E0)
674 * Index 5 : 80 ~ 9F (Second Byte after ED)
675 * Index 6 : 90 ~ BF (Second Byte after F0)
676 * Index 7 : 80 ~ 8F (Second Byte after F4)
677 * Index 8 : C2 ~ F4 (First Byte, non ascii)
678 * Index 9~15 : illegal: u >= 255 && u <= 0
679 */
680 static const uint8_t cjson_neon_range_min_tbl[] = {
681 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80,
682 0xC2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
683 };
684 static const uint8_t cjson_neon_range_max_tbl[] = {
685 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F,
686 0xF4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
687 };
688  
689 /*
690 * This table is for fast handling four special First Bytes(E0,ED,F0,F4), after
691 * which the Second Byte are not 80~BF. It contains "range index adjustment".
692 * - The idea is to minus byte with E0, use the result(0~31) as the index to
693 * lookup the "range index adjustment". Then add the adjustment to original
694 * range index to get the correct range.
695 * - Range index adjustment
696 * +------------+---------------+------------------+----------------+
697 * | First Byte | original range| range adjustment | adjusted range |
698 * +------------+---------------+------------------+----------------+
699 * | E0 | 2 | 2 | 4 |
700 * +------------+---------------+------------------+----------------+
701 * | ED | 2 | 3 | 5 |
702 * +------------+---------------+------------------+----------------+
703 * | F0 | 3 | 3 | 6 |
704 * +------------+---------------+------------------+----------------+
705 * | F4 | 4 | 4 | 8 |
706 * +------------+---------------+------------------+----------------+
707 * - Below is a uint8x16x2 table, data is interleaved in NEON register. So I'm
708 * putting it vertically. 1st column is for E0~EF, 2nd column for F0~FF.
709 */
710 static const uint8_t cjson_neon_range_adjust_tbl[] = {
711 /* index -> 0~15 16~31 <- index */
712 /* E0 -> */ 2, 3, /* <- F0 */
713 0, 0,
714 0, 0,
715 0, 0,
716 0, 4, /* <- F4 */
717 0, 0,
718 0, 0,
719 0, 0,
720 0, 0,
721 0, 0,
722 0, 0,
723 0, 0,
724 0, 0,
725 /* ED -> */ 3, 0,
726 0, 0,
727 0, 0,
728 };
729  
730 /* 2x ~ 4x faster than naive method */
731 /* Return 0 on success, -1 on error */
732 static int cjson_utf8_range_neon(const unsigned char *data, int len)
733 {
734 if (len >= 16) {
735 uint32_t token4;
736 uint8x16_t prev_input = vdupq_n_u8(0);
737 uint8x16_t prev_first_len = vdupq_n_u8(0);
738  
739 /* Cached tables */
740 const uint8x16_t first_len_tbl = vld1q_u8(cjson_neon_first_len_tbl);
741 const uint8x16_t first_range_tbl = vld1q_u8(cjson_neon_first_range_tbl);
742 const uint8x16_t range_min_tbl = vld1q_u8(cjson_neon_range_min_tbl);
743 const uint8x16_t range_max_tbl = vld1q_u8(cjson_neon_range_max_tbl);
744 const uint8x16x2_t range_adjust_tbl = vld2q_u8(cjson_neon_range_adjust_tbl);
745  
746 /* Cached values */
747 const uint8x16_t const_1 = vdupq_n_u8(1);
748 const uint8x16_t const_2 = vdupq_n_u8(2);
749 const uint8x16_t const_e0 = vdupq_n_u8(0xE0);
750  
751 /* We use two error registers to remove a dependency. */
752 uint8x16_t error1 = vdupq_n_u8(0);
753 uint8x16_t error2 = vdupq_n_u8(0);
754  
755 while (len >= 16) {
756 uint8x16_t tmp1, tmp2;
757 const uint8x16_t input = vld1q_u8(data);
758 uint8x16_t shift1, pos, minv, maxv;
759  
760 /* high_nibbles = input >> 4 */
761 const uint8x16_t high_nibbles = vshrq_n_u8(input, 4);
762  
763 /* first_len = legal character length minus 1 */
764 /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
765 /* first_len = first_len_tbl[high_nibbles] */
766 const uint8x16_t first_len =
767 vqtbl1q_u8(first_len_tbl, high_nibbles);
768  
769 /* First Byte: set range index to 8 for bytes within 0xC0 ~ 0xFF */
770 /* range = first_range_tbl[high_nibbles] */
771 uint8x16_t range = vqtbl1q_u8(first_range_tbl, high_nibbles);
772  
773 /* Second Byte: set range index to first_len */
774 /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */
775 /* range |= (first_len, prev_first_len) << 1 byte */
776 range =
777 vorrq_u8(range, vextq_u8(prev_first_len, first_len, 15));
778  
779 /* Third Byte: set range index to saturate_sub(first_len, 1) */
780 /* 0 for 00~7F, 0 for C0~DF, 1 for E0~EF, 2 for F0~FF */
781 /* tmp1 = (first_len, prev_first_len) << 2 bytes */
782 tmp1 = vextq_u8(prev_first_len, first_len, 14);
783 /* tmp1 = saturate_sub(tmp1, 1) */
784 tmp1 = vqsubq_u8(tmp1, const_1);
785 /* range |= tmp1 */
786 range = vorrq_u8(range, tmp1);
787  
788 /* Fourth Byte: set range index to saturate_sub(first_len, 2) */
789 /* 0 for 00~7F, 0 for C0~DF, 0 for E0~EF, 1 for F0~FF */
790 /* tmp2 = (first_len, prev_first_len) << 3 bytes */
791 tmp2 = vextq_u8(prev_first_len, first_len, 13);
792 /* tmp2 = saturate_sub(tmp2, 2) */
793 tmp2 = vqsubq_u8(tmp2, const_2);
794 /* range |= tmp2 */
795 range = vorrq_u8(range, tmp2);
796  
797 /*
798 * Now we have below range indices caluclated
799 * Correct cases:
800 * - 8 for C0~FF
801 * - 3 for 1st byte after F0~FF
802 * - 2 for 1st byte after E0~EF or 2nd byte after F0~FF
803 * - 1 for 1st byte after C0~DF or 2nd byte after E0~EF or
804 * 3rd byte after F0~FF
805 * - 0 for others
806 * Error cases:
807 * 9,10,11 if non ascii First Byte overlaps
808 * E.g., F1 80 C2 90 --> 8 3 10 2, where 10 indicates error
809 */
810  
811 /* Adjust Second Byte range for special First Bytes(E0,ED,F0,F4) */
812 /* See cjson_neon_range_adjust_tbl[] definition for details */
813 /* Overlaps lead to index 9~15, which are illegal in range table */
814 shift1 = vextq_u8(prev_input, input, 15);
815 pos = vsubq_u8(shift1, const_e0);
816 range = vaddq_u8(range, vqtbl2q_u8(range_adjust_tbl, pos));
817  
818 /* Load min and max values per calculated range index */
819 minv = vqtbl1q_u8(range_min_tbl, range);
820 maxv = vqtbl1q_u8(range_max_tbl, range);
821  
822 /* Check value range */
823 error1 = vorrq_u8(error1, vcltq_u8(input, minv));
824 error2 = vorrq_u8(error2, vcgtq_u8(input, maxv));
825  
826 prev_input = input;
827 prev_first_len = first_len;
828  
829 data += 16;
830 len -= 16;
831 }
832 /* Merge our error counters together */
833 error1 = vorrq_u8(error1, error2);
834  
835 /* Delay error check till loop ends */
836 if (vmaxvq_u8(error1))
837 return -1;
838  
839 /* Find previous token (not 80~BF) */
840 token4;
841 vst1q_lane_u32(&token4, vreinterpretq_u32_u8(prev_input), 3);
842  
843 {
844 const int8_t *token = (const int8_t *)&token4;
845 int lookahead = 0;
846 if (token[3] > (int8_t)0xBF)
847 lookahead = 1;
848 else if (token[2] > (int8_t)0xBF)
849 lookahead = 2;
850 else if (token[1] > (int8_t)0xBF)
851 lookahead = 3;
852 data -= lookahead;
853 len += lookahead;
854 }
855 }
856  
857 /* Check remaining bytes with naive method */
858 return cjson_utf8_naive(data, len);
859 }
860 #endif /* CJSON_HAVE_NEON */
861  
862 /* ------------------------------------------------------------- dispatch --- */
863 /* Which SIMD backend this CPU has; set from a constructor so the hot path
864 * never pays for a cpuid. Stays 0 (scalar) if no constructor ran. */
865 #if defined(CJSON_HAVE_X86_SIMD)
866 static int cjson_utf8_backend = 0; /* 0 = scalar, 1 = sse4.1, 2 = avx2 */
867  
868 __attribute__((constructor))
869 69 static void cjson_utf8_backend_init (void)
870 {
871 69 __builtin_cpu_init ();
872 69 ▶ if (__builtin_cpu_supports ("avx2"))
Branch: 1 branch
BranchTrueFalseTotal
if (__builtin_cpu_supports ("avx2")) 69 0 69
873 69 cjson_utf8_backend = 2;
874 0 ▶ else if (__builtin_cpu_supports ("sse4.1"))
Branch: 1 branch
BranchTrueFalseTotal
else if (__builtin_cpu_supports ("sse4.1")) 0 0 0
875 0 cjson_utf8_backend = 1;
876 69 }
877 #endif
878  
879 /* Offset of the first byte >= 0x80, or len if the input is pure ascii. Lets a
880 * pure-ascii document -- the common case, and one that never reaches the
881 * decoder -- cost a movemask pass instead of the whole range algorithm. */
882 #ifdef CJSON_HAVE_X86_SIMD
883 CJSON_SIMD_FN("avx2")
884 static size_t
885 74 cjson_first_high_avx2 (const unsigned char *d, size_t n)
886 {
887 74 size_t i = 0;
888 74 ▶ for (; i + 32 <= n; i += 32)
Branch: 1 branch
BranchTrueFalseTotal
for (; i + 32 <= n; i += 32) 53 21 74
889 {
890 106 __m256i v = _mm256_loadu_si256 ((const __m256i *)(d + i));
891 53 unsigned m = (unsigned)_mm256_movemask_epi8 (v);
892 53 ▶ if (m)
Branch: 1 branch
BranchTrueFalseTotal
if (m) 53 0 53
893 53 return i + (size_t)__builtin_ctz (m);
894 }
895 21 ▶ for (; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; i++) 21 0 21
896 21 ▶ if (d[i] >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (d[i] >= 0x80) 21 0 21
897 21 return i;
898 0 return n;
899 }
900  
901 CJSON_SIMD_FN("sse2")
902 static size_t
903 0 cjson_first_high_sse2 (const unsigned char *d, size_t n)
904 {
905 0 size_t i = 0;
906 0 ▶ for (; i + 16 <= n; i += 16)
Branch: 1 branch
BranchTrueFalseTotal
for (; i + 16 <= n; i += 16) 0 0 0
907 {
908 0 __m128i v = _mm_loadu_si128 ((const __m128i *)(d + i));
909 0 unsigned m = (unsigned)_mm_movemask_epi8 (v);
910 0 ▶ if (m)
Branch: 1 branch
BranchTrueFalseTotal
if (m) 0 0 0
911 0 return i + (size_t)__builtin_ctz (m);
912 }
913 0 ▶ for (; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; i++) 0 0 0
914 0 ▶ if (d[i] >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (d[i] >= 0x80) 0 0 0
915 0 return i;
916 0 return n;
917 }
918 #endif /* CJSON_HAVE_X86_SIMD */
919  
920 static size_t
921 0 cjson_first_high_scalar (const unsigned char *d, size_t n)
922 {
923 0 size_t i = 0;
924 0 ▶ for (; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; i++) 0 0 0
925 0 ▶ if (d[i] >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (d[i] >= 0x80) 0 0 0
926 0 return i;
927 0 return n;
928 }
929  
930 static size_t
931 74 cjson_utf8_first_high (const unsigned char *data, size_t len)
932 {
933 #if defined(CJSON_HAVE_X86_SIMD)
934 74 ▶ if (cjson_utf8_backend == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (cjson_utf8_backend == 2) 74 0 74
935 74 return cjson_first_high_avx2 (data, len);
936 0 ▶ if (cjson_utf8_backend == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (cjson_utf8_backend == 1) 0 0 0
937 0 return cjson_first_high_sse2 (data, len);
938 #endif
939 0 return cjson_first_high_scalar (data, len);
940 }
941  
942 int
943 74 cjson_utf8_validate (const unsigned char *data, size_t len)
944 {
945 int n;
946 size_t off;
947  
948 /* >2GB of input: don't bother, let the caller use its safe path */
949 74 ▶ if (len > (size_t)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (len > (size_t)INT_MAX) 0 74 74
950 0 return 0;
951  
952 /* skip a pure-ascii prefix: those bytes are valid by definition and each is
953 * a complete character, so the range algorithm may start right after it */
954 74 off = cjson_utf8_first_high (data, len);
955 74 ▶ if (off == len)
Branch: 1 branch
BranchTrueFalseTotal
if (off == len) 0 74 74
956 0 return 1;
957 74 data += off;
958 74 len -= off;
959  
960 74 n = (int)len;
961 74 ▶ if (n < 16)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 16) 0 74 74
962 0 return cjson_utf8_naive (data, n) == 0;
963 #if defined(CJSON_HAVE_X86_SIMD)
964 74 ▶ if (cjson_utf8_backend == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (cjson_utf8_backend == 2) 74 0 74
965 74 return cjson_utf8_range_avx2 (data, n) == 0;
966 0 ▶ if (cjson_utf8_backend == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (cjson_utf8_backend == 1) 0 0 0
967 0 return cjson_utf8_range_sse (data, n) == 0;
968 0 return cjson_utf8_naive (data, n) == 0;
969 #elif defined(CJSON_HAVE_NEON)
970 return cjson_utf8_range_neon (data, n) == 0;
971 #else
972 return cjson_utf8_naive (data, n) == 0;
973 #endif
974 }
« blib/lib/Cpanel/JSON/XS/Boolean.pm ↑ Summary XS.xs »