From: Ian Lynagh Date: Tue, 25 Mar 2008 20:26:34 +0000 (+0000) Subject: Move Word64/Int64/Word32/Int32 primitives into ghc-prim X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=4a5c1d89c976b28e9e5bc74fe8248e7043924506;p=ghc-base.git Move Word64/Int64/Word32/Int32 primitives into ghc-prim --- diff --git a/GHC/Int.hs b/GHC/Int.hs index 9e19f9f..e071089 100644 --- a/GHC/Int.hs +++ b/GHC/Int.hs @@ -24,6 +24,13 @@ 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 @@ -383,32 +390,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#) "fromIntegral/Word->Int32" fromIntegral = \(W# x#) -> I32# (word32ToInt32# (wordToWord32# x#)) @@ -574,8 +555,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 @@ -621,12 +601,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) - = smallInteger (int64ToInt# x#) - | otherwise = case int64ToInteger# x# of - (# s, d #) -> J# s d + toInteger (I64# x) = int64ToInteger x divInt64#, modInt64# :: Int64# -> Int64# -> Int64# @@ -685,35 +660,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#)) diff --git a/GHC/Word.hs b/GHC/Word.hs index 2e33310..18ef440 100644 --- a/GHC/Word.hs +++ b/GHC/Word.hs @@ -27,6 +27,13 @@ module GHC.Word ( 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 @@ -470,30 +477,6 @@ instance Bits Word32 where bitSize _ = 32 isSigned _ = False -foreign import unsafe "stg_eqWord32" eqWord32# :: Word32# -> Word32# -> Bool -foreign import unsafe "stg_neWord32" neWord32# :: Word32# -> Word32# -> Bool -foreign import unsafe "stg_ltWord32" ltWord32# :: Word32# -> Word32# -> Bool -foreign import unsafe "stg_leWord32" leWord32# :: Word32# -> Word32# -> Bool -foreign import unsafe "stg_gtWord32" gtWord32# :: Word32# -> Word32# -> Bool -foreign import unsafe "stg_geWord32" geWord32# :: Word32# -> Word32# -> Bool -foreign import unsafe "stg_int32ToWord32" int32ToWord32# :: Int32# -> Word32# -foreign import unsafe "stg_word32ToInt32" word32ToInt32# :: Word32# -> Int32# -foreign import unsafe "stg_intToInt32" intToInt32# :: Int# -> Int32# -foreign import unsafe "stg_wordToWord32" wordToWord32# :: Word# -> Word32# -foreign import unsafe "stg_word32ToWord" word32ToWord# :: Word32# -> Word# -foreign import unsafe "stg_plusInt32" plusInt32# :: Int32# -> Int32# -> Int32# -foreign import unsafe "stg_minusInt32" minusInt32# :: Int32# -> Int32# -> Int32# -foreign import unsafe "stg_timesInt32" timesInt32# :: Int32# -> Int32# -> Int32# -foreign import unsafe "stg_negateInt32" negateInt32# :: Int32# -> Int32# -foreign import unsafe "stg_quotWord32" quotWord32# :: Word32# -> Word32# -> Word32# -foreign import unsafe "stg_remWord32" remWord32# :: Word32# -> Word32# -> Word32# -foreign import unsafe "stg_and32" and32# :: Word32# -> Word32# -> Word32# -foreign import unsafe "stg_or32" or32# :: Word32# -> Word32# -> Word32# -foreign import unsafe "stg_xor32" xor32# :: Word32# -> Word32# -> Word32# -foreign import unsafe "stg_not32" not32# :: Word32# -> Word32# -foreign import unsafe "stg_shiftL32" shiftL32# :: Word32# -> Int# -> Word32# -foreign import unsafe "stg_shiftRL32" shiftRL32# :: Word32# -> Int# -> Word32# - {-# RULES "fromIntegral/Int->Word32" fromIntegral = \(I# x#) -> W32# (int32ToWord32# (intToInt32# x#)) "fromIntegral/Word->Word32" fromIntegral = \(W# x#) -> W32# (wordToWord32# x#) @@ -574,7 +557,7 @@ instance Integral Word32 where toInteger (W32# x#) #if WORD_SIZE_IN_BITS == 32 | i# >=# 0# = smallInteger i# - | otherwise = word2Integer x# + | otherwise = wordToInteger x# where i# = word2Int# x# #else @@ -665,8 +648,7 @@ instance Num Word64 where abs x = x signum 0 = 0 signum _ = 1 - fromInteger (S# i#) = W64# (int64ToWord64# (intToInt64# i#)) - fromInteger (J# s# d#) = W64# (integerToWord64# s# d#) + fromInteger i = W64# (integerToWord64 i) instance Enum Word64 where succ x @@ -706,9 +688,7 @@ instance Integral Word64 where divMod x@(W64# x#) y@(W64# y#) | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#)) | otherwise = divZeroError - toInteger x@(W64# x#) - | x <= 0x7FFFFFFF = S# (word2Int# (word64ToWord# x#)) - | otherwise = case word64ToInteger# x# of (# s, d #) -> J# s d + toInteger (W64# x#) = word64ToInteger x# instance Bits Word64 where {-# INLINE shift #-} @@ -742,34 +722,6 @@ a `shiftL64#` b | b >=# 64# = wordToWord64# (int2Word# 0#) a `shiftRL64#` b | b >=# 64# = wordToWord64# (int2Word# 0#) | otherwise = a `uncheckedShiftRL64#` b - -foreign import ccall unsafe "hs_eqWord64" eqWord64# :: Word64# -> Word64# -> Bool -foreign import ccall unsafe "hs_neWord64" neWord64# :: Word64# -> Word64# -> Bool -foreign import ccall unsafe "hs_ltWord64" ltWord64# :: Word64# -> Word64# -> Bool -foreign import ccall unsafe "hs_leWord64" leWord64# :: Word64# -> Word64# -> Bool -foreign import ccall unsafe "hs_gtWord64" gtWord64# :: Word64# -> Word64# -> Bool -foreign import ccall unsafe "hs_geWord64" geWord64# :: Word64# -> Word64# -> Bool -foreign import ccall unsafe "hs_int64ToWord64" int64ToWord64# :: Int64# -> Word64# -foreign import ccall unsafe "hs_word64ToInt64" word64ToInt64# :: Word64# -> Int64# -foreign import ccall unsafe "hs_intToInt64" intToInt64# :: Int# -> Int64# -foreign import ccall unsafe "hs_wordToWord64" wordToWord64# :: Word# -> Word64# -foreign import ccall unsafe "hs_word64ToWord" word64ToWord# :: Word64# -> Word# -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_quotWord64" quotWord64# :: Word64# -> Word64# -> Word64# -foreign import ccall unsafe "hs_remWord64" remWord64# :: Word64# -> Word64# -> Word64# -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_integerToWord64" integerToWord64# :: Int# -> ByteArray# -> Word64# - - {-# RULES "fromIntegral/Int->Word64" fromIntegral = \(I# x#) -> W64# (int64ToWord64# (intToInt64# x#)) "fromIntegral/Word->Word64" fromIntegral = \(W# x#) -> W64# (wordToWord64# x#) diff --git a/base.cabal b/base.cabal index 31c7c17..6d218fa 100644 --- a/base.cabal +++ b/base.cabal @@ -160,7 +160,6 @@ Library { cbits/consUtils.c cbits/dirUtils.c cbits/inputReady.c - cbits/longlong.c cbits/selectUtils.c include-dirs: include includes: HsBase.h diff --git a/cbits/longlong.c b/cbits/longlong.c deleted file mode 100644 index c814773..0000000 --- a/cbits/longlong.c +++ /dev/null @@ -1,129 +0,0 @@ -/* ----------------------------------------------------------------------------- - * $Id: longlong.c,v 1.4 2002/12/13 14:23:42 simonmar Exp $ - * - * (c) The GHC Team, 1998-1999 - * - * Primitive operations over (64-bit) long longs - * (only used on 32-bit platforms.) - * - * ---------------------------------------------------------------------------*/ - - -/* -Miscellaneous primitive operations on HsInt64 and HsWord64s. -N.B. These are not primops! - -Instead of going the normal (boring) route of making the list -of primitive operations even longer to cope with operations -over 64-bit entities, we implement them instead 'out-of-line'. - -The primitive ops get their own routine (in C) that implements -the operation, requiring the caller to _ccall_ out. This has -performance implications of course, but we currently don't -expect intensive use of either Int64 or Word64 types. - -The exceptions to the rule are primops that cast to and from -64-bit entities (these are defined in PrimOps.h) -*/ - -#include "Rts.h" - -#ifdef SUPPORT_LONG_LONGS - -/* Relational operators */ - -static inline HsBool mkBool(int b) { return b ? HS_BOOL_TRUE : HS_BOOL_FALSE; } - -HsBool hs_gtWord64 (HsWord64 a, HsWord64 b) {return mkBool(a > b);} -HsBool hs_geWord64 (HsWord64 a, HsWord64 b) {return mkBool(a >= b);} -HsBool hs_eqWord64 (HsWord64 a, HsWord64 b) {return mkBool(a == b);} -HsBool hs_neWord64 (HsWord64 a, HsWord64 b) {return mkBool(a != b);} -HsBool hs_ltWord64 (HsWord64 a, HsWord64 b) {return mkBool(a < b);} -HsBool hs_leWord64 (HsWord64 a, HsWord64 b) {return mkBool(a <= b);} - -HsBool hs_gtInt64 (HsInt64 a, HsInt64 b) {return mkBool(a > b);} -HsBool hs_geInt64 (HsInt64 a, HsInt64 b) {return mkBool(a >= b);} -HsBool hs_eqInt64 (HsInt64 a, HsInt64 b) {return mkBool(a == b);} -HsBool hs_neInt64 (HsInt64 a, HsInt64 b) {return mkBool(a != b);} -HsBool hs_ltInt64 (HsInt64 a, HsInt64 b) {return mkBool(a < b);} -HsBool hs_leInt64 (HsInt64 a, HsInt64 b) {return mkBool(a <= b);} - -/* Arithmetic operators */ - -HsWord64 hs_remWord64 (HsWord64 a, HsWord64 b) {return a % b;} -HsWord64 hs_quotWord64 (HsWord64 a, HsWord64 b) {return a / b;} - -HsInt64 hs_remInt64 (HsInt64 a, HsInt64 b) {return a % b;} -HsInt64 hs_quotInt64 (HsInt64 a, HsInt64 b) {return a / b;} -HsInt64 hs_negateInt64 (HsInt64 a) {return -a;} -HsInt64 hs_plusInt64 (HsInt64 a, HsInt64 b) {return a + b;} -HsInt64 hs_minusInt64 (HsInt64 a, HsInt64 b) {return a - b;} -HsInt64 hs_timesInt64 (HsInt64 a, HsInt64 b) {return a * b;} - -/* Logical operators: */ - -HsWord64 hs_and64 (HsWord64 a, HsWord64 b) {return a & b;} -HsWord64 hs_or64 (HsWord64 a, HsWord64 b) {return a | b;} -HsWord64 hs_xor64 (HsWord64 a, HsWord64 b) {return a ^ b;} -HsWord64 hs_not64 (HsWord64 a) {return ~a;} - -HsWord64 hs_uncheckedShiftL64 (HsWord64 a, HsInt b) {return a << b;} -HsWord64 hs_uncheckedShiftRL64 (HsWord64 a, HsInt b) {return a >> b;} -/* Right shifting of signed quantities is not portable in C, so - the behaviour you'll get from using these primops depends - on the whatever your C compiler is doing. ToDo: fix. -- sof 8/98 -*/ -HsInt64 hs_uncheckedIShiftL64 (HsInt64 a, HsInt b) {return a << b;} -HsInt64 hs_uncheckedIShiftRA64 (HsInt64 a, HsInt b) {return a >> b;} -HsInt64 hs_uncheckedIShiftRL64 (HsInt64 a, HsInt b) - {return (HsInt64) ((HsWord64) a >> b);} - -/* Casting between longs and longer longs. - (the primops that cast from long longs to Integers - expressed as macros, since these may cause some heap allocation). -*/ - -HsInt64 hs_intToInt64 (HsInt i) {return (HsInt64) i;} -HsInt hs_int64ToInt (HsInt64 i) {return (HsInt) i;} -HsWord64 hs_int64ToWord64 (HsInt64 i) {return (HsWord64) i;} -HsWord64 hs_wordToWord64 (HsWord w) {return (HsWord64) w;} -HsWord hs_word64ToWord (HsWord64 w) {return (HsWord) w;} -HsInt64 hs_word64ToInt64 (HsWord64 w) {return (HsInt64) w;} - -HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da) -{ - mp_limb_t* d; - HsInt s; - HsWord64 res; - d = (mp_limb_t *)da; - s = sa; - switch (s) { - case 0: res = 0; break; - case 1: res = d[0]; break; - case -1: res = -(HsWord64)d[0]; break; - default: - res = (HsWord64)d[0] + ((HsWord64)d[1] << (BITS_IN (mp_limb_t))); - if (s < 0) res = -res; - } - return res; -} - -HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da) -{ - mp_limb_t* d; - HsInt s; - HsInt64 res; - d = (mp_limb_t *)da; - s = (sa); - switch (s) { - case 0: res = 0; break; - case 1: res = d[0]; break; - case -1: res = -(HsInt64)d[0]; break; - default: - res = (HsInt64)d[0] + ((HsWord64)d[1] << (BITS_IN (mp_limb_t))); - if (s < 0) res = -res; - } - return res; -} - -#endif /* SUPPORT_LONG_LONGS */