Opened 4 years ago

Closed 4 years ago

#7233 closed bug (fixed)

Int64 division buggy on a 32-bit installation

Reported by: lerkok Owned by:
Priority: highest Milestone: 7.6.2
Component: libraries/base Version: 7.6.1
Keywords: Cc: mikhail.vorozhtsov@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: tests/numeric/should_run/T7233
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


This is with GHC 7.4.1, running on Linux, 32 bits:

Prelude> maxBound :: Int
Prelude> :m Data.Int
Prelude Data.Int> [5 `div` (minBound+k::Int64) | k <- [0 .. 10]]

Expected value: Last expression should evaluate to:


Change History (6)

comment:1 Changed 4 years ago by lerkok

Some more information. It appears that the expression:

    x `div` y

will be incorrectly computed for Int64 values when the internal word size is 32 bits, in the following cases:

   1.  If x > 0 && y < 0, then whenever ((x-y)-1) overflows/underflows the Int64 range
   2.  if x < 0 && y > 0, then whenever ((x-y)+1) overflows/underflows the Int64 range

So, this will impact whenever the difference between x and y are rather large; but errors will pop-up regularly through the data-space, not just at end points. (That is, for every valid value of x, there's a value of y that will exhibit the bug; and vice versa.)

I think the culprit is here: The code in there implements div in terms of quot, computing(x-y)-1 and (x-y)+1` without worrying about overflow/underflow.

comment:2 Changed 4 years ago by igloo

Component: Compilerlibraries/base
difficulty: Unknown
Milestone: 7.6.2
Priority: normalhighest

Thanks for the report and diagnosis.

comment:3 Changed 4 years ago by mikhail.vorozhtsov

Cc: mikhail.vorozhtsov@… added

comment:4 Changed 4 years ago by simonmar

Fixed, test still to come:

commit 4137f8c1259e02dc1992c9d6771f45f495e7cef4
Author: Simon Marlow <>
Date:   Wed Oct 24 16:57:39 2012 +0100

    Fix #7233: avoid overflow in divInt64#

comment:5 Changed 4 years ago by simonmar

Status: newmerge
Test Case: tests/numeric/should_run/T7233

comment:6 Changed 4 years ago by pcapriotti

Resolution: fixed
Status: mergeclosed
Note: See TracTickets for help on using tickets.