Funky Word32 arithmetic
module Main where
import Word
main = putStrLn (show (q * 2) ++ "\n" ++ show (q * 2 + 1))
q :: Word32
q = 2147483647
The above program, compiled thusly on a linux/386 box:
ghc -fglasgow-exts t.hs
prints:
4294967294
4294967295
But if we turn on -O, we get:
4294967294
1
This happens in both 4.09 and 4.08.
Amusingly enough, 4.08 comments:
Warning: Integer overflow in: *# 2147483647 2
but that isn't the operation that gives the problem!
Snooping in the Word library, I first suspected the rather suspicious definition of Num for Word32, which uses (signed) Int arithmetic. But even signed Int arithmetic would give the right answer here.
One fix is to define arithmetic on Word32 using real unsigned primitives - I tried it, it works and the problem goes away. I can check those changes in if you like. But I think there's another bug lurking here somewhere, since the signed arithmethic should also give the right answer.
Trac metadata
Trac field | Value |
---|---|
Version | None |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | ResolvedFixed |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |