|
From: Oswald B. <os...@kd...> - 2008-01-13 09:42:11
|
On Sun, Jan 13, 2008 at 01:47:58AM +0000, sv...@va... wrote:
> +++ branches/DATASYMS/coregrind/m_libcbase.c 2008-01-13 01:47:56 UTC (rev 7339)
> @@ -563,13 +563,14 @@
> +Int VG_(log2) ( UInt x )
> {
> Int i;
> /* Any more than 32 and we overflow anyway... */
> for (i = 0; i < 32; i++) {
> + if ((1U << i) == x) return i;
> }
inefficient ...
int pos = -1;
if (n >= 1<<16) { n >>= 16; pos += 16; }
if (n >= 1<< 8) { n >>= 8; pos += 8; }
if (n >= 1<< 4) { n >>= 4; pos += 4; }
if (n >= 1<< 2) { n >>= 2; pos += 2; }
return pos + n - ((n + 1) >> 2);
on x86, a single "bsr" will do the trick.
unless you actually *want* this: Returns -1 if x is not a power of two.
--
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Confusion, chaos, panic - my work here is done.
|