Skip to content
Snippets Groups Projects
Commit f2879ecf authored by Ian Lynagh's avatar Ian Lynagh
Browse files

() is now available, so use that instead of our own

parent b73a4bf2
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,7 @@ import GHC.Integer.Type ...@@ -43,6 +43,7 @@ import GHC.Integer.Type
import GHC.Bool import GHC.Bool
import GHC.Ordering import GHC.Ordering
import GHC.Prim import GHC.Prim
import GHC.Unit ()
#if WORD_SIZE_IN_BITS < 64 #if WORD_SIZE_IN_BITS < 64
import GHC.IntWord64 import GHC.IntWord64
#endif #endif
...@@ -406,27 +407,24 @@ hashInteger (!_) = 42# ...@@ -406,27 +407,24 @@ hashInteger (!_) = 42#
------------------------------------------------------------------- -------------------------------------------------------------------
-- The hard work is done on positive numbers -- The hard work is done on positive numbers
-- XXX Could move () above us
data Unit = Unit
onePositive :: Positive onePositive :: Positive
onePositive = Some 1## None onePositive = Some 1## None
halfBoundUp, fullBound :: Unit -> Digit halfBoundUp, fullBound :: () -> Digit
lowHalfMask :: Unit -> Digit lowHalfMask :: () -> Digit
highHalfShift :: Unit -> Int# highHalfShift :: () -> Int#
twoToTheThirtytwoPositive :: Positive twoToTheThirtytwoPositive :: Positive
#if WORD_SIZE_IN_BITS == 64 #if WORD_SIZE_IN_BITS == 64
halfBoundUp Unit = 0x8000000000000000## halfBoundUp () = 0x8000000000000000##
fullBound Unit = 0xFFFFFFFFFFFFFFFF## fullBound () = 0xFFFFFFFFFFFFFFFF##
lowHalfMask Unit = 0xFFFFFFFF## lowHalfMask () = 0xFFFFFFFF##
highHalfShift Unit = 32# highHalfShift () = 32#
twoToTheThirtytwoPositive = Some 0x100000000## None twoToTheThirtytwoPositive = Some 0x100000000## None
#elif WORD_SIZE_IN_BITS == 32 #elif WORD_SIZE_IN_BITS == 32
halfBoundUp Unit = 0x80000000## halfBoundUp () = 0x80000000##
fullBound Unit = 0xFFFFFFFF## fullBound () = 0xFFFFFFFF##
lowHalfMask Unit = 0xFFFF## lowHalfMask () = 0xFFFF##
highHalfShift Unit = 16# highHalfShift () = 16#
twoToTheThirtytwoPositive = Some 0## (Some 1## None) twoToTheThirtytwoPositive = Some 0## (Some 1## None)
#else #else
#error Unhandled WORD_SIZE_IN_BITS #error Unhandled WORD_SIZE_IN_BITS
...@@ -487,26 +485,26 @@ plusPositive x0 y0 = addWithCarry 0## x0 y0 ...@@ -487,26 +485,26 @@ plusPositive x0 y0 = addWithCarry 0## x0 y0
addWithCarry c xs@(Some x xs') ys@(Some y ys') addWithCarry c xs@(Some x xs') ys@(Some y ys')
= if x `ltWord#` y then addWithCarry c ys xs = if x `ltWord#` y then addWithCarry c ys xs
-- Now x >= y -- Now x >= y
else if y `geWord#` halfBoundUp Unit else if y `geWord#` halfBoundUp ()
-- So they are both at least halfBoundUp, so we subtract -- So they are both at least halfBoundUp, so we subtract
-- halfBoundUp from each and thus carry 1 -- halfBoundUp from each and thus carry 1
then case x `minusWord#` halfBoundUp Unit of then case x `minusWord#` halfBoundUp () of
x' -> x' ->
case y `minusWord#` halfBoundUp Unit of case y `minusWord#` halfBoundUp () of
y' -> y' ->
case x' `plusWord#` y' `plusWord#` c of case x' `plusWord#` y' `plusWord#` c of
this -> this ->
Some this withCarry Some this withCarry
else if x `geWord#` halfBoundUp Unit else if x `geWord#` halfBoundUp ()
then case x `minusWord#` halfBoundUp Unit of then case x `minusWord#` halfBoundUp () of
x' -> x' ->
case x' `plusWord#` y `plusWord#` c of case x' `plusWord#` y `plusWord#` c of
z -> z ->
-- We've taken off halfBoundUp, so now we need to -- We've taken off halfBoundUp, so now we need to
-- add it back on -- add it back on
if z `ltWord#` halfBoundUp Unit if z `ltWord#` halfBoundUp ()
then Some (z `plusWord#` halfBoundUp Unit) withoutCarry then Some (z `plusWord#` halfBoundUp ()) withoutCarry
else Some (z `minusWord#` halfBoundUp Unit) withCarry else Some (z `minusWord#` halfBoundUp ()) withCarry
else Some (x `plusWord#` y `plusWord#` c) withoutCarry else Some (x `plusWord#` y `plusWord#` c) withoutCarry
where withCarry = addWithCarry 1## xs' ys' where withCarry = addWithCarry 1## xs' ys'
withoutCarry = addWithCarry 0## xs' ys' withoutCarry = addWithCarry 0## xs' ys'
...@@ -520,7 +518,7 @@ plusPositive x0 y0 = addWithCarry 0## x0 y0 ...@@ -520,7 +518,7 @@ plusPositive x0 y0 = addWithCarry 0## x0 y0
-- digit `elem` [0, 1] -- digit `elem` [0, 1]
succPositive :: Positive -> Positive succPositive :: Positive -> Positive
succPositive None = Some 1## None succPositive None = Some 1## None
succPositive (Some w ws) = if w `eqWord#` fullBound Unit succPositive (Some w ws) = if w `eqWord#` fullBound ()
then Some 0## (succPositive ws) then Some 0## (succPositive ws)
else Some (w `plusWord#` 1##) ws else Some (w `plusWord#` 1##) ws
...@@ -534,7 +532,7 @@ Some x xs `minusPositive` Some y ys ...@@ -534,7 +532,7 @@ Some x xs `minusPositive` Some y ys
s -> Some 0## s s -> Some 0## s
else if x `gtWord#` y then else if x `gtWord#` y then
Some (x `minusWord#` y) (xs `minusPositive` ys) Some (x `minusWord#` y) (xs `minusPositive` ys)
else case (fullBound Unit `minusWord#` y) `plusWord#` 1## of else case (fullBound () `minusWord#` y) `plusWord#` 1## of
z -> -- z = 2^n - y, calculated without overflow z -> -- z = 2^n - y, calculated without overflow
case z `plusWord#` x of case z `plusWord#` x of
z' -> -- z = 2^n + (x - y), calculated without overflow z' -> -- z = 2^n + (x - y), calculated without overflow
...@@ -584,11 +582,11 @@ timesDigit (!x) (!y) ...@@ -584,11 +582,11 @@ timesDigit (!x) (!y)
xhyh -> xhyh ->
case splitHalves (xh `timesWord#` yl) of case splitHalves (xh `timesWord#` yl) of
(# xhylh, xhyll #) -> (# xhylh, xhyll #) ->
case xhyll `uncheckedShiftL#` highHalfShift Unit of case xhyll `uncheckedShiftL#` highHalfShift () of
xhyll' -> xhyll' ->
case splitHalves (xl `timesWord#` yh) of case splitHalves (xl `timesWord#` yh) of
(# xlyhh, xlyhl #) -> (# xlyhh, xlyhl #) ->
case xlyhl `uncheckedShiftL#` highHalfShift Unit of case xlyhl `uncheckedShiftL#` highHalfShift () of
xlyhl' -> xlyhl' ->
case xl `timesWord#` yl of case xl `timesWord#` yl of
xlyl -> xlyl ->
...@@ -611,8 +609,8 @@ timesDigit (!x) (!y) ...@@ -611,8 +609,8 @@ timesDigit (!x) (!y)
else Some 0## (Some high None) `plusPositive` low else Some 0## (Some high None) `plusPositive` low
splitHalves :: Digit -> (# {- High -} Digit, {- Low -} Digit #) splitHalves :: Digit -> (# {- High -} Digit, {- Low -} Digit #)
splitHalves (!x) = (# x `uncheckedShiftRL#` highHalfShift Unit, splitHalves (!x) = (# x `uncheckedShiftRL#` highHalfShift (),
x `and#` lowHalfMask Unit #) x `and#` lowHalfMask () #)
-- Assumes 0 <= i -- Assumes 0 <= i
shiftLPositive :: Positive -> Int# -> Positive shiftLPositive :: Positive -> Int# -> Positive
...@@ -731,7 +729,7 @@ doubleFromPositive (Some w ds) ...@@ -731,7 +729,7 @@ doubleFromPositive (Some w ds)
(# h, l #) -> (# h, l #) ->
(doubleFromPositive ds *## (2.0## **## WORD_SIZE_IN_BITS.0##)) (doubleFromPositive ds *## (2.0## **## WORD_SIZE_IN_BITS.0##))
+## (int2Double# (word2Int# h) *## +## (int2Double# (word2Int# h) *##
(2.0## **## int2Double# (highHalfShift Unit))) (2.0## **## int2Double# (highHalfShift ())))
+## int2Double# (word2Int# l) +## int2Double# (word2Int# l)
-- XXX We'd really like word2Float# for this -- XXX We'd really like word2Float# for this
...@@ -742,7 +740,7 @@ floatFromPositive (Some w ds) ...@@ -742,7 +740,7 @@ floatFromPositive (Some w ds)
(# h, l #) -> (# h, l #) ->
(floatFromPositive ds `timesFloat#` (2.0# `powerFloat#` WORD_SIZE_IN_BITS.0#)) (floatFromPositive ds `timesFloat#` (2.0# `powerFloat#` WORD_SIZE_IN_BITS.0#))
`plusFloat#` (int2Float# (word2Int# h) `timesFloat#` `plusFloat#` (int2Float# (word2Int# h) `timesFloat#`
(2.0# `powerFloat#` int2Float# (highHalfShift Unit))) (2.0# `powerFloat#` int2Float# (highHalfShift ())))
`plusFloat#` int2Float# (word2Int# l) `plusFloat#` int2Float# (word2Int# l)
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment