Home | History | Annotate | Download | only in lib
      1 /* ===-- clzsi2.c - Implement __clzsi2 -------------------------------------===
      2  *
      3  *               The LLVM Compiler Infrastructure
      4  *
      5  * This file is dual licensed under the MIT and the University of Illinois Open
      6  * Source Licenses. See LICENSE.TXT for details.
      7  *
      8  * ===----------------------------------------------------------------------===
      9  *
     10  * This file implements __clzsi2 for the compiler_rt library.
     11  *
     12  * ===----------------------------------------------------------------------===
     13  */
     14 
     15 #include "int_lib.h"
     16 
     17 /* Returns: the number of leading 0-bits */
     18 
     19 /* Precondition: a != 0 */
     20 
     21 COMPILER_RT_ABI si_int
     22 __clzsi2(si_int a)
     23 {
     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  *     }
     51  */
     52     return r + ((2 - x) & -((x & 2) == 0));
     53 }
     54