blob: e5ed56dbe6666c62890887333a7ce77fe4bd7e70 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Julius Werner7a8a4ab2015-05-22 16:26:40 -07003
4#include <types.h>
5
6/*
7 * Provide platform-independent backend implementation for __builtin_clz() in
8 * <lib.h> in case GCC does not have an assembly version for this arch.
9 */
10
Julius Wernercd49cce2019-03-05 16:53:33 -080011#if !CONFIG(ARCH_X86) /* work around lack of --gc-sections on x86 */ \
12 && !CONFIG(ARCH_RISCV_RV32) /* defined in rv32 libgcc.a */
Julius Werner7a8a4ab2015-05-22 16:26:40 -070013int __clzsi2(u32 a);
14int __clzsi2(u32 a)
15{
16 static const u8 four_bit_table[] = {
17 [0x0] = 4, [0x1] = 3, [0x2] = 2, [0x3] = 2,
18 [0x4] = 1, [0x5] = 1, [0x6] = 1, [0x7] = 1,
19 [0x8] = 0, [0x9] = 0, [0xa] = 0, [0xb] = 0,
20 [0xc] = 0, [0xd] = 0, [0xe] = 0, [0xf] = 0,
21 };
22 int r = 0;
23
Paul Menzel60132a42018-01-23 00:13:57 +010024 if (!(a & (0xffffU << 16))) {
Julius Werner7a8a4ab2015-05-22 16:26:40 -070025 r += 16;
26 a <<= 16;
27 }
28
Paul Menzel60132a42018-01-23 00:13:57 +010029 if (!(a & (0xffU << 24))) {
Julius Werner7a8a4ab2015-05-22 16:26:40 -070030 r += 8;
31 a <<= 8;
32 }
33
Paul Menzel60132a42018-01-23 00:13:57 +010034 if (!(a & (0xfU << 28))) {
Julius Werner7a8a4ab2015-05-22 16:26:40 -070035 r += 4;
36 a <<= 4;
37 }
38
39 return r + four_bit_table[a >> 28];
40}
41#endif