logoalt Hacker News

Bitwise conversion of doubles using only FP multiplication and addition (2020)

30 pointsby vitautyesterday at 2:55 PM3 commentsview on HN

Comments

sjrdtoday at 5:52 AM

Interesting. When compiling Scala.js to ECMAScript 5, we still have an implementation of bitwise floating point conversions based on double operations and integer shifts. [1] We also use a lookup table for powers of 2, and don't use anything but primitives (no log or pow, notably). We do have a few divisions, but I see now how I could turn them into multiplications. Dealing with subnormals is tricky because the inverse of the subnormal powers of 2 are not representable.

We have one loop: a binary search in the table of powers of 2 for the double-to-bits conversion. It has a fixed number of iterations. I had tried to unroll it, but that did not perform any better.

I'll have to dig more to understand how they got rid of the comparisons, though.

I wonder whether their implementation would be faster than ours. At the time I wrote our conversions, they beat every previous implementation I was aware of hands down.

[1] https://github.com/scala-js/scala-js/blob/v1.20.2/linker-pri...

augusteotoday at 2:36 AM

I love these "what if you only had X" puzzles. The constraint here (no bit access, only FP multiply and add) sounds impossible until you realize rounding behavior carries information.

The edge cases around negative zero and infinities make sense. Those values break the mathematical properties you'd need to distinguish them.

lifthrasiirtoday at 1:06 AM

Recommended readings:

Jim McCann, Tom Murphy VII, The fluint8 Software Integer Library. https://tom7.org/papers/fluint.pdf

Tom Murphy VII, GradIEEEnt half decent. https://tom7.org/grad/murphy2023grad.pdf