| ??? 10/05/07 08:25 Read: times |
#145450 - Fixed-point log2, without lookup table. Responding to: ???'s previous message |
A while ago, I was looking for an algorithm that calculates a log2 without using a lookup table (we needed to free memory, but had plenty of CPU cycles).
I found one that is similar to integer square root algorithms - it calculates the integer part of the log2 by simply counting the leading zeros, and then adds the fractional part bit by bit by squaring the value, checking whether it's > 2, and setting the bit in question and dividing the working value by 2 if it is. Of course, this algorithm has some limits (most notably numeric precision, since you keep squaring a value and truncating the result), but overall it delivered good precision for up to 16 output bits. It's also a bit of a CPU hog due to the number of multiplications required (one per output bit). |



