Last active
July 17, 2017 09:58
-
-
Save nshibano/3746312624f7de84ac4c96eec4fea182 to your computer and use it in GitHub Desktop.
Count leading zeros of uint32 in F#
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
let inline leading_zeros_of_ls4b_of_uint32 x = | |
match x with | |
| 0b0000u -> 4 | |
| 0b0001u -> 3 | |
| 0b0010u -> 2 | |
| 0b0011u -> 2 | |
| 0b0100u -> 1 | |
| 0b0101u -> 1 | |
| 0b0110u -> 1 | |
| 0b0111u -> 1 | |
| _ -> 0 | |
let leading_zeros_of_uint32 input = | |
let mutable n = 0; | |
let mutable reg = input | |
if (reg >>> 16) = 0u then n <- n + 16; reg <- reg <<< 16 | |
if (reg >>> 24) = 0u then n <- n + 8; reg <- reg <<< 8 | |
if (reg >>> 28) = 0u then n <- n + 4; reg <- reg <<< 4 | |
n + leading_zeros_of_ls4b_of_uint32 (reg >>> 28) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment