-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathscanning.h
1782 lines (1558 loc) · 53.2 KB
/
scanning.h
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "native.h"
#include "utf8.h"
#include "utils.h"
#include "parsing.h"
#include "lspace.h"
#include "atof_native.h"
#include "atof_eisel_lemire.h"
static always_inline long skip_number_1(const GoString *src, long *p);
static always_inline void vnumber_1(const GoString *src, long *p, JsonState *ret);
static always_inline long skip_string_1(const GoString *src, long *p, uint64_t flags);
static always_inline long skip_positive_1(const GoString *src, long *p);
static always_inline long skip_negative_1(const GoString *src, long *p);
static const uint64_t ODD_MASK = 0xaaaaaaaaaaaaaaaa;
static const uint64_t EVEN_MASK = 0x5555555555555555;
// NOTE: mask referenced from decoder/decoder.go
static const uint64_t MASK_VALIDATE_STRING = 1ull << 5;
static const uint64_t MASK_ALLOW_CONTROL = 1ull << 31;
static const double P10_TAB[23] = {
/* <= the connvertion to double is not exact when less than 1 => */ 1e-000,
1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010,
1e+011, 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020,
1e+021, 1e+022 /* <= the connvertion to double is not exact when larger, => */
};
static always_inline uint64_t add32(uint64_t v1, uint64_t v2, uint64_t *vo) {
uint32_t v;
uint32_t c = __builtin_uadd_overflow((uint32_t)v1, (uint32_t)v2, &v);
/* set the carry */
*vo = c;
return v;
}
static always_inline uint64_t add64(uint64_t v1, uint64_t v2, uint64_t *vo) {
unsigned long long v;
uint64_t c = __builtin_uaddll_overflow(v1, v2, &v);
/* set the carry */
*vo = c;
return v;
}
static always_inline char isspace(char ch) {
return ch == ' ' || ch == '\r' || ch == '\n' | ch == '\t';
}
const int MASK_USE_NUMBER = 1<<1;
static always_inline void vdigits(const GoString *src, long *p, JsonState *ret, uint64_t flag) {
--*p;
if (flag & MASK_USE_NUMBER) {
long i = skip_number_1(src, p);
if (i < 0) {
ret->vt = i;
return;
}
ret->vt = V_DOUBLE;
ret->ep = i;
return;
}
vnumber_1(src, p, ret);
}
static always_inline char advance_ns(const GoString *src, long *p) {
size_t vi = *p;
size_t nb = src->len;
const char * sp = src->buf;
/* it's likely to run into non-spaces within a few
* characters, so test up to 4 characters manually */
if (vi < nb && !isspace(sp[vi])) goto nospace; else vi++;
if (vi < nb && !isspace(sp[vi])) goto nospace; else vi++;
if (vi < nb && !isspace(sp[vi])) goto nospace; else vi++;
if (vi < nb && !isspace(sp[vi])) goto nospace; else vi++;
/* check EOF */
if (vi >= nb) {
*p = vi;
return 0;
}
/* too many spaces, use SIMD to search for characters */
if ((vi = lspace_1(sp, nb, vi)) >= nb) {
return 0;
}
nospace:
*p = vi + 1;
return src->buf[vi];
}
static always_inline int64_t advance_dword(const GoString *src, long *p, long dec, int64_t ret, uint32_t val) {
if (*p > src->len + dec - 4) {
*p = src->len;
return -ERR_EOF;
} else if (*(uint32_t *)(src->buf + *p - dec) == val) {
*p += 4 - dec;
return ret;
} else {
*p -= dec;
for (int i = 0; src->buf[*p] == (val & 0xff) && i < 4; i++, ++*p) { val >>= 8; }
return -ERR_INVAL;
}
}
static always_inline ssize_t advance_string_default(const GoString *src, long p, int64_t *ep) {
char ch;
uint64_t es;
uint64_t fe;
uint64_t os;
uint64_t m0;
uint64_t m1;
uint64_t cr = 0;
/* prevent out-of-bounds accessing */
if (unlikely(src->len == p)) {
return -ERR_EOF;
}
/* buffer pointers */
size_t nb = src->len;
const char * sp = src->buf;
const char * ss = src->buf;
#define ep_init() *ep = -1;
#define ep_setc() ep_setx(sp - ss - 1)
#define ep_setx(x) if (*ep == -1) { *ep = (x); }
/* seek to `p` */
nb -= p;
sp += p;
ep_init()
#if USE_AVX2
/* initialize vectors */
__m256i v0;
__m256i v1;
__m256i q0;
__m256i q1;
__m256i x0;
__m256i x1;
__m256i cq = _mm256_set1_epi8('"');
__m256i cx = _mm256_set1_epi8('\\');
/* partial masks */
uint32_t s0;
uint32_t s1;
uint32_t t0;
uint32_t t1;
#else
/* initialize vectors */
__m128i v0;
__m128i v1;
__m128i v2;
__m128i v3;
__m128i q0;
__m128i q1;
__m128i q2;
__m128i q3;
__m128i x0;
__m128i x1;
__m128i x2;
__m128i x3;
__m128i cq = _mm_set1_epi8('"');
__m128i cx = _mm_set1_epi8('\\');
/* partial masks */
uint32_t s0;
uint32_t s1;
uint32_t s2;
uint32_t s3;
uint32_t t0;
uint32_t t1;
uint32_t t2;
uint32_t t3;
#endif
#define m0_mask(add) \
m1 &= ~cr; \
fe = (m1 << 1) | cr; \
os = (m1 & ~fe) & ODD_MASK; \
es = add(os, m1, &cr) << 1; \
m0 &= ~(fe & (es ^ EVEN_MASK));
/* 64-byte SIMD loop */
while (likely(nb >= 64)) {
#if USE_AVX2
v0 = _mm256_loadu_si256 ((const void *)(sp + 0));
v1 = _mm256_loadu_si256 ((const void *)(sp + 32));
q0 = _mm256_cmpeq_epi8 (v0, cq);
q1 = _mm256_cmpeq_epi8 (v1, cq);
x0 = _mm256_cmpeq_epi8 (v0, cx);
x1 = _mm256_cmpeq_epi8 (v1, cx);
s0 = _mm256_movemask_epi8 (q0);
s1 = _mm256_movemask_epi8 (q1);
t0 = _mm256_movemask_epi8 (x0);
t1 = _mm256_movemask_epi8 (x1);
m0 = ((uint64_t)s1 << 32) | (uint64_t)s0;
m1 = ((uint64_t)t1 << 32) | (uint64_t)t0;
#else
v0 = _mm_loadu_si128 ((const void *)(sp + 0));
v1 = _mm_loadu_si128 ((const void *)(sp + 16));
v2 = _mm_loadu_si128 ((const void *)(sp + 32));
v3 = _mm_loadu_si128 ((const void *)(sp + 48));
q0 = _mm_cmpeq_epi8 (v0, cq);
q1 = _mm_cmpeq_epi8 (v1, cq);
q2 = _mm_cmpeq_epi8 (v2, cq);
q3 = _mm_cmpeq_epi8 (v3, cq);
x0 = _mm_cmpeq_epi8 (v0, cx);
x1 = _mm_cmpeq_epi8 (v1, cx);
x2 = _mm_cmpeq_epi8 (v2, cx);
x3 = _mm_cmpeq_epi8 (v3, cx);
s0 = _mm_movemask_epi8 (q0);
s1 = _mm_movemask_epi8 (q1);
s2 = _mm_movemask_epi8 (q2);
s3 = _mm_movemask_epi8 (q3);
t0 = _mm_movemask_epi8 (x0);
t1 = _mm_movemask_epi8 (x1);
t2 = _mm_movemask_epi8 (x2);
t3 = _mm_movemask_epi8 (x3);
m0 = ((uint64_t)s3 << 48) | ((uint64_t)s2 << 32) | ((uint64_t)s1 << 16) | (uint64_t)s0;
m1 = ((uint64_t)t3 << 48) | ((uint64_t)t2 << 32) | ((uint64_t)t1 << 16) | (uint64_t)t0;
#endif
/** update first quote position */
if (unlikely(m1 != 0)) {
ep_setx(sp - ss + __builtin_ctzll(m1))
}
/** mask all the escaped quotes */
if (unlikely(m1 != 0 || cr != 0)) {
m0_mask(add64)
}
/* check for end quote */
if (m0 != 0) {
return sp - ss + __builtin_ctzll(m0) + 1;
}
/* move to the next block */
sp += 64;
nb -= 64;
}
/* 32-byte SIMD round */
if (likely(nb >= 32)) {
#if USE_AVX2
v0 = _mm256_loadu_si256 ((const void *)sp);
q0 = _mm256_cmpeq_epi8 (v0, cq);
x0 = _mm256_cmpeq_epi8 (v0, cx);
s0 = _mm256_movemask_epi8 (q0);
t0 = _mm256_movemask_epi8 (x0);
m0 = (uint64_t)s0;
m1 = (uint64_t)t0;
#else
v0 = _mm_loadu_si128 ((const void *)(sp + 0));
v1 = _mm_loadu_si128 ((const void *)(sp + 16));
q0 = _mm_cmpeq_epi8 (v0, cq);
q1 = _mm_cmpeq_epi8 (v1, cq);
x0 = _mm_cmpeq_epi8 (v0, cx);
x1 = _mm_cmpeq_epi8 (v1, cx);
s0 = _mm_movemask_epi8 (q0);
s1 = _mm_movemask_epi8 (q1);
t0 = _mm_movemask_epi8 (x0);
t1 = _mm_movemask_epi8 (x1);
m0 = ((uint64_t)s1 << 16) | (uint64_t)s0;
m1 = ((uint64_t)t1 << 16) | (uint64_t)t0;
#endif
/** update first quote position */
if (unlikely(m1 != 0)) {
ep_setx(sp - ss + __builtin_ctzll(m1))
}
/** mask all the escaped quotes */
if (unlikely(m1 != 0 || cr != 0)) {
m0_mask(add32)
}
/* check for end quote */
if (m0 != 0) {
return sp - ss + __builtin_ctzll(m0) + 1;
}
/* move to the next block */
sp += 32;
nb -= 32;
}
/* check for carry */
if (unlikely(cr != 0)) {
if (nb == 0) {
return -ERR_EOF;
} else {
ep_setc()
sp++, nb--;
}
}
/* handle the remaining bytes with scalar code */
while (nb-- > 0 && (ch = *sp++) != '"') {
if (unlikely(ch == '\\')) {
if (nb == 0) {
return -ERR_EOF;
} else {
ep_setc()
sp++, nb--;
}
}
}
#undef ep_init
#undef ep_setc
#undef ep_setx
#undef m0_mask
/* check for quotes */
if (ch == '"') {
return sp - ss;
} else {
return -ERR_EOF;
}
}
#if USE_AVX2
static always_inline int _mm256_get_mask(__m256i v, __m256i t) {
return _mm256_movemask_epi8(_mm256_cmpeq_epi8(v, t));
}
// control char: 0x00 ~ 0x1F
static always_inline int _mm256_cchars_mask(__m256i v) {
__m256i e1 = _mm256_cmpgt_epi8 (v, _mm256_set1_epi8(-1));
__m256i e2 = _mm256_cmpgt_epi8 (v, _mm256_set1_epi8(31));
return _mm256_movemask_epi8 (_mm256_andnot_si256 (e2, e1));
}
// ascii: 0x00 ~ 0x7F
static always_inline int _mm256_nonascii_mask(__m256i v) {
return _mm256_movemask_epi8(v);
}
#endif
static always_inline int _mm_get_mask(__m128i v, __m128i t) {
return _mm_movemask_epi8(_mm_cmpeq_epi8(v, t));
}
// control char: 0x00 ~ 0x1F
static always_inline int _mm_cchars_mask(__m128i v) {
__m128i e1 = _mm_cmpgt_epi8 (v, _mm_set1_epi8(-1));
__m128i e2 = _mm_cmpgt_epi8 (v, _mm_set1_epi8(31));
return _mm_movemask_epi8 (_mm_andnot_si128 (e2, e1));
}
// ascii: 0x00 ~ 0x7F
static always_inline int _mm_nonascii_mask(__m128i v) {
return _mm_movemask_epi8(v);
}
static always_inline ssize_t advance_string_validate(const GoString *src, long p, int64_t *ep) {
char ch;
uint64_t m0, m1, m2;
uint64_t es, fe, os;
uint64_t cr = 0;
long qp = 0;
long np = 0;
/* buffer pointers */
size_t nb = src->len;
const char * sp = src->buf;
const char * ss = src->buf;
/* prevent out-of-bounds accessing */
if (unlikely(nb == p)) {
return -ERR_EOF;
}
#define ep_init() *ep = -1;
#define ep_setc() ep_setx(sp - ss - 1)
#define ep_setx(x) if (*ep == -1) { *ep = (x); }
#define ep_seterr(x) *ep = (x);
/* seek to `p` */
nb -= p;
sp += p;
ep_init()
#if USE_AVX2
/* initialize vectors */
__m256i v0;
__m256i v1;
__m256i cq = _mm256_set1_epi8('"');
__m256i cx = _mm256_set1_epi8('\\');
/* partial masks */
uint32_t s0, s1;
uint32_t t0, t1;
uint32_t c0, c1;
#else
/* initialize vectors */
__m128i v0;
__m128i v1;
__m128i v2;
__m128i v3;
__m128i cq = _mm_set1_epi8('"');
__m128i cx = _mm_set1_epi8('\\');
/* partial masks */
uint32_t s0, s1, s2, s3;
uint32_t t0, t1, t2, t3;
uint32_t c0, c1, c2, c3;
#endif
#define m0_mask(add) \
m1 &= ~cr; \
fe = (m1 << 1) | cr; \
os = (m1 & ~fe) & ODD_MASK; \
es = add(os, m1, &cr) << 1; \
m0 &= ~(fe & (es ^ EVEN_MASK));
/* 64-byte SIMD loop */
while (likely(nb >= 64)) {
#if USE_AVX2
v0 = _mm256_loadu_si256 ((const void *)(sp + 0));
v1 = _mm256_loadu_si256 ((const void *)(sp + 32));
s0 = _mm256_get_mask(v0, cq);
s1 = _mm256_get_mask(v1, cq);
t0 = _mm256_get_mask(v0, cx);
t1 = _mm256_get_mask(v1, cx);
c0 = _mm256_cchars_mask(v0);
c1 = _mm256_cchars_mask(v1);
m0 = ((uint64_t)s1 << 32) | (uint64_t)s0;
m1 = ((uint64_t)t1 << 32) | (uint64_t)t0;
m2 = ((uint64_t)c1 << 32) | (uint64_t)c0;
#else
v0 = _mm_loadu_si128 ((const void *)(sp + 0));
v1 = _mm_loadu_si128 ((const void *)(sp + 16));
v2 = _mm_loadu_si128 ((const void *)(sp + 32));
v3 = _mm_loadu_si128 ((const void *)(sp + 48));
s0 = _mm_get_mask(v0, cq);
s1 = _mm_get_mask(v1, cq);
s2 = _mm_get_mask(v2, cq);
s3 = _mm_get_mask(v3, cq);
t0 = _mm_get_mask(v0, cx);
t1 = _mm_get_mask(v1, cx);
t2 = _mm_get_mask(v2, cx);
t3 = _mm_get_mask(v3, cx);
c0 = _mm_cchars_mask(v0);
c1 = _mm_cchars_mask(v1);
c2 = _mm_cchars_mask(v2);
c3 = _mm_cchars_mask(v3);
m0 = ((uint64_t)s3 << 48) | ((uint64_t)s2 << 32) | ((uint64_t)s1 << 16) | (uint64_t)s0;
m1 = ((uint64_t)t3 << 48) | ((uint64_t)t2 << 32) | ((uint64_t)t1 << 16) | (uint64_t)t0;
m2 = ((uint64_t)c3 << 48) | ((uint64_t)c2 << 32) | ((uint64_t)c1 << 16) | (uint64_t)c0;
#endif
/** update first quote position */
if (unlikely(m1 != 0)) {
ep_setx(sp - ss + __builtin_ctzll(m1))
}
/** mask all the escaped quotes */
if (unlikely(m1 != 0 || cr != 0)) {
m0_mask(add64)
}
qp = m0 ? __builtin_ctzll(m0) : 64;
np = m2 ? __builtin_ctzll(m2) : 64;
/* get the position of end quote */
if (m0 != 0) {
/* check control chars in JSON string */
if (unlikely(np < qp)) {
ep_seterr(sp - ss + np)
return -ERR_INVAL;
}
return sp - ss + qp + 1;
}
/* check control chars in JSON string */
if (unlikely(m2 != 0)) {
ep_setx(sp - ss + np)
return -ERR_INVAL;
}
/* move to the next block */
sp += 64;
nb -= 64;
}
/* 32-byte SIMD round */
if (likely(nb >= 32)) {
#if USE_AVX2
v0 = _mm256_loadu_si256 ((const void *)sp);
s0 = _mm256_get_mask (v0, cq);
t0 = _mm256_get_mask (v0, cx);
c0 = _mm256_cchars_mask(v0);
m0 = (uint64_t)s0;
m1 = (uint64_t)t0;
m2 = (uint64_t)c0;
#else
v0 = _mm_loadu_si128 ((const void *)(sp + 0));
v1 = _mm_loadu_si128 ((const void *)(sp + 16));
s0 = _mm_get_mask(v0, cq);
s1 = _mm_get_mask(v1, cq);
t0 = _mm_get_mask(v0, cx);
t1 = _mm_get_mask(v1, cx);
c0 = _mm_cchars_mask(v0);
c1 = _mm_cchars_mask(v1);
m0 = ((uint64_t)s1 << 16) | (uint64_t)s0;
m1 = ((uint64_t)t1 << 16) | (uint64_t)t0;
m2 = ((uint64_t)c1 << 16) | (uint64_t)c0;
#endif
/** update first quote position */
if (unlikely(m1 != 0)) {
ep_setx(sp - ss + __builtin_ctzll(m1))
}
/** mask all the escaped quotes */
if (unlikely(m1 != 0 || cr != 0)) {
m0_mask(add32)
}
qp = m0 ? __builtin_ctzll(m0) : 64;
np = m2 ? __builtin_ctzll(m2) : 64;
/* get the position of end quote */
if (m0 != 0) {
if (unlikely(np < qp)) {
ep_seterr(sp - ss + np)
return -ERR_INVAL;
}
return sp - ss + qp + 1;
}
/* check control chars in JSON string */
if (unlikely(m2 != 0)) {
ep_seterr(sp - ss + __builtin_ctzll(m2))
return -ERR_INVAL;
}
/* move to the next block */
sp += 32;
nb -= 32;
}
/* check for carry */
if (unlikely(cr != 0)) {
if (nb == 0) {
return -ERR_EOF;
} else {
ep_setc()
sp++, nb--;
}
}
/* handle the remaining bytes with scalar code */
while (nb > 0) {
ch = *sp;
if (ch == '"') {
return sp - ss + 1;
}
/* valid the escaped chars */
if (unlikely(ch == '\\')) {
if (nb == 1) {
return -ERR_EOF;
}
ep_setx(sp - ss)
sp += 2, nb -= 2;
continue;
}
/* valid unescaped chars */
if (unlikely( ch >= 0 && ch <= 0x1f)) { // control chars
ep_seterr(sp - ss)
return -ERR_INVAL;
}
sp++, nb--;
}
return -ERR_EOF;
#undef ep_init
#undef ep_setc
#undef ep_setx
#undef ep_seterr
#undef m0_mask
}
static always_inline ssize_t advance_string(const GoString *src, long p, int64_t *ep, uint64_t flags) {
if ((flags & MASK_VALIDATE_STRING) != 0) {
return advance_string_validate(src, p, ep);
} else {
return advance_string_default(src, p, ep);
}
}
#define set_vt(t) \
ret->vt = t;
#define init_ret(t) \
ret->vt = t; \
ret->dv = 0.0; \
ret->iv = 0; \
ret->ep = *p;
#define check_eof() \
if (i >= n) { \
*p = n; \
ret->vt = -ERR_EOF; \
return; \
}
#define check_sign(on_neg) \
if (s[i] == '-') { \
i++; \
on_neg; \
check_eof() \
}
#define check_digit() \
if (s[i] < '0' || s[i] > '9') { \
*p = i; \
ret->vt = -ERR_INVAL; \
return; \
}
#define check_leading_zero() \
if (s[i] == '0' && (i >= n || (s[i + 1] != '.' && s[i + 1] != 'e' && s[i + 1] != 'E'))) { \
*p = ++i; \
return; \
}
#define parse_sign(sgn) \
if (s[i] == '+' || s[i] == '-') { \
sgn = s[i++] == '+' ? 1 : -1; \
check_eof() \
}
#define is_digit(val) \
'0' <= val && val <= '9'
#define add_integer_to_mantissa(man, man_nd, exp10, dig) \
if (man_nd < 19) { \
man = man * 10 + dig; \
man_nd++; \
} else { \
exp10++; \
}
#define add_float_to_mantissa(man, man_nd, exp10, dig) \
man = man * 10 + dig; \
man_nd++; \
exp10--;
#define parse_float_digits(val, sgn, ...) \
while (i < n && s[i] >= '0' && s[i] <= '9' __VA_ARGS__) { \
val *= 10; \
val += sgn * (s[i++] - '0'); \
}
#define parse_integer_digits(val, sgn, ovf) \
while (i < n && s[i] >= '0' && s[i] <= '9') { \
if (add_digit_overflow(val, sgn * (s[i++] - '0'))) { \
ovf = 1; \
break; \
} \
}
#define add_digit_overflow(val, chr) ( \
__builtin_mul_overflow(val, 10, &val) || \
__builtin_add_overflow(val, chr, &val) \
)
#define vinteger(type, sgn, on_neg) \
int ovf = 0; \
type val = 0; \
\
/* initial buffer pointers */ \
long i = *p; \
size_t n = src->len; \
const char * s = src->buf; \
\
/* initialize the result, and check for '-' */ \
init_ret(V_INTEGER) \
check_eof() \
check_sign(on_neg) \
\
/* check for leading zero or any digits */ \
check_digit() \
check_leading_zero() \
parse_integer_digits(val, sgn, ovf) \
\
/* check for overflow */ \
if (ovf) { \
*p = i - 1; \
ret->vt = -ERR_OVERFLOW; \
return; \
} \
\
/* check for the decimal part */ \
if (i < n && s[i] == '.') { \
*p = i; \
ret->vt = -ERR_NUMBER_FMT; \
return; \
} \
\
/* check for the exponent part */ \
if (i < n && (s[i] == 'e' || s[i] == 'E')) { \
*p = i; \
ret->vt = -ERR_NUMBER_FMT; \
return; \
} \
\
/* update the result */ \
*p = i; \
ret->iv = val;
/** check whether float can represent the val exactly **/
static always_inline bool is_atof_exact(uint64_t man, int exp, int sgn, double *val) {
*val = (double)man;
if (man >> 52 != 0) {
return false;
}
/* equal to if (sgn == -1) { *val *= -1; } */
*(uint64_t *)val |= ((uint64_t)(sgn) >> 63 << 63);
if (exp == 0 || man == 0) {
return true;
} else if (exp > 0 && exp <= 15+22) {
/* uint64 integers: accurate range <= 10^15 *
* Powers of 10: accurate range <= 10^22, as P10_TAB *
* Example: man 1, exp 36, is ok */
if (exp > 22) {
*val *= P10_TAB[exp-22];
exp = 22;
}
/* f is not accurate when too larger */
if (*val > 1e15 || *val < -1e15) {
return false;
}
*val *= P10_TAB[exp];
return true;
} else if (exp < 0 && exp >= -22) {
*val /= P10_TAB[-exp];
return true;
}
return false;
}
static always_inline double atof_fast(uint64_t man, int exp, int sgn, int trunc, double *val) {
double val_up = 0.0;
/* look-up for fast atof if the conversion can be exactly */
if (is_atof_exact(man, exp, sgn, val)) {
return true;
}
/* A fast atof algorithm for high percison */
if (atof_eisel_lemire64_1(man, exp, sgn, val)) {
if (!trunc || (atof_eisel_lemire64_1(man+1, exp, sgn, &val_up) && val_up == *val)) {
return true;
}
}
return false;
}
static bool always_inline is_overflow(uint64_t man, int sgn, int exp10) {
/* the former exp10 != 0 means man has overflowed
* the latter equals to man*sgn < INT64_MIN or > INT64_MAX */
return exp10 != 0 ||
((man >> 63) == 1 && ((uint64_t)sgn & man) != (1ull << 63));
}
static always_inline void vnumber_1(const GoString *src, long *p, JsonState *ret) {
int sgn = 1;
uint64_t man = 0; // mantissa for double (float64)
int man_nd = 0; // # digits of mantissa, 10 ^ 19 fits uint64_t
int exp10 = 0; // val = sgn * man * 10 ^ exp10
int trunc = 0;
double val = 0;
/* initial buffer pointers */
long i = *p;
size_t n = src->len;
const char * s = src->buf;
char *dbuf = ret->dbuf;
ssize_t dcap = ret->dcap;
/* initialize the result, and check for EOF */
init_ret(V_INTEGER)
check_eof()
check_sign(sgn = -1)
/* check for leading zero */
check_digit()
check_leading_zero()
/* parse the integer part */
while (i < n && is_digit(s[i])) {
add_integer_to_mantissa(man, man_nd, exp10, (s[i] - '0'))
i++;
}
if (exp10 > 0) {
trunc = 1;
}
/* check for decimal points */
if (i < n && s[i] == '.') {
i++;
set_vt(V_DOUBLE)
check_eof()
check_digit()
}
/* skip the leading zeros of 0.000xxxx */
if (man == 0 && exp10 == 0) {
while (i < n && s[i] == '0') {
i++;
exp10--;
}
man = 0;
man_nd = 0;
}
/* the fractional part (uint64_t mantissa can represent at most 19 digits) */
while (i < n && man_nd < 19 && is_digit(s[i])) {
add_float_to_mantissa(man, man_nd, exp10, (s[i] - '0'))
i++;
}
/* skip the remaining digits */
while (i < n && is_digit(s[i])) {
trunc = 1;
i++;
}
/* check for exponent */
if (i < n && (s[i] == 'e' || s[i] == 'E')) {
int esm = 1;
int exp = 0;
/* check for the '+' or '-' sign, and parse the power */
i++;
set_vt(V_DOUBLE)
check_eof()
parse_sign(esm)
check_digit()
while (i < n && is_digit(s[i])) {
if (exp < 10000) {
exp = exp * 10 + (s[i] - '0');
}
i++;
}
exp10 += exp * esm;
goto parse_float;
}
if (ret->vt == V_INTEGER) {
if (!is_overflow(man, sgn, exp10)) {
ret->iv = (int64_t)man * sgn;
/* following lines equal to ret->dv = (double)(man) * sgn */
ret->dv = (double)(man);
*(uint64_t *)&ret->dv |= ((uint64_t)(sgn) >> 63 << 63);
*p = i;
return;
}
set_vt(V_DOUBLE)
}
parse_float:
/* when fast algorithms failed, use slow fallback.*/
if(!atof_fast(man, exp10, sgn, trunc, &val)) {
val = atof_native_1(s + *p, i - *p, dbuf, dcap);
}
/* check parsed double val */
if (is_infinity(val)) {
ret->vt = -ERR_FLOAT_INF;
}
/* update the result */
ret->dv = val;
*p = i;
}
#define check_bits(mv) \
if (unlikely((v = mv & (mv - 1)) != 0)) { \
return -(sp - ss + __builtin_ctz(v) + 1); \
}
#define check_sidx(iv) \
if (likely(iv == -1)) { \
iv = sp - ss - 1; \
} else { \
return -(sp - ss); \
}
#define check_vidx(iv, mv) \
if (mv != 0) { \
if (likely(iv == -1)) { \
iv = sp - ss + __builtin_ctz(mv); \
} else { \
return -(sp - ss + __builtin_ctz(mv) + 1); \
} \
}
static always_inline long do_skip_number(const char *sp, size_t nb) {
long di = -1;
long ei = -1;
long si = -1;
const char * ss = sp;
/* check for EOF */
if (nb == 0) {
return -1;
}
/* special case of '0' */
if (*sp == '0' && (nb == 1 || (sp[1] != '.' && sp[1] != 'e' && sp[1] != 'E'))) {
return 1;
}
#if USE_AVX2
/* can do with AVX-2 */
if (likely(nb >= 32)) {
__m256i d9 = _mm256_set1_epi8('9');
__m256i ds = _mm256_set1_epi8('/');
__m256i dp = _mm256_set1_epi8('.');
__m256i el = _mm256_set1_epi8('e');
__m256i eu = _mm256_set1_epi8('E');
__m256i xp = _mm256_set1_epi8('+');
__m256i xm = _mm256_set1_epi8('-');
/* 32-byte loop */
do {
__m256i sb = _mm256_loadu_si256 ((const void *)sp);
__m256i i0 = _mm256_cmpgt_epi8 (sb, ds);
__m256i i9 = _mm256_cmpgt_epi8 (sb, d9);
__m256i id = _mm256_cmpeq_epi8 (sb, dp);
__m256i il = _mm256_cmpeq_epi8 (sb, el);
__m256i iu = _mm256_cmpeq_epi8 (sb, eu);
__m256i ip = _mm256_cmpeq_epi8 (sb, xp);
__m256i im = _mm256_cmpeq_epi8 (sb, xm);
__m256i iv = _mm256_andnot_si256 (i9, i0);
__m256i ie = _mm256_or_si256 (il, iu);
__m256i is = _mm256_or_si256 (ip, im);
__m256i rt = _mm256_or_si256 (iv, id);
__m256i ru = _mm256_or_si256 (ie, is);
__m256i rv = _mm256_or_si256 (rt, ru);
/* exponent and sign position */
uint32_t md = _mm256_movemask_epi8(id);
uint32_t me = _mm256_movemask_epi8(ie);
uint32_t ms = _mm256_movemask_epi8(is);
uint32_t mr = _mm256_movemask_epi8(rv);
/* mismatch position */
uint32_t v;
uint32_t i = __builtin_ctzll(~(uint64_t)mr | 0x0100000000);
/* mask out excess characters */
if (i != 32) {
md &= (1 << i) - 1;