simdjson  3.1.6
Ridiculously Fast JSON
simd.h
1 #ifndef SIMDJSON_HASWELL_SIMD_H
2 #define SIMDJSON_HASWELL_SIMD_H
3 
4 #include "simdjson/internal/simdprune_tables.h"
5 
6 namespace simdjson {
7 namespace SIMDJSON_IMPLEMENTATION {
8 namespace {
9 namespace simd {
10 
11  // Forward-declared so they can be used by splat and friends.
12  template<typename Child>
13  struct base {
14  __m256i value;
15 
16  // Zero constructor
17  simdjson_inline base() : value{__m256i()} {}
18 
19  // Conversion from SIMD register
20  simdjson_inline base(const __m256i _value) : value(_value) {}
21 
22  // Conversion to SIMD register
23  simdjson_inline operator const __m256i&() const { return this->value; }
24  simdjson_inline operator __m256i&() { return this->value; }
25 
26  // Bit operations
27  simdjson_inline Child operator|(const Child other) const { return _mm256_or_si256(*this, other); }
28  simdjson_inline Child operator&(const Child other) const { return _mm256_and_si256(*this, other); }
29  simdjson_inline Child operator^(const Child other) const { return _mm256_xor_si256(*this, other); }
30  simdjson_inline Child bit_andnot(const Child other) const { return _mm256_andnot_si256(other, *this); }
31  simdjson_inline Child& operator|=(const Child other) { auto this_cast = static_cast<Child*>(this); *this_cast = *this_cast | other; return *this_cast; }
32  simdjson_inline Child& operator&=(const Child other) { auto this_cast = static_cast<Child*>(this); *this_cast = *this_cast & other; return *this_cast; }
33  simdjson_inline Child& operator^=(const Child other) { auto this_cast = static_cast<Child*>(this); *this_cast = *this_cast ^ other; return *this_cast; }
34  };
35 
36  // Forward-declared so they can be used by splat and friends.
37  template<typename T>
38  struct simd8;
39 
40  template<typename T, typename Mask=simd8<bool>>
41  struct base8: base<simd8<T>> {
42  typedef uint32_t bitmask_t;
43  typedef uint64_t bitmask2_t;
44 
45  simdjson_inline base8() : base<simd8<T>>() {}
46  simdjson_inline base8(const __m256i _value) : base<simd8<T>>(_value) {}
47 
48  friend simdjson_really_inline Mask operator==(const simd8<T> lhs, const simd8<T> rhs) { return _mm256_cmpeq_epi8(lhs, rhs); }
49 
50  static const int SIZE = sizeof(base<T>::value);
51 
52  template<int N=1>
53  simdjson_inline simd8<T> prev(const simd8<T> prev_chunk) const {
54  return _mm256_alignr_epi8(*this, _mm256_permute2x128_si256(prev_chunk, *this, 0x21), 16 - N);
55  }
56  };
57 
58  // SIMD byte mask type (returned by things like eq and gt)
59  template<>
60  struct simd8<bool>: base8<bool> {
61  static simdjson_inline simd8<bool> splat(bool _value) { return _mm256_set1_epi8(uint8_t(-(!!_value))); }
62 
63  simdjson_inline simd8<bool>() : base8() {}
64  simdjson_inline simd8<bool>(const __m256i _value) : base8<bool>(_value) {}
65  // Splat constructor
66  simdjson_inline simd8<bool>(bool _value) : base8<bool>(splat(_value)) {}
67 
68  simdjson_inline int to_bitmask() const { return _mm256_movemask_epi8(*this); }
69  simdjson_inline bool any() const { return !_mm256_testz_si256(*this, *this); }
70  simdjson_inline simd8<bool> operator~() const { return *this ^ true; }
71  };
72 
73  template<typename T>
74  struct base8_numeric: base8<T> {
75  static simdjson_inline simd8<T> splat(T _value) { return _mm256_set1_epi8(_value); }
76  static simdjson_inline simd8<T> zero() { return _mm256_setzero_si256(); }
77  static simdjson_inline simd8<T> load(const T values[32]) {
78  return _mm256_loadu_si256(reinterpret_cast<const __m256i *>(values));
79  }
80  // Repeat 16 values as many times as necessary (usually for lookup tables)
81  static simdjson_inline simd8<T> repeat_16(
82  T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7,
83  T v8, T v9, T v10, T v11, T v12, T v13, T v14, T v15
84  ) {
85  return simd8<T>(
86  v0, v1, v2, v3, v4, v5, v6, v7,
87  v8, v9, v10,v11,v12,v13,v14,v15,
88  v0, v1, v2, v3, v4, v5, v6, v7,
89  v8, v9, v10,v11,v12,v13,v14,v15
90  );
91  }
92 
93  simdjson_inline base8_numeric() : base8<T>() {}
94  simdjson_inline base8_numeric(const __m256i _value) : base8<T>(_value) {}
95 
96  // Store to array
97  simdjson_inline void store(T dst[32]) const { return _mm256_storeu_si256(reinterpret_cast<__m256i *>(dst), *this); }
98 
99  // Addition/subtraction are the same for signed and unsigned
100  simdjson_inline simd8<T> operator+(const simd8<T> other) const { return _mm256_add_epi8(*this, other); }
101  simdjson_inline simd8<T> operator-(const simd8<T> other) const { return _mm256_sub_epi8(*this, other); }
102  simdjson_inline simd8<T>& operator+=(const simd8<T> other) { *this = *this + other; return *static_cast<simd8<T>*>(this); }
103  simdjson_inline simd8<T>& operator-=(const simd8<T> other) { *this = *this - other; return *static_cast<simd8<T>*>(this); }
104 
105  // Override to distinguish from bool version
106  simdjson_inline simd8<T> operator~() const { return *this ^ 0xFFu; }
107 
108  // Perform a lookup assuming the value is between 0 and 16 (undefined behavior for out of range values)
109  template<typename L>
110  simdjson_inline simd8<L> lookup_16(simd8<L> lookup_table) const {
111  return _mm256_shuffle_epi8(lookup_table, *this);
112  }
113 
114  // Copies to 'output" all bytes corresponding to a 0 in the mask (interpreted as a bitset).
115  // Passing a 0 value for mask would be equivalent to writing out every byte to output.
116  // Only the first 32 - count_ones(mask) bytes of the result are significant but 32 bytes
117  // get written.
118  // Design consideration: it seems like a function with the
119  // signature simd8<L> compress(uint32_t mask) would be
120  // sensible, but the AVX ISA makes this kind of approach difficult.
121  template<typename L>
122  simdjson_inline void compress(uint32_t mask, L * output) const {
123  using internal::thintable_epi8;
124  using internal::BitsSetTable256mul2;
125  using internal::pshufb_combine_table;
126  // this particular implementation was inspired by work done by @animetosho
127  // we do it in four steps, first 8 bytes and then second 8 bytes...
128  uint8_t mask1 = uint8_t(mask); // least significant 8 bits
129  uint8_t mask2 = uint8_t(mask >> 8); // second least significant 8 bits
130  uint8_t mask3 = uint8_t(mask >> 16); // ...
131  uint8_t mask4 = uint8_t(mask >> 24); // ...
132  // next line just loads the 64-bit values thintable_epi8[mask1] and
133  // thintable_epi8[mask2] into a 128-bit register, using only
134  // two instructions on most compilers.
135  __m256i shufmask = _mm256_set_epi64x(thintable_epi8[mask4], thintable_epi8[mask3],
136  thintable_epi8[mask2], thintable_epi8[mask1]);
137  // we increment by 0x08 the second half of the mask and so forth
138  shufmask =
139  _mm256_add_epi8(shufmask, _mm256_set_epi32(0x18181818, 0x18181818,
140  0x10101010, 0x10101010, 0x08080808, 0x08080808, 0, 0));
141  // this is the version "nearly pruned"
142  __m256i pruned = _mm256_shuffle_epi8(*this, shufmask);
143  // we still need to put the pieces back together.
144  // we compute the popcount of the first words:
145  int pop1 = BitsSetTable256mul2[mask1];
146  int pop3 = BitsSetTable256mul2[mask3];
147 
148  // then load the corresponding mask
149  // could be done with _mm256_loadu2_m128i but many standard libraries omit this intrinsic.
150  __m256i v256 = _mm256_castsi128_si256(
151  _mm_loadu_si128(reinterpret_cast<const __m128i *>(pshufb_combine_table + pop1 * 8)));
152  __m256i compactmask = _mm256_insertf128_si256(v256,
153  _mm_loadu_si128(reinterpret_cast<const __m128i *>(pshufb_combine_table + pop3 * 8)), 1);
154  __m256i almostthere = _mm256_shuffle_epi8(pruned, compactmask);
155  // We just need to write out the result.
156  // This is the tricky bit that is hard to do
157  // if we want to return a SIMD register, since there
158  // is no single-instruction approach to recombine
159  // the two 128-bit lanes with an offset.
160  __m128i v128;
161  v128 = _mm256_castsi256_si128(almostthere);
162  _mm_storeu_si128( reinterpret_cast<__m128i *>(output), v128);
163  v128 = _mm256_extractf128_si256(almostthere, 1);
164  _mm_storeu_si128( reinterpret_cast<__m128i *>(output + 16 - count_ones(mask & 0xFFFF)), v128);
165  }
166 
167  template<typename L>
168  simdjson_inline simd8<L> lookup_16(
169  L replace0, L replace1, L replace2, L replace3,
170  L replace4, L replace5, L replace6, L replace7,
171  L replace8, L replace9, L replace10, L replace11,
172  L replace12, L replace13, L replace14, L replace15) const {
173  return lookup_16(simd8<L>::repeat_16(
174  replace0, replace1, replace2, replace3,
175  replace4, replace5, replace6, replace7,
176  replace8, replace9, replace10, replace11,
177  replace12, replace13, replace14, replace15
178  ));
179  }
180  };
181 
182  // Signed bytes
183  template<>
184  struct simd8<int8_t> : base8_numeric<int8_t> {
185  simdjson_inline simd8() : base8_numeric<int8_t>() {}
186  simdjson_inline simd8(const __m256i _value) : base8_numeric<int8_t>(_value) {}
187  // Splat constructor
188  simdjson_inline simd8(int8_t _value) : simd8(splat(_value)) {}
189  // Array constructor
190  simdjson_inline simd8(const int8_t values[32]) : simd8(load(values)) {}
191  // Member-by-member initialization
192  simdjson_inline simd8(
193  int8_t v0, int8_t v1, int8_t v2, int8_t v3, int8_t v4, int8_t v5, int8_t v6, int8_t v7,
194  int8_t v8, int8_t v9, int8_t v10, int8_t v11, int8_t v12, int8_t v13, int8_t v14, int8_t v15,
195  int8_t v16, int8_t v17, int8_t v18, int8_t v19, int8_t v20, int8_t v21, int8_t v22, int8_t v23,
196  int8_t v24, int8_t v25, int8_t v26, int8_t v27, int8_t v28, int8_t v29, int8_t v30, int8_t v31
197  ) : simd8(_mm256_setr_epi8(
198  v0, v1, v2, v3, v4, v5, v6, v7,
199  v8, v9, v10,v11,v12,v13,v14,v15,
200  v16,v17,v18,v19,v20,v21,v22,v23,
201  v24,v25,v26,v27,v28,v29,v30,v31
202  )) {}
203  // Repeat 16 values as many times as necessary (usually for lookup tables)
204  simdjson_inline static simd8<int8_t> repeat_16(
205  int8_t v0, int8_t v1, int8_t v2, int8_t v3, int8_t v4, int8_t v5, int8_t v6, int8_t v7,
206  int8_t v8, int8_t v9, int8_t v10, int8_t v11, int8_t v12, int8_t v13, int8_t v14, int8_t v15
207  ) {
208  return simd8<int8_t>(
209  v0, v1, v2, v3, v4, v5, v6, v7,
210  v8, v9, v10,v11,v12,v13,v14,v15,
211  v0, v1, v2, v3, v4, v5, v6, v7,
212  v8, v9, v10,v11,v12,v13,v14,v15
213  );
214  }
215 
216  // Order-sensitive comparisons
217  simdjson_inline simd8<int8_t> max_val(const simd8<int8_t> other) const { return _mm256_max_epi8(*this, other); }
218  simdjson_inline simd8<int8_t> min_val(const simd8<int8_t> other) const { return _mm256_min_epi8(*this, other); }
219  simdjson_inline simd8<bool> operator>(const simd8<int8_t> other) const { return _mm256_cmpgt_epi8(*this, other); }
220  simdjson_inline simd8<bool> operator<(const simd8<int8_t> other) const { return _mm256_cmpgt_epi8(other, *this); }
221  };
222 
223  // Unsigned bytes
224  template<>
225  struct simd8<uint8_t>: base8_numeric<uint8_t> {
226  simdjson_inline simd8() : base8_numeric<uint8_t>() {}
227  simdjson_inline simd8(const __m256i _value) : base8_numeric<uint8_t>(_value) {}
228  // Splat constructor
229  simdjson_inline simd8(uint8_t _value) : simd8(splat(_value)) {}
230  // Array constructor
231  simdjson_inline simd8(const uint8_t values[32]) : simd8(load(values)) {}
232  // Member-by-member initialization
233  simdjson_inline simd8(
234  uint8_t v0, uint8_t v1, uint8_t v2, uint8_t v3, uint8_t v4, uint8_t v5, uint8_t v6, uint8_t v7,
235  uint8_t v8, uint8_t v9, uint8_t v10, uint8_t v11, uint8_t v12, uint8_t v13, uint8_t v14, uint8_t v15,
236  uint8_t v16, uint8_t v17, uint8_t v18, uint8_t v19, uint8_t v20, uint8_t v21, uint8_t v22, uint8_t v23,
237  uint8_t v24, uint8_t v25, uint8_t v26, uint8_t v27, uint8_t v28, uint8_t v29, uint8_t v30, uint8_t v31
238  ) : simd8(_mm256_setr_epi8(
239  v0, v1, v2, v3, v4, v5, v6, v7,
240  v8, v9, v10,v11,v12,v13,v14,v15,
241  v16,v17,v18,v19,v20,v21,v22,v23,
242  v24,v25,v26,v27,v28,v29,v30,v31
243  )) {}
244  // Repeat 16 values as many times as necessary (usually for lookup tables)
245  simdjson_inline static simd8<uint8_t> repeat_16(
246  uint8_t v0, uint8_t v1, uint8_t v2, uint8_t v3, uint8_t v4, uint8_t v5, uint8_t v6, uint8_t v7,
247  uint8_t v8, uint8_t v9, uint8_t v10, uint8_t v11, uint8_t v12, uint8_t v13, uint8_t v14, uint8_t v15
248  ) {
249  return simd8<uint8_t>(
250  v0, v1, v2, v3, v4, v5, v6, v7,
251  v8, v9, v10,v11,v12,v13,v14,v15,
252  v0, v1, v2, v3, v4, v5, v6, v7,
253  v8, v9, v10,v11,v12,v13,v14,v15
254  );
255  }
256 
257  // Saturated math
258  simdjson_inline simd8<uint8_t> saturating_add(const simd8<uint8_t> other) const { return _mm256_adds_epu8(*this, other); }
259  simdjson_inline simd8<uint8_t> saturating_sub(const simd8<uint8_t> other) const { return _mm256_subs_epu8(*this, other); }
260 
261  // Order-specific operations
262  simdjson_inline simd8<uint8_t> max_val(const simd8<uint8_t> other) const { return _mm256_max_epu8(*this, other); }
263  simdjson_inline simd8<uint8_t> min_val(const simd8<uint8_t> other) const { return _mm256_min_epu8(other, *this); }
264  // Same as >, but only guarantees true is nonzero (< guarantees true = -1)
265  simdjson_inline simd8<uint8_t> gt_bits(const simd8<uint8_t> other) const { return this->saturating_sub(other); }
266  // Same as <, but only guarantees true is nonzero (< guarantees true = -1)
267  simdjson_inline simd8<uint8_t> lt_bits(const simd8<uint8_t> other) const { return other.saturating_sub(*this); }
268  simdjson_inline simd8<bool> operator<=(const simd8<uint8_t> other) const { return other.max_val(*this) == other; }
269  simdjson_inline simd8<bool> operator>=(const simd8<uint8_t> other) const { return other.min_val(*this) == other; }
270  simdjson_inline simd8<bool> operator>(const simd8<uint8_t> other) const { return this->gt_bits(other).any_bits_set(); }
271  simdjson_inline simd8<bool> operator<(const simd8<uint8_t> other) const { return this->lt_bits(other).any_bits_set(); }
272 
273  // Bit-specific operations
274  simdjson_inline simd8<bool> bits_not_set() const { return *this == uint8_t(0); }
275  simdjson_inline simd8<bool> bits_not_set(simd8<uint8_t> bits) const { return (*this & bits).bits_not_set(); }
276  simdjson_inline simd8<bool> any_bits_set() const { return ~this->bits_not_set(); }
277  simdjson_inline simd8<bool> any_bits_set(simd8<uint8_t> bits) const { return ~this->bits_not_set(bits); }
278  simdjson_inline bool is_ascii() const { return _mm256_movemask_epi8(*this) == 0; }
279  simdjson_inline bool bits_not_set_anywhere() const { return _mm256_testz_si256(*this, *this); }
280  simdjson_inline bool any_bits_set_anywhere() const { return !bits_not_set_anywhere(); }
281  simdjson_inline bool bits_not_set_anywhere(simd8<uint8_t> bits) const { return _mm256_testz_si256(*this, bits); }
282  simdjson_inline bool any_bits_set_anywhere(simd8<uint8_t> bits) const { return !bits_not_set_anywhere(bits); }
283  template<int N>
284  simdjson_inline simd8<uint8_t> shr() const { return simd8<uint8_t>(_mm256_srli_epi16(*this, N)) & uint8_t(0xFFu >> N); }
285  template<int N>
286  simdjson_inline simd8<uint8_t> shl() const { return simd8<uint8_t>(_mm256_slli_epi16(*this, N)) & uint8_t(0xFFu << N); }
287  // Get one of the bits and make a bitmask out of it.
288  // e.g. value.get_bit<7>() gets the high bit
289  template<int N>
290  simdjson_inline int get_bit() const { return _mm256_movemask_epi8(_mm256_slli_epi16(*this, 7-N)); }
291  };
292 
293  template<typename T>
294  struct simd8x64 {
295  static constexpr int NUM_CHUNKS = 64 / sizeof(simd8<T>);
296  static_assert(NUM_CHUNKS == 2, "Haswell kernel should use two registers per 64-byte block.");
297  const simd8<T> chunks[NUM_CHUNKS];
298 
299  simd8x64(const simd8x64<T>& o) = delete; // no copy allowed
300  simd8x64<T>& operator=(const simd8<T>& other) = delete; // no assignment allowed
301  simd8x64() = delete; // no default constructor allowed
302 
303  simdjson_inline simd8x64(const simd8<T> chunk0, const simd8<T> chunk1) : chunks{chunk0, chunk1} {}
304  simdjson_inline simd8x64(const T ptr[64]) : chunks{simd8<T>::load(ptr), simd8<T>::load(ptr+32)} {}
305 
306  simdjson_inline uint64_t compress(uint64_t mask, T * output) const {
307  uint32_t mask1 = uint32_t(mask);
308  uint32_t mask2 = uint32_t(mask >> 32);
309  this->chunks[0].compress(mask1, output);
310  this->chunks[1].compress(mask2, output + 32 - count_ones(mask1));
311  return 64 - count_ones(mask);
312  }
313 
314  simdjson_inline void store(T ptr[64]) const {
315  this->chunks[0].store(ptr+sizeof(simd8<T>)*0);
316  this->chunks[1].store(ptr+sizeof(simd8<T>)*1);
317  }
318 
319  simdjson_inline uint64_t to_bitmask() const {
320  uint64_t r_lo = uint32_t(this->chunks[0].to_bitmask());
321  uint64_t r_hi = this->chunks[1].to_bitmask();
322  return r_lo | (r_hi << 32);
323  }
324 
325  simdjson_inline simd8<T> reduce_or() const {
326  return this->chunks[0] | this->chunks[1];
327  }
328 
329  simdjson_inline simd8x64<T> bit_or(const T m) const {
330  const simd8<T> mask = simd8<T>::splat(m);
331  return simd8x64<T>(
332  this->chunks[0] | mask,
333  this->chunks[1] | mask
334  );
335  }
336 
337  simdjson_inline uint64_t eq(const T m) const {
338  const simd8<T> mask = simd8<T>::splat(m);
339  return simd8x64<bool>(
340  this->chunks[0] == mask,
341  this->chunks[1] == mask
342  ).to_bitmask();
343  }
344 
345  simdjson_inline uint64_t eq(const simd8x64<uint8_t> &other) const {
346  return simd8x64<bool>(
347  this->chunks[0] == other.chunks[0],
348  this->chunks[1] == other.chunks[1]
349  ).to_bitmask();
350  }
351 
352  simdjson_inline uint64_t lteq(const T m) const {
353  const simd8<T> mask = simd8<T>::splat(m);
354  return simd8x64<bool>(
355  this->chunks[0] <= mask,
356  this->chunks[1] <= mask
357  ).to_bitmask();
358  }
359  }; // struct simd8x64<T>
360 
361 } // namespace simd
362 
363 } // unnamed namespace
364 } // namespace SIMDJSON_IMPLEMENTATION
365 } // namespace simdjson
366 
367 #endif // SIMDJSON_HASWELL_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...
We want to support argument-dependent lookup (ADL).