Coercion from a Word32/64 to a Float/Double

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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi,
Yes I did but since I saw that 'solution', I wanted to try it.
There is something that bothers me with your library: its licence.
Reading a float shouldn't force me to go GPL (I makes an utility to
read Blender files and like to make it BSD3).
Thanks for pointing it though.
Cheers,
Thu
2009/9/14 John Millikin
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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Sep 14, 2009 at 12:24 PM, minh thu
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'
I think that a more reliable (though not very pretty) way to do the casts is via pointer manipulation. Code like the following has worked for me in the past: getDouble :: Get Double getDouble = fmap word2Double getWord64le where word2Double w = unsafePerformIO $ with w $ \p -> do d :: CDouble <- peek (castPr p) return (realToFrac d :: Double) Best, -Judah

2009/9/14 Judah Jacobson
On Mon, Sep 14, 2009 at 12:24 PM, minh thu
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'
I think that a more reliable (though not very pretty) way to do the casts is via pointer manipulation. Code like the following has worked for me in the past:
getDouble :: Get Double getDouble = fmap word2Double getWord64le where word2Double w = unsafePerformIO $ with w $ \p -> do d :: CDouble <- peek (castPr p) return (realToFrac d :: Double)
Best, -Judah
Thank you, it compiles fine. Thu
participants (3)
-
John Millikin
-
Judah Jacobson
-
minh thu