9d7c276c225ef9d25c5b45af8a4f335fcd1eb410
[ghc-hetmet.git] / compiler / utils / FastTypes.lhs
1 %
2 % (c) The University of Glasgow, 2000-2006
3 %
4 \section{Fast integers and booleans}
5
6 \begin{code}
7 module FastTypes (
8     FastInt, _ILIT, iBox, iUnbox,
9     (+#), (-#), (*#), quotFastInt, negateFastInt,
10     (==#), (<#), (<=#), (>=#), (>#),
11
12     FastBool, fastBool, isFastTrue, fastOr, fastAnd
13   ) where
14
15 #include "HsVersions.h"
16
17 #if defined(__GLASGOW_HASKELL__)
18
19 -- Import the beggars
20 import GHC.Exts
21         ( Int(..), Int#, (+#), (-#), (*#), 
22           quotInt#, negateInt#, (==#), (<#), (<=#), (>=#), (>#)
23         )
24
25 type FastInt = Int#
26 _ILIT (I# x) = x
27 iBox x = I# x
28 iUnbox (I# x) = x
29 quotFastInt   = quotInt#
30 negateFastInt = negateInt#
31
32 type FastBool = Int#
33 fastBool True  = 1#
34 fastBool False = 0#
35 isFastTrue x = x ==# 1#
36
37 -- note that fastOr and fastAnd are strict in both arguments
38 -- since they are unboxed
39 fastOr 1# _ = 1#
40 fastOr 0# x = x
41
42 fastAnd 0# x = 0#
43 fastAnd 1# x = x
44
45 #else /* ! __GLASGOW_HASKELL__ */
46
47 type FastInt = Int
48 _ILIT x = x
49 iBox x = x
50 iUnbox x = x
51 (+#) = (+)
52 (-#) = (-)
53 (*#) = (*)
54 quotFastInt   = quot
55 negateFastInt = negate
56 (==#) = (==)
57 (<#)  = (<)
58 (<=#) = (<=)
59 (>=#) = (>=)
60 (>#)  = (>)
61
62 type FastBool = Bool
63 fastBool x = x
64 isFastTrue x = x
65 -- make sure these are as strict as the unboxed version,
66 -- so that the performance characteristics match
67 fastOr False False = False
68 fastOr _ _ = True
69 fastAnd True True = True
70 fastAnd _ _ = False
71
72 --These are among the type-signatures necessary for !ghc to compile
73 -- but break ghc (can't give a signature for an import...)
74 (+#) :: FastInt -> FastInt -> FastInt
75 (-#) :: FastInt -> FastInt -> FastInt
76 (*#) :: FastInt -> FastInt -> FastInt
77 (==#) :: FastInt -> FastInt -> Bool
78 (<#) :: FastInt -> FastInt -> Bool
79 (<=#) :: FastInt -> FastInt -> Bool
80 (>=#) :: FastInt -> FastInt -> Bool
81 (>#) :: FastInt -> FastInt -> Bool
82
83 #endif /* ! __GLASGOW_HASKELL__ */
84 -- however it's still possible to check that these are
85 -- valid signatures nonetheless (e.g., ==# returns Bool
86 -- not FastBool/Int# !)
87 _signatures =
88  ( (+#) :: FastInt -> FastInt -> FastInt
89  , (-#) :: FastInt -> FastInt -> FastInt
90  , (*#) :: FastInt -> FastInt -> FastInt
91  , (==#) :: FastInt -> FastInt -> Bool
92  , (<#) :: FastInt -> FastInt -> Bool
93  , (<=#) :: FastInt -> FastInt -> Bool
94  , (>=#) :: FastInt -> FastInt -> Bool
95  , (>#) :: FastInt -> FastInt -> Bool
96  )
97
98 -- type-signatures will improve the non-ghc-specific versions
99 -- and keep things accurate (and ABLE to compile!)
100 _ILIT :: Int -> FastInt
101 iBox :: FastInt -> Int
102 iUnbox :: Int -> FastInt
103
104 quotFastInt :: FastInt -> FastInt -> FastInt
105 negateFastInt :: FastInt -> FastInt
106
107 fastBool :: Bool -> FastBool
108 isFastTrue :: FastBool -> Bool
109 fastOr :: FastBool -> FastBool -> FastBool
110 fastAnd :: FastBool -> FastBool -> FastBool
111
112 \end{code}