In Haskell, the base
libraries and Hackage packages provide several means of converting binary IEEE-754 floating point data to and from the l
I'm the author of data-binary-ieee754
. It has at some point used each of the three options.
encodeFloat
and decodeFloat
work well enough for most cases, but the accessory code required to use them adds tremendous overhead. They do not react well to NaN
or Infinity
, so some GHC-specific assumptions are required for any casts based upon them.
unsafeCoerce
was an attempted replacement, to get better performance. It was really fast, but reports of other libraries having significant problems made me eventually decide to avoid it.
The FFI code has so far been the most reliable, and has decent performance. The overhead of allocation isn't as bad as it seems, likely due to the GHC memory model. And it actually doesn't depend on the internal format of floats, merely on the behavior of the Storable
instance. The compiler can use whatever representation it wants as long as Storable
is IEEE-754. GHC uses IEEE-754 internally anyway, and I don't worry much about non-GHC compilers any more, so it's a moot point.
Until the GHC developers see fit to bestow upon us unlifted fixed-width words, with associated conversion functions, FFI seems the best option.