1209231Sjchandra/* ===-- clzsi2.c - Implement __clzsi2 -------------------------------------=== 2178580Simp * 3178580Simp * The LLVM Compiler Infrastructure 4178580Simp * 5178580Simp * This file is dual licensed under the MIT and the University of Illinois Open 6178580Simp * Source Licenses. See LICENSE.TXT for details. 7178580Simp * 8178580Simp * ===----------------------------------------------------------------------=== 9178580Simp * 10178580Simp * This file implements __clzsi2 for the compiler_rt library. 11178580Simp * 12178580Simp * ===----------------------------------------------------------------------=== 13178580Simp */ 14178580Simp 15178580Simp#include "int_lib.h" 16178580Simp 17178580Simp/* Returns: the number of leading 0-bits */ 18178580Simp 19178580Simp/* Precondition: a != 0 */ 20178580Simp 21178580SimpCOMPILER_RT_ABI si_int 22178580Simp__clzsi2(si_int a) 23178580Simp{ 24178580Simp su_int x = (su_int)a; 25178580Simp si_int t = ((x & 0xFFFF0000) == 0) << 4; /* if (x is small) t = 16 else 0 */ 26178580Simp x >>= 16 - t; /* x = [0 - 0xFFFF] */ 27178580Simp su_int r = t; /* r = [0, 16] */ 28178580Simp /* return r + clz(x) */ 29178580Simp t = ((x & 0xFF00) == 0) << 3; 30178580Simp x >>= 8 - t; /* x = [0 - 0xFF] */ 31178580Simp r += t; /* r = [0, 8, 16, 24] */ 32178580Simp /* return r + clz(x) */ 33178580Simp t = ((x & 0xF0) == 0) << 2; 34178580Simp x >>= 4 - t; /* x = [0 - 0xF] */ 35178580Simp r += t; /* r = [0, 4, 8, 12, 16, 20, 24, 28] */ 36178580Simp /* return r + clz(x) */ 37178580Simp t = ((x & 0xC) == 0) << 1; 38178580Simp x >>= 2 - t; /* x = [0 - 3] */ 39178580Simp r += t; /* r = [0 - 30] and is even */ 40209231Sjchandra /* return r + clz(x) */ 41178580Simp/* switch (x) 42178580Simp * { 43178580Simp * case 0: 44178580Simp * return r + 2; 45178580Simp * case 1: 46178580Simp * return r + 1; 47178580Simp * case 2: 48178580Simp * case 3: 49178580Simp * return r; 50178580Simp * } 51178580Simp */ 52178580Simp return r + ((2 - x) & -((x & 2) == 0)); 53178580Simp} 54178580Simp