simdjson 5.0.2
Ridiculously Fast JSON
Loading...
Searching...
No Matches
simd.h
1#ifndef SIMDJSON_ARM64_SIMD_H
2#define SIMDJSON_ARM64_SIMD_H
3
4#ifndef SIMDJSON_CONDITIONAL_INCLUDE
5#include "simdjson/arm64/base.h"
6#include "simdjson/arm64/bitmanipulation.h"
7#include "simdjson/internal/simdprune_tables.h"
8#endif // SIMDJSON_CONDITIONAL_INCLUDE
9
10namespace simdjson {
11namespace arm64 {
12namespace {
13namespace simd {
14
15#if SIMDJSON_REGULAR_VISUAL_STUDIO
16namespace {
17// Start of private section with Visual Studio workaround
18
19
20#ifndef simdjson_make_uint8x16_t
21#define simdjson_make_uint8x16_t(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
22 x13, x14, x15, x16) \
23 ([=]() { \
24 uint8_t array[16] = {x1, x2, x3, x4, x5, x6, x7, x8, \
25 x9, x10, x11, x12, x13, x14, x15, x16}; \
26 return vld1q_u8(array); \
27 }())
28#endif
29#ifndef simdjson_make_int8x16_t
30#define simdjson_make_int8x16_t(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
31 x13, x14, x15, x16) \
32 ([=]() { \
33 int8_t array[16] = {x1, x2, x3, x4, x5, x6, x7, x8, \
34 x9, x10, x11, x12, x13, x14, x15, x16}; \
35 return vld1q_s8(array); \
36 }())
37#endif
38
39#ifndef simdjson_make_uint8x8_t
40#define simdjson_make_uint8x8_t(x1, x2, x3, x4, x5, x6, x7, x8) \
41 ([=]() { \
42 uint8_t array[8] = {x1, x2, x3, x4, x5, x6, x7, x8}; \
43 return vld1_u8(array); \
44 }())
45#endif
46#ifndef simdjson_make_int8x8_t
47#define simdjson_make_int8x8_t(x1, x2, x3, x4, x5, x6, x7, x8) \
48 ([=]() { \
49 int8_t array[8] = {x1, x2, x3, x4, x5, x6, x7, x8}; \
50 return vld1_s8(array); \
51 }())
52#endif
53#ifndef simdjson_make_uint16x8_t
54#define simdjson_make_uint16x8_t(x1, x2, x3, x4, x5, x6, x7, x8) \
55 ([=]() { \
56 uint16_t array[8] = {x1, x2, x3, x4, x5, x6, x7, x8}; \
57 return vld1q_u16(array); \
58 }())
59#endif
60#ifndef simdjson_make_int16x8_t
61#define simdjson_make_int16x8_t(x1, x2, x3, x4, x5, x6, x7, x8) \
62 ([=]() { \
63 int16_t array[8] = {x1, x2, x3, x4, x5, x6, x7, x8}; \
64 return vld1q_s16(array); \
65 }())
66#endif
67
68// End of private section with Visual Studio workaround
69} // namespace
70#endif // SIMDJSON_REGULAR_VISUAL_STUDIO
71
72
73 template<typename T>
74 struct simd8;
75
76 //
77 // Base class of simd8<uint8_t> and simd8<bool>, both of which use uint8x16_t internally.
78 //
79 template<typename T, typename Mask=simd8<bool>>
80 struct base_u8 {
81 uint8x16_t value;
82 static const int SIZE = sizeof(value);
83
84 // Conversion from/to SIMD register
85 simdjson_inline base_u8(const uint8x16_t _value) : value(_value) {}
86 simdjson_inline operator const uint8x16_t&() const { return this->value; }
87 simdjson_inline operator uint8x16_t&() { return this->value; }
88
89 // Bit operations
90 simdjson_inline simd8<T> operator|(const simd8<T> other) const { return vorrq_u8(*this, other); }
91 simdjson_inline simd8<T> operator&(const simd8<T> other) const { return vandq_u8(*this, other); }
92 simdjson_inline simd8<T> operator^(const simd8<T> other) const { return veorq_u8(*this, other); }
93 simdjson_inline simd8<T> bit_andnot(const simd8<T> other) const { return vbicq_u8(*this, other); }
94 simdjson_inline simd8<T> operator~() const { return *this ^ 0xFFu; }
95 simdjson_inline simd8<T>& operator|=(const simd8<T> other) { auto this_cast = static_cast<simd8<T>*>(this); *this_cast = *this_cast | other; return *this_cast; }
96 simdjson_inline simd8<T>& operator&=(const simd8<T> other) { auto this_cast = static_cast<simd8<T>*>(this); *this_cast = *this_cast & other; return *this_cast; }
97 simdjson_inline simd8<T>& operator^=(const simd8<T> other) { auto this_cast = static_cast<simd8<T>*>(this); *this_cast = *this_cast ^ other; return *this_cast; }
98
99 friend simdjson_inline Mask operator==(const simd8<T> lhs, const simd8<T> rhs) { return vceqq_u8(lhs, rhs); }
100
101 template<int N=1>
102 simdjson_inline simd8<T> prev(const simd8<T> prev_chunk) const {
103 return vextq_u8(prev_chunk, *this, 16 - N);
104 }
105 };
106
107 // SIMD byte mask type (returned by things like eq and gt)
108 template<>
109 struct simd8<bool>: base_u8<bool> {
110 typedef uint16_t bitmask_t;
111 typedef uint32_t bitmask2_t;
112
113 static simdjson_inline simd8<bool> splat(bool _value) { return vmovq_n_u8(uint8_t(-(!!_value))); }
114
115 simdjson_inline simd8(const uint8x16_t _value) : base_u8<bool>(_value) {}
116 // False constructor
117 simdjson_inline simd8() : simd8(vdupq_n_u8(0)) {}
118 // Splat constructor
119 simdjson_inline simd8(bool _value) : simd8(splat(_value)) {}
120
121 // We return uint32_t instead of uint16_t because that seems to be more efficient for most
122 // purposes (cutting it down to uint16_t costs performance in some compilers).
123 simdjson_inline uint32_t to_bitmask() const {
124#if SIMDJSON_REGULAR_VISUAL_STUDIO
125 const uint8x16_t bit_mask = simdjson_make_uint8x16_t(0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
126 0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80);
127#else
128 const uint8x16_t bit_mask = {0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
129 0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
130#endif
131 auto minput = *this & bit_mask;
132 uint8x16_t tmp = vpaddq_u8(minput, minput);
133 tmp = vpaddq_u8(tmp, tmp);
134 tmp = vpaddq_u8(tmp, tmp);
135 return vgetq_lane_u16(vreinterpretq_u16_u8(tmp), 0);
136 }
137 // Returns 4-bit out of each byte, alternating between the high 4 bits and low
138 // bits result it is 64 bit.
139 simdjson_inline uint64_t to_bitmask64() const {
140 return vget_lane_u64(
141 vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(*this), 4)), 0);
142 }
143 // Integer umaxv, not a float compare: flush-to-zero would treat a denormal as zero.
144 simdjson_inline bool any() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; }
145 };
146
147 // Unsigned bytes
148 template<>
149 struct simd8<uint8_t>: base_u8<uint8_t> {
150 static simdjson_inline uint8x16_t splat(uint8_t _value) { return vmovq_n_u8(_value); }
151 static simdjson_inline uint8x16_t zero() { return vdupq_n_u8(0); }
152 static simdjson_inline uint8x16_t load(const uint8_t* values) { return vld1q_u8(values); }
153
154 simdjson_inline simd8(const uint8x16_t _value) : base_u8<uint8_t>(_value) {}
155 // Zero constructor
156 simdjson_inline simd8() : simd8(zero()) {}
157 // Array constructor
158 simdjson_inline simd8(const uint8_t values[16]) : simd8(load(values)) {}
159 // Splat constructor
160 simdjson_inline simd8(uint8_t _value) : simd8(splat(_value)) {}
161 // Member-by-member initialization
162#if SIMDJSON_REGULAR_VISUAL_STUDIO
163 simdjson_inline simd8(
164 uint8_t v0, uint8_t v1, uint8_t v2, uint8_t v3, uint8_t v4, uint8_t v5, uint8_t v6, uint8_t v7,
165 uint8_t v8, uint8_t v9, uint8_t v10, uint8_t v11, uint8_t v12, uint8_t v13, uint8_t v14, uint8_t v15
166 ) : simd8(simdjson_make_uint8x16_t(
167 v0, v1, v2, v3, v4, v5, v6, v7,
168 v8, v9, v10,v11,v12,v13,v14,v15
169 )) {}
170#else
171 simdjson_inline simd8(
172 uint8_t v0, uint8_t v1, uint8_t v2, uint8_t v3, uint8_t v4, uint8_t v5, uint8_t v6, uint8_t v7,
173 uint8_t v8, uint8_t v9, uint8_t v10, uint8_t v11, uint8_t v12, uint8_t v13, uint8_t v14, uint8_t v15
174 ) : simd8(uint8x16_t{
175 v0, v1, v2, v3, v4, v5, v6, v7,
176 v8, v9, v10,v11,v12,v13,v14,v15
177 }) {}
178#endif
179
180 // Repeat 16 values as many times as necessary (usually for lookup tables)
181 simdjson_inline static simd8<uint8_t> repeat_16(
182 uint8_t v0, uint8_t v1, uint8_t v2, uint8_t v3, uint8_t v4, uint8_t v5, uint8_t v6, uint8_t v7,
183 uint8_t v8, uint8_t v9, uint8_t v10, uint8_t v11, uint8_t v12, uint8_t v13, uint8_t v14, uint8_t v15
184 ) {
185 return simd8<uint8_t>(
186 v0, v1, v2, v3, v4, v5, v6, v7,
187 v8, v9, v10,v11,v12,v13,v14,v15
188 );
189 }
190
191 // Store to array
192 simdjson_inline void store(uint8_t dst[16]) const { return vst1q_u8(dst, *this); }
193
194 // Saturated math
195 simdjson_inline simd8<uint8_t> saturating_add(const simd8<uint8_t> other) const { return vqaddq_u8(*this, other); }
196 simdjson_inline simd8<uint8_t> saturating_sub(const simd8<uint8_t> other) const { return vqsubq_u8(*this, other); }
197
198 // Addition/subtraction are the same for signed and unsigned
199 simdjson_inline simd8<uint8_t> operator+(const simd8<uint8_t> other) const { return vaddq_u8(*this, other); }
200 simdjson_inline simd8<uint8_t> operator-(const simd8<uint8_t> other) const { return vsubq_u8(*this, other); }
201 simdjson_inline simd8<uint8_t>& operator+=(const simd8<uint8_t> other) { *this = *this + other; return *this; }
202 simdjson_inline simd8<uint8_t>& operator-=(const simd8<uint8_t> other) { *this = *this - other; return *this; }
203
204 // Order-specific operations
205 simdjson_inline uint8_t max_val() const { return vmaxvq_u8(*this); }
206 simdjson_inline uint8_t min_val() const { return vminvq_u8(*this); }
207 simdjson_inline simd8<uint8_t> max_val(const simd8<uint8_t> other) const { return vmaxq_u8(*this, other); }
208 simdjson_inline simd8<uint8_t> min_val(const simd8<uint8_t> other) const { return vminq_u8(*this, other); }
209 simdjson_inline simd8<bool> operator<=(const simd8<uint8_t> other) const { return vcleq_u8(*this, other); }
210 simdjson_inline simd8<bool> operator>=(const simd8<uint8_t> other) const { return vcgeq_u8(*this, other); }
211 simdjson_inline simd8<bool> operator<(const simd8<uint8_t> other) const { return vcltq_u8(*this, other); }
212 simdjson_inline simd8<bool> operator>(const simd8<uint8_t> other) const { return vcgtq_u8(*this, other); }
213 // Same as >, but instead of guaranteeing all 1's == true, false = 0 and true = nonzero. For ARM, returns all 1's.
214 simdjson_inline simd8<uint8_t> gt_bits(const simd8<uint8_t> other) const { return simd8<uint8_t>(*this > other); }
215 // Same as <, but instead of guaranteeing all 1's == true, false = 0 and true = nonzero. For ARM, returns all 1's.
216 simdjson_inline simd8<uint8_t> lt_bits(const simd8<uint8_t> other) const { return simd8<uint8_t>(*this < other); }
217
218 // Bit-specific operations
219 simdjson_inline simd8<bool> any_bits_set(simd8<uint8_t> bits) const { return vtstq_u8(*this, bits); }
220 simdjson_inline bool any_bits_set_anywhere() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; }
221 simdjson_inline bool any_bits_set_anywhere(simd8<uint8_t> bits) const { return (*this & bits).any_bits_set_anywhere(); }
222 template<int N>
223 simdjson_inline simd8<uint8_t> shr() const { return vshrq_n_u8(*this, N); }
224 template<int N>
225 simdjson_inline simd8<uint8_t> shl() const { return vshlq_n_u8(*this, N); }
226
227 // Perform a lookup assuming the value is between 0 and 16 (undefined behavior for out of range values)
228 template<typename L>
229 simdjson_inline simd8<L> lookup_16(simd8<L> lookup_table) const {
230 return lookup_table.apply_lookup_16_to(*this);
231 }
232
233 // Returns 4-bit out of each byte, alternating between the high 4 bits and low
234 // bits result it is 64 bit.
235 simdjson_inline uint64_t to_bitmask64() const {
236 return vget_lane_u64(
237 vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(*this), 4)), 0);
238 }
239 // Copies to 'output" all bytes corresponding to a 0 in the mask (interpreted as a bitset).
240 // Passing a 0 value for mask would be equivalent to writing out every byte to output.
241 // Only the first 16 - count_ones(mask) bytes of the result are significant but 16 bytes
242 // get written.
243 // Design consideration: it seems like a function with the
244 // signature simd8<L> compress(uint16_t mask) would be
245 // sensible, but the AVX ISA makes this kind of approach difficult.
246 template<typename L>
247 simdjson_inline void compress(uint16_t mask, L * output) const {
248 using internal::thintable_epi8;
249 using internal::BitsSetTable256mul2;
250 using internal::pshufb_combine_table;
251 // this particular implementation was inspired by work done by @animetosho
252 // we do it in two steps, first 8 bytes and then second 8 bytes
253 uint8_t mask1 = uint8_t(mask); // least significant 8 bits
254 uint8_t mask2 = uint8_t(mask >> 8); // most significant 8 bits
255 // next line just loads the 64-bit values thintable_epi8[mask1] and
256 // thintable_epi8[mask2] into a 128-bit register, using only
257 // two instructions on most compilers.
258 uint64x2_t shufmask64 = {thintable_epi8[mask1], thintable_epi8[mask2]};
259 uint8x16_t shufmask = vreinterpretq_u8_u64(shufmask64);
260 // we increment by 0x08 the second half of the mask
261#if SIMDJSON_REGULAR_VISUAL_STUDIO
262 uint8x16_t inc = simdjson_make_uint8x16_t(0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08);
263#else
264 uint8x16_t inc = {0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08};
265#endif
266 shufmask = vaddq_u8(shufmask, inc);
267 // this is the version "nearly pruned"
268 uint8x16_t pruned = vqtbl1q_u8(*this, shufmask);
269 // we still need to put the two halves together.
270 // we compute the popcount of the first half:
271 int pop1 = BitsSetTable256mul2[mask1];
272 // then load the corresponding mask, what it does is to write
273 // only the first pop1 bytes from the first 8 bytes, and then
274 // it fills in with the bytes from the second 8 bytes + some filling
275 // at the end.
276 uint8x16_t compactmask = vld1q_u8(reinterpret_cast<const uint8_t *>(pshufb_combine_table + pop1 * 8));
277 uint8x16_t answer = vqtbl1q_u8(pruned, compactmask);
278 vst1q_u8(reinterpret_cast<uint8_t*>(output), answer);
279 }
280
281 // Copies all bytes corresponding to a 0 in the low half of the mask (interpreted as a
282 // bitset) to output1, then those corresponding to a 0 in the high half to output2.
283 template<typename L>
284 simdjson_inline void compress_halves(uint16_t mask, L *output1, L *output2) const {
285 using internal::thintable_epi8;
286 uint8_t mask1 = uint8_t(mask); // least significant 8 bits
287 uint8_t mask2 = uint8_t(mask >> 8); // most significant 8 bits
288 uint8x8_t compactmask1 = vcreate_u8(thintable_epi8[mask1]);
289 uint8x8_t compactmask2 = vcreate_u8(thintable_epi8[mask2]);
290 // we increment by 0x08 the second half of the mask
291#if SIMDJSON_REGULAR_VISUAL_STUDIO
292 uint8x8_t inc = simdjson_make_uint8x8_t(0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08);
293#else
294 uint8x8_t inc = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08};
295#endif
296 compactmask2 = vadd_u8(compactmask2, inc);
297 // store each result (with the second store possibly overlapping the first)
298 vst1_u8((uint8_t*)output1, vqtbl1_u8(*this, compactmask1));
299 vst1_u8((uint8_t*)output2, vqtbl1_u8(*this, compactmask2));
300 }
301
302 template<typename L>
303 simdjson_inline simd8<L> lookup_16(
304 L replace0, L replace1, L replace2, L replace3,
305 L replace4, L replace5, L replace6, L replace7,
306 L replace8, L replace9, L replace10, L replace11,
307 L replace12, L replace13, L replace14, L replace15) const {
308 return lookup_16(simd8<L>::repeat_16(
309 replace0, replace1, replace2, replace3,
310 replace4, replace5, replace6, replace7,
311 replace8, replace9, replace10, replace11,
312 replace12, replace13, replace14, replace15
313 ));
314 }
315
316 template<typename T>
317 simdjson_inline simd8<uint8_t> apply_lookup_16_to(const simd8<T> original) {
318 return vqtbl1q_u8(*this, simd8<uint8_t>(original));
319 }
320 };
321
322 // Signed bytes
323 template<>
324 struct simd8<int8_t> {
325 int8x16_t value;
326
327 static simdjson_inline simd8<int8_t> splat(int8_t _value) { return vmovq_n_s8(_value); }
328 static simdjson_inline simd8<int8_t> zero() { return vdupq_n_s8(0); }
329 static simdjson_inline simd8<int8_t> load(const int8_t values[16]) { return vld1q_s8(values); }
330
331 // Conversion from/to SIMD register
332 simdjson_inline simd8(const int8x16_t _value) : value{_value} {}
333 simdjson_inline operator const int8x16_t&() const { return this->value; }
334 simdjson_inline operator int8x16_t&() { return this->value; }
335
336 // Zero constructor
337 simdjson_inline simd8() : simd8(zero()) {}
338 // Splat constructor
339 simdjson_inline simd8(int8_t _value) : simd8(splat(_value)) {}
340 // Array constructor
341 simdjson_inline simd8(const int8_t* values) : simd8(load(values)) {}
342 // Member-by-member initialization
343#if SIMDJSON_REGULAR_VISUAL_STUDIO
344 simdjson_inline simd8(
345 int8_t v0, int8_t v1, int8_t v2, int8_t v3, int8_t v4, int8_t v5, int8_t v6, int8_t v7,
346 int8_t v8, int8_t v9, int8_t v10, int8_t v11, int8_t v12, int8_t v13, int8_t v14, int8_t v15
347 ) : simd8(simdjson_make_int8x16_t(
348 v0, v1, v2, v3, v4, v5, v6, v7,
349 v8, v9, v10,v11,v12,v13,v14,v15
350 )) {}
351#else
352 simdjson_inline simd8(
353 int8_t v0, int8_t v1, int8_t v2, int8_t v3, int8_t v4, int8_t v5, int8_t v6, int8_t v7,
354 int8_t v8, int8_t v9, int8_t v10, int8_t v11, int8_t v12, int8_t v13, int8_t v14, int8_t v15
355 ) : simd8(int8x16_t{
356 v0, v1, v2, v3, v4, v5, v6, v7,
357 v8, v9, v10,v11,v12,v13,v14,v15
358 }) {}
359#endif
360 // Repeat 16 values as many times as necessary (usually for lookup tables)
361 simdjson_inline static simd8<int8_t> repeat_16(
362 int8_t v0, int8_t v1, int8_t v2, int8_t v3, int8_t v4, int8_t v5, int8_t v6, int8_t v7,
363 int8_t v8, int8_t v9, int8_t v10, int8_t v11, int8_t v12, int8_t v13, int8_t v14, int8_t v15
364 ) {
365 return simd8<int8_t>(
366 v0, v1, v2, v3, v4, v5, v6, v7,
367 v8, v9, v10,v11,v12,v13,v14,v15
368 );
369 }
370
371 // Store to array
372 simdjson_inline void store(int8_t dst[16]) const { return vst1q_s8(dst, *this); }
373
374 // Explicit conversion to/from unsigned
375 //
376 // Under Visual Studio/ARM64 uint8x16_t and int8x16_t are apparently the same type.
377 // In theory, we could check this occurrence with std::same_as and std::enabled_if but it is C++14
378 // and relatively ugly and hard to read.
379#ifndef SIMDJSON_REGULAR_VISUAL_STUDIO
380 simdjson_inline explicit simd8(const uint8x16_t other): simd8(vreinterpretq_s8_u8(other)) {}
381#endif
382 simdjson_inline explicit operator simd8<uint8_t>() const { return vreinterpretq_u8_s8(this->value); }
383
384 // Math
385 simdjson_inline simd8<int8_t> operator+(const simd8<int8_t> other) const { return vaddq_s8(*this, other); }
386 simdjson_inline simd8<int8_t> operator-(const simd8<int8_t> other) const { return vsubq_s8(*this, other); }
387 simdjson_inline simd8<int8_t>& operator+=(const simd8<int8_t> other) { *this = *this + other; return *this; }
388 simdjson_inline simd8<int8_t>& operator-=(const simd8<int8_t> other) { *this = *this - other; return *this; }
389
390 // Order-sensitive comparisons
391 simdjson_inline simd8<int8_t> max_val(const simd8<int8_t> other) const { return vmaxq_s8(*this, other); }
392 simdjson_inline simd8<int8_t> min_val(const simd8<int8_t> other) const { return vminq_s8(*this, other); }
393 simdjson_inline simd8<bool> operator>(const simd8<int8_t> other) const { return vcgtq_s8(*this, other); }
394 simdjson_inline simd8<bool> operator<(const simd8<int8_t> other) const { return vcltq_s8(*this, other); }
395 simdjson_inline simd8<bool> operator==(const simd8<int8_t> other) const { return vceqq_s8(*this, other); }
396
397 template<int N=1>
398 simdjson_inline simd8<int8_t> prev(const simd8<int8_t> prev_chunk) const {
399 return vextq_s8(prev_chunk, *this, 16 - N);
400 }
401
402 // Perform a lookup assuming no value is larger than 16
403 template<typename L>
404 simdjson_inline simd8<L> lookup_16(simd8<L> lookup_table) const {
405 return lookup_table.apply_lookup_16_to(*this);
406 }
407 template<typename L>
408 simdjson_inline simd8<L> lookup_16(
409 L replace0, L replace1, L replace2, L replace3,
410 L replace4, L replace5, L replace6, L replace7,
411 L replace8, L replace9, L replace10, L replace11,
412 L replace12, L replace13, L replace14, L replace15) const {
413 return lookup_16(simd8<L>::repeat_16(
414 replace0, replace1, replace2, replace3,
415 replace4, replace5, replace6, replace7,
416 replace8, replace9, replace10, replace11,
417 replace12, replace13, replace14, replace15
418 ));
419 }
420
421 template<typename T>
422 simdjson_inline simd8<int8_t> apply_lookup_16_to(const simd8<T> original) {
423 return vqtbl1q_s8(*this, simd8<uint8_t>(original));
424 }
425 };
426
427 template<typename T>
428 struct simd8x64 {
429 static constexpr int NUM_CHUNKS = 64 / sizeof(simd8<T>);
430 static_assert(NUM_CHUNKS == 4, "ARM kernel should use four registers per 64-byte block.");
431 const simd8<T> chunks[NUM_CHUNKS];
432 template<int idx> simd8<uint8_t> get() const { return idx < NUM_CHUNKS ? chunks[idx] : simd8<T>(); }
433
434 simd8x64(const simd8x64<T>& o) = delete; // no copy allowed
435 simd8x64<T>& operator=(const simd8<T>& other) = delete; // no assignment allowed
436 simd8x64() = delete; // no default constructor allowed
437
438 simdjson_inline simd8x64(const simd8<T> chunk0, const simd8<T> chunk1, const simd8<T> chunk2, const simd8<T> chunk3) : chunks{chunk0, chunk1, chunk2, chunk3} {}
439 simdjson_inline simd8x64(const T ptr[64]) : chunks{simd8<T>::load(ptr), simd8<T>::load(ptr+16), simd8<T>::load(ptr+32), simd8<T>::load(ptr+48)} {}
440
441 simdjson_inline void store(T ptr[64]) const {
442 this->chunks[0].store(ptr+sizeof(simd8<T>)*0);
443 this->chunks[1].store(ptr+sizeof(simd8<T>)*1);
444 this->chunks[2].store(ptr+sizeof(simd8<T>)*2);
445 this->chunks[3].store(ptr+sizeof(simd8<T>)*3);
446 }
447
448 simdjson_inline simd8<T> reduce_or() const {
449 return (this->chunks[0] | this->chunks[1]) | (this->chunks[2] | this->chunks[3]);
450 }
451
452
453 simdjson_inline uint64_t compress(uint64_t mask, T * output) const {
454 uint64_t popcounts = vget_lane_u64(vreinterpret_u64_u8(vcnt_u8(vcreate_u8(~mask))), 0);
455 // compute the prefix sum of the popcounts of each byte
456 uint64_t offsets = popcounts * 0x0101010101010101;
457 this->chunks[0].compress_halves(uint16_t(mask), output, &output[popcounts & 0xFF]);
458 this->chunks[1].compress_halves(uint16_t(mask >> 16), &output[(offsets >> 8) & 0xFF], &output[(offsets >> 16) & 0xFF]);
459 this->chunks[2].compress_halves(uint16_t(mask >> 32), &output[(offsets >> 24) & 0xFF], &output[(offsets >> 32) & 0xFF]);
460 this->chunks[3].compress_halves(uint16_t(mask >> 48), &output[(offsets >> 40) & 0xFF], &output[(offsets >> 48) & 0xFF]);
461 return offsets >> 56;
462 }
463
464 simdjson_inline uint64_t to_bitmask() const {
465#if SIMDJSON_REGULAR_VISUAL_STUDIO
466 const uint8x16_t bit_mask = simdjson_make_uint8x16_t(
467 0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
468 0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80
469 );
470#else
471 const uint8x16_t bit_mask = {
472 0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
473 0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80
474 };
475#endif
476 // Add each of the elements next to each other, successively, to stuff each 8 byte mask into one.
477 uint8x16_t sum0 = vpaddq_u8(this->chunks[0] & bit_mask, this->chunks[1] & bit_mask);
478 uint8x16_t sum1 = vpaddq_u8(this->chunks[2] & bit_mask, this->chunks[3] & bit_mask);
479 sum0 = vpaddq_u8(sum0, sum1);
480 sum0 = vpaddq_u8(sum0, sum0);
481 return vgetq_lane_u64(vreinterpretq_u64_u8(sum0), 0);
482 }
483
484 simdjson_inline uint64_t eq(const T m) const {
485 const simd8<T> mask = simd8<T>::splat(m);
486 return simd8x64<bool>(
487 this->chunks[0] == mask,
488 this->chunks[1] == mask,
489 this->chunks[2] == mask,
490 this->chunks[3] == mask
491 ).to_bitmask();
492 }
493
494 simdjson_inline uint64_t lteq(const T m) const {
495 const simd8<T> mask = simd8<T>::splat(m);
496 return simd8x64<bool>(
497 this->chunks[0] <= mask,
498 this->chunks[1] <= mask,
499 this->chunks[2] <= mask,
500 this->chunks[3] <= mask
501 ).to_bitmask();
502 }
503 }; // struct simd8x64<T>
504
505} // namespace simd
506} // unnamed namespace
507} // namespace arm64
508} // namespace simdjson
509
510#endif // SIMDJSON_ARM64_SIMD_H
simdjson_unused simdjson_inline bool operator==(const raw_json_string &a, std::string_view c) noexcept
Comparisons between raw_json_string and std::string_view instances are potentially unsafe: the user i...
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8