[php-src] PR #25476: ext/standard: Port the simdutf base64 decoders
| From: | lemire | Date: | Mon, 16 Nov 2026 05:04:11 +0000 |
| Subject: | [php-src] PR #25476: ext/standard: Port the simdutf base64 decoders | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40668@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25476
Author: lemire
Follow-up to the discussion in #24774.
The SSSE3, AVX2, AVX-512 VBMI and NEON base64 decoders now share one loop, following
[simdutf](https://github.com/simdutf/simdutf). Each 64-byte block is translated to 6-bit values
together with a mask of the bytes outside the alphabet. When a block contains whitespace or other
ignorable bytes, its alphabet characters are compressed into a buffer of pending values instead of
falling back to the scalar loop. As a result:
- line-wrapped input (MIME, PEM) stays on the vector path; today it drops to the scalar loop at the
first line break;
- short input (keys, tokens) no longer goes through the scalar loop;
- strict mode is handled in the same loop: whitespace is skipped and padding is checked in the last
block. Only
= or an invalid byte before the last block goes to the scalar loop.
Output is unchanged, including every strict-mode rejection.
Other changes:
- The NEON encoder uses a 64-entry table lookup (vqtbl4q), and the AVX-512 VBMI encoder
handles the tail with masked loads and stores.
- The AVX-512 VBMI tier now also requires VBMI2 (vpcompressb). The plain AVX-512
decoder is removed; those CPUs use the AVX2 decoder.
## Performance
These are decode cycles per call measured with perf stat, master → this PR. The
numbers are from Ice Lake (Xeon 6338); Emerald Rapids gives similar results.
| | 24 B | 44 B |
|---|---|---|
| SSSE3 | 71 → 65 | 92 → 84 |
| AVX2 | 135 → 58 | 226 → 78 |
| AVX-512 VBMI | 162 → 69 | 275 → 92 |
On an Apple M4, decoding MIME input goes from 0.67 to 9.7 GB/s. Every kernel is at least as fast as
master in every case measured: 24 B, 44 B, 1 KB, 64 KB clean and 64 KB MIME. The AVX2, AVX-512 and
NEON decoders are within a few percent of simdutf.
## Testing
- A new test, ext/standard/tests/url/base64_decode_blocks.phpt, compares the result
with a character-at-a-time reference decoder in both modes. It covers lengths 0 to 150, line breaks,
padding, an inserted byte at every offset around block boundaries, and long inputs.
- A differential fuzzer compared every kernel with the scalar decoder under ASan and UBSan: x86-64
(SSSE3, AVX2, AVX-512 VBMI), arm64 (NEON, under qemu), 32-bit x86 and scalar. It ran about 150M
generated cases plus 136M libFuzzer executions with no mismatch.
## References
- Wojciech Muła, Daniel Lemire, Faster Base64 Encoding and Decoding Using AVX2 Instructions, ACM
Transactions on the Web 12 (3), 2018. https://doi.org/10.1145/3132709
- Wojciech Muła, Daniel Lemire, Base64 encoding and decoding at almost the speed of a memory copy,
Software: Practice and Experience 50 (2), 2020. https://doi.org/10.1002/spe.2777
- Daniel Lemire, Fast Forgiving Base64 Decoding with SIMD Instructions, ACM Transactions on
Architecture and Code Optimization, 2026. https://doi.org/10.1145/3859286