[project @ 2005-02-01 13:41:41 by simonmar]
[ghc-base.git] / NHC / SizedTypes.hs.cpp
1 module NHC.SizedTypes
2   -- This module just adds instances of Bits for Int/Word[8,16,32,64]
3   ( Int8,  Int16,  Int32,  Int64
4   , Word8, Word16, Word32, Word64
5   ) where
6
7 {- Note explicit braces and semicolons here - layout is corrupted by cpp. -}
8
9 {import NHC.FFI         (Int8,Int16,Int32,Int64,Word8,Word16,Word32,Word64)
10 ;import Data.Bits
11
12 #define SIZED_TYPE(T,BS,S)      \
13 ; FOREIGNS(T)                   \
14 ; INSTANCE_BITS(T,BS,S)
15
16
17 #define FOREIGNS(T)     \
18 ; foreign import ccall nhc_prim##T##And         :: T -> T   -> T        \
19 ; foreign import ccall nhc_prim##T##Or          :: T -> T   -> T        \
20 ; foreign import ccall nhc_prim##T##Xor         :: T -> T   -> T        \
21 ; foreign import ccall nhc_prim##T##Lsh         :: T -> Int -> T        \
22 ; foreign import ccall nhc_prim##T##Rsh         :: T -> Int -> T        \
23 ; foreign import ccall nhc_prim##T##Compl       :: T        -> T
24
25
26 #define INSTANCE_BITS(T,BS,S)           \
27 ; instance Bits T where                 \
28     { (.&.)      = nhc_prim##T##And     \
29     ; (.|.)      = nhc_prim##T##Or      \
30     ; xor        = nhc_prim##T##Xor     \
31     ; complement = nhc_prim##T##Compl   \
32     ; shiftL     = nhc_prim##T##Lsh     \
33     ; shiftR     = nhc_prim##T##Rsh     \
34     ; bitSize  _ = BS                   \
35     ; isSigned _ = S                    \
36     }
37
38 SIZED_TYPE(Int8,8,True)
39 SIZED_TYPE(Int16,16,True)
40 SIZED_TYPE(Int32,32,True)
41 SIZED_TYPE(Int64,64,True)
42
43 SIZED_TYPE(Word8,8,False)
44 SIZED_TYPE(Word16,16,False)
45 SIZED_TYPE(Word32,32,False)
46 SIZED_TYPE(Word64,64,False)
47
48 }