unsafeCoerce# between integral and floating point types

Simon Marlow marlowsd at gmail.com
Fri Jul 9 08:32:51 EDT 2010

On 08/07/2010 17:56, Daniel Fischer wrote:
> On Thursday 08 July 2010 18:15:44, Ian Lynagh wrote:
>> On Thu, Jul 08, 2010 at 04:49:00PM +0200, Daniel Fischer wrote:
>>> unsafeCoerce# :: Word64# ->  Double# ?
>>> By the docs, that isn't supposed to work. Is it not supposed to work
>>> only because it's not value-preserving (unsafeCoerce# 1## /=## 1.0##)
>>> or are there more pitfalls?
>> It can fail to compile, even; see
>> http://hackage.haskell.org/trac/ghc/ticket/2209
> Yeah, tried that myself, with optimisations:
> [2 of 2] Compiling Main             ( testUCastD.hs, testUCastD.o )
> ghc: panic! (the 'impossible' happened)
>    (GHC version 6.12.3 for i386-unknown-linux):
>          getRegister(x86) I64[R1 + 3]
> Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug
> But without optimisations, it compiles and seems to work (I won't rely on
> that, was just curious).
> Should I report it or is the panic okay since unsafeCoerce# isn't supposed
> to work for those types?

The panic is to be expected.  Basically this use of unsafeCoerce# can 
break some assumptions in the code generator, so it gets into a state 
where it was expecting an integer and it finds a float.  I think 
-dcmm-lint will catch it.

It's fixable; we have to add some explicit coercions, and extend the 
code generator to handle them.


More information about the Glasgow-haskell-users mailing list