xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/clzsi2.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* ===-- clzsi2.c - Implement __clzsi2 -------------------------------------===
2*0a6a1f1dSLionel Sambuc  *
3*0a6a1f1dSLionel Sambuc  *               The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc  *
5*0a6a1f1dSLionel Sambuc  * This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc  * Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * This file implements __clzsi2 for the compiler_rt library.
11*0a6a1f1dSLionel Sambuc  *
12*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
13*0a6a1f1dSLionel Sambuc  */
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "int_lib.h"
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc /* Returns: the number of leading 0-bits */
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc /* Precondition: a != 0 */
20*0a6a1f1dSLionel Sambuc 
21*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI si_int
__clzsi2(si_int a)22*0a6a1f1dSLionel Sambuc __clzsi2(si_int a)
23*0a6a1f1dSLionel Sambuc {
24*0a6a1f1dSLionel Sambuc     su_int x = (su_int)a;
25*0a6a1f1dSLionel Sambuc     si_int t = ((x & 0xFFFF0000) == 0) << 4;  /* if (x is small) t = 16 else 0 */
26*0a6a1f1dSLionel Sambuc     x >>= 16 - t;      /* x = [0 - 0xFFFF] */
27*0a6a1f1dSLionel Sambuc     su_int r = t;       /* r = [0, 16] */
28*0a6a1f1dSLionel Sambuc     /* return r + clz(x) */
29*0a6a1f1dSLionel Sambuc     t = ((x & 0xFF00) == 0) << 3;
30*0a6a1f1dSLionel Sambuc     x >>= 8 - t;       /* x = [0 - 0xFF] */
31*0a6a1f1dSLionel Sambuc     r += t;            /* r = [0, 8, 16, 24] */
32*0a6a1f1dSLionel Sambuc     /* return r + clz(x) */
33*0a6a1f1dSLionel Sambuc     t = ((x & 0xF0) == 0) << 2;
34*0a6a1f1dSLionel Sambuc     x >>= 4 - t;       /* x = [0 - 0xF] */
35*0a6a1f1dSLionel Sambuc     r += t;            /* r = [0, 4, 8, 12, 16, 20, 24, 28] */
36*0a6a1f1dSLionel Sambuc     /* return r + clz(x) */
37*0a6a1f1dSLionel Sambuc     t = ((x & 0xC) == 0) << 1;
38*0a6a1f1dSLionel Sambuc     x >>= 2 - t;       /* x = [0 - 3] */
39*0a6a1f1dSLionel Sambuc     r += t;            /* r = [0 - 30] and is even */
40*0a6a1f1dSLionel Sambuc     /* return r + clz(x) */
41*0a6a1f1dSLionel Sambuc /*     switch (x)
42*0a6a1f1dSLionel Sambuc  *     {
43*0a6a1f1dSLionel Sambuc  *     case 0:
44*0a6a1f1dSLionel Sambuc  *         return r + 2;
45*0a6a1f1dSLionel Sambuc  *     case 1:
46*0a6a1f1dSLionel Sambuc  *         return r + 1;
47*0a6a1f1dSLionel Sambuc  *     case 2:
48*0a6a1f1dSLionel Sambuc  *     case 3:
49*0a6a1f1dSLionel Sambuc  *         return r;
50*0a6a1f1dSLionel Sambuc  *     }
51*0a6a1f1dSLionel Sambuc  */
52*0a6a1f1dSLionel Sambuc     return r + ((2 - x) & -((x & 2) == 0));
53*0a6a1f1dSLionel Sambuc }
54