Skip to content

Instantly share code, notes, and snippets.

@kusma
Last active April 2, 2017 22:15
Show Gist options
  • Save kusma/a4ddf3513f51fff6578f2027d0082a97 to your computer and use it in GitHub Desktop.
Save kusma/a4ddf3513f51fff6578f2027d0082a97 to your computer and use it in GitHub Desktop.
clz emulation
static inline int clz(uint32_t input)
{
const uint8_t clz_lut[16] = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned c = 0;
if (input & 0xFFFF0000)
input >>= 16;
else
c = 16;
if (input & 0xFF00)
input >>= 8;
else
c += 8;
if (input & 0xF0)
input >>= 4;
else
c += 4;
return clz_lut[input] + c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment