Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 5,247
    • Issues 5,247
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 574
    • Merge requests 574
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #497
Closed
Open
Issue created Aug 03, 2000 by lewie@trac-lewie

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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking