Skip to content

xjb714/xjb

Repository files navigation

xjb

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.

algorithm correctness proof:

Execute the following command to verify the correctness of the algorithm.

cd bench
make check

todo:

(1) big-endian support (2) f16, f128 and f256 support

src files:

(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

examples:

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"

Algorithm characteristics:

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

benchmark:

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):

ramdom float on Apple M1
compiler: apple clang 17.0.0
ramdom double on Apple M1
compiler: apple clang 17.0.0
ramdom float on AMD R7-7840H
compiler: icpx 2025.0.4
ramdom double on AMD R7-7840H
compiler: icpx 2025.0.4

please refer to bench directory for more details.

related algorithm

algorithm Description
schubfach Raffaello Giulietti's algorithm.Origin source code from https://github.com/c4f7fcce9cb06515/Schubfach
schubfach_xjb It is improved by schubfach and has the same output result.
ryu Ulf Adams's Ryū algorithm.
dragonbox Junekey Jeon's CPP implementation.
fmt Victor Zverovich.https://github.com/fmtlib/fmt. version:12.1.0
yy_double yy's algorithm , origin link : https://github.com/ibireme/c_numconv_benchmark/blob/master/vendor/yy_double/yy_double.c
yy_json yy's algorithm. https://github.com/ibireme/yyjson
teju youtube link : https://www.youtube.com/watch?v=fPZ1ZdA7Iwc . An academic paper will be written to provide proof of correctness.https://github.com/cassioneri/teju_jagua
xjb XiangJunBo's algorithm. https://github.com/xjb714/xjb
schubfach_vitaut Vitaut's algorithm. https://github.com/vitaut/schubfach
zmij Vitaut's algorithm. https://github.com/vitaut/zmij
jnum Jing Leng's algorithm. https://github.com/lengjingzju/json/blob/master/jnum.c
uscale Russ Cox's algorithm. src: https://github.com/rsc/fpfmt commit 6255750 (19 Jan 2026). introduction: https://research.swtch.com/fp

acknowledgement:

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.

who is using this algorithm

  • 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.

About

fast float to string algorithm.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors