Skip to content

Wrong results in strict Word8 storage on x64

The following snippet produce two different results in function of the compiler platform used:

import Data.Word( Word8 )

-- removing the bang patterns on V definition makes
-- the problem go away.
data V = V !Word8 !Word8 deriving Show

toV :: Float -> V
toV d = V (truncate $ d * coeff) (fromIntegral $ exponent d + 128) where
  coeff = significand d *  255.9999 / d

main :: IO ()
main =
  print $ map toV [ 3.56158e-2, 0.7415215, 0.5383201, 0.1289829, 0.45520145 ]

On GHC 7.10.1 x86 (under windows and Linux) the output is:

[V 145 124,V 189 128,V 137 128,V 132 126,V 233 127]

On GHC 7.10.1 x64 (under windows and Linux), the (invalid) output is:

[V 0 124,V 0 128,V 0 128,V 0 126,V 0 127]

The bug appear at the following optimisation levels:

  • -O1
  • -O2
  • -O3

the results are the same at -O0

This bug was discovered in a bug report in the library JuicyPixels https://github.com/Twinside/Juicy.Pixels/issues/98.

The same problem has been seen with GHC 7.10.2 RC1

Edited by VincentBerthoux2
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information