blob: 7b173d3c858cbe44234dcaacc468e303ee049bc6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* libgcc replacement - count leading bits
*
* Copyright 2025, Heinrich Schuchardt <[email protected]>
*/
#include <linux/types.h>
/**
* __clzti2() - count number of leading zero bits
*
* @x: number to check
* Return: number of leading zero bits
*/
int __clzti2(long long x)
{
int ret = 64;
if (!x)
return 64;
if (x & 0xFFFFFFFF00000000LL) {
ret -= 32;
x >>= 32;
}
if (x & 0xFFFF0000LL) {
ret -= 16;
x >>= 16;
}
if (x & 0xFF00LL) {
ret -= 8;
x >>= 8;
}
if (x & 0xF0LL) {
ret -= 4;
x >>= 4;
}
if (x & 0xCLL) {
ret -= 2;
x >>= 2;
}
if (x & 0x2LL) {
ret -= 1;
x >>= 1;
}
if (x)
ret -= 1;
return ret;
}
/**
* __clzsi2() - count number of leading zero bits
*
* @x: number to check
* Return: number of leading zero bits
*/
int __clzsi2(int x)
{
int ret = 32;
if (!x)
return 32;
if (x & 0xFFFF0000) {
ret -= 16;
x >>= 16;
}
if (x & 0xFF00) {
ret -= 8;
x >>= 8;
}
if (x & 0xF0) {
ret -= 4;
x >>= 4;
}
if (x & 0xC) {
ret -= 2;
x >>= 2;
}
if (x & 0x2) {
ret -= 1;
x >>= 1;
}
if (x)
ret -= 1;
return ret;
}
/**
* __clzdi2() - count number of leading zero bits
*
* @x: number to check
* Return: number of leading zero bits
*/
int __clzdi2(long x)
{
#if BITS_PER_LONG == 64
return __clzti2(x);
#else
return __clzsi2(x);
#endif
}
|