AamirM wrote:Using MSVC calling convention, first param is in ecx. (Yes, I use MSVC

)
Code: Select all
unsigned int __fastcall log10(unsigned int number)
{
_asm
{
bsr ecx, ecx
mov eax, 1233
add ecx, 1
mul ecx
shr eax, 12
}
}
Return is in eax (again using MSVC convention). People with brains will get how to adapt to other conventions.
Bad ass now grinvader?

Congratulations, it took this long to rewrite verbatim PART of the suggestion given in the very first reply. It seems to me to be the best approach in this thread, unfortunately you missed half of it in your code (meaning you wouldn't get a precise answer).
grinvader; You were incorrect to dismiss this function just because you saw IntegerLogBase2 then assumed that it must use a multiply/LUT just because that was the only implementation you read. If you would have looked further above you would have seen several other implementations, including the "plain bit search" you mentioned, which they called the obvious way. Of course, they wouldn't have given the multiply/LUT method you dismissed as "retarded" if it weren't faster in any situations.
Nonetheless, many platforms implement counting bits in hardware (either FPU and/or integer instruction) and by these days it's usually a constant time operation so of course you can put for IntegerLog2 something very fast.
By the way, GCC also has a builtin for this operation. Check out _builtin_clz - now you won't have to rely on x86 ASM. Use it instead of IntegerLogBase2 in the example given.
Also, you usually shouldn't use sub-int types for locals (and function parameters, and return values) unless you specifically need for the value to not exceed sub-int range. Actual end-result will depend on platform but it'll often be slower.
Finally, we can't really tell what kind of performance characteristics the algorithm will have when we don't know what kind of input you'll be giving it. If you're not mainly using large numbers then the if chain could win, although there are probably combinations/compromises you can use between the constant time and log10(N) time algorithms.