Skip to content

Instantly share code, notes, and snippets.

@liuyix
Created January 14, 2013 12:20
Show Gist options
  • Select an option

  • Save liuyix/4529694 to your computer and use it in GitHub Desktop.

Select an option

Save liuyix/4529694 to your computer and use it in GitHub Desktop.
simple log 2 calculation implement (from pintool demo)
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