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

minh thu noteed at gmail.com
Mon Sep 14 15:24:53 EDT 2009


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


More information about the Haskell-Cafe mailing list