json 2.12.2 → 2.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ case RbConfig::CONFIG['host_cpu']
2
+ when /^(arm|aarch64)/
3
+ # Try to compile a small program using NEON instructions
4
+ header, type, init, extra = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)', nil
5
+ when /^(x86_64|x64)/
6
+ header, type, init, extra = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)', 'if (__builtin_cpu_supports("sse2")) { printf("OK"); }'
7
+ end
8
+ if header
9
+ if have_header(header) && try_compile(<<~SRC, '-Werror=implicit-function-declaration')
10
+ #{cpp_include(header)}
11
+ int main(int argc, char **argv) {
12
+ #{type} test = #{init};
13
+ #{extra}
14
+ if (argc > 100000) printf("%p", &test);
15
+ return 0;
16
+ }
17
+ SRC
18
+ $defs.push("-DJSON_ENABLE_SIMD")
19
+ else
20
+ puts "Disable SIMD"
21
+ end
22
+ end
23
+
24
+ have_header('cpuid.h')
@@ -0,0 +1,191 @@
1
+ #include "../json.h"
2
+
3
+ typedef enum {
4
+ SIMD_NONE,
5
+ SIMD_NEON,
6
+ SIMD_SSE2
7
+ } SIMD_Implementation;
8
+
9
+ #ifndef __has_builtin // Optional of course.
10
+ #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
11
+ #endif
12
+
13
+ #ifdef __clang__
14
+ # if __has_builtin(__builtin_ctzll)
15
+ # define HAVE_BUILTIN_CTZLL 1
16
+ # else
17
+ # define HAVE_BUILTIN_CTZLL 0
18
+ # endif
19
+ #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
20
+ # define HAVE_BUILTIN_CTZLL 1
21
+ #else
22
+ # define HAVE_BUILTIN_CTZLL 0
23
+ #endif
24
+
25
+ static inline uint32_t trailing_zeros64(uint64_t input)
26
+ {
27
+ JSON_ASSERT(input > 0); // __builtin_ctz(0) is undefined behavior
28
+
29
+ #if HAVE_BUILTIN_CTZLL
30
+ return __builtin_ctzll(input);
31
+ #else
32
+ uint32_t trailing_zeros = 0;
33
+ uint64_t temp = input;
34
+ while ((temp & 1) == 0 && temp > 0) {
35
+ trailing_zeros++;
36
+ temp >>= 1;
37
+ }
38
+ return trailing_zeros;
39
+ #endif
40
+ }
41
+
42
+ static inline int trailing_zeros(int input)
43
+ {
44
+ JSON_ASSERT(input > 0); // __builtin_ctz(0) is undefined behavior
45
+
46
+ #if HAVE_BUILTIN_CTZLL
47
+ return __builtin_ctz(input);
48
+ #else
49
+ int trailing_zeros = 0;
50
+ int temp = input;
51
+ while ((temp & 1) == 0 && temp > 0) {
52
+ trailing_zeros++;
53
+ temp >>= 1;
54
+ }
55
+ return trailing_zeros;
56
+ #endif
57
+ }
58
+
59
+ #ifdef JSON_ENABLE_SIMD
60
+
61
+ #define SIMD_MINIMUM_THRESHOLD 6
62
+
63
+ #if defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(__aarch64__) || defined(_M_ARM64)
64
+ #include <arm_neon.h>
65
+
66
+ #define FIND_SIMD_IMPLEMENTATION_DEFINED 1
67
+ static inline SIMD_Implementation find_simd_implementation(void)
68
+ {
69
+ return SIMD_NEON;
70
+ }
71
+
72
+ #define HAVE_SIMD 1
73
+ #define HAVE_SIMD_NEON 1
74
+
75
+ // See: https://community.arm.com/arm-community-blogs/b/servers-and-cloud-computing-blog/posts/porting-x86-vector-bitmask-optimizations-to-arm-neon
76
+ ALWAYS_INLINE(static) uint64_t neon_match_mask(uint8x16_t matches)
77
+ {
78
+ const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(matches), 4);
79
+ const uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(res), 0);
80
+ return mask & 0x8888888888888888ull;
81
+ }
82
+
83
+ ALWAYS_INLINE(static) uint64_t compute_chunk_mask_neon(const char *ptr)
84
+ {
85
+ uint8x16_t chunk = vld1q_u8((const unsigned char *)ptr);
86
+
87
+ // Trick: c < 32 || c == 34 can be factored as c ^ 2 < 33
88
+ // https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
89
+ const uint8x16_t too_low_or_dbl_quote = vcltq_u8(veorq_u8(chunk, vdupq_n_u8(2)), vdupq_n_u8(33));
90
+
91
+ uint8x16_t has_backslash = vceqq_u8(chunk, vdupq_n_u8('\\'));
92
+ uint8x16_t needs_escape = vorrq_u8(too_low_or_dbl_quote, has_backslash);
93
+ return neon_match_mask(needs_escape);
94
+ }
95
+
96
+ ALWAYS_INLINE(static) int string_scan_simd_neon(const char **ptr, const char *end, uint64_t *mask)
97
+ {
98
+ while (*ptr + sizeof(uint8x16_t) <= end) {
99
+ uint64_t chunk_mask = compute_chunk_mask_neon(*ptr);
100
+ if (chunk_mask) {
101
+ *mask = chunk_mask;
102
+ return 1;
103
+ }
104
+ *ptr += sizeof(uint8x16_t);
105
+ }
106
+ return 0;
107
+ }
108
+
109
+ static inline uint8x16x4_t load_uint8x16_4(const unsigned char *table)
110
+ {
111
+ uint8x16x4_t tab;
112
+ tab.val[0] = vld1q_u8(table);
113
+ tab.val[1] = vld1q_u8(table+16);
114
+ tab.val[2] = vld1q_u8(table+32);
115
+ tab.val[3] = vld1q_u8(table+48);
116
+ return tab;
117
+ }
118
+
119
+ #endif /* ARM Neon Support.*/
120
+
121
+ #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
122
+
123
+ #ifdef HAVE_X86INTRIN_H
124
+ #include <x86intrin.h>
125
+
126
+ #define HAVE_SIMD 1
127
+ #define HAVE_SIMD_SSE2 1
128
+
129
+ #ifdef HAVE_CPUID_H
130
+ #define FIND_SIMD_IMPLEMENTATION_DEFINED 1
131
+
132
+ #if defined(__clang__) || defined(__GNUC__)
133
+ #define TARGET_SSE2 __attribute__((target("sse2")))
134
+ #else
135
+ #define TARGET_SSE2
136
+ #endif
137
+
138
+ #define _mm_cmpge_epu8(a, b) _mm_cmpeq_epi8(_mm_max_epu8(a, b), a)
139
+ #define _mm_cmple_epu8(a, b) _mm_cmpge_epu8(b, a)
140
+ #define _mm_cmpgt_epu8(a, b) _mm_xor_si128(_mm_cmple_epu8(a, b), _mm_set1_epi8(-1))
141
+ #define _mm_cmplt_epu8(a, b) _mm_cmpgt_epu8(b, a)
142
+
143
+ ALWAYS_INLINE(static) TARGET_SSE2 int compute_chunk_mask_sse2(const char *ptr)
144
+ {
145
+ __m128i chunk = _mm_loadu_si128((__m128i const*)ptr);
146
+ // Trick: c < 32 || c == 34 can be factored as c ^ 2 < 33
147
+ // https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
148
+ __m128i too_low_or_dbl_quote = _mm_cmplt_epu8(_mm_xor_si128(chunk, _mm_set1_epi8(2)), _mm_set1_epi8(33));
149
+ __m128i has_backslash = _mm_cmpeq_epi8(chunk, _mm_set1_epi8('\\'));
150
+ __m128i needs_escape = _mm_or_si128(too_low_or_dbl_quote, has_backslash);
151
+ return _mm_movemask_epi8(needs_escape);
152
+ }
153
+
154
+ ALWAYS_INLINE(static) TARGET_SSE2 int string_scan_simd_sse2(const char **ptr, const char *end, int *mask)
155
+ {
156
+ while (*ptr + sizeof(__m128i) <= end) {
157
+ int chunk_mask = compute_chunk_mask_sse2(*ptr);
158
+ if (chunk_mask) {
159
+ *mask = chunk_mask;
160
+ return 1;
161
+ }
162
+ *ptr += sizeof(__m128i);
163
+ }
164
+
165
+ return 0;
166
+ }
167
+
168
+ #include <cpuid.h>
169
+ #endif /* HAVE_CPUID_H */
170
+
171
+ static inline SIMD_Implementation find_simd_implementation(void)
172
+ {
173
+ // TODO Revisit. I think the SSE version now only uses SSE2 instructions.
174
+ if (__builtin_cpu_supports("sse2")) {
175
+ return SIMD_SSE2;
176
+ }
177
+
178
+ return SIMD_NONE;
179
+ }
180
+
181
+ #endif /* HAVE_X86INTRIN_H */
182
+ #endif /* X86_64 Support */
183
+
184
+ #endif /* JSON_ENABLE_SIMD */
185
+
186
+ #ifndef FIND_SIMD_IMPLEMENTATION_DEFINED
187
+ static inline SIMD_Implementation find_simd_implementation(void)
188
+ {
189
+ return SIMD_NONE;
190
+ }
191
+ #endif
@@ -29,6 +29,10 @@
29
29
  #include <string.h>
