~ubuntu-branches/ubuntu/precise/ghc/precise

« back to all changes in this revision

Viewing changes to compiler/utils/FastBool.lhs

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2011-01-17 12:49:24 UTC
  • Revision ID: james.westby@ubuntu.com-20110117124924-do1pym1jlf5o636m
Tags: upstream-7.0.1
ImportĀ upstreamĀ versionĀ 7.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifdef DEBUG
 
17
import Panic
 
18
#endif
 
19
 
 
20
type FastBool = Int#
 
21
fastBool True  = 1#
 
22
fastBool False = 0#
 
23
 
 
24
#ifdef DEBUG
 
25
--then waste time deciding whether to panic. FastBool should normally
 
26
--be at least as fast as Bool, one would hope...
 
27
 
 
28
isFastTrue 1# = True
 
29
isFastTrue 0# = False
 
30
isFastTrue _ = panic "FastTypes: isFastTrue"
 
31
 
 
32
-- note that fastOr and fastAnd are strict in both arguments
 
33
-- since they are unboxed
 
34
fastOr 1# _ = 1#
 
35
fastOr 0# x = x
 
36
fastOr _  _ = panicFastInt "FastTypes: fastOr"
 
37
 
 
38
fastAnd 0# _ = 0#
 
39
fastAnd 1# x = x
 
40
fastAnd _  _ = panicFastInt "FastTypes: fastAnd"
 
41
 
 
42
--these "panicFastInt"s (formerly known as "panic#") rely on
 
43
--FastInt = FastBool ( = Int# presumably),
 
44
--haha, true enough when __GLASGOW_HASKELL__.  Why can't we have functions
 
45
--that return _|_ be kind-polymorphic ( ?? to be precise ) ?
 
46
 
 
47
#else /* ! DEBUG */
 
48
--Isn't comparison to zero sometimes faster on CPUs than comparison to 1?
 
49
-- (since using Int# as _synonym_ fails to guarantee that it will
 
50
--   only take on values of 0 and 1)
 
51
isFastTrue 0# = False
 
52
isFastTrue _ = True
 
53
 
 
54
-- note that fastOr and fastAnd are strict in both arguments
 
55
-- since they are unboxed
 
56
-- Also, to avoid incomplete-pattern warning
 
57
-- (and avoid wasting time with redundant runtime checks),
 
58
-- we don't pattern-match on both 0# and 1# .
 
59
fastOr 0# x = x
 
60
fastOr _  _ = 1#
 
61
 
 
62
fastAnd 0# _ = 0#
 
63
fastAnd _  x = x
 
64
 
 
65
#endif /* ! DEBUG */
 
66
 
 
67
 
 
68
#else /* ! __GLASGOW_HASKELL__ */
 
69
 
 
70
type FastBool = Bool
 
71
fastBool x = x
 
72
isFastTrue x = x
 
73
-- make sure these are as strict as the unboxed version,
 
74
-- so that the performance characteristics match
 
75
fastOr False False = False
 
76
fastOr _ _ = True
 
77
fastAnd True True = True
 
78
fastAnd _ _ = False
 
79
 
 
80
#endif /* ! __GLASGOW_HASKELL__ */
 
81
 
 
82
fastBool :: Bool -> FastBool
 
83
isFastTrue :: FastBool -> Bool
 
84
fastOr :: FastBool -> FastBool -> FastBool
 
85
fastAnd :: FastBool -> FastBool -> FastBool
 
86
 
 
87
\end{code}