Repository navigation
Conversation
Supersedes #125. With fast_float::chars_format::hex, from_chars parses hexadecimal floating-point numbers as std::from_chars does with std::chars_format::hex: the std::strtod form without the assumed 0x prefix, with an optional binary exponent (required with scientific but not fixed, not allowed with fixed but not scientific). The result is correctly rounded, ties to even, using only integer arithmetic, so it does not depend on the rounding mode. Subnormals, overflow and underflow follow the conventions of the decimal parser. The conversion is a cold, out-of-line function reached from the same branch as the JavaScript parser, so the decimal path is unchanged. For char inputs, eight hexadecimal digits are validated and converted at once (SWAR); other code units go one digit at a time through the existing lookup table. It works in constant expressions (C++20). Adds tests/hex_float_test.cpp (hand-written cases, round trips of random float/double through printf %a, random strings against strtod/strtof, every byte value at every position of a digit run) and benchmarks/bench_hex.cpp (fast_float vs std::from_chars vs strtod).
Some C libraries misround hexadecimal subnormals: glibc 2.35 and 2.39 (Ubuntu 22.04 and 24.04) round 682c27c.7e61b22p-1052 down, and the Windows UCRT returns 2^-1020 for values that round to 2^-1022. The random tests now compare against an exact reference that expands the digits into bits and rounds them. strtod still gives the number of characters consumed in check_all_bytes. Also apply clang-format.
lemire
force-pushed
the
hex-float-parsing
branch
from
November 28, 2026 15:23
3e4aa93 to
5aacb82
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #125 (thanks @Andersama for starting this, and @deadalnix for the D reference).
With
fast_float::chars_format::hex,from_charsnow parses hexadecimal floating-point numbers, asstd::from_charsdoes withstd::chars_format::hex:Behavior
std::strtodform for hex floats, with the0x/0Xprefix assumed (so"0x1p0"parses as0and stops at thex). Hex digits with an optional point, then an optional binary exponent (p/Pand an optionally signed decimal integer).scientificis set but notfixed, the exponent is required; iffixedis set but notscientific, the exponent is not allowed. Withhexalone, orhex | general, the exponent is optional.±infand±0withresult_out_of_range, as for decimal inputs.allow_leading_plus,skip_white_space,no_infnanand a custom decimal point all apply, andinfandnanare accepted.float,double,std::float16_tandstd::bfloat16_t, andchar,wchar_t,char16_tandchar32_tinputs. It also works in constant expressions (C++20).Benchmarks
New
benchmarks/bench_hex.cpp. It turns canada.txt, mesh.txt and 100k random doubles into%astrings (without0x) and parses them with fast_float,std::from_chars(..., std::chars_format::hex)andstrtod/strtof(with0x).Linux, Intel Xeon Gold 6548N, pinned to one core, median of 5 runs, libstdc++ from GCC 16.2. Values are ns per number (instructions per number in parentheses):
GCC 16.2
clang 23.1.5 (same libstdc++)
fast_float is 1.6–1.9× faster than libstdc++'s
std::from_charsfordouble, 2.1–3.1× faster forfloat, and 4–10× faster than glibc'sstrtod/strtof.