ulfjack / ryu

Converts floating point numbers to decimal strings
Apache License 2.0
1.19k stars 99 forks source link

D Ryu implementation reference #194

Closed 9il closed 3 years ago

9il commented 3 years ago

Hi Ulf,

Thank you for the great algorithm!

Mir/Dlang ryu port

We have only generic implementation for now.

Additional optimizations were applied:

  1. Mir fixed-length integers UInt!128 and UInt!256 are used instead of C's uint128 and manual ulong[4] (for uint256). They were implemented with LLVM intrinsics and generates ideal (or almost ideal) code for multiplications.
  2. /= and %= 10 operations are fused where possible and implemented using multiplication. C's uint128 doesn't support this kind of optimization for now and performs the actual call of divide function.
  3. UInt!64 is used for UInt!128 fordoubleandfloat`, all this reduces instructions size a lot.
  4. 188 + minor additional optimization

  5. A modification similar to #156
  6. decimal length isn't used at all. Instead, we print the number to the buffer and return its position in the buffer. In a practical case, this would work faster. Mir/D's printing routines get this buffer size and copy it to the actual string appender (or Mir's stack-allocated SmallString).