Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc2baa35b5 | ||
|
|
d8abf6a861 |
No files matched your search
@@ -40,7 +40,7 @@ header. See also the [macro overview page](../../features/macros.md).
|
||||
- [**JSON_HAS_THREE_WAY_COMPARISON**](json_has_three_way_comparison.md) - control 3-way comparison support
|
||||
- [**JSON_NO_AUTOMATIC_UDLS**](json_no_automatic_udls.md) - do not include the user-defined string literals (UDLs) automatically
|
||||
- [**JSON_NO_IO**](json_no_io.md) - switch off functions relying on certain C++ I/O headers
|
||||
- [**JSON_NO_SIMD**](json_no_simd.md) - use only portable code instead of vector instructions (SSE2, SSSE3, NEON)
|
||||
- [**JSON_NO_SIMD**](json_no_simd.md) - use only portable code instead of vector instructions (SSE2, SSSE3, AVX2, NEON)
|
||||
- [**JSON_NO_THREAD_LOCAL**](json_no_thread_local.md) - switch off the use of `thread_local` storage
|
||||
- [**JSON_RECURSION_DEPTH_LIMIT**](json_recursion_depth_limit.md) - set how many nesting levels the operations on a value
|
||||
recurse into on the call stack (default 128)
|
||||
|
||||
@@ -28,7 +28,9 @@ By default, `#!cpp JSON_NO_SIMD` is not defined, and vector instructions are use
|
||||
and writes the digits of floating-point numbers. The UTF-8 check of non-ASCII text needs SSSE3, which is not part
|
||||
of x86-64: it is compiled for SSSE3 with a function attribute and used when the CPU has SSSE3 (every x86-64 CPU
|
||||
since about 2011), else the portable check. Code compiled for SSSE3 anyway (e.g., with `-mssse3` or `-march=native`)
|
||||
skips this check of the CPU. 32-bit x86 uses SSE2 for strings when the compiler targets it.
|
||||
skips this check of the CPU. Where the CPU has AVX2, the UTF-8 check of strings takes 32 bytes at a time
|
||||
(compiled with a function attribute as well; not with MSVC or GCC for Windows). 32-bit x86 uses SSE2 for strings
|
||||
when the compiler targets it.
|
||||
- **AArch64:** NEON, which every 64-bit Arm CPU has, checks UTF-8 text and long runs of plain string characters 16
|
||||
bytes at a time, and writes the digits of floating-point numbers. With MSVC, only the floating-point digits use
|
||||
NEON.
|
||||
|
||||
@@ -176,7 +176,7 @@ See [full documentation of `JSON_NO_IO`](../api/macros/json_no_io.md).
|
||||
|
||||
## `JSON_NO_SIMD`
|
||||
|
||||
When defined, the library uses only portable code instead of vector instructions (SSE2, SSSE3, NEON) for scanning
|
||||
When defined, the library uses only portable code instead of vector instructions (SSE2, SSSE3, AVX2, NEON) for scanning
|
||||
strings and writing floating-point numbers. Results are the same either way. It is not defined by default.
|
||||
|
||||
See [full documentation of `JSON_NO_SIMD`](../api/macros/json_no_simd.md).
|
||||
|
||||
@@ -109,8 +109,8 @@ follow the usual `std::streambuf` contract, and
|
||||
[`JSON_DISABLE_UNLOCKED_FILE_IO`](../../api/macros/json_disable_unlocked_file_io.md) for programs in which another
|
||||
thread must use the same `#!cpp FILE` object while it is parsed.
|
||||
|
||||
Where the target has them, strings are scanned with vector instructions: SSE2 on x86-64 (and SSSE3 for UTF-8 text, when
|
||||
the CPU has it) and NEON on AArch64. [`JSON_NO_SIMD`](../../api/macros/json_no_simd.md) selects portable code instead,
|
||||
Where the target has them, strings are scanned with vector instructions: SSE2 on x86-64 (and SSSE3 or AVX2 for UTF-8 text,
|
||||
when the CPU has it) and NEON on AArch64. [`JSON_NO_SIMD`](../../api/macros/json_no_simd.md) selects portable code instead,
|
||||
again without changing any result.
|
||||
|
||||
## SAX vs. DOM parsing
|
||||
|
||||
@@ -79,6 +79,26 @@
|
||||
#define JSON_SCAN_SSSE3_DISPATCH 0 // NOLINT(cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
|
||||
#define JSON_SCAN_SSSE3_TARGET
|
||||
#endif
|
||||
// The UTF-8 check of string runs (vector_utf8_run) also has a 32-byte variant
|
||||
// for CPUs with AVX2 (most x86-64 CPUs since about 2015), compiled with a
|
||||
// function attribute and chosen at run time inside the out-of-line 16-byte
|
||||
// check, whatever the flags of the translation unit: the CPU check adds no code
|
||||
// to the lexer's string loop, and every translation unit has the same
|
||||
// definitions. Not with MSVC (not measured), and not with GCC for Windows,
|
||||
// which does not align the stack for spilled AVX registers (GCC bug 54412).
|
||||
// Not with NVIDIA HPC (nvc++ defines __GNUC__ but declares the AVX2 intrinsics
|
||||
// only when the translation unit is compiled for AVX2).
|
||||
#if JSON_SCAN_SSE2 && !defined(NLOHMANN_JSON_NO_TARGET_ATTRIBUTE) && !defined(__NVCOMPILER) && !(defined(_WIN32) && !defined(__clang__)) && ((defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && !defined(__cpp_modules)))
|
||||
#ifndef NLOHMANN_JSON_CPUID_INCLUDED
|
||||
#define NLOHMANN_JSON_CPUID_INCLUDED
|
||||
#include <cpuid.h>
|
||||
#endif
|
||||
#include <immintrin.h>
|
||||
#define JSON_SCAN_AVX2_DISPATCH 1 // NOLINT(cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
|
||||
#define JSON_SCAN_AVX2_TARGET __attribute__((target("avx2")))
|
||||
#else
|
||||
#define JSON_SCAN_AVX2_DISPATCH 0 // NOLINT(cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
|
||||
#endif
|
||||
#define JSON_SCAN_VECTOR (JSON_SCAN_NEON || JSON_SCAN_SSE2)
|
||||
#define JSON_SCAN_VECTOR_UTF8 (JSON_SCAN_NEON || JSON_SCAN_SSSE3 || JSON_SCAN_SSSE3_DISPATCH)
|
||||
|
||||
@@ -179,6 +199,55 @@ JSON_HEDLEY_ALWAYS_INLINE bool cpu_has_ssse3() noexcept
|
||||
}
|
||||
#endif
|
||||
|
||||
/// whether the CPU has AVX2 (CPUID leaf 7, EBX bit 5) and the OS saves the
|
||||
/// YMM registers (CPUID leaf 1, ECX bits 27 and 28; XCR0 bits 1 and 2); false
|
||||
/// where the library has no AVX2 code
|
||||
inline bool cpu_avx2() noexcept
|
||||
{
|
||||
#if JSON_SCAN_AVX2_DISPATCH
|
||||
unsigned eax = 0;
|
||||
unsigned ebx = 0;
|
||||
unsigned ecx = 0;
|
||||
unsigned edx = 0;
|
||||
if (__get_cpuid_max(0, nullptr) < 7 || __get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0 || (ecx & (3u << 27u)) != (3u << 27u))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
unsigned xcr0 = 0; // NOLINT(misc-const-correctness): written by the assembly
|
||||
unsigned xcr0_high = 0; // NOLINT(misc-const-correctness)
|
||||
__asm__ __volatile__("xgetbv" : "=a"(xcr0), "=d"(xcr0_high) : "c"(0)); // NOLINT(hicpp-no-assembler)
|
||||
if ((xcr0 & 6u) != 6u)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
__cpuid_count(7, 0, eax, ebx, ecx, edx);
|
||||
return (ebx & (1u << 5u)) != 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// the answer of cpu_avx2(), asked once (0: not asked yet, 1: no, 2: yes; see
|
||||
/// cpu_has_ssse3()); the tests store 1 to check the 16-byte code on CPUs with
|
||||
/// AVX2, and 0 to ask again
|
||||
inline std::atomic<int>& cpu_avx2_state() noexcept
|
||||
{
|
||||
static std::atomic<int> known{0};
|
||||
return known;
|
||||
}
|
||||
|
||||
/// whether the CPU has AVX2
|
||||
JSON_HEDLEY_ALWAYS_INLINE bool cpu_has_avx2() noexcept
|
||||
{
|
||||
int state = cpu_avx2_state().load(std::memory_order_relaxed);
|
||||
if (JSON_HEDLEY_UNLIKELY(state == 0))
|
||||
{
|
||||
state = cpu_avx2() ? 2 : 1;
|
||||
cpu_avx2_state().store(state, std::memory_order_relaxed);
|
||||
}
|
||||
return state == 2;
|
||||
}
|
||||
|
||||
#if JSON_SCAN_VECTOR_UTF8
|
||||
/// Tables of the UTF-8 check of J. Keiser and D. Lemire, "Validating UTF-8 In
|
||||
/// Less Than One Instruction Per Byte" (2021), as in simdjson ("lookup4"): each
|
||||
@@ -230,6 +299,112 @@ const std::array<std::uint8_t, 16> utf8_lookup4<Dummy>::byte_2_high =
|
||||
}
|
||||
};
|
||||
|
||||
#if JSON_SCAN_AVX2_DISPATCH
|
||||
/// the errors of the UTF-8 check in 16 bytes @a in after @a prev (see
|
||||
/// vector_utf8_run()), for the 32-byte functions
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_ALWAYS_INLINE __m128i utf8_errors_avx2_16(const __m128i in, const __m128i prev, const __m128i t1h, const __m128i t1l, const __m128i t2h) noexcept
|
||||
{
|
||||
const __m128i nibble = _mm_set1_epi8(0x0F);
|
||||
const __m128i prev1 = _mm_alignr_epi8(in, prev, 15);
|
||||
const __m128i sc = _mm_and_si128(_mm_and_si128(_mm_shuffle_epi8(t1h, _mm_and_si128(_mm_srli_epi16(prev1, 4), nibble)),
|
||||
_mm_shuffle_epi8(t1l, _mm_and_si128(prev1, nibble))),
|
||||
_mm_shuffle_epi8(t2h, _mm_and_si128(_mm_srli_epi16(in, 4), nibble)));
|
||||
const __m128i must23 = _mm_or_si128(_mm_subs_epu8(_mm_alignr_epi8(in, prev, 14), _mm_set1_epi8(0xE0 - 0x80)),
|
||||
_mm_subs_epu8(_mm_alignr_epi8(in, prev, 13), _mm_set1_epi8(0xF0 - 0x80)));
|
||||
return _mm_xor_si128(_mm_and_si128(must23, _mm_set1_epi8(static_cast<char>(-128))), sc);
|
||||
}
|
||||
|
||||
/// the errors of the UTF-8 check in 32 bytes @a in after @a prev: as with 16
|
||||
/// bytes, but the byte shifts work within each 16-byte lane, so the bytes
|
||||
/// before each lane are put together first
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_ALWAYS_INLINE __m256i utf8_errors_avx2_32(const __m256i in, const __m256i prev, const __m256i t1h, const __m256i t1l, const __m256i t2h) noexcept
|
||||
{
|
||||
const __m256i nibble = _mm256_set1_epi8(0x0F);
|
||||
const __m256i before = _mm256_permute2x128_si256(prev, in, 0x21); // the upper half of prev, the lower half of in
|
||||
const __m256i prev1 = _mm256_alignr_epi8(in, before, 15);
|
||||
const __m256i sc = _mm256_and_si256(_mm256_and_si256(_mm256_shuffle_epi8(t1h, _mm256_and_si256(_mm256_srli_epi16(prev1, 4), nibble)),
|
||||
_mm256_shuffle_epi8(t1l, _mm256_and_si256(prev1, nibble))),
|
||||
_mm256_shuffle_epi8(t2h, _mm256_and_si256(_mm256_srli_epi16(in, 4), nibble)));
|
||||
const __m256i must23 = _mm256_or_si256(_mm256_subs_epu8(_mm256_alignr_epi8(in, before, 14), _mm256_set1_epi8(0xE0 - 0x80)),
|
||||
_mm256_subs_epu8(_mm256_alignr_epi8(in, before, 13), _mm256_set1_epi8(0xF0 - 0x80)));
|
||||
return _mm256_xor_si256(_mm256_and_si256(must23, _mm256_set1_epi8(static_cast<char>(-128))), sc);
|
||||
}
|
||||
|
||||
/// the quotes, backslashes, and control characters in 16 bytes (see vector_utf8_run())
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_ALWAYS_INLINE __m128i string_specials_avx2_16(const __m128i in) noexcept
|
||||
{
|
||||
return _mm_or_si128(_mm_cmpeq_epi8(in, _mm_set1_epi8('\\')),
|
||||
_mm_cmpeq_epi8(_mm_subs_epu8(_mm_xor_si128(in, _mm_set1_epi8(2)), _mm_set1_epi8(32)), _mm_setzero_si128()));
|
||||
}
|
||||
|
||||
/// vector_utf8_run() with AVX2: 32 bytes per step, then one 16-byte step
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_NEVER_INLINE inline bool vector_utf8_run_avx2(const unsigned char* p, const unsigned char* e, const unsigned char*& block_out) noexcept
|
||||
{
|
||||
using lookup = utf8_lookup4<>;
|
||||
const unsigned char* block = p;
|
||||
const __m128i t1h_16 = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t1l_16 = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_low.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t2h_16 = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_2_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m256i t1h = _mm256_broadcastsi128_si256(t1h_16);
|
||||
const __m256i t1l = _mm256_broadcastsi128_si256(t1l_16);
|
||||
const __m256i t2h = _mm256_broadcastsi128_si256(t2h_16);
|
||||
const __m256i zero = _mm256_setzero_si256();
|
||||
__m256i prev = zero;
|
||||
while (e - block >= 32)
|
||||
{
|
||||
const __m256i in = _mm256_loadu_si256(static_cast<const __m256i*>(static_cast<const void*>(block))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m256i special = _mm256_or_si256(_mm256_cmpeq_epi8(in, _mm256_set1_epi8('\\')),
|
||||
_mm256_cmpeq_epi8(_mm256_subs_epu8(_mm256_xor_si256(in, _mm256_set1_epi8(2)), _mm256_set1_epi8(32)), zero));
|
||||
const __m256i err = utf8_errors_avx2_32(in, prev, t1h, t1l, t2h);
|
||||
const auto special_bits = static_cast<unsigned>(_mm256_movemask_epi8(special));
|
||||
const auto err_bits = ~static_cast<unsigned>(_mm256_movemask_epi8(_mm256_cmpeq_epi8(err, zero)));
|
||||
if (special_bits != 0)
|
||||
{
|
||||
// errors up to the special byte count (see vector_utf8_run()); for
|
||||
// k = 31, 2u << k is 0 and the mask has all bits set
|
||||
const auto k = static_cast<unsigned>(count_trailing_zeros(static_cast<std::uint64_t>(special_bits)));
|
||||
if ((err_bits & ((2u << k) - 1u)) == 0)
|
||||
{
|
||||
block_out = block + k;
|
||||
return true;
|
||||
}
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
if (err_bits != 0)
|
||||
{
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
prev = in;
|
||||
block += 32;
|
||||
}
|
||||
if (e - block >= 16)
|
||||
{
|
||||
const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(block))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i err = utf8_errors_avx2_16(in, _mm256_extracti128_si256(prev, 1), t1h_16, t1l_16, t2h_16);
|
||||
const auto special_bits = static_cast<unsigned>(_mm_movemask_epi8(string_specials_avx2_16(in)));
|
||||
const auto err_bits = ~static_cast<unsigned>(_mm_movemask_epi8(_mm_cmpeq_epi8(err, _mm_setzero_si128()))) & 0xFFFFu;
|
||||
if (special_bits != 0)
|
||||
{
|
||||
const auto k = static_cast<unsigned>(count_trailing_zeros(static_cast<std::uint64_t>(special_bits)));
|
||||
if ((err_bits & ((2u << k) - 1u)) == 0)
|
||||
{
|
||||
block_out = block + k;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (err_bits == 0)
|
||||
{
|
||||
block += 16;
|
||||
}
|
||||
}
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*!
|
||||
@brief the run of plain string bytes and well-formed UTF-8 at p (a character
|
||||
boundary), 16 bytes per step
|
||||
@@ -240,10 +415,17 @@ to it. Returns true with @a block at that byte; returns false with @a block at
|
||||
the start of the 16 bytes where the check found ill-formed UTF-8 or where
|
||||
fewer than 16 bytes are left, for the caller to finish one sequence at a time.
|
||||
Out of line, so that no constants of the check occupy registers in the
|
||||
caller's loop. On x86-64, it is compiled for SSSE3 (see cpu_has_ssse3()).
|
||||
caller's loop. On x86-64, it is compiled for SSSE3 (see cpu_has_ssse3()), and
|
||||
hands runs of 32 bytes or more to vector_utf8_run_avx2() where the CPU has AVX2.
|
||||
*/
|
||||
JSON_SCAN_SSSE3_TARGET JSON_HEDLEY_NEVER_INLINE inline bool vector_utf8_run(const unsigned char* p, const unsigned char* e, const unsigned char*& block_out) noexcept
|
||||
{
|
||||
#if JSON_SCAN_AVX2_DISPATCH
|
||||
if (e - p >= 32 && cpu_has_avx2())
|
||||
{
|
||||
return vector_utf8_run_avx2(p, e, block_out);
|
||||
}
|
||||
#endif
|
||||
using lookup = utf8_lookup4<>;
|
||||
const unsigned char* block = p;
|
||||
#if JSON_SCAN_NEON
|
||||
|
||||
@@ -223,157 +223,6 @@ inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avai
|
||||
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the transition table of the UTF-8 DFA of utf8_dfa_run() and utf8_dfa_prefix()
|
||||
|
||||
They accept exactly what validate_one_utf8() accepts. This is a shift-based
|
||||
DFA (after Per Vognsen; see also https://orlp.net/blog/utf8-dfa/): a state is
|
||||
6 times its number, and table[b] holds, for every state s, the next state on
|
||||
byte b at bit offset s. One step is a load and a shift without a branch on the
|
||||
byte. On CJK text, it made the lexer and the serializer without SSE2/NEON 30%
|
||||
faster and the BON8 reader 15% faster than a loop over validate_one_utf8()
|
||||
(Apple M, Apple Clang).
|
||||
|
||||
The states are 0 (error, absorbing), 6 (between sequences), 12/18/24 (1/2/3
|
||||
continuation bytes of 0x80-0xBF missing), and the states after the lead bytes
|
||||
that restrict the second byte: 30 (0xE0: 0xA0-0xBF), 36 (0xED: 0x80-0x9F), 42
|
||||
(0xF0: 0x90-0xBF), and 48 (0xF4: 0x80-0x8F). The test "UTF-8 DFA" in
|
||||
unit-class_lexer.cpp checks every entry of the table.
|
||||
*/
|
||||
inline const std::uint64_t* utf8_dfa_table() noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 256> table =
|
||||
{
|
||||
{
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000780ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000900ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000A80ull, 0x0000000000000600ull, 0x0000000000000600ull, 0x0000000000000600ull,
|
||||
0x0000000000000C00ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a run of non-ASCII bytes as UTF-8
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()).
|
||||
@return the end of the run (@a e or a byte below 0x80) if it consists of
|
||||
complete well-formed UTF-8 sequences, otherwise nullptr
|
||||
|
||||
It stops at the first ill-formed byte: the serializer's error handlers scan
|
||||
again after every ill-formed byte, and scanning on to the end of the run would
|
||||
make them quadratic. Without the position of the error, it is faster than
|
||||
utf8_dfa_prefix(), which made the lexer 15% slower on CJK text.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
if (JSON_HEDLEY_UNLIKELY((state & 63u) == 0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return (state & 63u) == 6 ? p : nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the end of the longest well-formed UTF-8 prefix of a run of non-ASCII bytes
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()) and
|
||||
returns the end of the longest prefix that consists of complete well-formed
|
||||
UTF-8 sequences. The whole run was valid if the result is @a e or points to a
|
||||
byte below 0x80; otherwise it points to the lead byte of the first ill-formed or
|
||||
incomplete sequence. Like utf8_dfa_run(), it stops at the first ill-formed byte.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_prefix(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
const unsigned char* last = p; // the end of the last complete sequence
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
const std::uint64_t current = state & 63u;
|
||||
if (JSON_HEDLEY_UNLIKELY(current == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
last = (current == 6) ? p : last;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Return the length of the longest prefix of [data, data+n) that consists of
|
||||
// ASCII characters and complete well-formed UTF-8 sequences; n if all of it is
|
||||
// valid UTF-8. Unlike scalar_string_bulk_run(), quotes, escapes, and control
|
||||
@@ -401,12 +250,12 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
continue;
|
||||
}
|
||||
|
||||
const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
|
||||
pos = static_cast<std::size_t>(last - data);
|
||||
if (pos != n && *last >= 0x80u)
|
||||
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||
if (seq == 0)
|
||||
{
|
||||
break; // ill-formed or truncated
|
||||
}
|
||||
pos += seq;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
@@ -591,14 +440,18 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t scalar_string_bulk_run(const unsigned char
|
||||
}
|
||||
#endif
|
||||
#if !JSON_SCAN_VECTOR_UTF8 || JSON_SCAN_SSSE3_DISPATCH
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated as a whole
|
||||
const unsigned char* const run_end = utf8_dfa_run(p, e);
|
||||
if (JSON_HEDLEY_UNLIKELY(run_end == nullptr))
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated sequence
|
||||
// by sequence without searching for the next special byte in between
|
||||
do
|
||||
{
|
||||
// ill-formed or truncated: let the byte path diagnose it
|
||||
return static_cast<std::size_t>(utf8_dfa_prefix(p, e) - data);
|
||||
const std::size_t seq = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (seq == 0)
|
||||
{
|
||||
return static_cast<std::size_t>(p - data); // ill-formed or truncated: let the byte path diagnose it
|
||||
}
|
||||
p += seq;
|
||||
}
|
||||
p = run_end;
|
||||
while (p != e && *p >= 0x80u);
|
||||
#endif
|
||||
}
|
||||
return static_cast<std::size_t>(p - data);
|
||||
@@ -703,7 +556,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
|
||||
// unaffected by this fast path.
|
||||
inline int hex_codepoint(const unsigned char* data) noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table =
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
{
|
||||
{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
#undef JSON_DTOA_NEON
|
||||
#undef JSON_LITTLE_ENDIAN_TARGET
|
||||
#undef JSON_BIG_ENDIAN_TARGET
|
||||
#undef JSON_SCAN_AVX2_DISPATCH
|
||||
#undef JSON_SCAN_AVX2_TARGET
|
||||
#undef JSON_SCAN_NEON
|
||||
#undef JSON_SCAN_SSE2
|
||||
#undef JSON_SCAN_SSSE3
|
||||
|
||||
@@ -206,10 +206,9 @@ places, which differ in speed, diagnostics, and how they read the input:
|
||||
the bytes as is and leave the check to dump() and the binary writers.
|
||||
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
|
||||
diagnostic for each kind of error.
|
||||
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
|
||||
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
|
||||
reader, and the BON8 writer. They must accept exactly what the lexer's
|
||||
switch accepts.
|
||||
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
|
||||
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
|
||||
They must accept exactly what the lexer's switch accepts.
|
||||
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
|
||||
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
|
||||
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).
|
||||
|
||||
Generated
+202
-166
@@ -7388,10 +7388,9 @@ places, which differ in speed, diagnostics, and how they read the input:
|
||||
the bytes as is and leave the check to dump() and the binary writers.
|
||||
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
|
||||
diagnostic for each kind of error.
|
||||
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
|
||||
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
|
||||
reader, and the BON8 writer. They must accept exactly what the lexer's
|
||||
switch accepts.
|
||||
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
|
||||
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
|
||||
They must accept exactly what the lexer's switch accepts.
|
||||
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
|
||||
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
|
||||
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).
|
||||
@@ -12636,6 +12635,26 @@ NLOHMANN_JSON_NAMESPACE_END
|
||||
#define JSON_SCAN_SSSE3_DISPATCH 0 // NOLINT(cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
|
||||
#define JSON_SCAN_SSSE3_TARGET
|
||||
#endif
|
||||
// The UTF-8 check of string runs (vector_utf8_run) also has a 32-byte variant
|
||||
// for CPUs with AVX2 (most x86-64 CPUs since about 2015), compiled with a
|
||||
// function attribute and chosen at run time inside the out-of-line 16-byte
|
||||
// check, whatever the flags of the translation unit: the CPU check adds no code
|
||||
// to the lexer's string loop, and every translation unit has the same
|
||||
// definitions. Not with MSVC (not measured), and not with GCC for Windows,
|
||||
// which does not align the stack for spilled AVX registers (GCC bug 54412).
|
||||
// Not with NVIDIA HPC (nvc++ defines __GNUC__ but declares the AVX2 intrinsics
|
||||
// only when the translation unit is compiled for AVX2).
|
||||
#if JSON_SCAN_SSE2 && !defined(NLOHMANN_JSON_NO_TARGET_ATTRIBUTE) && !defined(__NVCOMPILER) && !(defined(_WIN32) && !defined(__clang__)) && ((defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && !defined(__cpp_modules)))
|
||||
#ifndef NLOHMANN_JSON_CPUID_INCLUDED
|
||||
#define NLOHMANN_JSON_CPUID_INCLUDED
|
||||
#include <cpuid.h>
|
||||
#endif
|
||||
#include <immintrin.h>
|
||||
#define JSON_SCAN_AVX2_DISPATCH 1 // NOLINT(cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
|
||||
#define JSON_SCAN_AVX2_TARGET __attribute__((target("avx2")))
|
||||
#else
|
||||
#define JSON_SCAN_AVX2_DISPATCH 0 // NOLINT(cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
|
||||
#endif
|
||||
#define JSON_SCAN_VECTOR (JSON_SCAN_NEON || JSON_SCAN_SSE2)
|
||||
#define JSON_SCAN_VECTOR_UTF8 (JSON_SCAN_NEON || JSON_SCAN_SSSE3 || JSON_SCAN_SSSE3_DISPATCH)
|
||||
|
||||
@@ -12736,6 +12755,55 @@ JSON_HEDLEY_ALWAYS_INLINE bool cpu_has_ssse3() noexcept
|
||||
}
|
||||
#endif
|
||||
|
||||
/// whether the CPU has AVX2 (CPUID leaf 7, EBX bit 5) and the OS saves the
|
||||
/// YMM registers (CPUID leaf 1, ECX bits 27 and 28; XCR0 bits 1 and 2); false
|
||||
/// where the library has no AVX2 code
|
||||
inline bool cpu_avx2() noexcept
|
||||
{
|
||||
#if JSON_SCAN_AVX2_DISPATCH
|
||||
unsigned eax = 0;
|
||||
unsigned ebx = 0;
|
||||
unsigned ecx = 0;
|
||||
unsigned edx = 0;
|
||||
if (__get_cpuid_max(0, nullptr) < 7 || __get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0 || (ecx & (3u << 27u)) != (3u << 27u))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
unsigned xcr0 = 0; // NOLINT(misc-const-correctness): written by the assembly
|
||||
unsigned xcr0_high = 0; // NOLINT(misc-const-correctness)
|
||||
__asm__ __volatile__("xgetbv" : "=a"(xcr0), "=d"(xcr0_high) : "c"(0)); // NOLINT(hicpp-no-assembler)
|
||||
if ((xcr0 & 6u) != 6u)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
__cpuid_count(7, 0, eax, ebx, ecx, edx);
|
||||
return (ebx & (1u << 5u)) != 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// the answer of cpu_avx2(), asked once (0: not asked yet, 1: no, 2: yes; see
|
||||
/// cpu_has_ssse3()); the tests store 1 to check the 16-byte code on CPUs with
|
||||
/// AVX2, and 0 to ask again
|
||||
inline std::atomic<int>& cpu_avx2_state() noexcept
|
||||
{
|
||||
static std::atomic<int> known{0};
|
||||
return known;
|
||||
}
|
||||
|
||||
/// whether the CPU has AVX2
|
||||
JSON_HEDLEY_ALWAYS_INLINE bool cpu_has_avx2() noexcept
|
||||
{
|
||||
int state = cpu_avx2_state().load(std::memory_order_relaxed);
|
||||
if (JSON_HEDLEY_UNLIKELY(state == 0))
|
||||
{
|
||||
state = cpu_avx2() ? 2 : 1;
|
||||
cpu_avx2_state().store(state, std::memory_order_relaxed);
|
||||
}
|
||||
return state == 2;
|
||||
}
|
||||
|
||||
#if JSON_SCAN_VECTOR_UTF8
|
||||
/// Tables of the UTF-8 check of J. Keiser and D. Lemire, "Validating UTF-8 In
|
||||
/// Less Than One Instruction Per Byte" (2021), as in simdjson ("lookup4"): each
|
||||
@@ -12787,6 +12855,112 @@ const std::array<std::uint8_t, 16> utf8_lookup4<Dummy>::byte_2_high =
|
||||
}
|
||||
};
|
||||
|
||||
#if JSON_SCAN_AVX2_DISPATCH
|
||||
/// the errors of the UTF-8 check in 16 bytes @a in after @a prev (see
|
||||
/// vector_utf8_run()), for the 32-byte functions
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_ALWAYS_INLINE __m128i utf8_errors_avx2_16(const __m128i in, const __m128i prev, const __m128i t1h, const __m128i t1l, const __m128i t2h) noexcept
|
||||
{
|
||||
const __m128i nibble = _mm_set1_epi8(0x0F);
|
||||
const __m128i prev1 = _mm_alignr_epi8(in, prev, 15);
|
||||
const __m128i sc = _mm_and_si128(_mm_and_si128(_mm_shuffle_epi8(t1h, _mm_and_si128(_mm_srli_epi16(prev1, 4), nibble)),
|
||||
_mm_shuffle_epi8(t1l, _mm_and_si128(prev1, nibble))),
|
||||
_mm_shuffle_epi8(t2h, _mm_and_si128(_mm_srli_epi16(in, 4), nibble)));
|
||||
const __m128i must23 = _mm_or_si128(_mm_subs_epu8(_mm_alignr_epi8(in, prev, 14), _mm_set1_epi8(0xE0 - 0x80)),
|
||||
_mm_subs_epu8(_mm_alignr_epi8(in, prev, 13), _mm_set1_epi8(0xF0 - 0x80)));
|
||||
return _mm_xor_si128(_mm_and_si128(must23, _mm_set1_epi8(static_cast<char>(-128))), sc);
|
||||
}
|
||||
|
||||
/// the errors of the UTF-8 check in 32 bytes @a in after @a prev: as with 16
|
||||
/// bytes, but the byte shifts work within each 16-byte lane, so the bytes
|
||||
/// before each lane are put together first
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_ALWAYS_INLINE __m256i utf8_errors_avx2_32(const __m256i in, const __m256i prev, const __m256i t1h, const __m256i t1l, const __m256i t2h) noexcept
|
||||
{
|
||||
const __m256i nibble = _mm256_set1_epi8(0x0F);
|
||||
const __m256i before = _mm256_permute2x128_si256(prev, in, 0x21); // the upper half of prev, the lower half of in
|
||||
const __m256i prev1 = _mm256_alignr_epi8(in, before, 15);
|
||||
const __m256i sc = _mm256_and_si256(_mm256_and_si256(_mm256_shuffle_epi8(t1h, _mm256_and_si256(_mm256_srli_epi16(prev1, 4), nibble)),
|
||||
_mm256_shuffle_epi8(t1l, _mm256_and_si256(prev1, nibble))),
|
||||
_mm256_shuffle_epi8(t2h, _mm256_and_si256(_mm256_srli_epi16(in, 4), nibble)));
|
||||
const __m256i must23 = _mm256_or_si256(_mm256_subs_epu8(_mm256_alignr_epi8(in, before, 14), _mm256_set1_epi8(0xE0 - 0x80)),
|
||||
_mm256_subs_epu8(_mm256_alignr_epi8(in, before, 13), _mm256_set1_epi8(0xF0 - 0x80)));
|
||||
return _mm256_xor_si256(_mm256_and_si256(must23, _mm256_set1_epi8(static_cast<char>(-128))), sc);
|
||||
}
|
||||
|
||||
/// the quotes, backslashes, and control characters in 16 bytes (see vector_utf8_run())
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_ALWAYS_INLINE __m128i string_specials_avx2_16(const __m128i in) noexcept
|
||||
{
|
||||
return _mm_or_si128(_mm_cmpeq_epi8(in, _mm_set1_epi8('\\')),
|
||||
_mm_cmpeq_epi8(_mm_subs_epu8(_mm_xor_si128(in, _mm_set1_epi8(2)), _mm_set1_epi8(32)), _mm_setzero_si128()));
|
||||
}
|
||||
|
||||
/// vector_utf8_run() with AVX2: 32 bytes per step, then one 16-byte step
|
||||
JSON_SCAN_AVX2_TARGET JSON_HEDLEY_NEVER_INLINE inline bool vector_utf8_run_avx2(const unsigned char* p, const unsigned char* e, const unsigned char*& block_out) noexcept
|
||||
{
|
||||
using lookup = utf8_lookup4<>;
|
||||
const unsigned char* block = p;
|
||||
const __m128i t1h_16 = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t1l_16 = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_low.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t2h_16 = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_2_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m256i t1h = _mm256_broadcastsi128_si256(t1h_16);
|
||||
const __m256i t1l = _mm256_broadcastsi128_si256(t1l_16);
|
||||
const __m256i t2h = _mm256_broadcastsi128_si256(t2h_16);
|
||||
const __m256i zero = _mm256_setzero_si256();
|
||||
__m256i prev = zero;
|
||||
while (e - block >= 32)
|
||||
{
|
||||
const __m256i in = _mm256_loadu_si256(static_cast<const __m256i*>(static_cast<const void*>(block))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m256i special = _mm256_or_si256(_mm256_cmpeq_epi8(in, _mm256_set1_epi8('\\')),
|
||||
_mm256_cmpeq_epi8(_mm256_subs_epu8(_mm256_xor_si256(in, _mm256_set1_epi8(2)), _mm256_set1_epi8(32)), zero));
|
||||
const __m256i err = utf8_errors_avx2_32(in, prev, t1h, t1l, t2h);
|
||||
const auto special_bits = static_cast<unsigned>(_mm256_movemask_epi8(special));
|
||||
const auto err_bits = ~static_cast<unsigned>(_mm256_movemask_epi8(_mm256_cmpeq_epi8(err, zero)));
|
||||
if (special_bits != 0)
|
||||
{
|
||||
// errors up to the special byte count (see vector_utf8_run()); for
|
||||
// k = 31, 2u << k is 0 and the mask has all bits set
|
||||
const auto k = static_cast<unsigned>(count_trailing_zeros(static_cast<std::uint64_t>(special_bits)));
|
||||
if ((err_bits & ((2u << k) - 1u)) == 0)
|
||||
{
|
||||
block_out = block + k;
|
||||
return true;
|
||||
}
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
if (err_bits != 0)
|
||||
{
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
prev = in;
|
||||
block += 32;
|
||||
}
|
||||
if (e - block >= 16)
|
||||
{
|
||||
const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(block))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i err = utf8_errors_avx2_16(in, _mm256_extracti128_si256(prev, 1), t1h_16, t1l_16, t2h_16);
|
||||
const auto special_bits = static_cast<unsigned>(_mm_movemask_epi8(string_specials_avx2_16(in)));
|
||||
const auto err_bits = ~static_cast<unsigned>(_mm_movemask_epi8(_mm_cmpeq_epi8(err, _mm_setzero_si128()))) & 0xFFFFu;
|
||||
if (special_bits != 0)
|
||||
{
|
||||
const auto k = static_cast<unsigned>(count_trailing_zeros(static_cast<std::uint64_t>(special_bits)));
|
||||
if ((err_bits & ((2u << k) - 1u)) == 0)
|
||||
{
|
||||
block_out = block + k;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (err_bits == 0)
|
||||
{
|
||||
block += 16;
|
||||
}
|
||||
}
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*!
|
||||
@brief the run of plain string bytes and well-formed UTF-8 at p (a character
|
||||
boundary), 16 bytes per step
|
||||
@@ -12797,10 +12971,17 @@ to it. Returns true with @a block at that byte; returns false with @a block at
|
||||
the start of the 16 bytes where the check found ill-formed UTF-8 or where
|
||||
fewer than 16 bytes are left, for the caller to finish one sequence at a time.
|
||||
Out of line, so that no constants of the check occupy registers in the
|
||||
caller's loop. On x86-64, it is compiled for SSSE3 (see cpu_has_ssse3()).
|
||||
caller's loop. On x86-64, it is compiled for SSSE3 (see cpu_has_ssse3()), and
|
||||
hands runs of 32 bytes or more to vector_utf8_run_avx2() where the CPU has AVX2.
|
||||
*/
|
||||
JSON_SCAN_SSSE3_TARGET JSON_HEDLEY_NEVER_INLINE inline bool vector_utf8_run(const unsigned char* p, const unsigned char* e, const unsigned char*& block_out) noexcept
|
||||
{
|
||||
#if JSON_SCAN_AVX2_DISPATCH
|
||||
if (e - p >= 32 && cpu_has_avx2())
|
||||
{
|
||||
return vector_utf8_run_avx2(p, e, block_out);
|
||||
}
|
||||
#endif
|
||||
using lookup = utf8_lookup4<>;
|
||||
const unsigned char* block = p;
|
||||
#if JSON_SCAN_NEON
|
||||
@@ -13172,157 +13353,6 @@ inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avai
|
||||
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the transition table of the UTF-8 DFA of utf8_dfa_run() and utf8_dfa_prefix()
|
||||
|
||||
They accept exactly what validate_one_utf8() accepts. This is a shift-based
|
||||
DFA (after Per Vognsen; see also https://orlp.net/blog/utf8-dfa/): a state is
|
||||
6 times its number, and table[b] holds, for every state s, the next state on
|
||||
byte b at bit offset s. One step is a load and a shift without a branch on the
|
||||
byte. On CJK text, it made the lexer and the serializer without SSE2/NEON 30%
|
||||
faster and the BON8 reader 15% faster than a loop over validate_one_utf8()
|
||||
(Apple M, Apple Clang).
|
||||
|
||||
The states are 0 (error, absorbing), 6 (between sequences), 12/18/24 (1/2/3
|
||||
continuation bytes of 0x80-0xBF missing), and the states after the lead bytes
|
||||
that restrict the second byte: 30 (0xE0: 0xA0-0xBF), 36 (0xED: 0x80-0x9F), 42
|
||||
(0xF0: 0x90-0xBF), and 48 (0xF4: 0x80-0x8F). The test "UTF-8 DFA" in
|
||||
unit-class_lexer.cpp checks every entry of the table.
|
||||
*/
|
||||
inline const std::uint64_t* utf8_dfa_table() noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 256> table =
|
||||
{
|
||||
{
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000780ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000900ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000A80ull, 0x0000000000000600ull, 0x0000000000000600ull, 0x0000000000000600ull,
|
||||
0x0000000000000C00ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a run of non-ASCII bytes as UTF-8
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()).
|
||||
@return the end of the run (@a e or a byte below 0x80) if it consists of
|
||||
complete well-formed UTF-8 sequences, otherwise nullptr
|
||||
|
||||
It stops at the first ill-formed byte: the serializer's error handlers scan
|
||||
again after every ill-formed byte, and scanning on to the end of the run would
|
||||
make them quadratic. Without the position of the error, it is faster than
|
||||
utf8_dfa_prefix(), which made the lexer 15% slower on CJK text.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
if (JSON_HEDLEY_UNLIKELY((state & 63u) == 0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return (state & 63u) == 6 ? p : nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the end of the longest well-formed UTF-8 prefix of a run of non-ASCII bytes
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()) and
|
||||
returns the end of the longest prefix that consists of complete well-formed
|
||||
UTF-8 sequences. The whole run was valid if the result is @a e or points to a
|
||||
byte below 0x80; otherwise it points to the lead byte of the first ill-formed or
|
||||
incomplete sequence. Like utf8_dfa_run(), it stops at the first ill-formed byte.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_prefix(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
const unsigned char* last = p; // the end of the last complete sequence
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
const std::uint64_t current = state & 63u;
|
||||
if (JSON_HEDLEY_UNLIKELY(current == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
last = (current == 6) ? p : last;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Return the length of the longest prefix of [data, data+n) that consists of
|
||||
// ASCII characters and complete well-formed UTF-8 sequences; n if all of it is
|
||||
// valid UTF-8. Unlike scalar_string_bulk_run(), quotes, escapes, and control
|
||||
@@ -13350,12 +13380,12 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
continue;
|
||||
}
|
||||
|
||||
const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
|
||||
pos = static_cast<std::size_t>(last - data);
|
||||
if (pos != n && *last >= 0x80u)
|
||||
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||
if (seq == 0)
|
||||
{
|
||||
break; // ill-formed or truncated
|
||||
}
|
||||
pos += seq;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
@@ -13540,14 +13570,18 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t scalar_string_bulk_run(const unsigned char
|
||||
}
|
||||
#endif
|
||||
#if !JSON_SCAN_VECTOR_UTF8 || JSON_SCAN_SSSE3_DISPATCH
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated as a whole
|
||||
const unsigned char* const run_end = utf8_dfa_run(p, e);
|
||||
if (JSON_HEDLEY_UNLIKELY(run_end == nullptr))
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated sequence
|
||||
// by sequence without searching for the next special byte in between
|
||||
do
|
||||
{
|
||||
// ill-formed or truncated: let the byte path diagnose it
|
||||
return static_cast<std::size_t>(utf8_dfa_prefix(p, e) - data);
|
||||
const std::size_t seq = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (seq == 0)
|
||||
{
|
||||
return static_cast<std::size_t>(p - data); // ill-formed or truncated: let the byte path diagnose it
|
||||
}
|
||||
p += seq;
|
||||
}
|
||||
p = run_end;
|
||||
while (p != e && *p >= 0x80u);
|
||||
#endif
|
||||
}
|
||||
return static_cast<std::size_t>(p - data);
|
||||
@@ -13652,7 +13686,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
|
||||
// unaffected by this fast path.
|
||||
inline int hex_codepoint(const unsigned char* data) noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table =
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
{
|
||||
{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
|
||||
@@ -48961,6 +48995,8 @@ struct formatter<nlohmann::NLOHMANN_BASIC_JSON_TPL, char> // NOLINT(cert-dcl58-c
|
||||
#undef JSON_DTOA_NEON
|
||||
#undef JSON_LITTLE_ENDIAN_TARGET
|
||||
#undef JSON_BIG_ENDIAN_TARGET
|
||||
#undef JSON_SCAN_AVX2_DISPATCH
|
||||
#undef JSON_SCAN_AVX2_TARGET
|
||||
#undef JSON_SCAN_NEON
|
||||
#undef JSON_SCAN_SSE2
|
||||
#undef JSON_SCAN_SSSE3
|
||||
|
||||
@@ -2044,6 +2044,95 @@ TEST_CASE("string scanning kernels")
|
||||
CHECK(nlohmann::detail::read_eight_bytes(reinterpret_cast<const char*>(bytes.data()) + 3) == 0x0B0A090807060504u);
|
||||
}
|
||||
|
||||
TEST_CASE("vector UTF-8 check with and without AVX2")
|
||||
{
|
||||
// on x86-64 CPUs with AVX2, the UTF-8 check takes 32 bytes per step; it
|
||||
// must stop where the 16-byte check (and the byte-wise check) stops, for
|
||||
// every piece at every position of every block, and both are tested there
|
||||
const auto reference_bulk_run = [](const unsigned char* data, std::size_t n)
|
||||
{
|
||||
std::size_t i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
if (data[i] < 0x80u)
|
||||
{
|
||||
if (nlohmann::detail::is_string_special(data[i]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
const std::size_t seq = nlohmann::detail::validate_one_utf8(data + i, n - i);
|
||||
if (seq == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
i += seq;
|
||||
}
|
||||
return i;
|
||||
};
|
||||
const std::vector<std::string> backgrounds = {"x", "\xC3\xA9", "\xE6\x97\xA5", "\xF0\x9F\x98\x80"};
|
||||
const std::vector<std::string> pieces =
|
||||
{
|
||||
"", "\"", "\\", "\n", "\xC3\xA4", "\xE2\x82\xAC", "\xF0\x9F\x98\x80", "\xED\x9F\xBF",
|
||||
"\x80", "\xC0\x80", "\xC3", "\xE2\x82", "\xED\xA0\x80", "\xF4\x90\x80\x80", "\xFF", "\"\xFF", "\xFF\"",
|
||||
};
|
||||
|
||||
const auto check_all = [&]()
|
||||
{
|
||||
std::size_t checked = 0;
|
||||
for (const auto& background : backgrounds)
|
||||
{
|
||||
std::string filler;
|
||||
while (filler.size() < 100)
|
||||
{
|
||||
filler += background;
|
||||
}
|
||||
for (std::size_t length = 0; length <= 80; ++length)
|
||||
{
|
||||
for (std::size_t pos = 0; pos <= length; ++pos)
|
||||
{
|
||||
for (const auto& piece : pieces)
|
||||
{
|
||||
const std::string text = filler.substr(0, pos) + piece + filler.substr(pos, length - pos);
|
||||
const auto* data = reinterpret_cast<const unsigned char*>(text.data()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
const std::size_t run = nlohmann::detail::scalar_string_bulk_run(data, text.size());
|
||||
const bool valid = nlohmann::detail::valid_utf8_string(data, text.size());
|
||||
if (run != reference_bulk_run(data, text.size()) || valid != (nlohmann::detail::valid_utf8_prefix(data, text.size()) == text.size()))
|
||||
{
|
||||
CAPTURE(text)
|
||||
CHECK(run == reference_bulk_run(data, text.size()));
|
||||
CHECK(valid == (nlohmann::detail::valid_utf8_prefix(data, text.size()) == text.size()));
|
||||
}
|
||||
++checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// (one CHECK per string would make the test ten times slower)
|
||||
CHECK(checked == backgrounds.size() * pieces.size() * 81 * 82 / 2);
|
||||
};
|
||||
|
||||
// the answer of the CPU check is kept (always "no" where the library has
|
||||
// no AVX2 code)
|
||||
nlohmann::detail::cpu_avx2_state().store(0);
|
||||
CHECK(nlohmann::detail::cpu_has_avx2() == nlohmann::detail::cpu_avx2());
|
||||
CHECK(nlohmann::detail::cpu_avx2_state().load() == (nlohmann::detail::cpu_avx2() ? 2 : 1));
|
||||
|
||||
SECTION("16 bytes per step")
|
||||
{
|
||||
nlohmann::detail::cpu_avx2_state().store(1);
|
||||
check_all();
|
||||
nlohmann::detail::cpu_avx2_state().store(0);
|
||||
}
|
||||
|
||||
SECTION("the widest vectors of the CPU")
|
||||
{
|
||||
check_all();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("find_ascii_copyable_run agrees with is_ascii_copyable for every byte")
|
||||
{
|
||||
// the SWAR mask in find_ascii_copyable_run() encodes the predicate a
|
||||
@@ -2138,14 +2227,6 @@ TEST_CASE("UTF-8 validators accept the same sequences")
|
||||
CAPTURE(t)
|
||||
CHECK((dfa == expected && bulk == (expected ? length : 0) && bulk_short == 0));
|
||||
|
||||
// the DFA of the bulk paths: the whole sequence or nothing
|
||||
if (length >= 2)
|
||||
{
|
||||
const unsigned char* const run_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length);
|
||||
const unsigned char* const short_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length - 1);
|
||||
CHECK((run_end == bytes.data() + (expected ? length : 0) && short_end == bytes.data()));
|
||||
}
|
||||
|
||||
if (t < 2 && length >= 2)
|
||||
{
|
||||
// the lexer's byte path
|
||||
@@ -2173,70 +2254,6 @@ TEST_CASE("UTF-8 validators accept the same sequences")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("UTF-8 DFA")
|
||||
{
|
||||
// utf8_dfa_prefix() and utf8_dfa_run() against a loop over
|
||||
// validate_one_utf8(): every byte
|
||||
// after a prefix that leads to each state of the DFA checks every entry of
|
||||
// its table, and the bytes after it whether it stops at the right place
|
||||
const auto reference = [](const unsigned char* p, const unsigned char* e)
|
||||
{
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
const std::size_t length = nlohmann::detail::validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (length == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
p += length;
|
||||
}
|
||||
return p;
|
||||
};
|
||||
|
||||
// between sequences, after a valid sequence, 1/2/3 continuation bytes
|
||||
// missing, and after the lead bytes that restrict the second byte
|
||||
const std::vector<std::vector<unsigned char>> prefixes =
|
||||
{
|
||||
{}, {0xC3, 0xA4}, {0xC3}, {0xE1}, {0xF1}, {0xE1, 0x80}, {0xF1, 0x80}, {0xF1, 0x80, 0x80},
|
||||
{0xE0}, {0xED}, {0xF0}, {0xF4}
|
||||
};
|
||||
const std::vector<std::vector<unsigned char>> suffixes =
|
||||
{
|
||||
{}, {0x80}, {0xBF}, {0x41}, {0xC3, 0xA4}, {0xE4, 0xB8, 0xAD}, {0xF0, 0x9F, 0x98, 0x80}, {0xFF}
|
||||
};
|
||||
for (const auto& prefix : prefixes)
|
||||
{
|
||||
for (unsigned byte = 0; byte <= 0xFF; ++byte)
|
||||
{
|
||||
for (const auto& suffix : suffixes)
|
||||
{
|
||||
std::vector<unsigned char> input(prefix);
|
||||
input.push_back(static_cast<unsigned char>(byte));
|
||||
input.insert(input.end(), suffix.begin(), suffix.end());
|
||||
const unsigned char* const begin = input.data();
|
||||
const unsigned char* const end = begin + input.size();
|
||||
CAPTURE(prefix.size())
|
||||
CAPTURE(byte)
|
||||
CAPTURE(suffix.size())
|
||||
const unsigned char* const expected = reference(begin, end);
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(begin, end) - begin == expected - begin);
|
||||
|
||||
// utf8_dfa_run() only tells whether the whole run is valid
|
||||
const bool whole_run = expected == end || *expected < 0x80u;
|
||||
CHECK(nlohmann::detail::utf8_dfa_run(begin, end) == (whole_run ? expected : nullptr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// an ill-formed byte stops the scan at once, whatever follows
|
||||
std::vector<unsigned char> invalid(1000, 0xFF);
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data());
|
||||
CHECK(nlohmann::detail::utf8_dfa_run(invalid.data(), invalid.data() + invalid.size()) == nullptr);
|
||||
invalid[0] = 0xC3;
|
||||
invalid[1] = 0xA4;
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data() + 2);
|
||||
}
|
||||
|
||||
TEST_CASE("float_bigint")
|
||||
{
|
||||
// the big integer of the exact comparison of a float token with the
|
||||
|
||||
Reference in new issue
Block a user