1*7c3d14c8STreehugger Robot /* ===-- ctzsi2.c - Implement __ctzsi2 -------------------------------------===
2*7c3d14c8STreehugger Robot *
3*7c3d14c8STreehugger Robot * The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot *
5*7c3d14c8STreehugger Robot * This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot * Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot *
8*7c3d14c8STreehugger Robot * ===----------------------------------------------------------------------===
9*7c3d14c8STreehugger Robot *
10*7c3d14c8STreehugger Robot * This file implements __ctzsi2 for the compiler_rt library.
11*7c3d14c8STreehugger Robot *
12*7c3d14c8STreehugger Robot * ===----------------------------------------------------------------------===
13*7c3d14c8STreehugger Robot */
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot #include "int_lib.h"
16*7c3d14c8STreehugger Robot
17*7c3d14c8STreehugger Robot /* Returns: the number of trailing 0-bits */
18*7c3d14c8STreehugger Robot
19*7c3d14c8STreehugger Robot /* Precondition: a != 0 */
20*7c3d14c8STreehugger Robot
21*7c3d14c8STreehugger Robot COMPILER_RT_ABI si_int
__ctzsi2(si_int a)22*7c3d14c8STreehugger Robot __ctzsi2(si_int a)
23*7c3d14c8STreehugger Robot {
24*7c3d14c8STreehugger Robot su_int x = (su_int)a;
25*7c3d14c8STreehugger Robot si_int t = ((x & 0x0000FFFF) == 0) << 4; /* if (x has no small bits) t = 16 else 0 */
26*7c3d14c8STreehugger Robot x >>= t; /* x = [0 - 0xFFFF] + higher garbage bits */
27*7c3d14c8STreehugger Robot su_int r = t; /* r = [0, 16] */
28*7c3d14c8STreehugger Robot /* return r + ctz(x) */
29*7c3d14c8STreehugger Robot t = ((x & 0x00FF) == 0) << 3;
30*7c3d14c8STreehugger Robot x >>= t; /* x = [0 - 0xFF] + higher garbage bits */
31*7c3d14c8STreehugger Robot r += t; /* r = [0, 8, 16, 24] */
32*7c3d14c8STreehugger Robot /* return r + ctz(x) */
33*7c3d14c8STreehugger Robot t = ((x & 0x0F) == 0) << 2;
34*7c3d14c8STreehugger Robot x >>= t; /* x = [0 - 0xF] + higher garbage bits */
35*7c3d14c8STreehugger Robot r += t; /* r = [0, 4, 8, 12, 16, 20, 24, 28] */
36*7c3d14c8STreehugger Robot /* return r + ctz(x) */
37*7c3d14c8STreehugger Robot t = ((x & 0x3) == 0) << 1;
38*7c3d14c8STreehugger Robot x >>= t;
39*7c3d14c8STreehugger Robot x &= 3; /* x = [0 - 3] */
40*7c3d14c8STreehugger Robot r += t; /* r = [0 - 30] and is even */
41*7c3d14c8STreehugger Robot /* return r + ctz(x) */
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger Robot /* The branch-less return statement below is equivalent
44*7c3d14c8STreehugger Robot * to the following switch statement:
45*7c3d14c8STreehugger Robot * switch (x)
46*7c3d14c8STreehugger Robot * {
47*7c3d14c8STreehugger Robot * case 0:
48*7c3d14c8STreehugger Robot * return r + 2;
49*7c3d14c8STreehugger Robot * case 2:
50*7c3d14c8STreehugger Robot * return r + 1;
51*7c3d14c8STreehugger Robot * case 1:
52*7c3d14c8STreehugger Robot * case 3:
53*7c3d14c8STreehugger Robot * return r;
54*7c3d14c8STreehugger Robot * }
55*7c3d14c8STreehugger Robot */
56*7c3d14c8STreehugger Robot return r + ((2 - (x >> 1)) & -((x & 1) == 0));
57*7c3d14c8STreehugger Robot }
58