X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FInt.hs;h=7ee7b1bb658a794e84f397afcd39c1dfa43f995f;hb=a0d7892da0d00fee781a550ef353df8734be5884;hp=cd1259475ad6f9a61a014a194811502f881de579;hpb=b706340c451952adf230b5b8daecad8a1f34d714;p=haskell-directory.git diff --git a/GHC/Int.hs b/GHC/Int.hs index cd12594..7ee7b1b 100644 --- a/GHC/Int.hs +++ b/GHC/Int.hs @@ -1,4 +1,4 @@ -{-# OPTIONS -fno-implicit-prelude #-} +{-# OPTIONS_GHC -fno-implicit-prelude #-} ----------------------------------------------------------------------------- -- | -- Module : GHC.Int @@ -15,12 +15,14 @@ #include "MachDeps.h" +-- #hide module GHC.Int ( Int8(..), Int16(..), Int32(..), Int64(..)) where import Data.Bits +import {-# SOURCE #-} GHC.Err import GHC.Base import GHC.Enum import GHC.Num @@ -38,9 +40,7 @@ import GHC.Show -- and must ensure that it holds only values from its logical range. data Int8 = I8# Int# deriving (Eq, Ord) - -instance CCallable Int8 -instance CReturnable Int8 +-- ^ 8-bit signed integer type instance Show Int8 where showsPrec p x = showsPrec p (fromIntegral x :: Int) @@ -79,24 +79,24 @@ instance Enum Int8 where instance Integral Int8 where quot x@(I8# x#) y@(I8# y#) | y /= 0 = I8# (narrow8Int# (x# `quotInt#` y#)) - | otherwise = divZeroError "quot{Int8}" x + | otherwise = divZeroError rem x@(I8# x#) y@(I8# y#) | y /= 0 = I8# (narrow8Int# (x# `remInt#` y#)) - | otherwise = divZeroError "rem{Int8}" x + | otherwise = divZeroError div x@(I8# x#) y@(I8# y#) | y /= 0 = I8# (narrow8Int# (x# `divInt#` y#)) - | otherwise = divZeroError "div{Int8}" x + | otherwise = divZeroError mod x@(I8# x#) y@(I8# y#) | y /= 0 = I8# (narrow8Int# (x# `modInt#` y#)) - | otherwise = divZeroError "mod{Int8}" x + | otherwise = divZeroError quotRem x@(I8# x#) y@(I8# y#) | y /= 0 = (I8# (narrow8Int# (x# `quotInt#` y#)), I8# (narrow8Int# (x# `remInt#` y#))) - | otherwise = divZeroError "quotRem{Int8}" x + | otherwise = divZeroError divMod x@(I8# x#) y@(I8# y#) | y /= 0 = (I8# (narrow8Int# (x# `divInt#` y#)), I8# (narrow8Int# (x# `modInt#` y#))) - | otherwise = divZeroError "divMod{Int8}" x + | otherwise = divZeroError toInteger (I8# x#) = S# x# instance Bounded Int8 where @@ -105,14 +105,15 @@ instance Bounded Int8 where instance Ix Int8 where range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral (i - m) + unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m inRange (m,n) i = m <= i && i <= n - unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1 instance Read Int8 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] instance Bits Int8 where + {-# INLINE shift #-} + (I8# x#) .&. (I8# y#) = I8# (word2Int# (int2Word# x# `and#` int2Word# y#)) (I8# x#) .|. (I8# y#) = I8# (word2Int# (int2Word# x# `or#` int2Word# y#)) (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#)) @@ -124,8 +125,8 @@ instance Bits Int8 where | i'# ==# 0# = I8# x# | otherwise - = I8# (narrow8Int# (word2Int# ((x'# `shiftL#` i'#) `or#` - (x'# `shiftRL#` (8# -# i'#))))) + = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + (x'# `uncheckedShiftRL#` (8# -# i'#))))) where x'# = narrow8Word# (int2Word# x#) i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) @@ -146,9 +147,7 @@ instance Bits Int8 where -- and must ensure that it holds only values from its logical range. data Int16 = I16# Int# deriving (Eq, Ord) - -instance CCallable Int16 -instance CReturnable Int16 +-- ^ 16-bit signed integer type instance Show Int16 where showsPrec p x = showsPrec p (fromIntegral x :: Int) @@ -187,24 +186,24 @@ instance Enum Int16 where instance Integral Int16 where quot x@(I16# x#) y@(I16# y#) | y /= 0 = I16# (narrow16Int# (x# `quotInt#` y#)) - | otherwise = divZeroError "quot{Int16}" x + | otherwise = divZeroError rem x@(I16# x#) y@(I16# y#) | y /= 0 = I16# (narrow16Int# (x# `remInt#` y#)) - | otherwise = divZeroError "rem{Int16}" x + | otherwise = divZeroError div x@(I16# x#) y@(I16# y#) | y /= 0 = I16# (narrow16Int# (x# `divInt#` y#)) - | otherwise = divZeroError "div{Int16}" x + | otherwise = divZeroError mod x@(I16# x#) y@(I16# y#) | y /= 0 = I16# (narrow16Int# (x# `modInt#` y#)) - | otherwise = divZeroError "mod{Int16}" x + | otherwise = divZeroError quotRem x@(I16# x#) y@(I16# y#) | y /= 0 = (I16# (narrow16Int# (x# `quotInt#` y#)), I16# (narrow16Int# (x# `remInt#` y#))) - | otherwise = divZeroError "quotRem{Int16}" x + | otherwise = divZeroError divMod x@(I16# x#) y@(I16# y#) | y /= 0 = (I16# (narrow16Int# (x# `divInt#` y#)), I16# (narrow16Int# (x# `modInt#` y#))) - | otherwise = divZeroError "divMod{Int16}" x + | otherwise = divZeroError toInteger (I16# x#) = S# x# instance Bounded Int16 where @@ -213,14 +212,15 @@ instance Bounded Int16 where instance Ix Int16 where range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral (i - m) + unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m inRange (m,n) i = m <= i && i <= n - unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1 instance Read Int16 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] instance Bits Int16 where + {-# INLINE shift #-} + (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#)) (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#)) (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#)) @@ -232,8 +232,8 @@ instance Bits Int16 where | i'# ==# 0# = I16# x# | otherwise - = I16# (narrow16Int# (word2Int# ((x'# `shiftL#` i'#) `or#` - (x'# `shiftRL#` (16# -# i'#))))) + = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + (x'# `uncheckedShiftRL#` (16# -# i'#))))) where x'# = narrow16Word# (int2Word# x#) i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) @@ -255,6 +255,7 @@ instance Bits Int16 where #if WORD_SIZE_IN_BITS < 32 data Int32 = I32# Int32# +-- ^ 32-bit signed integer type instance Eq Int32 where (I32# x#) == (I32# y#) = x# `eqInt32#` y# @@ -302,22 +303,22 @@ instance Enum Int32 where instance Integral Int32 where quot x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (x# `quotInt32#` y#) - | otherwise = divZeroError "quot{Int32}" x + | otherwise = divZeroError rem x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (x# `remInt32#` y#) - | otherwise = divZeroError "rem{Int32}" x + | otherwise = divZeroError div x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (x# `divInt32#` y#) - | otherwise = divZeroError "div{Int32}" x + | otherwise = divZeroError mod x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (x# `modInt32#` y#) - | otherwise = divZeroError "mod{Int32}" x + | otherwise = divZeroError quotRem x@(I32# x#) y@(I32# y#) | y /= 0 = (I32# (x# `quotInt32#` y#), I32# (x# `remInt32#` y#)) - | otherwise = divZeroError "quotRem{Int32}" x + | otherwise = divZeroError divMod x@(I32# x#) y@(I32# y#) | y /= 0 = (I32# (x# `divInt32#` y#), I32# (x# `modInt32#` y#)) - | otherwise = divZeroError "divMod{Int32}" x + | otherwise = divZeroError toInteger x@(I32# x#) | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int) = S# (int32ToInt# x#) @@ -342,6 +343,8 @@ instance Read Int32 where readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] instance Bits Int32 where + {-# INLINE shift #-} + (I32# x#) .&. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `and32#` int32ToWord32# y#)) (I32# x#) .|. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `or32#` int32ToWord32# y#)) (I32# x#) `xor` (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `xor32#` int32ToWord32# y#)) @@ -406,6 +409,7 @@ foreign import "stg_shiftRL32" unsafe shiftRL32# :: Word32# -> Int# -> W #endif data Int32 = I32# Int# deriving (Eq, Ord) +-- ^ 32-bit signed integer type instance Show Int32 where showsPrec p x = showsPrec p (fromIntegral x :: Int) @@ -445,30 +449,32 @@ instance Enum Int32 where instance Integral Int32 where quot x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (narrow32Int# (x# `quotInt#` y#)) - | otherwise = divZeroError "quot{Int32}" x + | otherwise = divZeroError rem x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (narrow32Int# (x# `remInt#` y#)) - | otherwise = divZeroError "rem{Int32}" x + | otherwise = divZeroError div x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (narrow32Int# (x# `divInt#` y#)) - | otherwise = divZeroError "div{Int32}" x + | otherwise = divZeroError mod x@(I32# x#) y@(I32# y#) | y /= 0 = I32# (narrow32Int# (x# `modInt#` y#)) - | otherwise = divZeroError "mod{Int32}" x + | otherwise = divZeroError quotRem x@(I32# x#) y@(I32# y#) | y /= 0 = (I32# (narrow32Int# (x# `quotInt#` y#)), I32# (narrow32Int# (x# `remInt#` y#))) - | otherwise = divZeroError "quotRem{Int32}" x + | otherwise = divZeroError divMod x@(I32# x#) y@(I32# y#) | y /= 0 = (I32# (narrow32Int# (x# `divInt#` y#)), I32# (narrow32Int# (x# `modInt#` y#))) - | otherwise = divZeroError "divMod{Int32}" x + | otherwise = divZeroError toInteger (I32# x#) = S# x# instance Read Int32 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] instance Bits Int32 where + {-# INLINE shift #-} + (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#)) (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#)) (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#)) @@ -480,8 +486,8 @@ instance Bits Int32 where | i'# ==# 0# = I32# x# | otherwise - = I32# (narrow32Int# (word2Int# ((x'# `shiftL#` i'#) `or#` - (x'# `shiftRL#` (32# -# i'#))))) + = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + (x'# `uncheckedShiftRL#` (32# -# i'#))))) where x'# = narrow32Word# (int2Word# x#) i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) @@ -500,9 +506,6 @@ instance Bits Int32 where #endif -instance CCallable Int32 -instance CReturnable Int32 - instance Real Int32 where toRational x = toInteger x % 1 @@ -512,9 +515,8 @@ instance Bounded Int32 where instance Ix Int32 where range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral (i - m) + unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m inRange (m,n) i = m <= i && i <= n - unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1 ------------------------------------------------------------------------ -- type Int64 @@ -523,6 +525,7 @@ instance Ix Int32 where #if WORD_SIZE_IN_BITS < 64 data Int64 = I64# Int64# +-- ^ 64-bit signed integer type instance Eq Int64 where (I64# x#) == (I64# y#) = x# `eqInt64#` y# @@ -570,22 +573,22 @@ instance Enum Int64 where instance Integral Int64 where quot x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `quotInt64#` y#) - | otherwise = divZeroError "quot{Int64}" x + | otherwise = divZeroError rem x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `remInt64#` y#) - | otherwise = divZeroError "rem{Int64}" x + | otherwise = divZeroError div x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `divInt64#` y#) - | otherwise = divZeroError "div{Int64}" x + | otherwise = divZeroError mod x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `modInt64#` y#) - | otherwise = divZeroError "mod{Int64}" x + | otherwise = divZeroError quotRem x@(I64# x#) y@(I64# y#) | y /= 0 = (I64# (x# `quotInt64#` y#), I64# (x# `remInt64#` y#)) - | otherwise = divZeroError "quotRem{Int64}" x + | otherwise = divZeroError divMod x@(I64# x#) y@(I64# y#) | y /= 0 = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#)) - | otherwise = divZeroError "divMod{Int64}" x + | otherwise = divZeroError toInteger x@(I64# x#) | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int) = S# (int64ToInt# x#) @@ -611,6 +614,8 @@ instance Read Int64 where readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] instance Bits Int64 where + {-# INLINE shift #-} + (I64# x#) .&. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#)) (I64# x#) .|. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `or64#` int64ToWord64# y#)) (I64# x#) `xor` (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#)) @@ -692,6 +697,7 @@ foreign import ccall unsafe "stg_integerToInt64" integerToInt64# :: Int# -> By -- from its logical range. data Int64 = I64# Int# deriving (Eq, Ord) +-- ^ 64-bit signed integer type instance Show Int64 where showsPrec p x = showsPrec p (fromIntegral x :: Int) @@ -724,28 +730,30 @@ instance Enum Int64 where instance Integral Int64 where quot x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `quotInt#` y#) - | otherwise = divZeroError "quot{Int64}" x + | otherwise = divZeroError rem x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `remInt#` y#) - | otherwise = divZeroError "rem{Int64}" x + | otherwise = divZeroError div x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `divInt#` y#) - | otherwise = divZeroError "div{Int64}" x + | otherwise = divZeroError mod x@(I64# x#) y@(I64# y#) | y /= 0 = I64# (x# `modInt#` y#) - | otherwise = divZeroError "mod{Int64}" x + | otherwise = divZeroError quotRem x@(I64# x#) y@(I64# y#) | y /= 0 = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#)) - | otherwise = divZeroError "quotRem{Int64}" x + | otherwise = divZeroError divMod x@(I64# x#) y@(I64# y#) | y /= 0 = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#)) - | otherwise = divZeroError "divMod{Int64}" x + | otherwise = divZeroError toInteger (I64# x#) = S# x# instance Read Int64 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] instance Bits Int64 where + {-# INLINE shift #-} + (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#)) (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#)) (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#)) @@ -757,8 +765,8 @@ instance Bits Int64 where | i'# ==# 0# = I64# x# | otherwise - = I64# (word2Int# ((x'# `shiftL#` i'#) `or#` - (x'# `shiftRL#` (64# -# i'#)))) + = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + (x'# `uncheckedShiftRL#` (64# -# i'#)))) where x'# = int2Word# x# i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) @@ -772,9 +780,6 @@ instance Bits Int64 where #endif -instance CCallable Int64 -instance CReturnable Int64 - instance Real Int64 where toRational x = toInteger x % 1 @@ -784,6 +789,5 @@ instance Bounded Int64 where instance Ix Int64 where range (m,n) = [m..n] - unsafeIndex b@(m,_) i = fromIntegral (i - m) + unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m inRange (m,n) i = m <= i && i <= n - unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1