ryu
ryu copied to clipboard
Add f2fixed() and f2exp()?
https://github.com/ulfjack/ryu/blob/a73883c279c8e13262a6315471c3caeed113c3f2/ryu/d2fixed.c#L395 and https://github.com/ulfjack/ryu/blob/a73883c279c8e13262a6315471c3caeed113c3f2/ryu/d2fixed.c#L597 take 64-bit double but work with 32-bit float (because float can be losslessly widened to double; note that this is untrue for string-to-floating conversions, where rounding a string to a double and rounding again to a float is a subtle mistake).
Would dedicated 32-bit implementations be even faster, possibly by using narrower multiplications? Like how https://github.com/ulfjack/ryu/blob/a73883c279c8e13262a6315471c3caeed113c3f2/ryu/f2s.c#L154 is faster than https://github.com/ulfjack/ryu/blob/a73883c279c8e13262a6315471c3caeed113c3f2/ryu/d2s.c#L238
Data point: charconv's shortest fixed notation overload benefits from calling d2fixed_buffered_n() for large exactly-representable integer doubles, but not for floats. On x86, the long division technique on uint32_t[4] is significantly faster (71.4 ns versus 101.1 ns, end-to-end cost averaged over all inputs). On x64, long division is very slightly faster (51.4 ns versus 52.1 ns). As a result I'll continue using long division for float, unless/until f2fixed() is implemented.
Please refrain from shouting here. Also, if you have proposals for how to improve C++, this isn't really the right place to discuss them.