Check UTF-8 in strings 32 bytes at a time on CPUs with AVX2

The vector UTF-8 check of string runs in the lexer (vector_utf8_run) gets a
32-byte variant, compiled with a target attribute like the SSSE3 code and
chosen at run time where the CPU has AVX2 and the OS saves the YMM registers.
The CPU check sits inside the existing out-of-line 16-byte function, so it
adds no code to the lexer's string loop, and every translation unit has the
same definitions, whatever its flags. MSVC and GCC for Windows (GCC bug 54412)
keep the 16-byte code.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-25 21:41:41 +01:00
1 parent 322063822f
commit d8abf6a861
8 files changed
+462 -7

No files matched your search

+1 -1
View File
@@ -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)
+3 -1
View File
@@ -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.
+1 -1
View File
@@ -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).
+2 -2
View File
@@ -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