Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/clzsi2.c
35260 views
//===-- clzsi2.c - Implement __clzsi2 -------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements __clzsi2 for the compiler_rt library.9//10//===----------------------------------------------------------------------===//1112#include "int_lib.h"1314// Returns: the number of leading 0-bits1516// Precondition: a != 01718COMPILER_RT_ABI int __clzsi2(si_int a) {19su_int x = (su_int)a;20si_int t = ((x & 0xFFFF0000) == 0) << 4; // if (x is small) t = 16 else 021x >>= 16 - t; // x = [0 - 0xFFFF]22su_int r = t; // r = [0, 16]23// return r + clz(x)24t = ((x & 0xFF00) == 0) << 3;25x >>= 8 - t; // x = [0 - 0xFF]26r += t; // r = [0, 8, 16, 24]27// return r + clz(x)28t = ((x & 0xF0) == 0) << 2;29x >>= 4 - t; // x = [0 - 0xF]30r += t; // r = [0, 4, 8, 12, 16, 20, 24, 28]31// return r + clz(x)32t = ((x & 0xC) == 0) << 1;33x >>= 2 - t; // x = [0 - 3]34r += t; // r = [0 - 30] and is even35// return r + clz(x)36// switch (x)37// {38// case 0:39// return r + 2;40// case 1:41// return r + 1;42// case 2:43// case 3:44// return r;45// }46return r + ((2 - x) & -((x & 2) == 0));47}484950