#3563: A couple of additions to Data.Bits
---------------------------------+------------------------------------------
Reporter: porges | Owner:
Type: proposal | Status: new
Priority: normal | Milestone: Not GHC
Component: libraries/base | Version: 6.11
Keywords: | Difficulty: Unknown
Os: Unknown/Multiple | Testcase:
Architecture: Unknown/Multiple | Failure: None/Unknown
---------------------------------+------------------------------------------
Changes (by morrow):
* failure: => None/Unknown
Comment:
I was just doing some bit twiddling the other day, here's some (C ->
Haskell) stuff i ended up with:
{{{
pop64 :: Word64 -> Int
pop64 x =
let a = 0x5555555555555555
b = 0x3333333333333333
c = 0x0f0f0f0f0f0f0f0f
d = 0x0101010101010101
in case x - (x `shiftRL` 1) .&. a of
x -> case (x .&. b) + ((x `shiftRL` 2) .&. b) of
x -> case ((x + (x `shiftRL` 4)) .&. c) * d of
x -> case x `shiftRL` 56 of
x -> fromIntegral x
bsr64 :: Word64 -> Int
bsr64 x0 =
case (x0 .|. shiftRL x0 1) of
x1 -> case (x1 .|. shiftRL x1 2) of
x2 -> case (x2 .|. shiftRL x2 4) of
x3 -> case (x3 .|. shiftRL x3 8) of
x4 -> case (x4 .|. shiftRL x4 16) of
x5 -> case (x5 .|. shiftRL x5 32) of
x6 -> case pop64 x6 - 1 of
x -> fromIntegral x
-- from Data.IntMap
shiftRL :: Word64 -> Int -> Word64
shiftRL (W64# x) (I# i) = W64# (shiftRL# x i)
}}}
Matt
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3563#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs