1 //===-- clzsi2.c - Implement __clzsi2 -------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements __clzsi2 for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
15 // Returns: the number of leading 0-bits
17 // Precondition: a != 0
19 COMPILER_RT_ABI int __clzsi2(si_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]
25 t = ((x & 0xFF00) == 0) << 3;
26 x >>= 8 - t; // x = [0 - 0xFF]
27 r += t; // r = [0, 8, 16, 24]
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]
33 t = ((x & 0xC) == 0) << 1;
34 x >>= 2 - t; // x = [0 - 3]
35 r += t; // r = [0 - 30] and is even
47 return r + ((2 - x) & -((x & 2) == 0));