[Haskell-cafe] Data.Binary and little endian encoding

Sven Panne Sven.Panne at aedion.de
Sun May 17 07:04:43 EDT 2009

Am Freitag, 15. Mai 2009 06:37:22 schrieb Don Stewart:
> timd:
> > On a related matter, I am using Data.Binary to serialise data from
> > haskell for use from other languages. [...]
> [...]
> Yep, it's possible, just not portably so. Google for Data.Binary IEEE
> discussions.

I think this topic pops up over and over again, and the proposed "solutions" 
are no solutions at all, neither from a performance point of view, nor from an 
ease of use point of view. Proposing insane bit fiddling by hand when all one 
technically needs is often a "peek" or "poke" amounts to simply ignoring an 
API problem. ;-)

I think most problems can be fixed in a rather pragmatic way by adding a few 
functions to the binary package:

Add to Data.Binary.Builder:

   putFloatIEEEbe :: Float -> Builder
   putDoubleIEEEbe :: Double -> Builder
   putFloatIEEEle :: Float -> Builder
   putDoubleIEEEle :: Double -> Builder
   putFloatIEEEhost :: Float -> Builder
   putDoubleIEEEhost :: Double -> Builder

Add to Data.Binary.Get:

   getFloatIEEEbe :: Get Float
   getDoubleIEEEbe :: Get Double
   getFloatIEEEle :: Get Float
   getDoubleIEEEle :: Get Double
   getFloatIEEEhost :: Get Float
   getDoubleIEEEhost :: Get Double

Add to Data.Binary.Put:

   putFloatIEEEbe ::  Float -> Put
   putDoubleIEEEbe ::  Double -> Put
   putFloatIEEEle ::  Float -> Put
   putDoubleIEEEle ::  Double -> Put
   putFloatIEEEhost ::  Float -> Put
   putDoubleIEEEhost ::  Double -> Put

The *host functions are basically peek/poke for most platforms. The *le/*be 
functions can use peek/poke if the endianess matches (compile time decision) 
*and* the alignment is OK for the given platform (runtime decision). Non-IEEE 
platforms always have to do the bit fiddling internally, but all this is 
hidden behind the above API.

IIRC I have proposed something similar 1-2 years ago, but I can't remember any 
reason why this hasn't been implemented. Any comments on the above functions?

One final remarks: I think the low level functions of the binary package 
should really keep the notions of "endianess" and "alignment constraints" 
separate, something which isn't done currently: The *host functions have 
alignment restrictions, the *be/*le functions don't. There is no good reason 
for this non-orthogonality.


More information about the Haskell-Cafe mailing list