Created
December 23, 2014 04:39
-
-
Save axman6/6db2acd1b70d5a81c9e5 to your computer and use it in GitHub Desktop.
log2 Word64 and count leading zeros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
import Data.Bits | |
import Data.Word | |
nlz :: Word64 -> Int | |
nlz x = go x x 0 where | |
go !x !y !n | |
| x < 0 = n | |
| y == 0 = 64 - n | |
| otherwise = go (shiftL x 1) (shiftR y 1) (n+1) | |
log2 :: Word64 -> Int | |
log2 x = 63 - nlz x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment