|
1 | 1 | {-# LANGUAGE CPP #-} |
2 | | -#if __GLASGOW_HASKELL__ |
3 | | -{-# LANGUAGE MagicHash #-} |
4 | | -#endif |
5 | 2 | #if !defined(TESTING) && defined(__GLASGOW_HASKELL__) |
6 | 3 | {-# LANGUAGE Safe #-} |
7 | 4 | #endif |
|
31 | 28 | -- closely. |
32 | 29 |
|
33 | 30 | module Utils.Containers.Internal.BitUtil |
34 | | - ( bitcount |
35 | | - , highestBitMask |
| 31 | + ( highestBitMask |
36 | 32 | , shiftLL |
37 | 33 | , shiftRL |
38 | 34 | , wordSize |
39 | 35 | ) where |
40 | 36 |
|
41 | | -import Data.Bits (popCount, unsafeShiftL, unsafeShiftR |
| 37 | +import Data.Bits (unsafeShiftL, unsafeShiftR |
42 | 38 | , countLeadingZeros, finiteBitSize |
43 | 39 | ) |
44 | 40 |
|
45 | | - |
46 | | -{---------------------------------------------------------------------- |
47 | | - [bitcount] as posted by David F. Place to haskell-cafe on April 11, 2006, |
48 | | - based on the code on |
49 | | - http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan, |
50 | | - where the following source is given: |
51 | | - Published in 1988, the C Programming Language 2nd Ed. (by Brian W. |
52 | | - Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April |
53 | | - 19, 2006 Don Knuth pointed out to me that this method "was first published |
54 | | - by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by |
55 | | - Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)" |
56 | | -----------------------------------------------------------------------} |
57 | | - |
58 | | -bitcount :: Int -> Word -> Int |
59 | | -bitcount a x = a + popCount x |
60 | | -{-# INLINE bitcount #-} |
61 | | - |
62 | | --- The highestBitMask implementation is based on |
63 | | --- http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 |
64 | | --- which has been put in the public domain. |
65 | | - |
66 | 41 | -- | Return a word where only the highest bit is set. |
67 | 42 | highestBitMask :: Word -> Word |
68 | 43 | highestBitMask w = shiftLL 1 (wordSize - 1 - countLeadingZeros w) |
|
0 commit comments