From: simonmar Date: Mon, 1 Jul 2002 11:37:35 +0000 (+0000) Subject: [project @ 2002-07-01 11:37:35 by simonmar] X-Git-Tag: nhc98-1-18-release~960 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=1702d51a5974c23f8de861a0c01fc2925fed4477;p=ghc-base.git [project @ 2002-07-01 11:37:35 by simonmar] Replace divInt# with a version which doesn't suffer from overflow problems (thanks to Dylan Thurston for the code). --- diff --git a/GHC/Base.lhs b/GHC/Base.lhs index 8abd593..3945397 100644 --- a/GHC/Base.lhs +++ b/GHC/Base.lhs @@ -644,11 +644,18 @@ data (:*:) a b = a :*: b %********************************************************* \begin{code} -divInt#, modInt# :: Int# -> Int# -> Int# +divInt# :: Int# -> Int# -> Int# x# `divInt#` y# - | (x# ># 0#) && (y# <# 0#) = ((x# -# y#) -# 1#) `quotInt#` y# - | (x# <# 0#) && (y# ># 0#) = ((x# -# y#) +# 1#) `quotInt#` y# + -- careful NOT to overflow if we do any additional arithmetic + -- on the arguments... the following previous version of this + -- code has problems with overflow: +-- | (x# ># 0#) && (y# <# 0#) = ((x# -# y#) -# 1#) `quotInt#` y# +-- | (x# <# 0#) && (y# ># 0#) = ((x# -# y#) +# 1#) `quotInt#` y# + | (x# ># 0#) && (y# <# 0#) = (x# -# 1#) `quotInt#` y# -# 1# + | (x# <# 0#) && (y# ># 0#) = (x# +# 1#) `quotInt#` y# -# 1# | otherwise = x# `quotInt#` y# + +modInt# :: Int# -> Int# -> Int# x# `modInt#` y# | (x# ># 0#) && (y# <# 0#) || (x# <# 0#) && (y# ># 0#) = if r# /=# 0# then r# +# y# else 0#