X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FInt.hs;h=9f53937cc7ce9effc51bf6b7426f364e5c20fce5;hb=9520c5735e69668a33013c36f85152a1ef656b8d;hp=e37d2bbd786995e4c55611f6701008e8f8369dce;hpb=4ef39162b8ae3ab239009e6386b5787475f172d9;p=ghc-base.git diff --git a/GHC/Int.hs b/GHC/Int.hs index e37d2bb..9f53937 100644 --- a/GHC/Int.hs +++ b/GHC/Int.hs @@ -1,4 +1,4 @@ -{-# OPTIONS_GHC -fno-implicit-prelude #-} +{-# OPTIONS_GHC -XNoImplicitPrelude #-} {-# OPTIONS_HADDOCK hide #-} ----------------------------------------------------------------------------- -- | @@ -24,12 +24,20 @@ module GHC.Int ( import Data.Bits +#if WORD_SIZE_IN_BITS < 32 +import GHC.IntWord32 +#endif +#if WORD_SIZE_IN_BITS < 64 +import GHC.IntWord64 +#endif + import GHC.Base import GHC.Enum import GHC.Num import GHC.Real import GHC.Read import GHC.Arr +import GHC.Err import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#) import GHC.Show @@ -56,8 +64,7 @@ instance Num Int8 where signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I8# (narrow8Int# i#) - fromInteger (J# s# d#) = I8# (narrow8Int# (integer2Int# s# d#)) + fromInteger i = I8# (narrow8Int# (toInt# i)) instance Real Int8 where toRational x = toInteger x % 1 @@ -104,16 +111,16 @@ instance Integral Int8 where | x == minBound && y == (-1) = overflowError | otherwise = (I8# (narrow8Int# (x# `divInt#` y#)), I8# (narrow8Int# (x# `modInt#` y#))) - toInteger (I8# x#) = S# x# + toInteger (I8# x#) = smallInteger x# instance Bounded Int8 where minBound = -0x80 maxBound = 0x7F instance Ix Int8 where - range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m - inRange (m,n) i = m <= i && i <= n + range (m,n) = [m..n] + unsafeIndex (m,_) i = fromIntegral i - fromIntegral m + inRange (m,n) i = m <= i && i <= n instance Read Int8 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] @@ -135,8 +142,8 @@ instance Bits Int8 where = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (8# -# i'#))))) where - x'# = narrow8Word# (int2Word# x#) - i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) + !x'# = narrow8Word# (int2Word# x#) + !i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) bitSize _ = 8 isSigned _ = True @@ -169,8 +176,7 @@ instance Num Int16 where signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I16# (narrow16Int# i#) - fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#)) + fromInteger i = I16# (narrow16Int# (toInt# i)) instance Real Int16 where toRational x = toInteger x % 1 @@ -217,16 +223,16 @@ instance Integral Int16 where | x == minBound && y == (-1) = overflowError | otherwise = (I16# (narrow16Int# (x# `divInt#` y#)), I16# (narrow16Int# (x# `modInt#` y#))) - toInteger (I16# x#) = S# x# + toInteger (I16# x#) = smallInteger x# instance Bounded Int16 where minBound = -0x8000 maxBound = 0x7FFF instance Ix Int16 where - range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m - inRange (m,n) i = m <= i && i <= n + range (m,n) = [m..n] + unsafeIndex (m,_) i = fromIntegral i - fromIntegral m + inRange (m,n) i = m <= i && i <= n instance Read Int16 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] @@ -248,11 +254,12 @@ instance Bits Int16 where = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (16# -# i'#))))) where - x'# = narrow16Word# (int2Word# x#) - i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) + !x'# = narrow16Word# (int2Word# x#) + !i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) bitSize _ = 16 isSigned _ = True + {-# RULES "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#) "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x# @@ -342,7 +349,7 @@ instance Integral Int32 where I32# (x# `modInt32#` y#)) toInteger x@(I32# x#) | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int) - = S# (int32ToInt# x#) + = smallInteger (int32ToInt# x#) | otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d divInt32#, modInt32# :: Int32# -> Int32# -> Int32# @@ -385,31 +392,6 @@ instance Bits Int32 where bitSize _ = 32 isSigned _ = True -foreign import "stg_eqInt32" unsafe eqInt32# :: Int32# -> Int32# -> Bool -foreign import "stg_neInt32" unsafe neInt32# :: Int32# -> Int32# -> Bool -foreign import "stg_ltInt32" unsafe ltInt32# :: Int32# -> Int32# -> Bool -foreign import "stg_leInt32" unsafe leInt32# :: Int32# -> Int32# -> Bool -foreign import "stg_gtInt32" unsafe gtInt32# :: Int32# -> Int32# -> Bool -foreign import "stg_geInt32" unsafe geInt32# :: Int32# -> Int32# -> Bool -foreign import "stg_plusInt32" unsafe plusInt32# :: Int32# -> Int32# -> Int32# -foreign import "stg_minusInt32" unsafe minusInt32# :: Int32# -> Int32# -> Int32# -foreign import "stg_timesInt32" unsafe timesInt32# :: Int32# -> Int32# -> Int32# -foreign import "stg_negateInt32" unsafe negateInt32# :: Int32# -> Int32# -foreign import "stg_quotInt32" unsafe quotInt32# :: Int32# -> Int32# -> Int32# -foreign import "stg_remInt32" unsafe remInt32# :: Int32# -> Int32# -> Int32# -foreign import "stg_intToInt32" unsafe intToInt32# :: Int# -> Int32# -foreign import "stg_int32ToInt" unsafe int32ToInt# :: Int32# -> Int# -foreign import "stg_wordToWord32" unsafe wordToWord32# :: Word# -> Word32# -foreign import "stg_int32ToWord32" unsafe int32ToWord32# :: Int32# -> Word32# -foreign import "stg_word32ToInt32" unsafe word32ToInt32# :: Word32# -> Int32# -foreign import "stg_and32" unsafe and32# :: Word32# -> Word32# -> Word32# -foreign import "stg_or32" unsafe or32# :: Word32# -> Word32# -> Word32# -foreign import "stg_xor32" unsafe xor32# :: Word32# -> Word32# -> Word32# -foreign import "stg_not32" unsafe not32# :: Word32# -> Word32# -foreign import "stg_iShiftL32" unsafe iShiftL32# :: Int32# -> Int# -> Int32# -foreign import "stg_iShiftRA32" unsafe iShiftRA32# :: Int32# -> Int# -> Int32# -foreign import "stg_shiftL32" unsafe shiftL32# :: Word32# -> Int# -> Word32# -foreign import "stg_shiftRL32" unsafe shiftRL32# :: Word32# -> Int# -> Word32# {-# RULES "fromIntegral/Int->Int32" fromIntegral = \(I# x#) -> I32# (intToInt32# x#) @@ -445,8 +427,7 @@ instance Num Int32 where signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I32# (narrow32Int# i#) - fromInteger (J# s# d#) = I32# (narrow32Int# (integer2Int# s# d#)) + fromInteger i = I32# (narrow32Int# (toInt# i)) instance Enum Int32 where succ x @@ -494,7 +475,7 @@ instance Integral Int32 where | x == minBound && y == (-1) = overflowError | otherwise = (I32# (narrow32Int# (x# `divInt#` y#)), I32# (narrow32Int# (x# `modInt#` y#))) - toInteger (I32# x#) = S# x# + toInteger (I32# x#) = smallInteger x# instance Read Int32 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] @@ -516,8 +497,8 @@ instance Bits Int32 where = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (32# -# i'#))))) where - x'# = narrow32Word# (int2Word# x#) - i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) + !x'# = narrow32Word# (int2Word# x#) + !i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) bitSize _ = 32 isSigned _ = True @@ -541,9 +522,9 @@ instance Bounded Int32 where maxBound = 0x7FFFFFFF instance Ix Int32 where - range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m - inRange (m,n) i = m <= i && i <= n + range (m,n) = [m..n] + unsafeIndex (m,_) i = fromIntegral i - fromIntegral m + inRange (m,n) i = m <= i && i <= n ------------------------------------------------------------------------ -- type Int64 @@ -577,8 +558,7 @@ instance Num Int64 where signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I64# (intToInt64# i#) - fromInteger (J# s# d#) = I64# (integerToInt64# s# d#) + fromInteger i = I64# (integerToInt64 i) instance Enum Int64 where succ x @@ -624,12 +604,7 @@ instance Integral Int64 where | x == minBound && y == (-1) = overflowError | otherwise = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#)) - toInteger x@(I64# x#) - | x >= fromIntegral (minBound::Int) && - x <= fromIntegral (maxBound::Int) - = S# (int64ToInt# x#) - | otherwise = case int64ToInteger# x# of - (# s, d #) -> J# s d + toInteger (I64# x) = int64ToInteger x divInt64#, modInt64# :: Int64# -> Int64# -> Int64# @@ -645,7 +620,7 @@ x# `modInt64#` y# = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0# | otherwise = r# where - r# = x# `remInt64#` y# + !r# = x# `remInt64#` y# instance Read Int64 where readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] @@ -667,12 +642,11 @@ instance Bits Int64 where = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#` (x'# `uncheckedShiftRL64#` (64# -# i'#)))) where - x'# = int64ToWord64# x# - i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) + !x'# = int64ToWord64# x# + !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) bitSize _ = 64 isSigned _ = True - -- give the 64-bit shift operations the same treatment as the 32-bit -- ones (see GHC.Base), namely we wrap them in tests to catch the -- cases when we're shifting more than 64 bits to avoid unspecified @@ -688,35 +662,6 @@ a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#) else intToInt64# 0# | otherwise = a `uncheckedIShiftRA64#` b - -foreign import ccall unsafe "hs_eqInt64" eqInt64# :: Int64# -> Int64# -> Bool -foreign import ccall unsafe "hs_neInt64" neInt64# :: Int64# -> Int64# -> Bool -foreign import ccall unsafe "hs_ltInt64" ltInt64# :: Int64# -> Int64# -> Bool -foreign import ccall unsafe "hs_leInt64" leInt64# :: Int64# -> Int64# -> Bool -foreign import ccall unsafe "hs_gtInt64" gtInt64# :: Int64# -> Int64# -> Bool -foreign import ccall unsafe "hs_geInt64" geInt64# :: Int64# -> Int64# -> Bool -foreign import ccall unsafe "hs_plusInt64" plusInt64# :: Int64# -> Int64# -> Int64# -foreign import ccall unsafe "hs_minusInt64" minusInt64# :: Int64# -> Int64# -> Int64# -foreign import ccall unsafe "hs_timesInt64" timesInt64# :: Int64# -> Int64# -> Int64# -foreign import ccall unsafe "hs_negateInt64" negateInt64# :: Int64# -> Int64# -foreign import ccall unsafe "hs_quotInt64" quotInt64# :: Int64# -> Int64# -> Int64# -foreign import ccall unsafe "hs_remInt64" remInt64# :: Int64# -> Int64# -> Int64# -foreign import ccall unsafe "hs_intToInt64" intToInt64# :: Int# -> Int64# -foreign import ccall unsafe "hs_int64ToInt" int64ToInt# :: Int64# -> Int# -foreign import ccall unsafe "hs_wordToWord64" wordToWord64# :: Word# -> Word64# -foreign import ccall unsafe "hs_int64ToWord64" int64ToWord64# :: Int64# -> Word64# -foreign import ccall unsafe "hs_word64ToInt64" word64ToInt64# :: Word64# -> Int64# -foreign import ccall unsafe "hs_and64" and64# :: Word64# -> Word64# -> Word64# -foreign import ccall unsafe "hs_or64" or64# :: Word64# -> Word64# -> Word64# -foreign import ccall unsafe "hs_xor64" xor64# :: Word64# -> Word64# -> Word64# -foreign import ccall unsafe "hs_not64" not64# :: Word64# -> Word64# -foreign import ccall unsafe "hs_uncheckedShiftL64" uncheckedShiftL64# :: Word64# -> Int# -> Word64# -foreign import ccall unsafe "hs_uncheckedShiftRL64" uncheckedShiftRL64# :: Word64# -> Int# -> Word64# -foreign import ccall unsafe "hs_uncheckedIShiftL64" uncheckedIShiftL64# :: Int64# -> Int# -> Int64# -foreign import ccall unsafe "hs_uncheckedIShiftRA64" uncheckedIShiftRA64# :: Int64# -> Int# -> Int64# - -foreign import ccall unsafe "hs_integerToInt64" integerToInt64# :: Int# -> ByteArray# -> Int64# - {-# RULES "fromIntegral/Int->Int64" fromIntegral = \(I# x#) -> I64# (intToInt64# x#) "fromIntegral/Word->Int64" fromIntegral = \(W# x#) -> I64# (word64ToInt64# (wordToWord64# x#)) @@ -749,8 +694,7 @@ instance Num Int64 where signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I64# i# - fromInteger (J# s# d#) = I64# (integer2Int# s# d#) + fromInteger i = I64# (toInt# i) instance Enum Int64 where succ x @@ -789,7 +733,7 @@ instance Integral Int64 where | y == 0 = divZeroError | x == minBound && y == (-1) = overflowError | otherwise = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#)) - toInteger (I64# x#) = S# x# + toInteger (I64# x#) = smallInteger x# instance Read Int64 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] @@ -811,8 +755,8 @@ instance Bits Int64 where = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (64# -# i'#)))) where - x'# = int2Word# x# - i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) + !x'# = int2Word# x# + !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) bitSize _ = 64 isSigned _ = True @@ -821,7 +765,10 @@ instance Bits Int64 where "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#) #-} +uncheckedIShiftL64# :: Int# -> Int# -> Int# uncheckedIShiftL64# = uncheckedIShiftL# + +uncheckedIShiftRA64# :: Int# -> Int# -> Int# uncheckedIShiftRA64# = uncheckedIShiftRA# #endif @@ -833,6 +780,6 @@ instance Bounded Int64 where maxBound = 0x7FFFFFFFFFFFFFFF instance Ix Int64 where - range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m - inRange (m,n) i = m <= i && i <= n + range (m,n) = [m..n] + unsafeIndex (m,_) i = fromIntegral i - fromIntegral m + inRange (m,n) i = m <= i && i <= n