Fast integer log2?
I'm looking for a really fast integer log2. It can assume that the input is always a power of 2, or it can simply round off non-pow2, or whatever.
This is partly useful for me and mostly just curiosity to see what kind of wacky optimizations/fast routines you guys can come up with.
x86 ASM opcode: BSR (bit scan reverse). Returns the index of the highest non-zero bit. I.e. the log base two, rounding down.
Results are undefined if the original number is zero.
Results are undefined if the original number is zero.
Damn, that was easy.
[EDIT] Uh...I'm not too good with assembly. If I tell it to put the result in eax, can I use that to not give the function a real return statement?
[EDIT] Uh...I'm not too good with assembly. If I tell it to put the result in eax, can I use that to not give the function a real return statement?
DWORD dwNumber=something other than 0;DWORD dwLog2;__asm{ mov dwLog2, dwNumber bsr dwLog2}
I'm not good with asm (well not x86 at least) either, but that would be my best guess. Maybe someone can point out any mistakes I made?
int pow2( int x ){ __asm { bsr eax, x ret } //dummy return return 0;}
That caused an access violation. I thought return values were in eax?
Here is the code with stack prolog-epilog code:
#include <iostream>using namespace std;// logarithm function__declspec (naked) int log2(int val){ // prepare stack __asm { push ebp mov ebp, esp sub esp, __LOCAL_SIZE } __asm { mov eax,[val] jz log2end bsr eax,eaxlog2end: } // Cleanup function __asm { mov esp, ebp pop ebp ret }}int main(){ cout << "Result: " << log2(2) <<endl; return 0;}
The zero case will not work correctly for that however. mov doesn't set flags, you need a test as well
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement