[ASan/Win tests] Bring back -GS- as SEH tests fail otherwise
[blocksruntime.git] / lib / builtins / clzsi2.c
blob25b8ed2c4c240f5c0e64cad0dd3d821dd2dfaeaa
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 * ===----------------------------------------------------------------------===
15 #include "int_lib.h"
17 /* Returns: the number of leading 0-bits */
19 /* Precondition: a != 0 */
21 COMPILER_RT_ABI si_int
22 __clzsi2(si_int a)
24 su_int x = (su_int)a;
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) */
41 /* switch (x)
42 * {
43 * case 0:
44 * return r + 2;
45 * case 1:
46 * return r + 1;
47 * case 2:
48 * case 3:
49 * return r;
50 * }
52 return r + ((2 - x) & -((x & 2) == 0));