ded373fd4d3c67d6fdaa2a46ca770f3da64e1824
[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 --Note that the comparisons actually do return Bools not FastBools.
75 (+#) :: FastInt -> FastInt -> FastInt
76 (-#) :: FastInt -> FastInt -> FastInt
77 (*#) :: FastInt -> FastInt -> FastInt
78 (==#) :: FastInt -> FastInt -> Bool
79 (<#) :: FastInt -> FastInt -> Bool
80 (<=#) :: FastInt -> FastInt -> Bool
81 (>=#) :: FastInt -> FastInt -> Bool
82 (>#) :: FastInt -> FastInt -> Bool
83
84 #endif /* ! __GLASGOW_HASKELL__ */
85
86 -- type-signatures will improve the non-ghc-specific versions
87 -- and keep things accurate (and ABLE to compile!)
88 _ILIT :: Int -> FastInt
89 iBox :: FastInt -> Int
90 iUnbox :: Int -> FastInt
91
92 quotFastInt :: FastInt -> FastInt -> FastInt
93 negateFastInt :: FastInt -> FastInt
94
95 fastBool :: Bool -> FastBool
96 isFastTrue :: FastBool -> Bool
97 fastOr :: FastBool -> FastBool -> FastBool
98 fastAnd :: FastBool -> FastBool -> FastBool
99
100 \end{code}