~jvernay/JvParseFloat

C99 small and fast implementation of string parsing into floats.
Added fast_float to benchmark
Fixed pointer constness in strtof.
Added int128 impl

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~jvernay/JvParseFloat
read/write
git@git.sr.ht:~jvernay/JvParseFloat

You can also use your local clone with git send-email.

#JvParseFloat

JvParseFloat is a small and quick C99 implementation of parsing a decimal string into a floating-point number. Its implementation has been explained in a series of blog posts.

#API

/// Parses at most `strSize` bytes from `pStr` into `pDest`.
/// Returns the number of bytes parsed.
ptrdiff_t jvParseFloat32(char const* pStr, ptrdiff_t strSize, float* pDest);

/// Parses at most `strSize` bytes from `pStr` into `pDest`.
/// Returns the number of bytes parsed.
ptrdiff_t jvParseFloat64(char const* pStr, ptrdiff_t strSize, double* pDest);

#Results

The test data collected by Nigel Tao has been of tremendous help: it is a suite of plain-text files agglomerating the test suites of multiple open-source projects implementing float parsers: google-wuffs, rapidjson, freetype, etc. My test program source code is readable online.

total_nb_f32_correct   = 5295186
total_nb_f32_1ulp_away = 4821
total_nb_f32_bad       = 0
total_nb_f64_correct   = 3340643
total_nb_f64_1ulp_away = 1959364
total_nb_f64_bad       = 0

Each test case counts towards one of three categories:

  • correct if the result is bitwise identical to the expected result.
  • 1ulp_away if the result only differs due to the least significant bit (ULP = Unit in the Last Place).
  • bad if the result differs more than the least significant bit.

In terms of performance, comparison was done using the same test program, but replacing the implementation by a dummy function which does nothing, and an adapter function using MSVC's strtof/d implementation. The dummy function shows the cost of the pure IO code. It was tested on a Windows 11 laptop, which has CPU AMD Ryzen 5 7520U. The numbers in the table are the best timing found on 5 consecutive runs. In each run, 10 600 014 floats are parsed (half into 32-bit, half into 64-bit).

Dummy (Debug) jvParseFloat (Debug) strtof/d (Debug) Dummy (Release) jvParseFloat (Release) strtof/d (Release)
Total run time 7.11 s 13.76 s 20.75 s 1.98 s 2.47 s 5.11 s
Run time relative to dummy 0 s 6.65 s 13.64 s 0 s 0.49 s 3.13 s
Average time per float parsed 0 us/call 0.63 us/call 1.29 us/call 0 us/call 0.05 us/call 0.29 us/call

#License

JvParseFloat is authored by Julien Vernay, in 2024. ( dev AT jvernay DOT fr ) The software is released to the public domain (see UNLICENSE.txt).