Should Data.Bits have a bit scan operation; can the compiler generate a BSR instruction?

Ryan Newton rrnewton at gmail.com
Mon Jun 27 04:29:33 CEST 2011


It would be nice to have something equivalent to GCC's __builtin_clz
intrinsic that could generate a BSR instruction on x86.  Is there any way to
get GHC to generate such an instruction presently?

If Data.Bits had bitScan methods they would have straightforward default
implementations but would also open up the possibility of generating
efficient code for some instances.

Cheers,
  -Ryan

P.S. Here would be example default implementations:

-- The number of leading zero bits:
bitScanReverse :: Bits a => a -> Int
bitScanReverse num = loop (size - 1)
  where
   size = bitSize num
   loop i | i < 0         = size
          | testBit num i = size - 1 - i
  | otherwise     = loop (i-1)

-- The number of trailing zero bits:
bitScanForward :: Bits a => a -> Int
bitScanForward num = loop 0
  where
   size = bitSize num
   loop i | i == size     = size
          | testBit num i = i
  | otherwise     = loop (i+1)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20110626/e59a8ee3/attachment.htm>


More information about the Libraries mailing list