Skip to content

Parse hexadecimal floating-point numbers with chars_format::hex - #441

Open
lemire wants to merge 3 commits into
mainfrom
hex-float-parsing
Open

lemire wants to merge 3 commits into
mainfrom
hex-float-parsing

Conversation

@lemire

@lemire lemire commented Nov 27, 2026

Copy link
Copy Markdown
Member

Supersedes #125 (thanks @Andersama for starting this, and @deadalnix for the D reference).

With fast_float::chars_format::hex, from_chars now parses hexadecimal floating-point numbers, as std::from_chars does with std::chars_format::hex:

std::string input = "1.921fb54442d18p+1"; // printf("%a", pi) without the "0x"
double result;
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result, fast_float::chars_format::hex);

Behavior

  • The std::strtod form for hex floats, with the 0x/0X prefix assumed (so "0x1p0" parses as 0 and stops at the x). Hex digits with an optional point, then an optional binary exponent (p/P and an optionally signed decimal integer).
  • Like the standard: if scientific is set but not fixed, the exponent is required; if fixed is set but not scientific, the exponent is not allowed. With hex alone, or hex | general, the exponent is optional.
  • Rounding is correct (to nearest, ties to even) and uses only integer arithmetic, so the rounding mode doesn't matter. Subnormals are handled. Overflow and underflow give ±inf and ±0 with result_out_of_range, as for decimal inputs.
  • allow_leading_plus, skip_white_space, no_infnan and a custom decimal point all apply, and inf and nan are accepted.
  • Supports float, double, std::float16_t and std::bfloat16_t, and char, wchar_t, char16_t and char32_t inputs. 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 %a strings (without 0x) and parses them with fast_float, std::from_chars(..., std::chars_format::hex) and strtod/strtof (with 0x).

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

data (bytes/number) type fast_float std::from_chars strtod/strtof vs from_chars
canada (18.1) double 19.1 (337) 30.4 (568) 135.4 (851) 1.6×
canada float 18.7 (336) 48.8 (600) 113.8 (707) 2.6×
mesh (11.4) double 13.3 (271) 21.6 (407) 60.1 (615) 1.6×
mesh float 13.1 (270) 28.6 (418) 54.0 (558) 2.2×
random (17.9) double 15.2 (337) 28.5 (575) 156.9 (853) 1.9×
random float 15.2 (336) 46.6 (609) 129.3 (701) 3.1×

clang 23.1.5 (same libstdc++)

data type fast_float std::from_chars strtod/strtof vs from_chars
canada double 18.4 (342) 31.9 (564) 135.2 (852) 1.7×
canada float 18.3 (340) 48.5 (596) 113.7 (708) 2.7×
mesh double 13.4 (288) 21.1 (403) 60.0 (616) 1.6×
mesh float 13.3 (286) 28.1 (414) 54.2 (559) 2.1×
random double 15.7 (346) 28.8 (571) 156.5 (854) 1.8×
random float 15.7 (344) 46.2 (605) 129.1 (702) 2.9×

fast_float is 1.6–1.9× faster than libstdc++'s std::from_chars for double, 2.1–3.1× faster for float, and 4–10× faster than glibc's strtod/strtof.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant