diff options
author | apt <unknown> | 2001-08-17 17:18:54 +0000 |
---|---|---|
committer | apt <unknown> | 2001-08-17 17:18:54 +0000 |
commit | 1dfaee318171836b32f6b33a14231c69adfdef2f (patch) | |
tree | 5a130da45e21740751393ca2dc3bef8ab14db3a2 /ghc/lib/std | |
parent | d30f8fc14ae1fb699a4b4d2e4bbb03fbc7f88d04 (diff) | |
download | haskell-1dfaee318171836b32f6b33a14231c69adfdef2f.tar.gz |
[project @ 2001-08-17 17:18:51 by apt]
How I spent my summer vacation.
Primops
-------
The format of the primops.txt.pp file has been enhanced to allow
(latex-style) primop descriptions to be included. There is a new flag
to genprimopcode that generates documentation including these
descriptions. A first cut at descriptions of the more interesting
primops has been made, and the file has been reordered a bit.
31-bit words
------------
The front end now can cope with the possibility of 31-bit (or even 30-bit)
Int# and Word# types. The only current use of this is to generate
external .core files that can be translated into OCAML source files
(OCAML uses a one-bit tag to distinguish integers from pointers).
The only way to get this right now is by hand-defining the preprocessor
symbol WORD_SIZE_IN_BITS, which is normally set automatically from
the familiar WORD_SIZE_IN_BYTES.
Just in case 31-bit words are used, we now have Int32# and Word32# primitive types
and an associated family of operators, paralleling the existing 64-bit
stuff. Of course, none of the operators actually need to be implemented
in the absence of a 31-bit backend.
There has also been some minor re-jigging of the 32 vs. 64 bit stuff.
See the description at the top of primops.txt.pp file for more details.
Note that, for the first time, the *type* of a primop can now depend
on the target word size.
Also, the family of primops intToInt8#, intToInt16#, etc.
have been renamed narrow8Int#, narrow16Int#, etc., to emphasize
that they work on Int#'s and don't actually convert between types.
Addresses
---------
As another part of coping with the possibility of 31-bit ints,
the addr2Int# and int2Addr# primops are now thoroughly deprecated
(and not even defined in the 31-bit case) and all uses
of them have been removed except from the (deprecated) module
hslibs/lang/Addr
Addr# should now be treated as a proper abstract type, and has these suitable operators:
nullAddr# : Int# -> Addr# (ignores its argument; nullary primops cause problems at various places)
plusAddr# : Addr# -> Int# -> Addr#
minusAddr : Addr# -> Addr# -> Int#
remAddr# : Addr# -> Int# -> Int#
Obviously, these don't allow completely arbitrary offsets if 31-bit ints are
in use, but they should do for all practical purposes.
It is also still possible to generate an address constant, and there is a built-in rule
that makes use of this to remove the nullAddr# calls.
Misc
----
There is a new compile flag -fno-code that causes GHC to quit after generating .hi files
and .core files (if requested) but before generating STG.
Z-encoded names for tuples have been rationalized; e.g.,
Z3H now means an unboxed 3-tuple, rather than an unboxed
tuple with 3 commas (i.e., a 4-tuple)!
Removed misc. litlits in hslibs/lang
Misc. small changes to external core format. The external core description
has also been substantially updated, and incorporates the automatically-generated
primop documentation; its in the repository at /papers/ext-core/core.tex.
A little make-system addition to allow passing CPP options to compiler and
library builds.
Diffstat (limited to 'ghc/lib/std')
-rw-r--r-- | ghc/lib/std/Makefile | 3 | ||||
-rw-r--r-- | ghc/lib/std/PrelBase.lhs | 17 | ||||
-rw-r--r-- | ghc/lib/std/PrelBits.lhs | 15 | ||||
-rw-r--r-- | ghc/lib/std/PrelEnum.lhs | 5 | ||||
-rw-r--r-- | ghc/lib/std/PrelGHC.hi-boot | 1506 | ||||
-rw-r--r-- | ghc/lib/std/PrelGHC.hi-boot.pp | 70 | ||||
-rw-r--r-- | ghc/lib/std/PrelInt.lhs | 303 | ||||
-rw-r--r-- | ghc/lib/std/PrelPtr.lhs | 16 | ||||
-rw-r--r-- | ghc/lib/std/PrelStorable.lhs | 46 | ||||
-rw-r--r-- | ghc/lib/std/PrelWord.lhs | 272 |
10 files changed, 2000 insertions, 253 deletions
diff --git a/ghc/lib/std/Makefile b/ghc/lib/std/Makefile index 783aabedc4..24c9afe199 100644 --- a/ghc/lib/std/Makefile +++ b/ghc/lib/std/Makefile @@ -65,7 +65,8 @@ SRC_MKDEPENDHS_OPTS += -I$(GHC_INCLUDE_DIR) #----------------------------------------------------------------------------- # Pre-processing (.pp) files -SRC_CPP_OPTS += -I$(GHC_INCLUDE_DIR) +SRC_CPP_OPTS += -I$(GHC_INCLUDE_DIR) -traditional +SRC_CPP_OPTS += ${GhcLibCppOpts} #----------------------------------------------------------------------------- # Rules diff --git a/ghc/lib/std/PrelBase.lhs b/ghc/lib/std/PrelBase.lhs index 2208f7fb84..4230561eb6 100644 --- a/ghc/lib/std/PrelBase.lhs +++ b/ghc/lib/std/PrelBase.lhs @@ -1,5 +1,5 @@ % ----------------------------------------------------------------------------- -% $Id: PrelBase.lhs,v 1.50 2001/05/03 19:03:27 qrczak Exp $ +% $Id: PrelBase.lhs,v 1.51 2001/08/17 17:18:54 apt Exp $ % % (c) The University of Glasgow, 1992-2000 % @@ -465,10 +465,15 @@ zeroInt, oneInt, twoInt, maxInt, minInt :: Int zeroInt = I# 0# oneInt = I# 1# twoInt = I# 2# -#if WORD_SIZE_IN_BYTES == 4 + +{- Seems clumsy. Should perhaps put minInt and MaxInt directly into MachDeps.h -} +#if WORD_SIZE_IN_BITS == 31 +minInt = I# (-0x40000000#) +maxInt = I# 0x3FFFFFFF# +#elif WORD_SIZE_IN_BITS == 32 minInt = I# (-0x80000000#) maxInt = I# 0x7FFFFFFF# -#else +#else minInt = I# (-0x8000000000000000#) maxInt = I# 0x7FFFFFFFFFFFFFFF# #endif @@ -657,10 +662,10 @@ gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool "x# <=# x#" forall x#. x# <=# x# = True #-} -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS == 32 {-# RULES -"intToInt32#" forall x#. intToInt32# x# = x# -"wordToWord32#" forall x#. wordToWord32# x# = x# +"narrow32Int#" forall x#. narrow32Int# x# = x# +"narrow32Word#" forall x#. narrow32Word# x# = x# #-} #endif diff --git a/ghc/lib/std/PrelBits.lhs b/ghc/lib/std/PrelBits.lhs index d8a8ffd1fe..68b496fc28 100644 --- a/ghc/lib/std/PrelBits.lhs +++ b/ghc/lib/std/PrelBits.lhs @@ -64,19 +64,12 @@ instance Bits Int where | i# >=# 0# = I# (x# `iShiftL#` i#) | otherwise = I# (x# `iShiftRA#` negateInt# i#) (I# x#) `rotate` (I# i#) = -#if WORD_SIZE_IN_BYTES == 4 I# (word2Int# ((x'# `shiftL#` i'#) `or#` - (x'# `shiftRL#` (32# -# i'#)))) + (x'# `shiftRL#` (wsib -# i'#)))) where x'# = int2Word# x# - i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) -#else - I# (word2Int# ((x'# `shiftL#` i'#) `or#` - (x'# `shiftRL#` (64# -# i'#)))) - where - x'# = int2Word# x# - i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) -#endif - bitSize _ = WORD_SIZE_IN_BYTES * 8 + i'# = word2Int# (int2Word# i# `and#` int2Word# (wsib -# 1#)) + wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -} + bitSize _ = WORD_SIZE_IN_BITS isSigned _ = True \end{code} diff --git a/ghc/lib/std/PrelEnum.lhs b/ghc/lib/std/PrelEnum.lhs index c0874a3009..882d69a037 100644 --- a/ghc/lib/std/PrelEnum.lhs +++ b/ghc/lib/std/PrelEnum.lhs @@ -1,5 +1,5 @@ % ----------------------------------------------------------------------------- -% $Id: PrelEnum.lhs,v 1.14 2001/07/24 06:31:35 ken Exp $ +% $Id: PrelEnum.lhs,v 1.15 2001/08/17 17:18:54 apt Exp $ % % (c) The University of Glasgow, 1992-2000 % @@ -314,7 +314,8 @@ instance Enum Int where fromEnum x = x {-# INLINE enumFrom #-} - enumFrom (I# x) = case maxInt of I# y -> eftInt x y + enumFrom (I# x) = eftInt x maxInt# + where I# maxInt# = maxInt -- Blarg: technically I guess enumFrom isn't strict! {-# INLINE enumFromTo #-} diff --git a/ghc/lib/std/PrelGHC.hi-boot b/ghc/lib/std/PrelGHC.hi-boot new file mode 100644 index 0000000000..efedce4445 --- /dev/null +++ b/ghc/lib/std/PrelGHC.hi-boot @@ -0,0 +1,1506 @@ +--------------------------------------------------------------------------- +-- PrelGHC.hi-boot +-- +-- This hand-written interface file allows you to bring into scope the +-- primitive operations and types that GHC knows about. +--------------------------------------------------------------------------- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +__interface "std" PrelGHC 1 0 where + +__export PrelGHC + + ZLzmzgZR -- (->) + + CCallable + CReturnable + +-- Magical assert thingy + assert + + -- constructor tags + tagToEnumzh + getTagzh + dataToTagzh + + -- I/O primitives + RealWorld + realWorldzh + Statezh + + -- Concurrency primitives + ThreadIdzh + myThreadIdzh + forkzh + yieldzh + killThreadzh + blockAsyncExceptionszh + unblockAsyncExceptionszh + delayzh + waitReadzh + waitWritezh + + -- MVars + MVarzh + sameMVarzh + newMVarzh + takeMVarzh + putMVarzh + tryTakeMVarzh + tryPutMVarzh + isEmptyMVarzh + + -- Parallel + seqzh + parzh + parGlobalzh + parLocalzh + parAtzh + parAtAbszh + parAtRelzh + parAtForNowzh + + -- Character Type + Charzh + gtCharzh + geCharzh + eqCharzh + neCharzh + ltCharzh + leCharzh + ordzh + chrzh + + -- Int Type + Intzh + zgzh + zgzezh + zezezh + zszezh + zlzh + zlzezh + zpzh + zmzh + ztzh + quotIntzh + remIntzh + gcdIntzh + negateIntzh + iShiftLzh + iShiftRAzh + iShiftRLzh + addIntCzh + subIntCzh + mulIntCzh + + Wordzh + gtWordzh + geWordzh + eqWordzh + neWordzh + ltWordzh + leWordzh + plusWordzh + minusWordzh + timesWordzh + quotWordzh + remWordzh + andzh + orzh + notzh + xorzh + shiftLzh + shiftRLzh + int2Wordzh + word2Intzh + + narrow8Intzh + narrow16Intzh + narrow32Intzh + narrow8Wordzh + narrow16Wordzh + narrow32Wordzh + + + + + + + + Int64zh + Word64zh + + + Addrzh + nullAddrzh + plusAddrzh + minusAddrzh + remAddrzh + + addr2Intzh + int2Addrzh + + gtAddrzh + geAddrzh + eqAddrzh + neAddrzh + ltAddrzh + leAddrzh + + Floatzh + gtFloatzh + geFloatzh + eqFloatzh + neFloatzh + ltFloatzh + leFloatzh + plusFloatzh + minusFloatzh + timesFloatzh + divideFloatzh + negateFloatzh + float2Intzh + int2Floatzh + expFloatzh + logFloatzh + sqrtFloatzh + sinFloatzh + cosFloatzh + tanFloatzh + asinFloatzh + acosFloatzh + atanFloatzh + sinhFloatzh + coshFloatzh + tanhFloatzh + powerFloatzh + decodeFloatzh + + Doublezh + zgzhzh + zgzezhzh + zezezhzh + zszezhzh + zlzhzh + zlzezhzh + zpzhzh + zmzhzh + ztzhzh + zszhzh + negateDoublezh + double2Intzh + int2Doublezh + double2Floatzh + float2Doublezh + expDoublezh + logDoublezh + sqrtDoublezh + sinDoublezh + cosDoublezh + tanDoublezh + asinDoublezh + acosDoublezh + atanDoublezh + sinhDoublezh + coshDoublezh + tanhDoublezh + ztztzhzh + decodeDoublezh + + cmpIntegerzh + cmpIntegerIntzh + plusIntegerzh + minusIntegerzh + timesIntegerzh + gcdIntegerzh + quotIntegerzh + remIntegerzh + gcdIntegerzh + gcdIntegerIntzh + divExactIntegerzh + quotRemIntegerzh + divModIntegerzh + integer2Intzh + integer2Wordzh + int2Integerzh + word2Integerzh + + + + + + + + integerToInt64zh + integerToWord64zh + int64ToIntegerzh + word64ToIntegerzh + + andIntegerzh + orIntegerzh + xorIntegerzh + complementIntegerzh + + Arrayzh + ByteArrayzh + MutableArrayzh + MutableByteArrayzh + sameMutableArrayzh + sameMutableByteArrayzh + newArrayzh + newByteArrayzh + newPinnedByteArrayzh + byteArrayContentszh + + indexArrayzh + indexCharArrayzh + indexWideCharArrayzh + indexIntArrayzh + indexWordArrayzh + indexAddrArrayzh + indexFloatArrayzh + indexDoubleArrayzh + indexStablePtrArrayzh + indexInt8Arrayzh + indexInt16Arrayzh + indexInt32Arrayzh + indexInt64Arrayzh + indexWord8Arrayzh + indexWord16Arrayzh + indexWord32Arrayzh + indexWord64Arrayzh + + readArrayzh + readCharArrayzh + readWideCharArrayzh + readIntArrayzh + readWordArrayzh + readAddrArrayzh + readFloatArrayzh + readDoubleArrayzh + readStablePtrArrayzh + readInt8Arrayzh + readInt16Arrayzh + readInt32Arrayzh + readInt64Arrayzh + readWord8Arrayzh + readWord16Arrayzh + readWord32Arrayzh + readWord64Arrayzh + + writeArrayzh + writeCharArrayzh + writeWideCharArrayzh + writeIntArrayzh + writeWordArrayzh + writeAddrArrayzh + writeFloatArrayzh + writeDoubleArrayzh + writeStablePtrArrayzh + writeInt8Arrayzh + writeInt16Arrayzh + writeInt32Arrayzh + writeInt64Arrayzh + writeWord8Arrayzh + writeWord16Arrayzh + writeWord32Arrayzh + writeWord64Arrayzh + + indexCharOffAddrzh + indexWideCharOffAddrzh + indexIntOffAddrzh + indexWordOffAddrzh + indexAddrOffAddrzh + indexFloatOffAddrzh + indexDoubleOffAddrzh + indexStablePtrOffAddrzh + indexInt8OffAddrzh + indexInt16OffAddrzh + indexInt32OffAddrzh + indexInt64OffAddrzh + indexWord8OffAddrzh + indexWord16OffAddrzh + indexWord32OffAddrzh + indexWord64OffAddrzh + + readCharOffAddrzh + readWideCharOffAddrzh + readIntOffAddrzh + readWordOffAddrzh + readAddrOffAddrzh + readFloatOffAddrzh + readDoubleOffAddrzh + readStablePtrOffAddrzh + readInt8OffAddrzh + readInt16OffAddrzh + readInt32OffAddrzh + readInt64OffAddrzh + readWord8OffAddrzh + readWord16OffAddrzh + readWord32OffAddrzh + readWord64OffAddrzh + + writeCharOffAddrzh + writeWideCharOffAddrzh + writeIntOffAddrzh + writeWordOffAddrzh + writeAddrOffAddrzh + writeForeignObjOffAddrzh + writeFloatOffAddrzh + writeDoubleOffAddrzh + writeStablePtrOffAddrzh + writeInt8OffAddrzh + writeInt16OffAddrzh + writeInt32OffAddrzh + writeInt64OffAddrzh + writeWord8OffAddrzh + writeWord16OffAddrzh + writeWord32OffAddrzh + writeWord64OffAddrzh + + eqForeignObjzh + indexCharOffForeignObjzh + indexWideCharOffForeignObjzh + indexIntOffForeignObjzh + indexWordOffForeignObjzh + indexAddrOffForeignObjzh + indexFloatOffForeignObjzh + indexDoubleOffForeignObjzh + indexStablePtrOffForeignObjzh + indexInt8OffForeignObjzh + indexInt16OffForeignObjzh + indexInt32OffForeignObjzh + indexInt64OffForeignObjzh + indexWord8OffForeignObjzh + indexWord16OffForeignObjzh + indexWord32OffForeignObjzh + indexWord64OffForeignObjzh + + unsafeFreezzeArrayzh -- Note zz in the middle + unsafeFreezzeByteArrayzh -- Ditto + + unsafeThawArrayzh + + sizzeofByteArrayzh -- Ditto + sizzeofMutableByteArrayzh -- Ditto + + MutVarzh + newMutVarzh + readMutVarzh + writeMutVarzh + sameMutVarzh + + catchzh + raisezh + + Weakzh + mkWeakzh + deRefWeakzh + finalizzeWeakzh + + ForeignObjzh + mkForeignObjzh + writeForeignObjzh + foreignObjToAddrzh + touchzh + + StablePtrzh + makeStablePtrzh + deRefStablePtrzh + eqStablePtrzh + + StableNamezh + makeStableNamezh + eqStableNamezh + stableNameToIntzh + + reallyUnsafePtrEqualityzh + + newBCOzh + BCOzh + mkApUpd0zh + + unsafeCoercezh + addrToHValuezh +; + +-- Export PrelErr.error, so that others do not have to import PrelErr +__export PrelErr error ; + + +-------------------------------------------------- +instance {CCallable Charzh} = zdfCCallableCharzh; +instance {CCallable Doublezh} = zdfCCallableDoublezh; +instance {CCallable Floatzh} = zdfCCallableFloatzh; +instance {CCallable Intzh} = zdfCCallableIntzh; +instance {CCallable Addrzh} = zdfCCallableAddrzh; +instance {CCallable Int64zh} = zdfCCallableInt64zh; +instance {CCallable Word64zh} = zdfCCallableWord64zh; +instance {CCallable Wordzh} = zdfCCallableWordzh; +instance {CCallable ByteArrayzh} = zdfCCallableByteArrayzh; +instance __forall s => {CCallable (MutableByteArrayzh s)} = zdfCCallableMutableByteArrayzh; +instance {CCallable ForeignObjzh} = zdfCCallableForeignObjzh; +instance __forall s => {CCallable (StablePtrzh s)} = zdfCCallableStablePtrzh; +-- CCallable and CReturnable have kind (Type AnyBox) so that +-- things like Int# can be instances of CCallable. +1 class CCallable a :: ? ; +1 class CReturnable a :: ? ; + +1 assert :: __forall a => PrelBase.Bool -> a -> a ; + +-- These guys do not really exist: +-- +1 zdfCCallableCharzh :: {CCallable Charzh} ; +1 zdfCCallableDoublezh :: {CCallable Doublezh} ; +1 zdfCCallableFloatzh :: {CCallable Floatzh} ; +1 zdfCCallableIntzh :: {CCallable Intzh} ; +1 zdfCCallableAddrzh :: {CCallable Addrzh} ; +1 zdfCCallableInt64zh :: {CCallable Int64zh} ; +1 zdfCCallableWord64zh :: {CCallable Word64zh} ; +1 zdfCCallableWordzh :: {CCallable Wordzh} ; +1 zdfCCallableByteArrayzh :: {CCallable ByteArrayzh} ; +1 zdfCCallableMutableByteArrayzh :: __forall s => {CCallable (MutableByteArrayzh s)} ; +1 zdfCCallableForeignObjzh :: {CCallable ForeignObjzh} ; +1 zdfCCallableStablePtrzh :: __forall a => {CCallable (StablePtrzh a)} ; diff --git a/ghc/lib/std/PrelGHC.hi-boot.pp b/ghc/lib/std/PrelGHC.hi-boot.pp index 3dbacc3085..5880ec1a8c 100644 --- a/ghc/lib/std/PrelGHC.hi-boot.pp +++ b/ghc/lib/std/PrelGHC.hi-boot.pp @@ -5,8 +5,7 @@ -- primitive operations and types that GHC knows about. --------------------------------------------------------------------------- -#include "config.h" -#include "Derived.h" +#include "MachDeps.h" __interface "std" PrelGHC 1 0 where @@ -116,25 +115,38 @@ __export PrelGHC int2Wordzh word2Intzh + narrow8Intzh + narrow16Intzh + narrow32Intzh + narrow8Wordzh + narrow16Wordzh + narrow32Wordzh + +#if WORD_SIZE_IN_BITS < 32 + Int32zh + Word32zh +#endif + +#if WORD_SIZE_IN_BITS < 64 Int64zh Word64zh - - intToInt8zh - intToInt16zh - intToInt32zh - wordToWord8zh - wordToWord16zh - wordToWord32zh +#endif Addrzh + nullAddrzh + plusAddrzh + minusAddrzh + remAddrzh +#if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64) + addr2Intzh + int2Addrzh +#endif gtAddrzh geAddrzh eqAddrzh neAddrzh ltAddrzh leAddrzh - int2Addrzh - addr2Intzh Floatzh gtFloatzh @@ -213,7 +225,13 @@ __export PrelGHC integer2Wordzh int2Integerzh word2Integerzh -#ifdef SUPPORT_LONG_LONGS +#if WORD_SIZE_IN_BITS < 32 + integerToInt32zh + integerToWord32zh + int32ToIntegerzh + word32ToIntegerzh +#endif +#if WORD_SIZE_IN_BITS < 64 integerToInt64zh integerToWord64zh int64ToIntegerzh @@ -247,15 +265,11 @@ __export PrelGHC indexInt8Arrayzh indexInt16Arrayzh indexInt32Arrayzh -#ifdef SUPPORT_LONG_LONGS indexInt64Arrayzh -#endif indexWord8Arrayzh indexWord16Arrayzh indexWord32Arrayzh -#ifdef SUPPORT_LONG_LONGS indexWord64Arrayzh -#endif readArrayzh readCharArrayzh @@ -269,15 +283,11 @@ __export PrelGHC readInt8Arrayzh readInt16Arrayzh readInt32Arrayzh -#ifdef SUPPORT_LONG_LONGS readInt64Arrayzh -#endif readWord8Arrayzh readWord16Arrayzh readWord32Arrayzh -#ifdef SUPPORT_LONG_LONGS readWord64Arrayzh -#endif writeArrayzh writeCharArrayzh @@ -291,15 +301,11 @@ __export PrelGHC writeInt8Arrayzh writeInt16Arrayzh writeInt32Arrayzh -#ifdef SUPPORT_LONG_LONGS writeInt64Arrayzh -#endif writeWord8Arrayzh writeWord16Arrayzh writeWord32Arrayzh -#ifdef SUPPORT_LONG_LONGS writeWord64Arrayzh -#endif indexCharOffAddrzh indexWideCharOffAddrzh @@ -312,15 +318,11 @@ __export PrelGHC indexInt8OffAddrzh indexInt16OffAddrzh indexInt32OffAddrzh -#ifdef SUPPORT_LONG_LONGS indexInt64OffAddrzh -#endif indexWord8OffAddrzh indexWord16OffAddrzh indexWord32OffAddrzh -#ifdef SUPPORT_LONG_LONGS indexWord64OffAddrzh -#endif readCharOffAddrzh readWideCharOffAddrzh @@ -333,15 +335,11 @@ __export PrelGHC readInt8OffAddrzh readInt16OffAddrzh readInt32OffAddrzh -#ifdef SUPPORT_LONG_LONGS readInt64OffAddrzh -#endif readWord8OffAddrzh readWord16OffAddrzh readWord32OffAddrzh -#ifdef SUPPORT_LONG_LONGS readWord64OffAddrzh -#endif writeCharOffAddrzh writeWideCharOffAddrzh @@ -355,15 +353,11 @@ __export PrelGHC writeInt8OffAddrzh writeInt16OffAddrzh writeInt32OffAddrzh -#ifdef SUPPORT_LONG_LONGS writeInt64OffAddrzh -#endif writeWord8OffAddrzh writeWord16OffAddrzh writeWord32OffAddrzh -#ifdef SUPPORT_LONG_LONGS writeWord64OffAddrzh -#endif eqForeignObjzh indexCharOffForeignObjzh @@ -377,15 +371,11 @@ __export PrelGHC indexInt8OffForeignObjzh indexInt16OffForeignObjzh indexInt32OffForeignObjzh -#ifdef SUPPORT_LONG_LONGS indexInt64OffForeignObjzh -#endif indexWord8OffForeignObjzh indexWord16OffForeignObjzh indexWord32OffForeignObjzh -#ifdef SUPPORT_LONG_LONGS indexWord64OffForeignObjzh -#endif unsafeFreezzeArrayzh -- Note zz in the middle unsafeFreezzeByteArrayzh -- Ditto diff --git a/ghc/lib/std/PrelInt.lhs b/ghc/lib/std/PrelInt.lhs index bd292b0816..f5be4f4015 100644 --- a/ghc/lib/std/PrelInt.lhs +++ b/ghc/lib/std/PrelInt.lhs @@ -38,17 +38,17 @@ instance Show Int8 where showsPrec p x = showsPrec p (fromIntegral x :: Int) instance Num Int8 where - (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#)) - (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#)) - (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#)) - negate (I8# x#) = I8# (intToInt8# (negateInt# x#)) + (I8# x#) + (I8# y#) = I8# (narrow8Int# (x# +# y#)) + (I8# x#) - (I8# y#) = I8# (narrow8Int# (x# -# y#)) + (I8# x#) * (I8# y#) = I8# (narrow8Int# (x# *# y#)) + negate (I8# x#) = I8# (narrow8Int# (negateInt# x#)) abs x | x >= 0 = x | otherwise = negate x signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I8# (intToInt8# i#) - fromInteger (J# s# d#) = I8# (intToInt8# (integer2Int# s# d#)) + fromInteger (S# i#) = I8# (narrow8Int# i#) + fromInteger (J# s# d#) = I8# (narrow8Int# (integer2Int# s# d#)) instance Real Int8 where toRational x = toInteger x % 1 @@ -70,24 +70,24 @@ instance Enum Int8 where instance Integral Int8 where quot x@(I8# x#) y@(I8# y#) - | y /= 0 = I8# (intToInt8# (x# `quotInt#` y#)) + | y /= 0 = I8# (narrow8Int# (x# `quotInt#` y#)) | otherwise = divZeroError "quot{Int8}" x rem x@(I8# x#) y@(I8# y#) - | y /= 0 = I8# (intToInt8# (x# `remInt#` y#)) + | y /= 0 = I8# (narrow8Int# (x# `remInt#` y#)) | otherwise = divZeroError "rem{Int8}" x div x@(I8# x#) y@(I8# y#) - | y /= 0 = I8# (intToInt8# (x# `divInt#` y#)) + | y /= 0 = I8# (narrow8Int# (x# `divInt#` y#)) | otherwise = divZeroError "div{Int8}" x mod x@(I8# x#) y@(I8# y#) - | y /= 0 = I8# (intToInt8# (x# `modInt#` y#)) + | y /= 0 = I8# (narrow8Int# (x# `modInt#` y#)) | otherwise = divZeroError "mod{Int8}" x quotRem x@(I8# x#) y@(I8# y#) - | y /= 0 = (I8# (intToInt8# (x# `quotInt#` y#)), - I8# (intToInt8# (x# `remInt#` y#))) + | y /= 0 = (I8# (narrow8Int# (x# `quotInt#` y#)), + I8# (narrow8Int# (x# `remInt#` y#))) | otherwise = divZeroError "quotRem{Int8}" x divMod x@(I8# x#) y@(I8# y#) - | y /= 0 = (I8# (intToInt8# (x# `divInt#` y#)), - I8# (intToInt8# (x# `modInt#` y#))) + | y /= 0 = (I8# (narrow8Int# (x# `divInt#` y#)), + I8# (narrow8Int# (x# `modInt#` y#))) | otherwise = divZeroError "divMod{Int8}" x toInteger (I8# x#) = S# x# @@ -111,20 +111,20 @@ instance Bits Int8 where (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#)) complement (I8# x#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# (-1#))) (I8# x#) `shift` (I# i#) - | i# >=# 0# = I8# (intToInt8# (x# `iShiftL#` i#)) + | i# >=# 0# = I8# (narrow8Int# (x# `iShiftL#` i#)) | otherwise = I8# (x# `iShiftRA#` negateInt# i#) (I8# x#) `rotate` (I# i#) = - I8# (intToInt8# (word2Int# ((x'# `shiftL#` i'#) `or#` + I8# (narrow8Int# (word2Int# ((x'# `shiftL#` i'#) `or#` (x'# `shiftRL#` (8# -# i'#))))) where - x'# = wordToWord8# (int2Word# x#) + x'# = narrow8Word# (int2Word# x#) i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) bitSize _ = 8 isSigned _ = True {-# RULES "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8 -"fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (intToInt8# x#) +"fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#) "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#) #-} @@ -144,17 +144,17 @@ instance Show Int16 where showsPrec p x = showsPrec p (fromIntegral x :: Int) instance Num Int16 where - (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#)) - (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#)) - (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#)) - negate (I16# x#) = I16# (intToInt16# (negateInt# x#)) + (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#)) + (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#)) + (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#)) + negate (I16# x#) = I16# (narrow16Int# (negateInt# x#)) abs x | x >= 0 = x | otherwise = negate x signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I16# (intToInt16# i#) - fromInteger (J# s# d#) = I16# (intToInt16# (integer2Int# s# d#)) + fromInteger (S# i#) = I16# (narrow16Int# i#) + fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#)) instance Real Int16 where toRational x = toInteger x % 1 @@ -176,24 +176,24 @@ instance Enum Int16 where instance Integral Int16 where quot x@(I16# x#) y@(I16# y#) - | y /= 0 = I16# (intToInt16# (x# `quotInt#` y#)) + | y /= 0 = I16# (narrow16Int# (x# `quotInt#` y#)) | otherwise = divZeroError "quot{Int16}" x rem x@(I16# x#) y@(I16# y#) - | y /= 0 = I16# (intToInt16# (x# `remInt#` y#)) + | y /= 0 = I16# (narrow16Int# (x# `remInt#` y#)) | otherwise = divZeroError "rem{Int16}" x div x@(I16# x#) y@(I16# y#) - | y /= 0 = I16# (intToInt16# (x# `divInt#` y#)) + | y /= 0 = I16# (narrow16Int# (x# `divInt#` y#)) | otherwise = divZeroError "div{Int16}" x mod x@(I16# x#) y@(I16# y#) - | y /= 0 = I16# (intToInt16# (x# `modInt#` y#)) + | y /= 0 = I16# (narrow16Int# (x# `modInt#` y#)) | otherwise = divZeroError "mod{Int16}" x quotRem x@(I16# x#) y@(I16# y#) - | y /= 0 = (I16# (intToInt16# (x# `quotInt#` y#)), - I16# (intToInt16# (x# `remInt#` y#))) + | y /= 0 = (I16# (narrow16Int# (x# `quotInt#` y#)), + I16# (narrow16Int# (x# `remInt#` y#))) | otherwise = divZeroError "quotRem{Int16}" x divMod x@(I16# x#) y@(I16# y#) - | y /= 0 = (I16# (intToInt16# (x# `divInt#` y#)), - I16# (intToInt16# (x# `modInt#` y#))) + | y /= 0 = (I16# (narrow16Int# (x# `divInt#` y#)), + I16# (narrow16Int# (x# `modInt#` y#))) | otherwise = divZeroError "divMod{Int16}" x toInteger (I16# x#) = S# x# @@ -217,13 +217,13 @@ instance Bits Int16 where (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#)) complement (I16# x#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#))) (I16# x#) `shift` (I# i#) - | i# >=# 0# = I16# (intToInt16# (x# `iShiftL#` i#)) + | i# >=# 0# = I16# (narrow16Int# (x# `iShiftL#` i#)) | otherwise = I16# (x# `iShiftRA#` negateInt# i#) (I16# x#) `rotate` (I# i#) = - I16# (intToInt16# (word2Int# ((x'# `shiftL#` i'#) `or#` + I16# (narrow16Int# (word2Int# ((x'# `shiftL#` i'#) `or#` (x'# `shiftRL#` (16# -# i'#))))) where - x'# = wordToWord16# (int2Word# x#) + x'# = narrow16Word# (int2Word# x#) i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) bitSize _ = 16 isSigned _ = True @@ -232,7 +232,7 @@ instance Bits Int16 where "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#) "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x# "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16 -"fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (intToInt16# x#) +"fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#) "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#) #-} @@ -240,35 +240,173 @@ instance Bits Int16 where -- type Int32 ------------------------------------------------------------------------ +#if WORD_SIZE_IN_BITS < 32 + +data Int32 = I32# Int32# + +instance Eq Int32 where + (I32# x#) == (I32# y#) = x# `eqInt32#` y# + (I32# x#) /= (I32# y#) = x# `neInt32#` y# + +instance Ord Int32 where + (I32# x#) < (I32# y#) = x# `ltInt32#` y# + (I32# x#) <= (I32# y#) = x# `leInt32#` y# + (I32# x#) > (I32# y#) = x# `gtInt32#` y# + (I32# x#) >= (I32# y#) = x# `geInt32#` y# + +instance Show Int32 where + showsPrec p x = showsPrec p (toInteger x) + +instance Num Int32 where + (I32# x#) + (I32# y#) = I32# (x# `plusInt32#` y#) + (I32# x#) - (I32# y#) = I32# (x# `minusInt32#` y#) + (I32# x#) * (I32# y#) = I32# (x# `timesInt32#` y#) + negate (I32# x#) = I32# (negateInt32# x#) + abs x | x >= 0 = x + | otherwise = negate x + signum x | x > 0 = 1 + signum 0 = 0 + signum _ = -1 + fromInteger (S# i#) = I32# (intToInt32# i#) + fromInteger (J# s# d#) = I32# (integerToInt32# s# d#) + +instance Enum Int32 where + succ x + | x /= maxBound = x + 1 + | otherwise = succError "Int32" + pred x + | x /= minBound = x - 1 + | otherwise = predError "Int32" + toEnum (I# i#) = I32# (intToInt32# i#) + fromEnum x@(I32# x#) + | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int) + = I# (int32ToInt# x#) + | otherwise = fromEnumError "Int32" x + enumFrom = integralEnumFrom + enumFromThen = integralEnumFromThen + enumFromTo = integralEnumFromTo + enumFromThenTo = integralEnumFromThenTo + +instance Integral Int32 where + quot x@(I32# x#) y@(I32# y#) + | y /= 0 = I32# (x# `quotInt32#` y#) + | otherwise = divZeroError "quot{Int32}" x + rem x@(I32# x#) y@(I32# y#) + | y /= 0 = I32# (x# `remInt32#` y#) + | otherwise = divZeroError "rem{Int32}" x + div x@(I32# x#) y@(I32# y#) + | y /= 0 = I32# (x# `divInt32#` y#) + | otherwise = divZeroError "div{Int32}" x + mod x@(I32# x#) y@(I32# y#) + | y /= 0 = I32# (x# `modInt32#` y#) + | otherwise = divZeroError "mod{Int32}" x + quotRem x@(I32# x#) y@(I32# y#) + | y /= 0 = (I32# (x# `quotInt32#` y#), I32# (x# `remInt32#` y#)) + | otherwise = divZeroError "quotRem{Int32}" x + divMod x@(I32# x#) y@(I32# y#) + | y /= 0 = (I32# (x# `divInt32#` y#), I32# (x# `modInt32#` y#)) + | otherwise = divZeroError "divMod{Int32}" x + toInteger x@(I32# x#) + | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int) + = S# (int32ToInt# x#) + | otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d + +divInt32#, modInt32# :: Int32# -> Int32# -> Int32# +x# `divInt32#` y# + | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) + = ((x# `minusInt32#` y#) `minusInt32#` intToInt32# 1#) `quotInt32#` y# + | (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#) + = ((x# `minusInt32#` y#) `plusInt32#` intToInt32# 1#) `quotInt32#` y# + | otherwise = x# `quotInt32#` y# +x# `modInt32#` y# + | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) || + (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#) + = if r# `neInt32#` intToInt32# 0# then r# `plusInt32#` y# else intToInt32# 0# + | otherwise = r# + where + r# = x# `remInt32#` y# + +instance Read Int32 where + readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] + +instance Bits Int32 where + (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#)) + complement (I32# x#) = I32# (word32ToInt32# (not32# (int32ToWord32# x#))) + (I32# x#) `shift` (I# i#) + | i# >=# 0# = I32# (x# `iShiftL32#` i#) + | otherwise = I32# (x# `iShiftRA32#` negateInt# i#) + (I32# x#) `rotate` (I# i#) = + I32# (word32ToInt32# ((x'# `shiftL32#` i'#) `or32#` + (x'# `shiftRL32#` (32# -# i'#)))) + where + x'# = int32ToWord32# x# + i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) + 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#)) +"fromIntegral/Word32->Int32" fromIntegral = \(W32# x#) -> I32# (word32ToInt32# x#) +"fromIntegral/Int32->Int" fromIntegral = \(I32# x#) -> I# (int32ToInt# x#) +"fromIntegral/Int32->Word" fromIntegral = \(I32# x#) -> W# (int2Word# (int32ToInt# x#)) +"fromIntegral/Int32->Word32" fromIntegral = \(I32# x#) -> W32# (int32ToWord32# x#) +"fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32 + #-} + +#else + -- Int32 is represented in the same way as Int. -#if WORD_SIZE_IN_BYTES == 8 +#if WORD_SIZE_IN_BITS > 32 -- Operations may assume and must ensure that it holds only values -- from its logical range. #endif data Int32 = I32# Int# deriving (Eq, Ord) -instance CCallable Int32 -instance CReturnable Int32 - instance Show Int32 where showsPrec p x = showsPrec p (fromIntegral x :: Int) instance Num Int32 where - (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#)) - (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#)) - (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#)) - negate (I32# x#) = I32# (intToInt32# (negateInt# x#)) + (I32# x#) + (I32# y#) = I32# (narrow32Int# (x# +# y#)) + (I32# x#) - (I32# y#) = I32# (narrow32Int# (x# -# y#)) + (I32# x#) * (I32# y#) = I32# (narrow32Int# (x# *# y#)) + negate (I32# x#) = I32# (narrow32Int# (negateInt# x#)) abs x | x >= 0 = x | otherwise = negate x signum x | x > 0 = 1 signum 0 = 0 signum _ = -1 - fromInteger (S# i#) = I32# (intToInt32# i#) - fromInteger (J# s# d#) = I32# (intToInt32# (integer2Int# s# d#)) - -instance Real Int32 where - toRational x = toInteger x % 1 + fromInteger (S# i#) = I32# (narrow32Int# i#) + fromInteger (J# s# d#) = I32# (narrow32Int# (integer2Int# s# d#)) instance Enum Int32 where succ x @@ -277,7 +415,7 @@ instance Enum Int32 where pred x | x /= minBound = x - 1 | otherwise = predError "Int32" -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS == 32 toEnum (I# i#) = I32# i# #else toEnum i@(I# i#) @@ -291,38 +429,27 @@ instance Enum Int32 where instance Integral Int32 where quot x@(I32# x#) y@(I32# y#) - | y /= 0 = I32# (intToInt32# (x# `quotInt#` y#)) + | y /= 0 = I32# (narrow32Int# (x# `quotInt#` y#)) | otherwise = divZeroError "quot{Int32}" x rem x@(I32# x#) y@(I32# y#) - | y /= 0 = I32# (intToInt32# (x# `remInt#` y#)) + | y /= 0 = I32# (narrow32Int# (x# `remInt#` y#)) | otherwise = divZeroError "rem{Int32}" x div x@(I32# x#) y@(I32# y#) - | y /= 0 = I32# (intToInt32# (x# `divInt#` y#)) + | y /= 0 = I32# (narrow32Int# (x# `divInt#` y#)) | otherwise = divZeroError "div{Int32}" x mod x@(I32# x#) y@(I32# y#) - | y /= 0 = I32# (intToInt32# (x# `modInt#` y#)) + | y /= 0 = I32# (narrow32Int# (x# `modInt#` y#)) | otherwise = divZeroError "mod{Int32}" x quotRem x@(I32# x#) y@(I32# y#) - | y /= 0 = (I32# (intToInt32# (x# `quotInt#` y#)), - I32# (intToInt32# (x# `remInt#` y#))) + | y /= 0 = (I32# (narrow32Int# (x# `quotInt#` y#)), + I32# (narrow32Int# (x# `remInt#` y#))) | otherwise = divZeroError "quotRem{Int32}" x divMod x@(I32# x#) y@(I32# y#) - | y /= 0 = (I32# (intToInt32# (x# `divInt#` y#)), - I32# (intToInt32# (x# `modInt#` y#))) + | y /= 0 = (I32# (narrow32Int# (x# `divInt#` y#)), + I32# (narrow32Int# (x# `modInt#` y#))) | otherwise = divZeroError "divMod{Int32}" x toInteger (I32# x#) = S# x# -instance Bounded Int32 where - minBound = -0x80000000 - maxBound = 0x7FFFFFFF - -instance Ix Int32 where - range (m,n) = [m..n] - index b@(m,_) i - | inRange b i = fromIntegral (i - m) - | otherwise = indexError b i "Int32" - inRange (m,n) i = m <= i && i <= n - instance Read Int32 where readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] @@ -332,13 +459,13 @@ instance Bits Int32 where (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#)) complement (I32# x#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#))) (I32# x#) `shift` (I# i#) - | i# >=# 0# = I32# (intToInt32# (x# `iShiftL#` i#)) + | i# >=# 0# = I32# (narrow32Int# (x# `iShiftL#` i#)) | otherwise = I32# (x# `iShiftRA#` negateInt# i#) (I32# x#) `rotate` (I# i#) = - I32# (intToInt32# (word2Int# ((x'# `shiftL#` i'#) `or#` + I32# (narrow32Int# (word2Int# ((x'# `shiftL#` i'#) `or#` (x'# `shiftRL#` (32# -# i'#))))) where - x'# = wordToWord32# (int2Word# x#) + x'# = narrow32Word# (int2Word# x#) i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) bitSize _ = 32 isSigned _ = True @@ -349,15 +476,34 @@ instance Bits Int32 where "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x# "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x# "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32 -"fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (intToInt32# x#) +"fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#) "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#) #-} +#endif + +instance CCallable Int32 +instance CReturnable Int32 + +instance Real Int32 where + toRational x = toInteger x % 1 + +instance Bounded Int32 where + minBound = -0x80000000 + maxBound = 0x7FFFFFFF + +instance Ix Int32 where + range (m,n) = [m..n] + index b@(m,_) i + | inRange b i = fromIntegral (i - m) + | otherwise = indexError b i "Int32" + inRange (m,n) i = m <= i && i <= n + ------------------------------------------------------------------------ -- type Int64 ------------------------------------------------------------------------ -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS < 64 data Int64 = I64# Int64# @@ -424,10 +570,11 @@ instance Integral Int64 where | y /= 0 = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#)) | otherwise = divZeroError "divMod{Int64}" x toInteger x@(I64# x#) - | x >= -0x80000000 && x <= 0x7FFFFFFF + | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int) = S# (int64ToInt# x#) | otherwise = case int64ToInteger# x# of (# s, d #) -> J# s d + divInt64#, modInt64# :: Int64# -> Int64# -> Int64# x# `divInt64#` y# | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) @@ -499,7 +646,11 @@ foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> W "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64 #-} -#else +#else + +-- Int64 is represented in the same way as Int. +-- Operations may assume and must ensure that it holds only values +-- from its logical range. data Int64 = I64# Int# deriving (Eq, Ord) diff --git a/ghc/lib/std/PrelPtr.lhs b/ghc/lib/std/PrelPtr.lhs index e81e960302..cbf076cb5b 100644 --- a/ghc/lib/std/PrelPtr.lhs +++ b/ghc/lib/std/PrelPtr.lhs @@ -1,5 +1,5 @@ ----------------------------------------------------------------------------- --- $Id: PrelPtr.lhs,v 1.2 2001/04/13 21:37:43 panne Exp $ +-- $Id: PrelPtr.lhs,v 1.3 2001/08/17 17:18:54 apt Exp $ -- -- (c) 2000 -- @@ -17,23 +17,22 @@ import PrelBase data Ptr a = Ptr Addr# deriving (Eq, Ord) nullPtr :: Ptr a -nullPtr = Ptr (int2Addr# 0#) +nullPtr = Ptr (nullAddr# 0#) castPtr :: Ptr a -> Ptr b castPtr (Ptr addr) = Ptr addr plusPtr :: Ptr a -> Int -> Ptr b -plusPtr (Ptr addr) (I# d) = Ptr (int2Addr# (addr2Int# addr +# d)) +plusPtr (Ptr addr) (I# d) = Ptr (plusAddr# addr d) alignPtr :: Ptr a -> Int -> Ptr a alignPtr addr@(Ptr a) (I# i) - = case addr2Int# a of { ai -> - case remInt# ai i of { + = case remAddr# a i of { 0# -> addr; - n -> Ptr (int2Addr# (ai +# (i -# n))) }} + n -> Ptr (plusAddr# a (i -# n)) } minusPtr :: Ptr a -> Ptr b -> Int -minusPtr (Ptr a1) (Ptr a2) = I# (addr2Int# a1 -# addr2Int# a2) +minusPtr (Ptr a1) (Ptr a2) = I# (minusAddr# a1 a2) instance CCallable (Ptr a) instance CReturnable (Ptr a) @@ -44,7 +43,7 @@ instance CReturnable (Ptr a) data FunPtr a = FunPtr Addr# deriving (Eq, Ord) nullFunPtr :: FunPtr a -nullFunPtr = FunPtr (int2Addr# 0#) +nullFunPtr = FunPtr (nullAddr# 0#) castFunPtr :: FunPtr a -> FunPtr b castFunPtr (FunPtr addr) = FunPtr addr @@ -58,3 +57,4 @@ castPtrToFunPtr (Ptr addr) = FunPtr addr instance CCallable (FunPtr a) instance CReturnable (FunPtr a) \end{code} + diff --git a/ghc/lib/std/PrelStorable.lhs b/ghc/lib/std/PrelStorable.lhs index 92a39b00b5..01662322c9 100644 --- a/ghc/lib/std/PrelStorable.lhs +++ b/ghc/lib/std/PrelStorable.lhs @@ -1,5 +1,5 @@ % ----------------------------------------------------------------------------- -% $Id: PrelStorable.lhs,v 1.8 2001/07/24 06:31:35 ken Exp $ +% $Id: PrelStorable.lhs,v 1.9 2001/08/17 17:18:54 apt Exp $ % % (c) The FFI task force, 2000 % @@ -220,30 +220,20 @@ readStablePtrOffPtr (Ptr a) (I# i) = IO $ \s -> case readStablePtrOffAddr# a i s of (# s2, x #) -> (# s2, StablePtr x #) readInt8OffPtr (Ptr a) (I# i) = IO $ \s -> case readInt8OffAddr# a i s of (# s2, x #) -> (# s2, I8# x #) -readInt16OffPtr (Ptr a) (I# i) - = IO $ \s -> case readInt16OffAddr# a i s of (# s2, x #) -> (# s2, I16# x #) -readInt32OffPtr (Ptr a) (I# i) - = IO $ \s -> case readInt32OffAddr# a i s of (# s2, x #) -> (# s2, I32# x #) -#if WORD_SIZE_IN_BYTES == 4 -readInt64OffPtr (Ptr a) (I# i) - = IO $ \s -> case readInt64OffAddr# a i s of (# s2, x #) -> (# s2, I64# x #) -#else -readInt64OffPtr (Ptr a) (I# i) - = IO $ \s -> case readIntOffAddr# a i s of (# s2, x #) -> (# s2, I64# x #) -#endif readWord8OffPtr (Ptr a) (I# i) = IO $ \s -> case readWord8OffAddr# a i s of (# s2, x #) -> (# s2, W8# x #) +readInt16OffPtr (Ptr a) (I# i) + = IO $ \s -> case readInt16OffAddr# a i s of (# s2, x #) -> (# s2, I16# x #) readWord16OffPtr (Ptr a) (I# i) = IO $ \s -> case readWord16OffAddr# a i s of (# s2, x #) -> (# s2, W16# x #) +readInt32OffPtr (Ptr a) (I# i) + = IO $ \s -> case readInt32OffAddr# a i s of (# s2, x #) -> (# s2, I32# x #) readWord32OffPtr (Ptr a) (I# i) = IO $ \s -> case readWord32OffAddr# a i s of (# s2, x #) -> (# s2, W32# x #) -#if WORD_SIZE_IN_BYTES == 4 +readInt64OffPtr (Ptr a) (I# i) + = IO $ \s -> case readInt64OffAddr# a i s of (# s2, x #) -> (# s2, I64# x #) readWord64OffPtr (Ptr a) (I# i) = IO $ \s -> case readWord64OffAddr# a i s of (# s2, x #) -> (# s2, W64# x #) -#else -readWord64OffPtr (Ptr a) (I# i) - = IO $ \s -> case readWordOffAddr# a i s of (# s2, x #) -> (# s2, W64# x #) -#endif writeWideCharOffPtr :: Ptr Char -> Int -> Char -> IO () writeIntOffPtr :: Ptr Int -> Int -> Int -> IO () @@ -280,30 +270,20 @@ writeStablePtrOffPtr (Ptr a) (I# i) (StablePtr x) = IO $ \s -> case writeStablePtrOffAddr# a i x s of s2 -> (# s2 , () #) writeInt8OffPtr (Ptr a) (I# i) (I8# x) = IO $ \s -> case writeInt8OffAddr# a i x s of s2 -> (# s2, () #) -writeInt16OffPtr (Ptr a) (I# i) (I16# x) - = IO $ \s -> case writeInt16OffAddr# a i x s of s2 -> (# s2, () #) -writeInt32OffPtr (Ptr a) (I# i) (I32# x) - = IO $ \s -> case writeInt32OffAddr# a i x s of s2 -> (# s2, () #) -#if WORD_SIZE_IN_BYTES == 4 -writeInt64OffPtr (Ptr a) (I# i) (I64# x) - = IO $ \s -> case writeInt64OffAddr# a i x s of s2 -> (# s2, () #) -#else -writeInt64OffPtr (Ptr a) (I# i) (I64# x) - = IO $ \s -> case writeIntOffAddr# a i x s of s2 -> (# s2, () #) -#endif writeWord8OffPtr (Ptr a) (I# i) (W8# x) = IO $ \s -> case writeWord8OffAddr# a i x s of s2 -> (# s2, () #) +writeInt16OffPtr (Ptr a) (I# i) (I16# x) + = IO $ \s -> case writeInt16OffAddr# a i x s of s2 -> (# s2, () #) writeWord16OffPtr (Ptr a) (I# i) (W16# x) = IO $ \s -> case writeWord16OffAddr# a i x s of s2 -> (# s2, () #) +writeInt32OffPtr (Ptr a) (I# i) (I32# x) + = IO $ \s -> case writeInt32OffAddr# a i x s of s2 -> (# s2, () #) writeWord32OffPtr (Ptr a) (I# i) (W32# x) = IO $ \s -> case writeWord32OffAddr# a i x s of s2 -> (# s2, () #) -#if WORD_SIZE_IN_BYTES == 4 +writeInt64OffPtr (Ptr a) (I# i) (I64# x) + = IO $ \s -> case writeInt64OffAddr# a i x s of s2 -> (# s2, () #) writeWord64OffPtr (Ptr a) (I# i) (W64# x) = IO $ \s -> case writeWord64OffAddr# a i x s of s2 -> (# s2, () #) -#else -writeWord64OffPtr (Ptr a) (I# i) (W64# x) - = IO $ \s -> case writeWordOffAddr# a i x s of s2 -> (# s2, () #) -#endif #endif /* __GLASGOW_HASKELL__ */ \end{code} diff --git a/ghc/lib/std/PrelWord.lhs b/ghc/lib/std/PrelWord.lhs index 0a8bc1dfa6..5cefedb875 100644 --- a/ghc/lib/std/PrelWord.lhs +++ b/ghc/lib/std/PrelWord.lhs @@ -131,7 +131,9 @@ instance Integral Word where instance Bounded Word where minBound = 0 -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS == 31 + maxBound = 0x7FFFFFFF +#elif WORD_SIZE_IN_BITS == 32 maxBound = 0xFFFFFFFF #else maxBound = 0xFFFFFFFFFFFFFFFF @@ -155,16 +157,11 @@ instance Bits Word where (W# x#) `shift` (I# i#) | i# >=# 0# = W# (x# `shiftL#` i#) | otherwise = W# (x# `shiftRL#` negateInt# i#) -#if WORD_SIZE_IN_BYTES == 4 - (W# x#) `rotate` (I# i#) = W# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (32# -# i'#))) + (W# x#) `rotate` (I# i#) = W# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (wsib -# i'#))) where - i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) -#else - (W# x#) `rotate` (I# i#) = W# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (64# -# i'#))) - where - i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) -#endif - bitSize _ = WORD_SIZE_IN_BYTES * 8 + i'# = word2Int# (int2Word# i# `and#` int2Word# (wsib -# 1#)) + wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -} + bitSize _ = WORD_SIZE_IN_BITS isSigned _ = False {-# RULES @@ -189,15 +186,15 @@ instance Show Word8 where showsPrec p x = showsPrec p (fromIntegral x :: Int) instance Num Word8 where - (W8# x#) + (W8# y#) = W8# (wordToWord8# (x# `plusWord#` y#)) - (W8# x#) - (W8# y#) = W8# (wordToWord8# (x# `minusWord#` y#)) - (W8# x#) * (W8# y#) = W8# (wordToWord8# (x# `timesWord#` y#)) - negate (W8# x#) = W8# (wordToWord8# (int2Word# (negateInt# (word2Int# x#)))) + (W8# x#) + (W8# y#) = W8# (narrow8Word# (x# `plusWord#` y#)) + (W8# x#) - (W8# y#) = W8# (narrow8Word# (x# `minusWord#` y#)) + (W8# x#) * (W8# y#) = W8# (narrow8Word# (x# `timesWord#` y#)) + negate (W8# x#) = W8# (narrow8Word# (int2Word# (negateInt# (word2Int# x#)))) abs x = x signum 0 = 0 signum _ = 1 - fromInteger (S# i#) = W8# (wordToWord8# (int2Word# i#)) - fromInteger (J# s# d#) = W8# (wordToWord8# (integer2Word# s# d#)) + fromInteger (S# i#) = W8# (narrow8Word# (int2Word# i#)) + fromInteger (J# s# d#) = W8# (narrow8Word# (integer2Word# s# d#)) instance Real Word8 where toRational x = toInteger x % 1 @@ -258,9 +255,9 @@ instance Bits Word8 where (W8# x#) `xor` (W8# y#) = W8# (x# `xor#` y#) complement (W8# x#) = W8# (x# `xor#` mb#) where W8# mb# = maxBound (W8# x#) `shift` (I# i#) - | i# >=# 0# = W8# (wordToWord8# (x# `shiftL#` i#)) + | i# >=# 0# = W8# (narrow8Word# (x# `shiftL#` i#)) | otherwise = W8# (x# `shiftRL#` negateInt# i#) - (W8# x#) `rotate` (I# i#) = W8# (wordToWord8# ((x# `shiftL#` i'#) `or#` + (W8# x#) `rotate` (I# i#) = W8# (narrow8Word# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (8# -# i'#)))) where i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) @@ -270,7 +267,7 @@ instance Bits Word8 where {-# RULES "fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8 "fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer -"fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (wordToWord8# x#) +"fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (narrow8Word# x#) "fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#) #-} @@ -290,15 +287,15 @@ instance Show Word16 where showsPrec p x = showsPrec p (fromIntegral x :: Int) instance Num Word16 where - (W16# x#) + (W16# y#) = W16# (wordToWord16# (x# `plusWord#` y#)) - (W16# x#) - (W16# y#) = W16# (wordToWord16# (x# `minusWord#` y#)) - (W16# x#) * (W16# y#) = W16# (wordToWord16# (x# `timesWord#` y#)) - negate (W16# x#) = W16# (wordToWord16# (int2Word# (negateInt# (word2Int# x#)))) + (W16# x#) + (W16# y#) = W16# (narrow16Word# (x# `plusWord#` y#)) + (W16# x#) - (W16# y#) = W16# (narrow16Word# (x# `minusWord#` y#)) + (W16# x#) * (W16# y#) = W16# (narrow16Word# (x# `timesWord#` y#)) + negate (W16# x#) = W16# (narrow16Word# (int2Word# (negateInt# (word2Int# x#)))) abs x = x signum 0 = 0 signum _ = 1 - fromInteger (S# i#) = W16# (wordToWord16# (int2Word# i#)) - fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#)) + fromInteger (S# i#) = W16# (narrow16Word# (int2Word# i#)) + fromInteger (J# s# d#) = W16# (narrow16Word# (integer2Word# s# d#)) instance Real Word16 where toRational x = toInteger x % 1 @@ -359,9 +356,9 @@ instance Bits Word16 where (W16# x#) `xor` (W16# y#) = W16# (x# `xor#` y#) complement (W16# x#) = W16# (x# `xor#` mb#) where W16# mb# = maxBound (W16# x#) `shift` (I# i#) - | i# >=# 0# = W16# (wordToWord16# (x# `shiftL#` i#)) + | i# >=# 0# = W16# (narrow16Word# (x# `shiftL#` i#)) | otherwise = W16# (x# `shiftRL#` negateInt# i#) - (W16# x#) `rotate` (I# i#) = W16# (wordToWord16# ((x# `shiftL#` i'#) `or#` + (W16# x#) `rotate` (I# i#) = W16# (narrow16Word# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (16# -# i'#)))) where i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) @@ -372,7 +369,7 @@ instance Bits Word16 where "fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x# "fromIntegral/Word16->Word16" fromIntegral = id :: Word16 -> Word16 "fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer -"fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (wordToWord16# x#) +"fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (narrow16Word# x#) "fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#) #-} @@ -380,37 +377,140 @@ instance Bits Word16 where -- type Word32 ------------------------------------------------------------------------ +#if WORD_SIZE_IN_BITS < 32 + +data Word32 = W32# Word32# + +instance Eq Word32 where + (W32# x#) == (W32# y#) = x# `eqWord32#` y# + (W32# x#) /= (W32# y#) = x# `neWord32#` y# + +instance Ord Word32 where + (W32# x#) < (W32# y#) = x# `ltWord32#` y# + (W32# x#) <= (W32# y#) = x# `leWord32#` y# + (W32# x#) > (W32# y#) = x# `gtWord32#` y# + (W32# x#) >= (W32# y#) = x# `geWord32#` y# + +instance Num Word32 where + (W32# x#) + (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `plusInt32#` word32ToInt32# y#)) + (W32# x#) - (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `minusInt32#` word32ToInt32# y#)) + (W32# x#) * (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `timesInt32#` word32ToInt32# y#)) + negate (W32# x#) = W32# (int32ToWord32# (negateInt32# (word32ToInt32# x#))) + abs x = x + signum 0 = 0 + signum _ = 1 + fromInteger (S# i#) = W32# (int32ToWord32# (intToInt32# i#)) + fromInteger (J# s# d#) = W32# (integerToWord32# s# d#) + +instance Enum Word32 where + succ x + | x /= maxBound = x + 1 + | otherwise = succError "Word32" + pred x + | x /= minBound = x - 1 + | otherwise = predError "Word32" + toEnum i@(I# i#) + | i >= 0 = W32# (wordToWord32# (int2Word# i#)) + | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32) + fromEnum x@(W32# x#) + | x <= fromIntegral (maxBound::Int) + = I# (word2Int# (word32ToWord# x#)) + | otherwise = fromEnumError "Word32" x + enumFrom = integralEnumFrom + enumFromThen = integralEnumFromThen + enumFromTo = integralEnumFromTo + enumFromThenTo = integralEnumFromThenTo + +instance Integral Word32 where + quot x@(W32# x#) y@(W32# y#) + | y /= 0 = W32# (x# `quotWord32#` y#) + | otherwise = divZeroError "quot{Word32}" x + rem x@(W32# x#) y@(W32# y#) + | y /= 0 = W32# (x# `remWord32#` y#) + | otherwise = divZeroError "rem{Word32}" x + div x@(W32# x#) y@(W32# y#) + | y /= 0 = W32# (x# `quotWord32#` y#) + | otherwise = divZeroError "div{Word32}" x + mod x@(W32# x#) y@(W32# y#) + | y /= 0 = W32# (x# `remWord32#` y#) + | otherwise = divZeroError "mod{Word32}" x + quotRem x@(W32# x#) y@(W32# y#) + | y /= 0 = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#)) + | otherwise = divZeroError "quotRem{Word32}" x + divMod x@(W32# x#) y@(W32# y#) + | y /= 0 = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#)) + | otherwise = divZeroError "quotRem{Word32}" x + toInteger x@(W32# x#) + | x <= fromIntegral (maxBound::Int) = S# (word2Int# (word32ToWord# x#)) + | otherwise = case word32ToInteger# x# of (# s, d #) -> J# s d + +instance Bits Word32 where + (W32# x#) .&. (W32# y#) = W32# (x# `and32#` y#) + (W32# x#) .|. (W32# y#) = W32# (x# `or32#` y#) + (W32# x#) `xor` (W32# y#) = W32# (x# `xor32#` y#) + complement (W32# x#) = W32# (not32# x#) + (W32# x#) `shift` (I# i#) + | i# >=# 0# = W32# (x# `shiftL32#` i#) + | otherwise = W32# (x# `shiftRL32#` negateInt# i#) + (W32# x#) `rotate` (I# i#) = W32# ((x# `shiftL32#` i'#) `or32#` + (x# `shiftRL32#` (32# -# i'#))) + where + i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) + bitSize _ = 32 + isSigned _ = False + +foreign import "stg_eqWord32" unsafe eqWord32# :: Word32# -> Word32# -> Bool +foreign import "stg_neWord32" unsafe neWord32# :: Word32# -> Word32# -> Bool +foreign import "stg_ltWord32" unsafe ltWord32# :: Word32# -> Word32# -> Bool +foreign import "stg_leWord32" unsafe leWord32# :: Word32# -> Word32# -> Bool +foreign import "stg_gtWord32" unsafe gtWord32# :: Word32# -> Word32# -> Bool +foreign import "stg_geWord32" unsafe geWord32# :: Word32# -> Word32# -> Bool +foreign import "stg_int32ToWord32" unsafe int32ToWord32# :: Int32# -> Word32# +foreign import "stg_word32ToInt32" unsafe word32ToInt32# :: Word32# -> Int32# +foreign import "stg_intToInt32" unsafe intToInt32# :: Int# -> Int32# +foreign import "stg_wordToWord32" unsafe wordToWord32# :: Word# -> Word32# +foreign import "stg_word32ToWord" unsafe word32ToWord# :: Word32# -> Word# +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_quotWord32" unsafe quotWord32# :: Word32# -> Word32# -> Word32# +foreign import "stg_remWord32" unsafe remWord32# :: Word32# -> Word32# -> Word32# +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_shiftL32" unsafe shiftL32# :: Word32# -> Int# -> Word32# +foreign import "stg_shiftRL32" unsafe shiftRL32# :: Word32# -> Int# -> Word32# + +{-# RULES +"fromIntegral/Int->Word32" fromIntegral = \(I# x#) -> W32# (int32ToWord32# (intToInt32# x#)) +"fromIntegral/Word->Word32" fromIntegral = \(W# x#) -> W32# (wordToWord32# x#) +"fromIntegral/Word32->Int" fromIntegral = \(W32# x#) -> I# (word2Int# (word32ToWord# x#)) +"fromIntegral/Word32->Word" fromIntegral = \(W32# x#) -> W# (word32ToWord# x#) +"fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32 + #-} + +#else + -- Word32 is represented in the same way as Word. -#if WORD_SIZE_IN_BYTES == 8 +#if WORD_SIZE_IN_BITS > 32 -- Operations may assume and must ensure that it holds only values -- from its logical range. #endif data Word32 = W32# Word# deriving (Eq, Ord) -instance CCallable Word32 -instance CReturnable Word32 - -instance Show Word32 where -#if WORD_SIZE_IN_BYTES == 4 - showsPrec p x = showsPrec p (toInteger x) -#else - showsPrec p x = showsPrec p (fromIntegral x :: Int) -#endif - instance Num Word32 where - (W32# x#) + (W32# y#) = W32# (wordToWord32# (x# `plusWord#` y#)) - (W32# x#) - (W32# y#) = W32# (wordToWord32# (x# `minusWord#` y#)) - (W32# x#) * (W32# y#) = W32# (wordToWord32# (x# `timesWord#` y#)) - negate (W32# x#) = W32# (wordToWord32# (int2Word# (negateInt# (word2Int# x#)))) + (W32# x#) + (W32# y#) = W32# (narrow32Word# (x# `plusWord#` y#)) + (W32# x#) - (W32# y#) = W32# (narrow32Word# (x# `minusWord#` y#)) + (W32# x#) * (W32# y#) = W32# (narrow32Word# (x# `timesWord#` y#)) + negate (W32# x#) = W32# (narrow32Word# (int2Word# (negateInt# (word2Int# x#)))) abs x = x signum 0 = 0 signum _ = 1 - fromInteger (S# i#) = W32# (wordToWord32# (int2Word# i#)) - fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#)) - -instance Real Word32 where - toRational x = toInteger x % 1 + fromInteger (S# i#) = W32# (narrow32Word# (int2Word# i#)) + fromInteger (J# s# d#) = W32# (narrow32Word# (integer2Word# s# d#)) instance Enum Word32 where succ x @@ -421,12 +521,12 @@ instance Enum Word32 where | otherwise = predError "Word32" toEnum i@(I# i#) | i >= 0 -#if WORD_SIZE_IN_BYTES == 8 +#if WORD_SIZE_IN_BITS > 32 && i <= fromIntegral (maxBound::Word32) #endif = W32# (int2Word# i#) | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32) -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS == 32 fromEnum x@(W32# x#) | x <= fromIntegral (maxBound::Int) = I# (word2Int# x#) @@ -461,7 +561,7 @@ instance Integral Word32 where | y /= 0 = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#)) | otherwise = divZeroError "quotRem{Word32}" x toInteger (W32# x#) -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS == 32 | i# >=# 0# = S# i# | otherwise = case word2Integer# x# of (# s, d #) -> J# s d where @@ -470,33 +570,15 @@ instance Integral Word32 where = S# (word2Int# x#) #endif -instance Bounded Word32 where - minBound = 0 - maxBound = 0xFFFFFFFF - -instance Ix Word32 where - range (m,n) = [m..n] - index b@(m,_) i - | inRange b i = fromIntegral (i - m) - | otherwise = indexError b i "Word32" - inRange (m,n) i = m <= i && i <= n - -instance Read Word32 where -#if WORD_SIZE_IN_BYTES == 4 - readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] -#else - readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] -#endif - instance Bits Word32 where (W32# x#) .&. (W32# y#) = W32# (x# `and#` y#) (W32# x#) .|. (W32# y#) = W32# (x# `or#` y#) (W32# x#) `xor` (W32# y#) = W32# (x# `xor#` y#) complement (W32# x#) = W32# (x# `xor#` mb#) where W32# mb# = maxBound (W32# x#) `shift` (I# i#) - | i# >=# 0# = W32# (wordToWord32# (x# `shiftL#` i#)) + | i# >=# 0# = W32# (narrow32Word# (x# `shiftL#` i#)) | otherwise = W32# (x# `shiftRL#` negateInt# i#) - (W32# x#) `rotate` (I# i#) = W32# (wordToWord32# ((x# `shiftL#` i'#) `or#` + (W32# x#) `rotate` (I# i#) = W32# (narrow32Word# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (32# -# i'#)))) where i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) @@ -508,15 +590,49 @@ instance Bits Word32 where "fromIntegral/Word16->Word32" fromIntegral = \(W16# x#) -> W32# x# "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32 "fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer -"fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (wordToWord32# x#) +"fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (narrow32Word# x#) "fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#) #-} +#endif + +instance CCallable Word32 +instance CReturnable Word32 + +instance Show Word32 where +#if WORD_SIZE_IN_BITS < 33 + showsPrec p x = showsPrec p (toInteger x) +#else + showsPrec p x = showsPrec p (fromIntegral x :: Int) +#endif + + +instance Real Word32 where + toRational x = toInteger x % 1 + +instance Bounded Word32 where + minBound = 0 + maxBound = 0xFFFFFFFF + +instance Ix Word32 where + range (m,n) = [m..n] + index b@(m,_) i + | inRange b i = fromIntegral (i - m) + | otherwise = indexError b i "Word32" + inRange (m,n) i = m <= i && i <= n + +instance Read Word32 where +#if WORD_SIZE_IN_BITS < 33 + readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] +#else + readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s] +#endif + ------------------------------------------------------------------------ -- type Word64 ------------------------------------------------------------------------ -#if WORD_SIZE_IN_BYTES == 4 +#if WORD_SIZE_IN_BITS < 64 data Word64 = W64# Word64# @@ -606,13 +722,13 @@ foreign import "stg_gtWord64" unsafe gtWord64# :: Word64# -> Word64# - foreign import "stg_geWord64" unsafe geWord64# :: Word64# -> Word64# -> Bool foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64# foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64# +foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64# +foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64# +foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word# foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64# foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64# foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64# foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64# -foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64# -foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64# -foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word# foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64# foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64# foreign import "stg_and64" unsafe and64# :: Word64# -> Word64# -> Word64# @@ -632,6 +748,10 @@ foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> W #else +-- Word64 is represented in the same way as Word. +-- Operations may assume and must ensure that it holds only values +-- from its logical range. + data Word64 = W64# Word# deriving (Eq, Ord) instance Num Word64 where |