Created
January 14, 2013 12:20
-
-
Save liuyix/4529694 to your computer and use it in GitHub Desktop.
simple log 2 calculation implement (from pintool demo)
This file contains hidden or 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
| int32_t FloorLog2(uint32_t n) | |
| { | |
| int32_t p = 0; | |
| if (n == 0) return -1; | |
| if (n & 0xffff0000) { p += 16; n >>= 16; } | |
| if (n & 0x0000ff00) { p += 8; n >>= 8; } | |
| if (n & 0x000000f0) { p += 4; n >>= 4; } | |
| if (n & 0x0000000c) { p += 2; n >>= 2; } | |
| if (n & 0x00000002) { p += 1; } | |
| return p; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment