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 * ===----------------------------------------------------------------------===
18 /* Returns: the number of leading 0-bits */
20 /* Precondition: a != 0 */
22 COMPILER_RT_ABI si_int
26 si_int t = ((x & 0xFFFF0000) == 0) << 4; /* if (x is small) t = 16 else 0 */
27 x >>= 16 - t; /* x = [0 - 0xFFFF] */
28 su_int r = t; /* r = [0, 16] */
29 /* return r + clz(x) */
30 t = ((x & 0xFF00) == 0) << 3;
31 x >>= 8 - t; /* x = [0 - 0xFF] */
32 r += t; /* r = [0, 8, 16, 24] */
33 /* return r + clz(x) */
34 t = ((x & 0xF0) == 0) << 2;
35 x >>= 4 - t; /* x = [0 - 0xF] */
36 r += t; /* r = [0, 4, 8, 12, 16, 20, 24, 28] */
37 /* return r + clz(x) */
38 t = ((x & 0xC) == 0) << 1;
39 x >>= 2 - t; /* x = [0 - 3] */
40 r += t; /* r = [0 - 30] and is even */
41 /* return r + clz(x) */
53 return r + ((2 - x) & -((x & 2) == 0));