Commit 6f847e97 authored by Simon Marlow's avatar Simon Marlow
Browse files

Fix #7233: avoid overflow in divInt64#

parent 3c083fae
...@@ -633,18 +633,26 @@ instance Integral Int64 where ...@@ -633,18 +633,26 @@ instance Integral Int64 where
divInt64#, modInt64# :: Int64# -> Int64# -> Int64# divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
-- Define div in terms of quot, being careful to avoid overflow (#7233)
x# `divInt64#` y# x# `divInt64#` y#
| (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) | (x# `gtInt64#` zero) && (y# `ltInt64#` zero)
= ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y# = ((x# `minusInt64#` one) `quotInt64#` y#) `minusInt64#` one
| (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#) | (x# `ltInt64#` zero) && (y# `gtInt64#` zero)
= ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y# = ((x# `plusInt64#` one) `quotInt64#` y#) `minusInt64#` one
| otherwise = x# `quotInt64#` y# | otherwise
= x# `quotInt64#` y#
where
!zero = intToInt64# 0#
!one = intToInt64# 1#
x# `modInt64#` y# x# `modInt64#` y#
| (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) || | (x# `gtInt64#` zero) && (y# `ltInt64#` zero) ||
(x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#) (x# `ltInt64#` zero) && (y# `gtInt64#` zero)
= if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0# = if r# `neInt64#` zero then r# `plusInt64#` y# else zero
| otherwise = r# | otherwise = r#
where where
!zero = intToInt64# 0#
!r# = x# `remInt64#` y# !r# = x# `remInt64#` y#
instance Read Int64 where instance Read Int64 where
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment