Skip to content

Commit e3bd02d

Browse files
authored
Remove bitcount; use popCount directly (#1031)
1 parent 41005b5 commit e3bd02d

File tree

3 files changed

+3
-29
lines changed

3 files changed

+3
-29
lines changed

containers-tests/tests/IntSetValidity.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Data.IntSet.Internal
77
import Data.List (intercalate)
88
import Numeric (showHex)
99
import Test.Tasty.QuickCheck (Property, counterexample, property, (.&&.))
10-
import Utils.Containers.Internal.BitUtil (bitcount)
1110

1211
{--------------------------------------------------------------------
1312
Assertions

containers/src/Data/IntSet/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ size :: IntSet -> Int
367367
size = go 0
368368
where
369369
go !acc (Bin _ l r) = go (go acc l) r
370-
go acc (Tip _ bm) = acc + bitcount 0 bm
370+
go acc (Tip _ bm) = acc + popCount bm
371371
go acc Nil = acc
372372

373373
-- | \(O(\min(n,W))\). Is the value a member of the set?

containers/src/Utils/Containers/Internal/BitUtil.hs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{-# LANGUAGE CPP #-}
2-
#if __GLASGOW_HASKELL__
3-
{-# LANGUAGE MagicHash #-}
4-
#endif
52
#if !defined(TESTING) && defined(__GLASGOW_HASKELL__)
63
{-# LANGUAGE Safe #-}
74
#endif
@@ -31,38 +28,16 @@
3128
-- closely.
3229

3330
module Utils.Containers.Internal.BitUtil
34-
( bitcount
35-
, highestBitMask
31+
( highestBitMask
3632
, shiftLL
3733
, shiftRL
3834
, wordSize
3935
) where
4036

41-
import Data.Bits (popCount, unsafeShiftL, unsafeShiftR
37+
import Data.Bits (unsafeShiftL, unsafeShiftR
4238
, countLeadingZeros, finiteBitSize
4339
)
4440

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-
6641
-- | Return a word where only the highest bit is set.
6742
highestBitMask :: Word -> Word
6843
highestBitMask w = shiftLL 1 (wordSize - 1 - countLeadingZeros w)

0 commit comments

Comments
 (0)