Here's my implementation, taking advantage of the FPU internal representation: converting the number into floating-point effectively converts it to the sign-exponent-mantissa representation of a float: we only need to extract the exponent and we're done:
unsigned long kFloorLogTwo(unsigned long Value)
{
unsigned long Result = 0;
if(Value)
{
const float FloatValue = Value;
Result = ((((*(unsigned long *)&FloatValue) & 0x7f800000) >> 23) - 127);
}
return Result;
}
unsigned long kCeilLogTwo(unsigned long Value)
{
unsigned long Result = 0;
if(Value)
{
const float FloatValue = Value;
Result = ((((*(unsigned long*)&FloatValue) & 0x7f800000) >> 23) - 126);
}
return Result;
}
JF











