Skip to content

Commit e21b939

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

File tree

5 files changed

+133
-63
lines changed

5 files changed

+133
-63
lines changed

Diff for: 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~~

Diff for: build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ fn configure_check_cfg() {
165165
"__bswapdi2",
166166
"__bswapti2",
167167
"__clzsi2",
168+
"__clzdi2",
168169
"__divdi3",
169170
"__divsi3",
170171
"__divmoddi4",
@@ -382,7 +383,6 @@ mod c {
382383
sources.extend(&[
383384
("__absvti2", "absvti2.c"),
384385
("__addvti3", "addvti3.c"),
385-
("__clzti2", "clzti2.c"),
386386
("__cmpti2", "cmpti2.c"),
387387
("__ctzti2", "ctzti2.c"),
388388
("__ffsti2", "ffsti2.c"),

Diff for: src/int/leading_zeros.rs

+62-33
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// adding a zero check at the beginning, but `__clzsi2` has a precondition that `x != 0`.
44
// Compilers will insert the check for zero in cases where it is needed.
55

6+
use crate::int::{CastInto, Int};
7+
68
public_test_dep! {
79
/// Returns the number of leading binary zeros in `x`.
810
#[allow(dead_code)]
9-
pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {
11+
pub(crate) fn leading_zeros_default<T: Int + CastInto<usize>>(x: T) -> usize {
1012
// The basic idea is to test if the higher bits of `x` are zero and bisect the number
1113
// of leading zeros. It is possible for all branches of the bisection to use the same
1214
// code path by conditionally shifting the higher parts down to let the next bisection
@@ -16,46 +18,47 @@ pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {
1618
// because it simplifies the final bisection step.
1719
let mut x = x;
1820
// the number of potential leading zeros
19-
let mut z = usize::MAX.count_ones() as usize;
21+
let mut z = T::BITS as usize;
2022
// a temporary
21-
let mut t: usize;
22-
#[cfg(target_pointer_width = "64")]
23-
{
23+
let mut t: T;
24+
25+
assert!(T::BITS <= 64);
26+
if T::BITS >= 64 {
2427
t = x >> 32;
25-
if t != 0 {
28+
if t != T::ZERO {
2629
z -= 32;
2730
x = t;
2831
}
2932
}
30-
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
31-
{
33+
if T::BITS >= 32 {
3234
t = x >> 16;
33-
if t != 0 {
35+
if t != T::ZERO {
3436
z -= 16;
3537
x = t;
3638
}
3739
}
40+
assert!(T::BITS >= 16);
3841
t = x >> 8;
39-
if t != 0 {
42+
if t != T::ZERO {
4043
z -= 8;
4144
x = t;
4245
}
4346
t = x >> 4;
44-
if t != 0 {
47+
if t != T::ZERO {
4548
z -= 4;
4649
x = t;
4750
}
4851
t = x >> 2;
49-
if t != 0 {
52+
if t != T::ZERO {
5053
z -= 2;
5154
x = t;
5255
}
5356
// the last two bisections are combined into one conditional
5457
t = x >> 1;
55-
if t != 0 {
58+
if t != T::ZERO {
5659
z - 2
5760
} else {
58-
z - x
61+
z - x.cast()
5962
}
6063

6164
// We could potentially save a few cycles by using the LUT trick from
@@ -80,12 +83,12 @@ pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {
8083
public_test_dep! {
8184
/// Returns the number of leading binary zeros in `x`.
8285
#[allow(dead_code)]
83-
pub(crate) fn usize_leading_zeros_riscv(x: usize) -> usize {
86+
pub(crate) fn leading_zeros_riscv<T: Int + CastInto<usize>>(x: T) -> usize {
8487
let mut x = x;
8588
// the number of potential leading zeros
86-
let mut z = usize::MAX.count_ones() as usize;
89+
let mut z = T::BITS;
8790
// a temporary
88-
let mut t: usize;
91+
let mut t: u32;
8992

9093
// RISC-V does not have a set-if-greater-than-or-equal instruction and
9194
// `(x >= power-of-two) as usize` will get compiled into two instructions, but this is
@@ -95,39 +98,39 @@ pub(crate) fn usize_leading_zeros_riscv(x: usize) -> usize {
9598
// right). If we try to save an instruction by using `x < imm` for each bisection, we
9699
// have to shift `x` left and compare with powers of two approaching `usize::MAX + 1`,
97100
// but the immediate will never fit into 12 bits and never save an instruction.
98-
#[cfg(target_pointer_width = "64")]
99-
{
101+
assert!(T::BITS <= 64);
102+
if T::BITS >= 64 {
100103
// If the upper 32 bits of `x` are not all 0, `t` is set to `1 << 5`, otherwise
101104
// `t` is set to 0.
102-
t = ((x >= (1 << 32)) as usize) << 5;
105+
t = ((x >= (T::ONE << 32)) as u32) << 5;
103106
// If `t` was set to `1 << 5`, then the upper 32 bits are shifted down for the
104107
// next step to process.
105108
x >>= t;
106109
// If `t` was set to `1 << 5`, then we subtract 32 from the number of potential
107110
// leading zeros
108111
z -= t;
109112
}
110-
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
111-
{
112-
t = ((x >= (1 << 16)) as usize) << 4;
113+
if T::BITS >= 32 {
114+
t = ((x >= (T::ONE << 16)) as u32) << 4;
113115
x >>= t;
114116
z -= t;
115117
}
116-
t = ((x >= (1 << 8)) as usize) << 3;
118+
assert!(T::BITS >= 16);
119+
t = ((x >= (T::ONE << 8)) as u32) << 3;
117120
x >>= t;
118121
z -= t;
119-
t = ((x >= (1 << 4)) as usize) << 2;
122+
t = ((x >= (T::ONE << 4)) as u32) << 2;
120123
x >>= t;
121124
z -= t;
122-
t = ((x >= (1 << 2)) as usize) << 1;
125+
t = ((x >= (T::ONE << 2)) as u32) << 1;
123126
x >>= t;
124127
z -= t;
125-
t = (x >= (1 << 1)) as usize;
128+
t = (x >= (T::ONE << 1)) as u32;
126129
x >>= t;
127130
z -= t;
128131
// All bits except the LSB are guaranteed to be zero for this final bisection step.
129132
// If `x != 0` then `x == 1` and subtracts one potential zero from `z`.
130-
z - x
133+
z as usize - x.cast()
131134
}
132135
}
133136

@@ -136,14 +139,40 @@ intrinsics! {
136139
#[cfg(any(
137140
target_pointer_width = "16",
138141
target_pointer_width = "32",
139-
target_pointer_width = "64"
140142
))]
141-
/// Returns the number of leading binary zeros in `x`.
142-
pub extern "C" fn __clzsi2(x: usize) -> usize {
143+
/// Returns the number of leading binary zeros in `x`
144+
pub extern "C" fn __clzsi2(x: u32) -> usize {
143145
if cfg!(any(target_arch = "riscv32", target_arch = "riscv64")) {
144-
usize_leading_zeros_riscv(x)
146+
leading_zeros_riscv(x)
147+
} else {
148+
leading_zeros_default(x)
149+
}
150+
}
151+
152+
#[maybe_use_optimized_c_shim]
153+
#[cfg(any(
154+
target_pointer_width = "32",
155+
target_pointer_width = "64",
156+
))]
157+
/// Returns the number of leading binary zeros in `x`
158+
pub extern "C" fn __clzdi2(x: u64) -> usize {
159+
if cfg!(any(target_arch = "riscv32", target_arch = "riscv64")) {
160+
leading_zeros_riscv(x)
161+
} else {
162+
leading_zeros_default(x)
163+
}
164+
}
165+
166+
#[cfg(any(
167+
target_pointer_width = "64"
168+
))]
169+
/// Returns the number of leading binary zeros in `x`
170+
pub extern "C" fn __clzti2(x: u128) -> usize {
171+
let hi = (x >> 64) as u64;
172+
if hi == 0 {
173+
64 + __clzdi2(x as u64)
145174
} else {
146-
usize_leading_zeros_default(x)
175+
__clzdi2(hi)
147176
}
148177
}
149178
}

Diff for: 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.

Diff for: testcrate/tests/misc.rs

+67-25
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,73 @@ 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| {
73-
let lz = x.leading_zeros() as usize;
74-
let lz0 = __clzsi2(x);
75-
let lz1 = usize_leading_zeros_default(x);
76-
let lz2 = usize_leading_zeros_riscv(x);
77-
if lz0 != lz {
78-
panic!("__clzsi2({}): std: {}, builtins: {}", x, lz, lz0);
79-
}
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-
})
68+
use compiler_builtins::int::leading_zeros::{leading_zeros_default, leading_zeros_riscv};
69+
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
70+
{
71+
use compiler_builtins::int::leading_zeros::__clzsi2;
72+
fuzz(N, |x: u32| {
73+
if x == 0 {
74+
return; // undefined value for an intrinsic
75+
}
76+
let lz = x.leading_zeros() as usize;
77+
let lz0 = __clzsi2(x);
78+
let lz1 = leading_zeros_default(x);
79+
let lz2 = leading_zeros_riscv(x);
80+
if lz0 != lz {
81+
panic!("__clzsi2({}): std: {}, builtins: {}", x, lz, lz0);
82+
}
83+
if lz1 != lz {
84+
panic!(
85+
"leading_zeros_default({}): std: {}, builtins: {}",
86+
x, lz, lz1
87+
);
88+
}
89+
if lz2 != lz {
90+
panic!("leading_zeros_riscv({}): std: {}, builtins: {}", x, lz, lz2);
91+
}
92+
});
93+
}
94+
95+
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
96+
{
97+
use compiler_builtins::int::leading_zeros::__clzdi2;
98+
fuzz(N, |x: u64| {
99+
if x == 0 {
100+
return; // undefined value for an intrinsic
101+
}
102+
let lz = x.leading_zeros() as usize;
103+
let lz0 = __clzdi2(x);
104+
let lz1 = leading_zeros_default(x);
105+
let lz2 = leading_zeros_riscv(x);
106+
if lz0 != lz {
107+
panic!("__clzdi2({}): std: {}, builtins: {}", x, lz, lz0);
108+
}
109+
if lz1 != lz {
110+
panic!(
111+
"leading_zeros_default({}): std: {}, builtins: {}",
112+
x, lz, lz1
113+
);
114+
}
115+
if lz2 != lz {
116+
panic!("leading_zeros_riscv({}): std: {}, builtins: {}", x, lz, lz2);
117+
}
118+
});
119+
}
120+
121+
#[cfg(target_pointer_width = "64")]
122+
{
123+
use compiler_builtins::int::leading_zeros::__clzti2;
124+
fuzz(N, |x: u128| {
125+
if x == 0 {
126+
return; // undefined value for an intrinsic
127+
}
128+
let lz = x.leading_zeros() as usize;
129+
let lz0 = __clzti2(x);
130+
if lz0 != lz {
131+
panic!("__clzti2({}): std: {}, builtins: {}", x, lz, lz0);
132+
}
133+
});
134+
}
93135
}
94136

95137
#[test]

0 commit comments

Comments
 (0)