llvm-project
48 строк · 1.5 Кб
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
19COMPILER_RT_ABI int __clzsi2(si_int a) {20su_int x = (su_int)a;21si_int t = ((x & 0xFFFF0000) == 0) << 4; // if (x is small) t = 16 else 022x >>= 16 - t; // x = [0 - 0xFFFF]23su_int r = t; // r = [0, 16]24// return r + clz(x)25t = ((x & 0xFF00) == 0) << 3;26x >>= 8 - t; // x = [0 - 0xFF]27r += t; // r = [0, 8, 16, 24]28// return r + clz(x)29t = ((x & 0xF0) == 0) << 2;30x >>= 4 - t; // x = [0 - 0xF]31r += t; // r = [0, 4, 8, 12, 16, 20, 24, 28]32// return r + clz(x)33t = ((x & 0xC) == 0) << 1;34x >>= 2 - t; // x = [0 - 3]35r += t; // r = [0 - 30] and is even36// 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// }47return r + ((2 - x) & -((x & 2) == 0));48}
49