30
30
  #include <stdint.h>
31
31
 
32
+ #if JSON_DEBUG
33
+ #include <assert.h>
34
+ #endif
35
+
32
36
  #define npowers 87
33
37
  #define steppowers 8
34
38
  #define firstpower -348 /* 10 ^ -348 */
@@ -320,15 +324,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
320
324
  {
321
325
  int exp = absv(K + ndigits - 1);
322
326
 
323
- int max_trailing_zeros = 7;
324
-
325
- if(neg) {
326
- max_trailing_zeros -= 1;
327
- }
328
-
329
- /* write plain integer */
330
- if(K >= 0 && (exp < (ndigits + max_trailing_zeros))) {
331
-
327
+ if(K >= 0 && exp < 15) {
332
328
  memcpy(dest, digits, ndigits);
333
329
  memset(dest + ndigits, '0', K);
334
330
 
@@ -432,10 +428,12 @@ static int filter_special(double fp, char* dest)
432
428
  *
433
429
  * Input:
434
430
  * fp -> the double to convert, dest -> destination buffer.
435
- * The generated string will never be longer than 28 characters.
436
- * Make sure to pass a pointer to at least 28 bytes of memory.
431
+ * The generated string will never be longer than 32 characters.
432
+ * Make sure to pass a pointer to at least 32 bytes of memory.
437
433
  * The emitted string will not be null terminated.
438
434
  *
435
+ *
436
+ *
439
437
  * Output:
440
438
  * The number of written characters.
441
439
  *
@@ -474,6 +472,9 @@ static int fpconv_dtoa(double d, char dest[28])
474
472
  int ndigits = grisu2(d, digits, &K);
475
473
 
476
474
  str_len += emit_digits(digits, ndigits, dest + str_len, K, neg);
475
+ #if JSON_DEBUG
476
+ assert(str_len <= 32);
477
+ #endif
477
478
 
478
479
  return str_len;
479
480
  }