simdjson 4.2.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
stringparsing_defs.h
1#ifndef SIMDJSON_WESTMERE_STRINGPARSING_DEFS_H
2#define SIMDJSON_WESTMERE_STRINGPARSING_DEFS_H
3
4#include "simdjson/westmere/bitmanipulation.h"
5#include "simdjson/westmere/simd.h"
6
7namespace simdjson {
8namespace westmere {
9namespace {
10
11using namespace simd;
12
13// Holds backslashes and quotes locations.
14struct backslash_and_quote {
15public:
16 static constexpr uint32_t BYTES_PROCESSED = 32;
17 simdjson_inline backslash_and_quote copy_and_find(const uint8_t *src, uint8_t *dst);
18
19 simdjson_inline bool has_quote_first() { return ((bs_bits - 1) & quote_bits) != 0; }
20 simdjson_inline bool has_backslash() { return bs_bits != 0; }
21 simdjson_inline int quote_index() { return trailing_zeroes(quote_bits); }
22 simdjson_inline int backslash_index() { return trailing_zeroes(bs_bits); }
23
24 uint32_t bs_bits;
25 uint32_t quote_bits;
26}; // struct backslash_and_quote
27
28simdjson_inline backslash_and_quote backslash_and_quote::copy_and_find(const uint8_t *src, uint8_t *dst) {
29 // this can read up to 31 bytes beyond the buffer size, but we require
30 // SIMDJSON_PADDING of padding
31 static_assert(SIMDJSON_PADDING >= (BYTES_PROCESSED - 1), "backslash and quote finder must process fewer than SIMDJSON_PADDING bytes");
32 simd8<uint8_t> v0(src);
33 simd8<uint8_t> v1(src + 16);
34 v0.store(dst);
35 v1.store(dst + 16);
36 uint64_t bs_and_quote = simd8x64<bool>(v0 == '\\', v1 == '\\', v0 == '"', v1 == '"').to_bitmask();
37 return {
38 uint32_t(bs_and_quote), // bs_bits
39 uint32_t(bs_and_quote >> 32) // quote_bits
40 };
41}
42
43
44struct escaping {
45 static constexpr uint32_t BYTES_PROCESSED = 16;
46 simdjson_inline static escaping copy_and_find(const uint8_t *src, uint8_t *dst);
47
48 simdjson_inline bool has_escape() { return escape_bits != 0; }
49 simdjson_inline int escape_index() { return trailing_zeroes(escape_bits); }
50
51 uint64_t escape_bits;
52}; // struct escaping
53
54
55
56simdjson_inline escaping escaping::copy_and_find(const uint8_t *src, uint8_t *dst) {
57 static_assert(SIMDJSON_PADDING >= (BYTES_PROCESSED - 1), "escaping finder must process fewer than SIMDJSON_PADDING bytes");
58 simd8<uint8_t> v(src);
59 v.store(dst);
60 simd8<bool> is_quote = (v == '"');
61 simd8<bool> is_backslash = (v == '\\');
62 simd8<bool> is_control = (v < 32);
63 return {
64 uint64_t((is_backslash | is_quote | is_control).to_bitmask())
65 };
66}
67
68} // unnamed namespace
69} // namespace westmere
70} // namespace simdjson
71
72#endif // SIMDJSON_WESTMERE_STRINGPARSING_DEFS_H
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.
Definition base.h:33