Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <types.h> |
| 17 | |
| 18 | /* |
| 19 | * Provide platform-independent backend implementation for __builtin_clz() in |
| 20 | * <lib.h> in case GCC does not have an assembly version for this arch. |
| 21 | */ |
| 22 | |
Philipp Hug | b09e500 | 2019-02-06 06:48:51 +0100 | [diff] [blame] | 23 | #if !IS_ENABLED(CONFIG_ARCH_X86) /* work around lack of --gc-sections on x86 */ \ |
| 24 | && !IS_ENABLED(CONFIG_ARCH_RISCV_RV32) /* defined in rv32 libgcc.a */ |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 25 | int __clzsi2(u32 a); |
| 26 | int __clzsi2(u32 a) |
| 27 | { |
| 28 | static const u8 four_bit_table[] = { |
| 29 | [0x0] = 4, [0x1] = 3, [0x2] = 2, [0x3] = 2, |
| 30 | [0x4] = 1, [0x5] = 1, [0x6] = 1, [0x7] = 1, |
| 31 | [0x8] = 0, [0x9] = 0, [0xa] = 0, [0xb] = 0, |
| 32 | [0xc] = 0, [0xd] = 0, [0xe] = 0, [0xf] = 0, |
| 33 | }; |
| 34 | int r = 0; |
| 35 | |
Paul Menzel | 60132a4 | 2018-01-23 00:13:57 +0100 | [diff] [blame] | 36 | if (!(a & (0xffffU << 16))) { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 37 | r += 16; |
| 38 | a <<= 16; |
| 39 | } |
| 40 | |
Paul Menzel | 60132a4 | 2018-01-23 00:13:57 +0100 | [diff] [blame] | 41 | if (!(a & (0xffU << 24))) { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 42 | r += 8; |
| 43 | a <<= 8; |
| 44 | } |
| 45 | |
Paul Menzel | 60132a4 | 2018-01-23 00:13:57 +0100 | [diff] [blame] | 46 | if (!(a & (0xfU << 28))) { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 47 | r += 4; |
| 48 | a <<= 4; |
| 49 | } |
| 50 | |
| 51 | return r + four_bit_table[a >> 28]; |
| 52 | } |
| 53 | #endif |