Skip to content

Commit 0e0ac14

Browse files
ethercrowtreeowl
authored andcommitted
Use countLeadingZeros to implement highestBitMask (#583)
1 parent ca3406c commit 0e0ac14

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Utils/Containers/Internal/BitUtil.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ module Utils.Containers.Internal.BitUtil
3939
) where
4040

4141
import Data.Bits ((.|.), xor)
42-
import Data.Bits (popCount, unsafeShiftL, unsafeShiftR)
42+
import Data.Bits (popCount, unsafeShiftL, unsafeShiftR
43+
#if MIN_VERSION_base(4,8,0)
44+
, countLeadingZeros
45+
#endif
46+
)
4347
#if MIN_VERSION_base(4,7,0)
4448
import Data.Bits (finiteBitSize)
4549
#else
@@ -72,6 +76,9 @@ bitcount a x = a + popCount x
7276

7377
-- | Return a word where only the highest bit is set.
7478
highestBitMask :: Word -> Word
79+
#if MIN_VERSION_base(4,8,0)
80+
highestBitMask w = shiftLL 1 (wordSize - 1 - countLeadingZeros w)
81+
#else
7582
highestBitMask x1 = let x2 = x1 .|. x1 `shiftRL` 1
7683
x3 = x2 .|. x2 `shiftRL` 2
7784
x4 = x3 .|. x3 `shiftRL` 4
@@ -83,6 +90,7 @@ highestBitMask x1 = let x2 = x1 .|. x1 `shiftRL` 1
8390
#else
8491
in x6 `xor` (x6 `shiftRL` 1)
8592
#endif
93+
#endif
8694
{-# INLINE highestBitMask #-}
8795

8896
-- Right and left logical shifts.

0 commit comments

Comments
 (0)