1 /* ===-- clzsi2.c - Implement __clzsi2 -------------------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __clzsi2 for the compiler_rt library.
12 * ===----------------------------------------------------------------------===
17 /* Returns: the number of leading 0-bits */
19 /* Precondition: a != 0 */
21 COMPILER_RT_ABI si_int
25 si_int t
= ((x
& 0xFFFF0000) == 0) << 4; /* if (x is small) t = 16 else 0 */
26 x
>>= 16 - t
; /* x = [0 - 0xFFFF] */
27 su_int r
= t
; /* r = [0, 16] */
28 /* return r + clz(x) */
29 t
= ((x
& 0xFF00) == 0) << 3;
30 x
>>= 8 - t
; /* x = [0 - 0xFF] */
31 r
+= t
; /* r = [0, 8, 16, 24] */
32 /* return r + clz(x) */
33 t
= ((x
& 0xF0) == 0) << 2;
34 x
>>= 4 - t
; /* x = [0 - 0xF] */
35 r
+= t
; /* r = [0, 4, 8, 12, 16, 20, 24, 28] */
36 /* return r + clz(x) */
37 t
= ((x
& 0xC) == 0) << 1;
38 x
>>= 2 - t
; /* x = [0 - 3] */
39 r
+= t
; /* r = [0 - 30] and is even */
40 /* return r + clz(x) */
52 return r
+ ((2 - x
) & -((x
& 2) == 0));