Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 3 | |
| 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 Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 11 | #if !CONFIG(ARCH_X86) /* work around lack of --gc-sections on x86 */ \ |
| 12 | && !CONFIG(ARCH_RISCV_RV32) /* defined in rv32 libgcc.a */ |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 13 | int __clzsi2(u32 a); |
| 14 | int __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 Menzel | 60132a4 | 2018-01-23 00:13:57 +0100 | [diff] [blame] | 24 | if (!(a & (0xffffU << 16))) { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 25 | r += 16; |
| 26 | a <<= 16; |
| 27 | } |
| 28 | |
Paul Menzel | 60132a4 | 2018-01-23 00:13:57 +0100 | [diff] [blame] | 29 | if (!(a & (0xffU << 24))) { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 30 | r += 8; |
| 31 | a <<= 8; |
| 32 | } |
| 33 | |
Paul Menzel | 60132a4 | 2018-01-23 00:13:57 +0100 | [diff] [blame] | 34 | if (!(a & (0xfU << 28))) { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 35 | r += 4; |
| 36 | a <<= 4; |
| 37 | } |
| 38 | |
| 39 | return r + four_bit_table[a >> 28]; |
| 40 | } |
| 41 | #endif |