[Haskell-cafe] Coercion from a Word32/64 to a Float/Double

John Millikin jmillikin at gmail.com
Mon Sep 14 16:15:37 EDT 2009


If you don't mind a small performance penalty, have you considered
using my library data-binary-ieee754? It contains functions for
parsing floats and doubles within the Get monad.

On Mon, Sep 14, 2009 at 12:24, minh thu <noteed at gmail.com> wrote:
> Hi,
>
> I'd like to know if this should work:
>
> -- GHC coercion
> getFloat :: BHeader -> Get Float
> getFloat h =
>  case endianness h of
>    LittleEndian -> fmap (coerce . fromIntegral) getWord32le
>    BigEndian -> fmap (coerce . fromIntegral) getWord32be
>  where coerce (I32# x) = F# (unsafeCoerce# x)
>
> -- GHC coercion
> getDouble :: BHeader -> Get Double
> getDouble h =
>  case endianness h of
>    LittleEndian -> fmap (coerce . fromIntegral) getWord64le
>    BigEndian -> fmap (coerce . fromIntegral) getWord64be
>  where coerce (I64# x) = D# (unsafeCoerce# x)
>
> Loading this into ghci compiles fine (but I haven't try to parse
> data), but compiling with ghc (6.10.1 or 6.10.4) reports:
> /tmp/ghc1967_0/ghc1967_0.s: Assembler messages:
>
> /tmp/ghc1967_0/ghc1967_0.s:287:0:
>     Error: bad register name `%fake0'
>
> /tmp/ghc1967_0/ghc1967_0.s:349:0:
>     Error: bad register name `%fake0'
>
> Thanks for any help,
> Thu
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list