ANNOUNCE: protocol-buffers-0.2.9 for Haskell is ready

Chris Kuklewicz haskell at list.mightyreason.com
Mon Sep 22 06:48:16 EDT 2008


I am cross-posting this message to several lists.

I had learned the trick before the documentation was updated.  It seems I have 
used a very unreliable trick.  And the "use castToSTUArray" suggested 
alternative is a really poor one since I am not using arrays at all.

Who can suggest a way to cast from Float to Word32 and Double to Word64 using 
ghc?  The actual task is that I need to write out the Float as a little endian 
sequence of four bytes and also be able to read it back in.  The writing and 
reading are done in Put and Get monads to ByteString (from the "binary" package).

The alloca/poke/peek work around I have looks like
castWord32ToFloat :: Word32 -> Float
castWord32ToFloat x = unsafePerformIO $
   alloca $ \p -> poke p x >> peek (castPtr p)

castFloatToWord32 :: Float -> Word32
castFloatToWord32 x = unsafePerformIO $
   alloca $ \p -> poke p x >> peek (castPtr p)

The unsafeCoerce trick that is no longer working looks like:
castWord64ToDouble :: Word64 -> Double
castWord64ToDouble (W64# w) = D# (unsafeCoerce# w)

castDoubleToWord64 :: Double -> Word64
castDoubleToWord64 (D# d) = W64# (unsafeCoerce# d)

Any ideas? Or is the alloca trick the only way to do this?

   Chris


Ian Lynagh wrote:
> Hi Chris,
> 
> On Sun, Sep 21, 2008 at 05:37:33PM +0100, Chris Kuklewicz wrote:
>> Also, I tried two tricks:
>> (D# x) <-> (W64# x) which works fine
>> (F# x) <-> (W32# x) which produced garbage, so I had to replace it with 
>> alloca/poke/peek.
> 
> This isn't supported, and I suspect is the cause of the -fasm problems.
> 
> Please see
>     http://hackage.haskell.org/trac/ghc/ticket/2209
> for more details and suggested alternative.
> 
> 
> Thanks
> Ian
> 



More information about the Libraries mailing list