525f098524835790beeaaeac466dee347175708d
[ghc.git] / libraries / base / Data / Bits.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Data.Bits
7 -- Copyright : (c) The University of Glasgow 2001
8 -- License : BSD-style (see the file libraries/base/LICENSE)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : portable
13 --
14 -- This module defines bitwise operations for signed and unsigned
15 -- integers. Instances of the class 'Bits' for the 'Int' and
16 -- 'Integer' types are available from this module, and instances for
17 -- explicitly sized integral types are available from the
18 -- "Data.Int" and "Data.Word" modules.
19 --
20 -----------------------------------------------------------------------------
21
22 module Data.Bits (
23 Bits(
24 (.&.), (.|.), xor,
25 complement,
26 shift,
27 rotate,
28 zeroBits,
29 bit,
30 setBit,
31 clearBit,
32 complementBit,
33 testBit,
34 bitSizeMaybe,
35 bitSize,
36 isSigned,
37 shiftL, shiftR,
38 unsafeShiftL, unsafeShiftR,
39 rotateL, rotateR,
40 popCount
41 ),
42 FiniteBits(
43 finiteBitSize,
44 countLeadingZeros,
45 countTrailingZeros
46 ),
47
48 bitDefault,
49 testBitDefault,
50 popCountDefault,
51 toIntegralSized
52 ) where
53
54 -- Defines the @Bits@ class containing bit-based operations.
55 -- See library document for details on the semantics of the
56 -- individual operations.
57
58 #include "MachDeps.h"
59
60 #ifdef MIN_VERSION_integer_gmp
61 # define HAVE_INTEGER_GMP1 MIN_VERSION_integer_gmp(1,0,0)
62 #endif
63
64 import Data.Maybe
65 import GHC.Enum
66 import GHC.Num
67 import GHC.Base
68 import GHC.Real
69
70 #if HAVE_INTEGER_GMP1
71 import GHC.Integer.GMP.Internals (bitInteger, popCountInteger)
72 #endif
73
74 infixl 8 `shift`, `rotate`, `shiftL`, `shiftR`, `rotateL`, `rotateR`
75 infixl 7 .&.
76 infixl 6 `xor`
77 infixl 5 .|.
78
79 {-# DEPRECATED bitSize "Use 'bitSizeMaybe' or 'finiteBitSize' instead" #-} -- deprecated in 7.8
80
81 -- | The 'Bits' class defines bitwise operations over integral types.
82 --
83 -- * Bits are numbered from 0 with bit 0 being the least
84 -- significant bit.
85 class Eq a => Bits a where
86 {-# MINIMAL (.&.), (.|.), xor, complement,
87 (shift | (shiftL, shiftR)),
88 (rotate | (rotateL, rotateR)),
89 bitSize, bitSizeMaybe, isSigned, testBit, bit, popCount #-}
90
91 -- | Bitwise \"and\"
92 (.&.) :: a -> a -> a
93
94 -- | Bitwise \"or\"
95 (.|.) :: a -> a -> a
96
97 -- | Bitwise \"xor\"
98 xor :: a -> a -> a
99
100 {-| Reverse all the bits in the argument -}
101 complement :: a -> a
102
103 {-| @'shift' x i@ shifts @x@ left by @i@ bits if @i@ is positive,
104 or right by @-i@ bits otherwise.
105 Right shifts perform sign extension on signed number types;
106 i.e. they fill the top bits with 1 if the @x@ is negative
107 and with 0 otherwise.
108
109 An instance can define either this unified 'shift' or 'shiftL' and
110 'shiftR', depending on which is more convenient for the type in
111 question. -}
112 shift :: a -> Int -> a
113
114 x `shift` i | i<0 = x `shiftR` (-i)
115 | i>0 = x `shiftL` i
116 | otherwise = x
117
118 {-| @'rotate' x i@ rotates @x@ left by @i@ bits if @i@ is positive,
119 or right by @-i@ bits otherwise.
120
121 For unbounded types like 'Integer', 'rotate' is equivalent to 'shift'.
122
123 An instance can define either this unified 'rotate' or 'rotateL' and
124 'rotateR', depending on which is more convenient for the type in
125 question. -}
126 rotate :: a -> Int -> a
127
128 x `rotate` i | i<0 = x `rotateR` (-i)
129 | i>0 = x `rotateL` i
130 | otherwise = x
131
132 {-
133 -- Rotation can be implemented in terms of two shifts, but care is
134 -- needed for negative values. This suggested implementation assumes
135 -- 2's-complement arithmetic. It is commented out because it would
136 -- require an extra context (Ord a) on the signature of 'rotate'.
137 x `rotate` i | i<0 && isSigned x && x<0
138 = let left = i+bitSize x in
139 ((x `shift` i) .&. complement ((-1) `shift` left))
140 .|. (x `shift` left)
141 | i<0 = (x `shift` i) .|. (x `shift` (i+bitSize x))
142 | i==0 = x
143 | i>0 = (x `shift` i) .|. (x `shift` (i-bitSize x))
144 -}
145
146 -- | 'zeroBits' is the value with all bits unset.
147 --
148 -- The following laws ought to hold (for all valid bit indices @/n/@):
149 --
150 -- * @'clearBit' 'zeroBits' /n/ == 'zeroBits'@
151 -- * @'setBit' 'zeroBits' /n/ == 'bit' /n/@
152 -- * @'testBit' 'zeroBits' /n/ == False@
153 -- * @'popCount' 'zeroBits' == 0@
154 --
155 -- This method uses @'clearBit' ('bit' 0) 0@ as its default
156 -- implementation (which ought to be equivalent to 'zeroBits' for
157 -- types which possess a 0th bit).
158 --
159 -- @since 4.7.0.0
160 zeroBits :: a
161 zeroBits = clearBit (bit 0) 0
162
163 -- | @bit /i/@ is a value with the @/i/@th bit set and all other bits clear.
164 --
165 -- Can be implemented using `bitDefault' if @a@ is also an
166 -- instance of 'Num'.
167 --
168 -- See also 'zeroBits'.
169 bit :: Int -> a
170
171 -- | @x \`setBit\` i@ is the same as @x .|. bit i@
172 setBit :: a -> Int -> a
173
174 -- | @x \`clearBit\` i@ is the same as @x .&. complement (bit i)@
175 clearBit :: a -> Int -> a
176
177 -- | @x \`complementBit\` i@ is the same as @x \`xor\` bit i@
178 complementBit :: a -> Int -> a
179
180 -- | Return 'True' if the @n@th bit of the argument is 1
181 --
182 -- Can be implemented using `testBitDefault' if @a@ is also an
183 -- instance of 'Num'.
184 testBit :: a -> Int -> Bool
185
186 {-| Return the number of bits in the type of the argument. The actual
187 value of the argument is ignored. Returns Nothing
188 for types that do not have a fixed bitsize, like 'Integer'.
189
190 @since 4.7.0.0
191 -}
192 bitSizeMaybe :: a -> Maybe Int
193
194 {-| Return the number of bits in the type of the argument. The actual
195 value of the argument is ignored. The function 'bitSize' is
196 undefined for types that do not have a fixed bitsize, like 'Integer'.
197 -}
198 bitSize :: a -> Int
199
200 {-| Return 'True' if the argument is a signed type. The actual
201 value of the argument is ignored -}
202 isSigned :: a -> Bool
203
204 {-# INLINE setBit #-}
205 {-# INLINE clearBit #-}
206 {-# INLINE complementBit #-}
207 x `setBit` i = x .|. bit i
208 x `clearBit` i = x .&. complement (bit i)
209 x `complementBit` i = x `xor` bit i
210
211 {-| Shift the argument left by the specified number of bits
212 (which must be non-negative).
213
214 An instance can define either this and 'shiftR' or the unified
215 'shift', depending on which is more convenient for the type in
216 question. -}
217 shiftL :: a -> Int -> a
218 {-# INLINE shiftL #-}
219 x `shiftL` i = x `shift` i
220
221 {-| Shift the argument left by the specified number of bits. The
222 result is undefined for negative shift amounts and shift amounts
223 greater or equal to the 'bitSize'.
224
225 Defaults to 'shiftL' unless defined explicitly by an instance.
226
227 @since 4.5.0.0 -}
228 unsafeShiftL :: a -> Int -> a
229 {-# INLINE unsafeShiftL #-}
230 x `unsafeShiftL` i = x `shiftL` i
231
232 {-| Shift the first argument right by the specified number of bits. The
233 result is undefined for negative shift amounts and shift amounts
234 greater or equal to the 'bitSize'.
235
236 Right shifts perform sign extension on signed number types;
237 i.e. they fill the top bits with 1 if the @x@ is negative
238 and with 0 otherwise.
239
240 An instance can define either this and 'shiftL' or the unified
241 'shift', depending on which is more convenient for the type in
242 question. -}
243 shiftR :: a -> Int -> a
244 {-# INLINE shiftR #-}
245 x `shiftR` i = x `shift` (-i)
246
247 {-| Shift the first argument right by the specified number of bits, which
248 must be non-negative an smaller than the number of bits in the type.
249
250 Right shifts perform sign extension on signed number types;
251 i.e. they fill the top bits with 1 if the @x@ is negative
252 and with 0 otherwise.
253
254 Defaults to 'shiftR' unless defined explicitly by an instance.
255
256 @since 4.5.0.0 -}
257 unsafeShiftR :: a -> Int -> a
258 {-# INLINE unsafeShiftR #-}
259 x `unsafeShiftR` i = x `shiftR` i
260
261 {-| Rotate the argument left by the specified number of bits
262 (which must be non-negative).
263
264 An instance can define either this and 'rotateR' or the unified
265 'rotate', depending on which is more convenient for the type in
266 question. -}
267 rotateL :: a -> Int -> a
268 {-# INLINE rotateL #-}
269 x `rotateL` i = x `rotate` i
270
271 {-| Rotate the argument right by the specified number of bits
272 (which must be non-negative).
273
274 An instance can define either this and 'rotateL' or the unified
275 'rotate', depending on which is more convenient for the type in
276 question. -}
277 rotateR :: a -> Int -> a
278 {-# INLINE rotateR #-}
279 x `rotateR` i = x `rotate` (-i)
280
281 {-| Return the number of set bits in the argument. This number is
282 known as the population count or the Hamming weight.
283
284 Can be implemented using `popCountDefault' if @a@ is also an
285 instance of 'Num'.
286
287 @since 4.5.0.0 -}
288 popCount :: a -> Int
289
290 -- |The 'FiniteBits' class denotes types with a finite, fixed number of bits.
291 --
292 -- @since 4.7.0.0
293 class Bits b => FiniteBits b where
294 -- | Return the number of bits in the type of the argument.
295 -- The actual value of the argument is ignored. Moreover, 'finiteBitSize'
296 -- is total, in contrast to the deprecated 'bitSize' function it replaces.
297 --
298 -- @
299 -- 'finiteBitSize' = 'bitSize'
300 -- 'bitSizeMaybe' = 'Just' . 'finiteBitSize'
301 -- @
302 --
303 -- @since 4.7.0.0
304 finiteBitSize :: b -> Int
305
306 -- | Count number of zero bits preceding the most significant set bit.
307 --
308 -- @
309 -- 'countLeadingZeros' ('zeroBits' :: a) = finiteBitSize ('zeroBits' :: a)
310 -- @
311 --
312 -- 'countLeadingZeros' can be used to compute log base 2 via
313 --
314 -- @
315 -- logBase2 x = 'finiteBitSize' x - 1 - 'countLeadingZeros' x
316 -- @
317 --
318 -- Note: The default implementation for this method is intentionally
319 -- naive. However, the instances provided for the primitive
320 -- integral types are implemented using CPU specific machine
321 -- instructions.
322 --
323 -- @since 4.8.0.0
324 countLeadingZeros :: b -> Int
325 countLeadingZeros x = (w-1) - go (w-1)
326 where
327 go i | i < 0 = i -- no bit set
328 | testBit x i = i
329 | otherwise = go (i-1)
330
331 w = finiteBitSize x
332
333 -- | Count number of zero bits following the least significant set bit.
334 --
335 -- @
336 -- 'countTrailingZeros' ('zeroBits' :: a) = finiteBitSize ('zeroBits' :: a)
337 -- 'countTrailingZeros' . 'negate' = 'countTrailingZeros'
338 -- @
339 --
340 -- The related
341 -- <http://en.wikipedia.org/wiki/Find_first_set find-first-set operation>
342 -- can be expressed in terms of 'countTrailingZeros' as follows
343 --
344 -- @
345 -- findFirstSet x = 1 + 'countTrailingZeros' x
346 -- @
347 --
348 -- Note: The default implementation for this method is intentionally
349 -- naive. However, the instances provided for the primitive
350 -- integral types are implemented using CPU specific machine
351 -- instructions.
352 --
353 -- @since 4.8.0.0
354 countTrailingZeros :: b -> Int
355 countTrailingZeros x = go 0
356 where
357 go i | i >= w = i
358 | testBit x i = i
359 | otherwise = go (i+1)
360
361 w = finiteBitSize x
362
363
364 -- The defaults below are written with lambdas so that e.g.
365 -- bit = bitDefault
366 -- is fully applied, so inlining will happen
367
368 -- | Default implementation for 'bit'.
369 --
370 -- Note that: @bitDefault i = 1 `shiftL` i@
371 --
372 -- @since 4.6.0.0
373 bitDefault :: (Bits a, Num a) => Int -> a
374 bitDefault = \i -> 1 `shiftL` i
375 {-# INLINE bitDefault #-}
376
377 -- | Default implementation for 'testBit'.
378 --
379 -- Note that: @testBitDefault x i = (x .&. bit i) /= 0@
380 --
381 -- @since 4.6.0.0
382 testBitDefault :: (Bits a, Num a) => a -> Int -> Bool
383 testBitDefault = \x i -> (x .&. bit i) /= 0
384 {-# INLINE testBitDefault #-}
385
386 -- | Default implementation for 'popCount'.
387 --
388 -- This implementation is intentionally naive. Instances are expected to provide
389 -- an optimized implementation for their size.
390 --
391 -- @since 4.6.0.0
392 popCountDefault :: (Bits a, Num a) => a -> Int
393 popCountDefault = go 0
394 where
395 go !c 0 = c
396 go c w = go (c+1) (w .&. (w - 1)) -- clear the least significant
397 {-# INLINABLE popCountDefault #-}
398
399
400 -- | Interpret 'Bool' as 1-bit bit-field
401 --
402 -- @since 4.7.0.0
403 instance Bits Bool where
404 (.&.) = (&&)
405
406 (.|.) = (||)
407
408 xor = (/=)
409
410 complement = not
411
412 shift x 0 = x
413 shift _ _ = False
414
415 rotate x _ = x
416
417 bit 0 = True
418 bit _ = False
419
420 testBit x 0 = x
421 testBit _ _ = False
422
423 bitSizeMaybe _ = Just 1
424
425 bitSize _ = 1
426
427 isSigned _ = False
428
429 popCount False = 0
430 popCount True = 1
431
432 -- | @since 4.7.0.0
433 instance FiniteBits Bool where
434 finiteBitSize _ = 1
435 countTrailingZeros x = if x then 0 else 1
436 countLeadingZeros x = if x then 0 else 1
437
438 -- | @since 2.01
439 instance Bits Int where
440 {-# INLINE shift #-}
441 {-# INLINE bit #-}
442 {-# INLINE testBit #-}
443
444 zeroBits = 0
445
446 bit = bitDefault
447
448 testBit = testBitDefault
449
450 (I# x#) .&. (I# y#) = I# (x# `andI#` y#)
451 (I# x#) .|. (I# y#) = I# (x# `orI#` y#)
452 (I# x#) `xor` (I# y#) = I# (x# `xorI#` y#)
453 complement (I# x#) = I# (notI# x#)
454 (I# x#) `shift` (I# i#)
455 | isTrue# (i# >=# 0#) = I# (x# `iShiftL#` i#)
456 | otherwise = I# (x# `iShiftRA#` negateInt# i#)
457 (I# x#) `shiftL` (I# i#) = I# (x# `iShiftL#` i#)
458 (I# x#) `unsafeShiftL` (I# i#) = I# (x# `uncheckedIShiftL#` i#)
459 (I# x#) `shiftR` (I# i#) = I# (x# `iShiftRA#` i#)
460 (I# x#) `unsafeShiftR` (I# i#) = I# (x# `uncheckedIShiftRA#` i#)
461
462 {-# INLINE rotate #-} -- See Note [Constant folding for rotate]
463 (I# x#) `rotate` (I# i#) =
464 I# ((x# `uncheckedIShiftL#` i'#) `orI#` (x# `uncheckedIShiftRL#` (wsib -# i'#)))
465 where
466 !i'# = i# `andI#` (wsib -# 1#)
467 !wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
468 bitSizeMaybe i = Just (finiteBitSize i)
469 bitSize i = finiteBitSize i
470
471 popCount (I# x#) = I# (word2Int# (popCnt# (int2Word# x#)))
472
473 isSigned _ = True
474
475 -- | @since 4.6.0.0
476 instance FiniteBits Int where
477 finiteBitSize _ = WORD_SIZE_IN_BITS
478 countLeadingZeros (I# x#) = I# (word2Int# (clz# (int2Word# x#)))
479 countTrailingZeros (I# x#) = I# (word2Int# (ctz# (int2Word# x#)))
480
481 -- | @since 2.01
482 instance Bits Word where
483 {-# INLINE shift #-}
484 {-# INLINE bit #-}
485 {-# INLINE testBit #-}
486
487 (W# x#) .&. (W# y#) = W# (x# `and#` y#)
488 (W# x#) .|. (W# y#) = W# (x# `or#` y#)
489 (W# x#) `xor` (W# y#) = W# (x# `xor#` y#)
490 complement (W# x#) = W# (x# `xor#` mb#)
491 where !(W# mb#) = maxBound
492 (W# x#) `shift` (I# i#)
493 | isTrue# (i# >=# 0#) = W# (x# `shiftL#` i#)
494 | otherwise = W# (x# `shiftRL#` negateInt# i#)
495 (W# x#) `shiftL` (I# i#) = W# (x# `shiftL#` i#)
496 (W# x#) `unsafeShiftL` (I# i#) = W# (x# `uncheckedShiftL#` i#)
497 (W# x#) `shiftR` (I# i#) = W# (x# `shiftRL#` i#)
498 (W# x#) `unsafeShiftR` (I# i#) = W# (x# `uncheckedShiftRL#` i#)
499 (W# x#) `rotate` (I# i#)
500 | isTrue# (i'# ==# 0#) = W# x#
501 | otherwise = W# ((x# `uncheckedShiftL#` i'#) `or#` (x# `uncheckedShiftRL#` (wsib -# i'#)))
502 where
503 !i'# = i# `andI#` (wsib -# 1#)
504 !wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
505 bitSizeMaybe i = Just (finiteBitSize i)
506 bitSize i = finiteBitSize i
507 isSigned _ = False
508 popCount (W# x#) = I# (word2Int# (popCnt# x#))
509 bit = bitDefault
510 testBit = testBitDefault
511
512 -- | @since 4.6.0.0
513 instance FiniteBits Word where
514 finiteBitSize _ = WORD_SIZE_IN_BITS
515 countLeadingZeros (W# x#) = I# (word2Int# (clz# x#))
516 countTrailingZeros (W# x#) = I# (word2Int# (ctz# x#))
517
518 -- | @since 2.01
519 instance Bits Integer where
520 (.&.) = andInteger
521 (.|.) = orInteger
522 xor = xorInteger
523 complement = complementInteger
524 shift x i@(I# i#) | i >= 0 = shiftLInteger x i#
525 | otherwise = shiftRInteger x (negateInt# i#)
526 testBit x (I# i) = testBitInteger x i
527 zeroBits = 0
528
529 #if HAVE_INTEGER_GMP1
530 bit (I# i#) = bitInteger i#
531 popCount x = I# (popCountInteger x)
532 #else
533 bit = bitDefault
534 popCount = popCountDefault
535 #endif
536
537 rotate x i = shift x i -- since an Integer never wraps around
538
539 bitSizeMaybe _ = Nothing
540 bitSize _ = errorWithoutStackTrace "Data.Bits.bitSize(Integer)"
541 isSigned _ = True
542
543 -----------------------------------------------------------------------------
544
545 -- | Attempt to convert an 'Integral' type @a@ to an 'Integral' type @b@ using
546 -- the size of the types as measured by 'Bits' methods.
547 --
548 -- A simpler version of this function is:
549 --
550 -- > toIntegral :: (Integral a, Integral b) => a -> Maybe b
551 -- > toIntegral x
552 -- > | toInteger x == y = Just (fromInteger y)
553 -- > | otherwise = Nothing
554 -- > where
555 -- > y = toInteger x
556 --
557 -- This version requires going through 'Integer', which can be inefficient.
558 -- However, @toIntegralSized@ is optimized to allow GHC to statically determine
559 -- the relative type sizes (as measured by 'bitSizeMaybe' and 'isSigned') and
560 -- avoid going through 'Integer' for many types. (The implementation uses
561 -- 'fromIntegral', which is itself optimized with rules for @base@ types but may
562 -- go through 'Integer' for some type pairs.)
563 --
564 -- @since 4.8.0.0
565
566 toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b
567 toIntegralSized x -- See Note [toIntegralSized optimization]
568 | maybe True (<= x) yMinBound
569 , maybe True (x <=) yMaxBound = Just y
570 | otherwise = Nothing
571 where
572 y = fromIntegral x
573
574 xWidth = bitSizeMaybe x
575 yWidth = bitSizeMaybe y
576
577 yMinBound
578 | isBitSubType x y = Nothing
579 | isSigned x, not (isSigned y) = Just 0
580 | isSigned x, isSigned y
581 , Just yW <- yWidth = Just (negate $ bit (yW-1)) -- Assumes sub-type
582 | otherwise = Nothing
583
584 yMaxBound
585 | isBitSubType x y = Nothing
586 | isSigned x, not (isSigned y)
587 , Just xW <- xWidth, Just yW <- yWidth
588 , xW <= yW+1 = Nothing -- Max bound beyond a's domain
589 | Just yW <- yWidth = if isSigned y
590 then Just (bit (yW-1)-1)
591 else Just (bit yW-1)
592 | otherwise = Nothing
593 {-# INLINABLE toIntegralSized #-}
594
595 -- | 'True' if the size of @a@ is @<=@ the size of @b@, where size is measured
596 -- by 'bitSizeMaybe' and 'isSigned'.
597 isBitSubType :: (Bits a, Bits b) => a -> b -> Bool
598 isBitSubType x y
599 -- Reflexive
600 | xWidth == yWidth, xSigned == ySigned = True
601
602 -- Every integer is a subset of 'Integer'
603 | ySigned, Nothing == yWidth = True
604 | not xSigned, not ySigned, Nothing == yWidth = True
605
606 -- Sub-type relations between fixed-with types
607 | xSigned == ySigned, Just xW <- xWidth, Just yW <- yWidth = xW <= yW
608 | not xSigned, ySigned, Just xW <- xWidth, Just yW <- yWidth = xW < yW
609
610 | otherwise = False
611 where
612 xWidth = bitSizeMaybe x
613 xSigned = isSigned x
614
615 yWidth = bitSizeMaybe y
616 ySigned = isSigned y
617 {-# INLINE isBitSubType #-}
618
619 {- Note [Constant folding for rotate]
620 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
621 The INLINE on the Int instance of rotate enables it to be constant
622 folded. For example:
623 sumU . mapU (`rotate` 3) . replicateU 10000000 $ (7 :: Int)
624 goes to:
625 Main.$wfold =
626 \ (ww_sO7 :: Int#) (ww1_sOb :: Int#) ->
627 case ww1_sOb of wild_XM {
628 __DEFAULT -> Main.$wfold (+# ww_sO7 56) (+# wild_XM 1);
629 10000000 -> ww_sO7
630 whereas before it was left as a call to $wrotate.
631
632 All other Bits instances seem to inline well enough on their
633 own to enable constant folding; for example 'shift':
634 sumU . mapU (`shift` 3) . replicateU 10000000 $ (7 :: Int)
635 goes to:
636 Main.$wfold =
637 \ (ww_sOb :: Int#) (ww1_sOf :: Int#) ->
638 case ww1_sOf of wild_XM {
639 __DEFAULT -> Main.$wfold (+# ww_sOb 56) (+# wild_XM 1);
640 10000000 -> ww_sOb
641 }
642 -}
643
644 -- Note [toIntegralSized optimization]
645 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
646 -- The code in 'toIntegralSized' relies on GHC optimizing away statically
647 -- decidable branches.
648 --
649 -- If both integral types are statically known, GHC will be able optimize the
650 -- code significantly (for @-O1@ and better).
651 --
652 -- For instance (as of GHC 7.8.1) the following definitions:
653 --
654 -- > w16_to_i32 = toIntegralSized :: Word16 -> Maybe Int32
655 -- >
656 -- > i16_to_w16 = toIntegralSized :: Int16 -> Maybe Word16
657 --
658 -- are translated into the following (simplified) /GHC Core/ language:
659 --
660 -- > w16_to_i32 = \x -> Just (case x of _ { W16# x# -> I32# (word2Int# x#) })
661 -- >
662 -- > i16_to_w16 = \x -> case eta of _
663 -- > { I16# b1 -> case tagToEnum# (<=# 0 b1) of _
664 -- > { False -> Nothing
665 -- > ; True -> Just (W16# (narrow16Word# (int2Word# b1)))
666 -- > }
667 -- > }