Opened 18 years ago

Closed 18 years ago

Last modified 49 years ago

#497 closed bug (Fixed)

Funky Word32 arithmetic

Reported by: lewie Owned by: nobody
Priority: normal Milestone:
Component: Compiler Version: None
Keywords: Cc:
Operating System: Architecture:
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


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



But if we turn on -O, we get:


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.

Change History (3)

comment:1 Changed 18 years ago by lewie

Ignore the bit about fixing it using unsigned prims.  That info is incorrect.

This seems to simply be a bug in the optimizer.  Using -fvia-C (but no -O!) also works to avoid the bug.

comment:2 Changed 18 years ago by spanne

I belive that I've just fixed this problem in the CVS version. It was a wrong conversion from large Words to Ints, consequently constant folding (enabled with -O) sometimes yielded incorrect results.

When the fix is tested *and* I find out how to set this bug to "resolved", I'll do the latter...  :-}

comment:3 Changed 18 years ago by lewie

Status: assignedclosed
Note: See TracTickets for help on using tickets.