Dear libstdc++ developers,

I developed a novel algorithm called Teju Jagua which finds the shortest
representation of a floating-point number. It can be used by std::to_chars
which currently uses Ryu.

IMHO, Teju Jagua is much simpler than Ryu and, according to my measurements,
for doubles, Teju Jagua is twice faster than Ryu. Similarly, it's simpler
and ~27% faster than Dragonbox which, to the best of my knowledge, was the
fastest algorithm for this task until now.

Teju Jagua can bring another benefit. All the aforementioned algorithms use
look-up tables that often raise memory footprint concerns. At least in their
reference implementations, one or more tables is provided for each
floating-point type. I don't know about the others but for Teju Jagua the
table for a type can be also used for any smaller type. Hence, it just
needs to provide the table for the largest type (e.g., float128_t) of the
platform.

I already started an implementation in libstdc++ and will send a subsequent
RFC to gather feedback and to give you a better understanding of this work.
The first RFC will replace Ryu only for floats. After this is reviewed and
if there's still interest from others, I will send subsequent patches to
replace Ryu for each one of the other floating-point types. In the end I'll
send a patch to remove Ryu. (Note: This concerns only regular Ryu, which
finds the shortest representation, Ryu-printf that produces the result for a
given precision is out of the scope of this work and will remain.)

A reference C implementation (it's a WIP which lacks documentation) is found
in [1].

I presented this algorithm in C++ Now 2024, C++ on Sea 2024 and I'll show
it again at CppCon 2024. If any libstdc++ developer plans to attend, I'd be
more than happy to discuss this work in person at the conference. The video
recording of my C++ Now 2024 talk is on YouTube [2].

[1] https://github.com/cassioneri/teju_jagua:
[2] https://www.youtube.com/watch?v=w0WrRdW7eqg

Reply via email to