1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;