module Data.BloomFilter.Util
(
FastShift(..)
, nextPowerOfTwo
, (:*)(..)
) where
import Data.Bits ((.|.))
import qualified Data.Bits as Bits
import GHC.Base
import GHC.Word
data a :* b = !a :* !b
deriving (Eq, Ord, Show)
nextPowerOfTwo :: Int -> Int
nextPowerOfTwo n =
let a = n 1
b = a .|. (a `shiftR` 1)
c = b .|. (b `shiftR` 2)
d = c .|. (c `shiftR` 4)
e = d .|. (d `shiftR` 8)
f = e .|. (e `shiftR` 16)
g = f .|. (f `shiftR` 32)
!h = g + 1
in h
class FastShift a where
shiftL :: a -> Int -> a
shiftR :: a -> Int -> a
instance FastShift Word32 where
shiftL (W32# x#) (I# i#) = W32# (x# `uncheckedShiftL#` i#)
shiftR (W32# x#) (I# i#) = W32# (x# `uncheckedShiftRL#` i#)
instance FastShift Word64 where
shiftL (W64# x#) (I# i#) = W64# (x# `uncheckedShiftL64#` i#)
shiftR (W64# x#) (I# i#) = W64# (x# `uncheckedShiftRL64#` i#)
instance FastShift Int where
shiftL (I# x#) (I# i#) = I# (x# `iShiftL#` i#)
shiftR (I# x#) (I# i#) = I# (x# `iShiftRA#` i#)
instance FastShift Integer where
shiftL = Bits.shiftL
shiftR = Bits.shiftR