I tried to write a simple assembler code in LINUX (Intel) that does the ceil of log2. It just equates the BSF and BSR. If they are equal, then returns one of them, if they are not equal, returns 1 more then BSR. I get for 512 a result of 10 even through I know that BSR and BSF are both equal to 9 here. Where am I going wrong? (i.e. Jlog2(512) returns 10 and not 9 !!) Code below.
Thanks,
Jeff
inline int Jlog2(register int a)
{
int b;
asm (
"BSR %%eax,%0; \n"
"BSF %%ebx,%0; \n"
"CMP %%eax,%%ebx; \n"
"JE EXT; \n"
"INC %%eax; \n"
"EXT:"
:"=r"(b) /* output */
:"a"(a) /* input */
);
return b;
}
Edited by TkTech, 15 July 2010 - 11:32 AM.
Added code tags.


Sign In
Create Account

Back to top









