Skip to content

Instantly share code, notes, and snippets.

@resilar
resilar / ctz.c
Created June 15, 2016 13:59
de Bruijn CTZ with proper handling of 0
// you faggots probably know the de bruijn trick to count trailing zeros:
inline int ctz32_retarded(uint32_t x)
{
static const unsigned char debruijn_ctz32[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
return debruijn_ctz32[((x & -x) * 0x077CB531) >> 27];
}