1 //===-- clzsi2.c - Implement __clzsi2 -------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements __clzsi2 for the compiler_rt library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "int_lib.h" 14 15 // Returns: the number of leading 0-bits 16 17 // Precondition: a != 0 18 19 COMPILER_RT_ABI int __clzsi2(si_int a) { 20 su_int x = (su_int)a; 21 si_int t = ((x & 0xFFFF0000) == 0) << 4; // if (x is small) t = 16 else 0 22 x >>= 16 - t; // x = [0 - 0xFFFF] 23 su_int r = t; // r = [0, 16] 24 // return r + clz(x) 25 t = ((x & 0xFF00) == 0) << 3; 26 x >>= 8 - t; // x = [0 - 0xFF] 27 r += t; // r = [0, 8, 16, 24] 28 // return r + clz(x) 29 t = ((x & 0xF0) == 0) << 2; 30 x >>= 4 - t; // x = [0 - 0xF] 31 r += t; // r = [0, 4, 8, 12, 16, 20, 24, 28] 32 // return r + clz(x) 33 t = ((x & 0xC) == 0) << 1; 34 x >>= 2 - t; // x = [0 - 3] 35 r += t; // r = [0 - 30] and is even 36 // return r + clz(x) 37 // switch (x) 38 // { 39 // case 0: 40 // return r + 2; 41 // case 1: 42 // return r + 1; 43 // case 2: 44 // case 3: 45 // return r; 46 // } 47 return r + ((2 - x) & -((x & 2) == 0)); 48 } 49