#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 Difficulty: Unknown
Test Case: tests/numeric/should_run/T7233 Blocked By:
Blocking: Related Tickets:

Description

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

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

Expected value: Last expression should evaluate to:

[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]

Change History (6)

comment:1 Changed 20 months 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: http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-Int.html#divInt64. 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 20 months ago by igloo

  • Component changed from Compiler to libraries/base
  • Difficulty set to Unknown
  • Milestone set to 7.6.2
  • Priority changed from normal to highest

Thanks for the report and diagnosis.

comment:3 Changed 19 months ago by mikhail.vorozhtsov

  • Cc mikhail.vorozhtsov@… added

comment:4 Changed 18 months ago by simonmar

Fixed, test still to come:

commit 4137f8c1259e02dc1992c9d6771f45f495e7cef4
Author: Simon Marlow <marlowsd@gmail.com>
Date:   Wed Oct 24 16:57:39 2012 +0100

    Fix #7233: avoid overflow in divInt64#

comment:5 Changed 18 months ago by simonmar

  • Status changed from new to merge
  • Test Case set to tests/numeric/should_run/T7233

comment:6 Changed 18 months ago by pcapriotti

  • Resolution set to fixed
  • Status changed from merge to closed
Note: See TracTickets for help on using tickets.