1 /* ===-- ctzsi2.c - Implement __ctzsi2 -------------------------------------===
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 __ctzsi2 for the compiler_rt library.
12 * ===----------------------------------------------------------------------===
17 /* Returns: the number of trailing 0-bits */
19 /* Precondition: a != 0 */
21 COMPILER_RT_ABI si_int
25 si_int t
= ((x
& 0x0000FFFF) == 0) << 4; /* if (x has no small bits) t = 16 else 0 */
26 x
>>= t
; /* x = [0 - 0xFFFF] + higher garbage bits */
27 su_int r
= t
; /* r = [0, 16] */
28 /* return r + ctz(x) */
29 t
= ((x
& 0x00FF) == 0) << 3;
30 x
>>= t
; /* x = [0 - 0xFF] + higher garbage bits */
31 r
+= t
; /* r = [0, 8, 16, 24] */
32 /* return r + ctz(x) */
33 t
= ((x
& 0x0F) == 0) << 2;
34 x
>>= t
; /* x = [0 - 0xF] + higher garbage bits */
35 r
+= t
; /* r = [0, 4, 8, 12, 16, 20, 24, 28] */
36 /* return r + ctz(x) */
37 t
= ((x
& 0x3) == 0) << 1;
39 x
&= 3; /* x = [0 - 3] */
40 r
+= t
; /* r = [0 - 30] and is even */
41 /* return r + ctz(x) */
43 /* The branch-less return statement below is equivalent
44 * to the following switch statement:
56 return r
+ ((2 - (x
>> 1)) & -((x
& 1) == 0));