[XviD-devel] log2 hacking...

Christoph Lampert xvid-devel@xvid.org
Sat, 4 Jan 2003 13:12:22 +0100 (CET)


Hi,

not really XVID related, although I'll use it in GMC: 


I'm need a quick way to calculate the next higher (or equal) power
of two than a given value X: 

So for X=320 is should be 512 and for X=720 it should be 1024. For X=1024
it should be 1024 as well, etc. 
I also need the log2() of this value, so the routine should be something
like 

{
 exponent = log2(X);
 value = 1<< exponent; 
}


* I can either use float log() for that (boooh!), but there is no
direct log2() function, so this sucks...

 exponent = round(log(X)/log(2)) ;  
 value = 1 << exponent; 

* Or I can use a loop: 

value=16;  // lower than 16 won't happen 
exponent=4;

while (value<X) { exponent++; value+=value; }



* Or I can use Intel "bitsearch" instruction: 

 function IntLog2( Value: integer ): integer;
 asm
    BSR EAX, EAX
 end;


which is of course very clever, but only possible on i386, and 
also I don't want to use inline assembler. 
Do you know any way to let the compiler "know" what I want and 
make him call 'BSR' or an equivalent on another plattform directly?

gruel