xjb : a fast float to string algorithm.
float/double to string single file implementation : src/ftoa.cpp
for json lib, satisfy RFC8259 or https://tc39.es/ecma262/#sec-numeric-types-number-tostring : src/ftoa_json.cpp
This code is still being updated continuously, so it may not be the final version. Later, I will write documentation to explain in detail the function of each line of code and the proof process.
Execute the following command to verify the correctness of the algorithm.
cd bench
make check
(1) big-endian support (2) f16, f128 and f256 support
(1)float/double to decimal algorithm
xjb32 : for float (IEEE754-binary32) ; bench/xjb/xjb32_i.cpp;
xjb64 : for double(IEEE754-binary64) ; bench/xjb/xjb64_i.cpp;
(2)float/double to string algorithm
full lookup table : bench/xjb/ftoa.cpp
compress lookup table : bench/xjb/xjb_comp.cpp
lookup table size:
| full table:ftoa.cpp | compress table:xjb_comp.cpp | |
| float | 1352 byte | 64 byte |
| double | 15336 byte | 592 byte |
Here are a few examples for double to string algorithm:
| double | print result |
| 123.45 | "123.45" |
| 1000 | "1000.0" |
| 123 | "123.0" |
| 123000 | "123000.0" |
| 1.2e23 | "1.2e23" |
| 1e100 | "1e100" |
| 0.0123 | "0.0123" |
| 0.001 | "0.001" |
| 0.000123 | "0.000123" |
| 1.2e-08 | "1.2e-08" |
| 0 | "0.0" |
| NaN | "nan" |
| Inf | "inf" |
This algorithm is based on the schubfach algorithm.
This algorithm is inspired by algorithms such as schubfach, yy, dragonbox, and grisu.
(1) Support double and float
(2) Algorithm process optimization
(3) Low branch prediction failure rate
(4) Acceleration using SIMD instruction set
(5) Low instruction dependency, high IPC
(6) Fewer instructions
The benchmark test is in the bench directory. just run make to build the benchmark program.
you can run bench/main.cpp to test the performance of the algorithm.
The latest benchmark results on Apple M1 and AMD R7-7840H CPU(date : 2026.2.18):
please refer to bench directory for more details.
Thanks to the following authors:
-
Yaoyuan Guo (@ibireme) - Author of the yyjson and yy_double algorithm , provide benchmark data and test code. The code inspiration for this project comes from the yy algorithm.
-
Dougall Johnson (@dougallj) - Authored the NEON implementation, which is used in the xjb algorithm.
-
Daniel Lemire (@lemire) - Authored the AVX512IFMA implementation(Convert 8/16 digit integer to decimal strings), which is used in the xjb algorithm. code
-
Raffaello Giulietti (@rgiulietti) - Author of the Schubfach algorithm, whose work forms a foundational basis for xjb.
- ssrJSON - A SIMD boosted high-performance and correct Python JSON parsing library, faster than the fastest.
- jsoniter-scala - Scala macros for compile-time generation of safe and ultra-fast JSON codecs + circe booster.