summaryrefslogtreecommitdiff
path: root/riscv/riscv-probe/libfemto/std/clz.c
blob: bc9479546282e19d6a0354f0b6b72e7eab85506a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// See LICENSE for license details.

#include <stdint.h>

#define GLUE_HELPER(x, y) x##y
#define GLUE(x, y) GLUE_HELPER(x, y)

#define DEFINE_CLZ(T,bits)         \
int GLUE(clz,bits)(T val)          \
{                                  \
    int n = 0;                     \
    for (n = 0; n < bits; n++) {   \
        if (val < 0) break;        \
        val <<= 1;                 \
    }                              \
    return n;                      \
}

DEFINE_CLZ(int8_t,8)
DEFINE_CLZ(int16_t,16)
DEFINE_CLZ(int32_t,32)
DEFINE_CLZ(int64_t,64)