int32_t log2_int(uint32_t val)
{
uint32_t r;
uint32_t tmp;
uint32_t exp;
#if defined(CPU_ARM) && ARM_ARCH >= 5
exp = __builtin_clz(val);
val <<= exp - 1;
r = (32 - exp) << 24;
#else
exp = 31;
if (val < 0x8000)
{
val <<= 16;
exp -= 16;
}
if (val < 0x800000)
{
val <<= 8;
exp -= 8;
}
if (val < 0x8000000)
{
val <<= 4;
exp -= 4;
}
if (val < 0x20000000)
{
val <<= 2;
exp -= 2;
}
if (val < 0x40000000)
{
val <<= 1;
exp -= 1;
}
r = exp << 24;