Boost logo

Boost Users :

Subject: Re: [Boost-users] run-time integer log2
From: ajb_at_[hidden]
Date: 2010-07-14 22:13:03

G'day all.

Quoting joel falcou <joel.falcou_at_[hidden]>:

> 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

     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

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at