lots of portability changes (#1405)
[ghc-hetmet.git] / compiler / utils / FastBool.lhs
1 %
2 % (c) The University of Glasgow, 2000-2006
3 %
4 \section{Fast booleans}
5
6 \begin{code}
7 module FastBool (
8     --fastBool could be called bBox; isFastTrue, bUnbox; but they're not
9     FastBool, fastBool, isFastTrue, fastOr, fastAnd
10   ) where
11
12 #if defined(__GLASGOW_HASKELL__)
13
14 -- Import the beggars
15 import GHC.Exts
16 import Panic
17
18 type FastBool = Int#
19 fastBool True  = 1#
20 fastBool False = 0#
21
22 #ifdef DEBUG
23 --then waste time deciding whether to panic. FastBool should normally
24 --be at least as fast as Bool, one would hope...
25
26 isFastTrue 1# = True
27 isFastTrue 0# = False
28 isFastTrue _ = panic "FastTypes: isFastTrue"
29
30 -- note that fastOr and fastAnd are strict in both arguments
31 -- since they are unboxed
32 fastOr 1# _ = 1#
33 fastOr 0# x = x
34 fastOr _  _ = panicFastInt "FastTypes: fastOr"
35
36 fastAnd 0# _ = 0#
37 fastAnd 1# x = x
38 fastAnd _  _ = panicFastInt "FastTypes: fastAnd"
39
40 --these "panicFastInt"s (formerly known as "panic#") rely on
41 --FastInt = FastBool ( = Int# presumably),
42 --haha, true enough when __GLASGOW_HASKELL__.  Why can't we have functions
43 --that return _|_ be kind-polymorphic ( ?? to be precise ) ?
44
45 #else /* ! DEBUG */
46 --Isn't comparison to zero sometimes faster on CPUs than comparison to 1?
47 -- (since using Int# as _synonym_ fails to guarantee that it will
48 --   only take on values of 0 and 1)
49 isFastTrue 0# = False
50 isFastTrue _ = True
51
52 -- note that fastOr and fastAnd are strict in both arguments
53 -- since they are unboxed
54 -- Also, to avoid incomplete-pattern warning
55 -- (and avoid wasting time with redundant runtime checks),
56 -- we don't pattern-match on both 0# and 1# .
57 fastOr 0# x = x
58 fastOr _  _ = 1#
59
60 fastAnd 0# _ = 0#
61 fastAnd _  x = x
62
63 #endif /* ! DEBUG */
64
65
66 #else /* ! __GLASGOW_HASKELL__ */
67
68 type FastBool = Bool
69 fastBool x = x
70 isFastTrue x = x
71 -- make sure these are as strict as the unboxed version,
72 -- so that the performance characteristics match
73 fastOr False False = False
74 fastOr _ _ = True
75 fastAnd True True = True
76 fastAnd _ _ = False
77
78 #endif /* ! __GLASGOW_HASKELL__ */
79
80 fastBool :: Bool -> FastBool
81 isFastTrue :: FastBool -> Bool
82 fastOr :: FastBool -> FastBool -> FastBool
83 fastAnd :: FastBool -> FastBool -> FastBool
84
85 \end{code}