Skip to content

Commit b4ab209

Browse files
committed
Implement remaining __clz*i2 intrinsics
1 parent 06db2de commit b4ab209

File tree

5 files changed

+52
-166
lines changed

5 files changed

+52
-166
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ rely on CI.
157157
- [x] bswapdi2.c
158158
- [x] bswapsi2.c
159159
- [x] bswapti2.c
160+
- [x] clzdi2.c
161+
- [x] clzsi2.c
162+
- [x] clzti2.c
160163
- [x] comparedf2.c
161164
- [x] comparesf2.c
162165
- [x] divdf3.c
@@ -325,9 +328,6 @@ These builtins are never called by LLVM.
325328
- ~~arm/switch32.S~~
326329
- ~~arm/switch8.S~~
327330
- ~~arm/switchu8.S~~
328-
- ~~clzdi2.c~~
329-
- ~~clzsi2.c~~
330-
- ~~clzti2.c~~
331331
- ~~cmpdi2.c~~
332332
- ~~cmpti2.c~~
333333
- ~~ctzdi2.c~~

build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ fn configure_check_cfg() {
165165
"__bswapdi2",
166166
"__bswapti2",
167167
"__clzsi2",
168+
"__clzdi2",
169+
"__clzti2",
168170
"__divdi3",
169171
"__divsi3",
170172
"__divmoddi4",

src/int/leading_zeros.rs

+12-142
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,19 @@
1-
// Note: these functions happen to produce the correct `usize::leading_zeros(0)` value
2-
// without a explicit zero check. Zero is probably common enough that it could warrant
3-
// adding a zero check at the beginning, but `__clzsi2` has a precondition that `x != 0`.
4-
// Compilers will insert the check for zero in cases where it is needed.
5-
6-
public_test_dep! {
7-
/// Returns the number of leading binary zeros in `x`.
8-
#[allow(dead_code)]
9-
pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {
10-
// The basic idea is to test if the higher bits of `x` are zero and bisect the number
11-
// of leading zeros. It is possible for all branches of the bisection to use the same
12-
// code path by conditionally shifting the higher parts down to let the next bisection
13-
// step work on the higher or lower parts of `x`. Instead of starting with `z == 0`
14-
// and adding to the number of zeros, it is slightly faster to start with
15-
// `z == usize::MAX.count_ones()` and subtract from the potential number of zeros,
16-
// because it simplifies the final bisection step.
17-
let mut x = x;
18-
// the number of potential leading zeros
19-
let mut z = usize::MAX.count_ones() as usize;
20-
// a temporary
21-
let mut t: usize;
22-
#[cfg(target_pointer_width = "64")]
23-
{
24-
t = x >> 32;
25-
if t != 0 {
26-
z -= 32;
27-
x = t;
28-
}
29-
}
30-
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
31-
{
32-
t = x >> 16;
33-
if t != 0 {
34-
z -= 16;
35-
x = t;
36-
}
37-
}
38-
t = x >> 8;
39-
if t != 0 {
40-
z -= 8;
41-
x = t;
42-
}
43-
t = x >> 4;
44-
if t != 0 {
45-
z -= 4;
46-
x = t;
47-
}
48-
t = x >> 2;
49-
if t != 0 {
50-
z -= 2;
51-
x = t;
52-
}
53-
// the last two bisections are combined into one conditional
54-
t = x >> 1;
55-
if t != 0 {
56-
z - 2
57-
} else {
58-
z - x
1+
intrinsics! {
2+
#[maybe_use_optimized_c_shim]
3+
/// Returns the number of leading binary zeros in `x` (SI aka 32 bit version)
4+
pub extern "C" fn __clzsi2(x: u32) -> usize {
5+
x.leading_zeros() as usize
596
}
607

61-
// We could potentially save a few cycles by using the LUT trick from
62-
// "https://embeddedgurus.com/state-space/2014/09/
63-
// fast-deterministic-and-portable-counting-leading-zeros/".
64-
// However, 256 bytes for a LUT is too large for embedded use cases. We could remove
65-
// the last 3 bisections and use this 16 byte LUT for the rest of the work:
66-
//const LUT: [u8; 16] = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4];
67-
//z -= LUT[x] as usize;
68-
//z
69-
// However, it ends up generating about the same number of instructions. When benchmarked
70-
// on x86_64, it is slightly faster to use the LUT, but this is probably because of OOO
71-
// execution effects. Changing to using a LUT and branching is risky for smaller cores.
72-
}
73-
}
74-
75-
// The above method does not compile well on RISC-V (because of the lack of predicated
76-
// instructions), producing code with many branches or using an excessively long
77-
// branchless solution. This method takes advantage of the set-if-less-than instruction on
78-
// RISC-V that allows `(x >= power-of-two) as usize` to be branchless.
79-
80-
public_test_dep! {
81-
/// Returns the number of leading binary zeros in `x`.
82-
#[allow(dead_code)]
83-
pub(crate) fn usize_leading_zeros_riscv(x: usize) -> usize {
84-
let mut x = x;
85-
// the number of potential leading zeros
86-
let mut z = usize::MAX.count_ones() as usize;
87-
// a temporary
88-
let mut t: usize;
89-
90-
// RISC-V does not have a set-if-greater-than-or-equal instruction and
91-
// `(x >= power-of-two) as usize` will get compiled into two instructions, but this is
92-
// still the most optimal method. A conditional set can only be turned into a single
93-
// immediate instruction if `x` is compared with an immediate `imm` (that can fit into
94-
// 12 bits) like `x < imm` but not `imm < x` (because the immediate is always on the
95-
// right). If we try to save an instruction by using `x < imm` for each bisection, we
96-
// have to shift `x` left and compare with powers of two approaching `usize::MAX + 1`,
97-
// but the immediate will never fit into 12 bits and never save an instruction.
98-
#[cfg(target_pointer_width = "64")]
99-
{
100-
// If the upper 32 bits of `x` are not all 0, `t` is set to `1 << 5`, otherwise
101-
// `t` is set to 0.
102-
t = ((x >= (1 << 32)) as usize) << 5;
103-
// If `t` was set to `1 << 5`, then the upper 32 bits are shifted down for the
104-
// next step to process.
105-
x >>= t;
106-
// If `t` was set to `1 << 5`, then we subtract 32 from the number of potential
107-
// leading zeros
108-
z -= t;
109-
}
110-
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
111-
{
112-
t = ((x >= (1 << 16)) as usize) << 4;
113-
x >>= t;
114-
z -= t;
8+
#[maybe_use_optimized_c_shim]
9+
/// Returns the number of leading binary zeros in `x` (DI aka 64 bit version).
10+
pub extern "C" fn __clzdi2(x: u64) -> usize {
11+
x.leading_zeros() as usize
11512
}
116-
t = ((x >= (1 << 8)) as usize) << 3;
117-
x >>= t;
118-
z -= t;
119-
t = ((x >= (1 << 4)) as usize) << 2;
120-
x >>= t;
121-
z -= t;
122-
t = ((x >= (1 << 2)) as usize) << 1;
123-
x >>= t;
124-
z -= t;
125-
t = (x >= (1 << 1)) as usize;
126-
x >>= t;
127-
z -= t;
128-
// All bits except the LSB are guaranteed to be zero for this final bisection step.
129-
// If `x != 0` then `x == 1` and subtracts one potential zero from `z`.
130-
z - x
131-
}
132-
}
13313

134-
intrinsics! {
13514
#[maybe_use_optimized_c_shim]
136-
#[cfg(any(
137-
target_pointer_width = "16",
138-
target_pointer_width = "32",
139-
target_pointer_width = "64"
140-
))]
141-
/// Returns the number of leading binary zeros in `x`.
142-
pub extern "C" fn __clzsi2(x: usize) -> usize {
143-
if cfg!(any(target_arch = "riscv32", target_arch = "riscv64")) {
144-
usize_leading_zeros_riscv(x)
145-
} else {
146-
usize_leading_zeros_default(x)
147-
}
15+
/// Returns the number of leading binary zeros in `x` (TI mode int aka int128_t).
16+
pub extern "C" fn __clzti2(x: u128) -> usize {
17+
x.leading_zeros() as usize
14818
}
14919
}

src/int/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub mod shift;
1212
pub mod udiv;
1313

1414
pub use big::{i256, u256};
15-
pub use leading_zeros::__clzsi2;
1615

1716
public_test_dep! {
1817
/// Minimal integer implementations needed on all integer types, including wide integers.

testcrate/tests/misc.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,46 @@ fn fuzz_values() {
6565

6666
#[test]
6767
fn leading_zeros() {
68-
use compiler_builtins::int::__clzsi2;
69-
use compiler_builtins::int::leading_zeros::{
70-
usize_leading_zeros_default, usize_leading_zeros_riscv,
71-
};
72-
fuzz(N, |x: usize| {
68+
use compiler_builtins::int::leading_zeros::__clzsi2;
69+
fuzz(N, |x: u32| {
70+
if x == 0 {
71+
return; // undefined value for an intrinsic
72+
}
7373
let lz = x.leading_zeros() as usize;
7474
let lz0 = __clzsi2(x);
75-
let lz1 = usize_leading_zeros_default(x);
76-
let lz2 = usize_leading_zeros_riscv(x);
7775
if lz0 != lz {
7876
panic!("__clzsi2({}): std: {}, builtins: {}", x, lz, lz0);
7977
}
80-
if lz1 != lz {
81-
panic!(
82-
"usize_leading_zeros_default({}): std: {}, builtins: {}",
83-
x, lz, lz1
84-
);
85-
}
86-
if lz2 != lz {
87-
panic!(
88-
"usize_leading_zeros_riscv({}): std: {}, builtins: {}",
89-
x, lz, lz2
90-
);
91-
}
92-
})
78+
});
79+
80+
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
81+
{
82+
use compiler_builtins::int::leading_zeros::__clzdi2;
83+
fuzz(N, |x: u64| {
84+
if x == 0 {
85+
return; // undefined value for an intrinsic
86+
}
87+
let lz = x.leading_zeros() as usize;
88+
let lz0 = __clzdi2(x);
89+
if lz0 != lz {
90+
panic!("__clzdi2({}): std: {}, builtins: {}", x, lz, lz0);
91+
}
92+
});
93+
}
94+
#[cfg(target_pointer_width = "64")]
95+
{
96+
use compiler_builtins::int::leading_zeros::__clzti2;
97+
fuzz(N, |x: u128| {
98+
if x == 0 {
99+
return; // undefined value for an intrinsic
100+
}
101+
let lz = x.leading_zeros() as usize;
102+
let lz0 = __clzti2(x);
103+
if lz0 != lz {
104+
panic!("__clzti2({}): std: {}, builtins: {}", x, lz, lz0);
105+
}
106+
});
107+
}
93108
}
94109

95110
#[test]

0 commit comments

Comments
 (0)