/ src / lib / libgcc.c
libgcc.c
 1  /* SPDX-License-Identifier: GPL-2.0-only */
 2  
 3  #include <types.h>
 4  
 5  /*
 6   * Provide platform-independent backend implementation for __builtin_clz() in
 7   * <lib.h> in case GCC does not have an assembly version for this arch.
 8   */
 9  
10  /*
11   * FIXME
12   * work around lack of --gc-sections on x86
13   * defined in rv32 libgcc.a
14   */
15  
16  #if !ENV_X86 && !ENV_RISCV
17  int __clzsi2(u32 a);
18  int __clzsi2(u32 a)
19  {
20  	static const u8 four_bit_table[] = {
21  		[0x0] = 4, [0x1] = 3, [0x2] = 2, [0x3] = 2,
22  		[0x4] = 1, [0x5] = 1, [0x6] = 1, [0x7] = 1,
23  		[0x8] = 0, [0x9] = 0, [0xa] = 0, [0xb] = 0,
24  		[0xc] = 0, [0xd] = 0, [0xe] = 0, [0xf] = 0,
25  	};
26  	int r = 0;
27  
28  	if (!(a & (0xffffU << 16))) {
29  		r += 16;
30  		a <<= 16;
31  	}
32  
33  	if (!(a & (0xffU << 24))) {
34  		r += 8;
35  		a <<= 8;
36  	}
37  
38  	if (!(a & (0xfU << 28))) {
39  		r += 4;
40  		a <<= 4;
41  	}
42  
43  	return r + four_bit_table[a >> 28];
44  }
45  #endif