
15 Jul
2010
15 Jul
'10
2:13 a.m.
G'day all.
Quoting joel falcou
In the good'ol Bits Twiddling Hacks page:
Many modern ISAs (at least EM64T, PPC and recent ARMs; these are just the ones I know about) have a single-instruction priority encoder usually called "count leading zeroes". In GCC, it's available as a builtin: uint64_t log2v = v == 1 ? 0 : 64 - __builtin_clzll(v - 1); Note that this is branch free if your compiler is feeling speculative and has a conditional move or skip instruction available. If not, it's an easy branch to predict. Andrew Bromage