Skip to content

Commit b78d0f1

Browse files
authored
Merge pull request #639 from tea/clz
Implement remaining __clz*i2 intrinsics
2 parents d86170d + 29d3466 commit b78d0f1

File tree

5 files changed

+121
-76
lines changed

5 files changed

+121
-76
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

-9
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ fn configure_check_cfg() {
164164
"__bswapsi2",
165165
"__bswapdi2",
166166
"__bswapti2",
167-
"__clzsi2",
168167
"__divdi3",
169168
"__divsi3",
170169
"__divmoddi4",
@@ -345,8 +344,6 @@ mod c {
345344
("__absvsi2", "absvsi2.c"),
346345
("__addvdi3", "addvdi3.c"),
347346
("__addvsi3", "addvsi3.c"),
348-
("__clzdi2", "clzdi2.c"),
349-
("__clzsi2", "clzsi2.c"),
350347
("__cmpdi2", "cmpdi2.c"),
351348
("__ctzdi2", "ctzdi2.c"),
352349
("__ctzsi2", "ctzsi2.c"),
@@ -382,7 +379,6 @@ mod c {
382379
sources.extend(&[
383380
("__absvti2", "absvti2.c"),
384381
("__addvti3", "addvti3.c"),
385-
("__clzti2", "clzti2.c"),
386382
("__cmpti2", "cmpti2.c"),
387383
("__ctzti2", "ctzti2.c"),
388384
("__ffsti2", "ffsti2.c"),
@@ -435,8 +431,6 @@ mod c {
435431
("__aeabi_frsub", "arm/aeabi_frsub.c"),
436432
("__bswapdi2", "arm/bswapdi2.S"),
437433
("__bswapsi2", "arm/bswapsi2.S"),
438-
("__clzdi2", "arm/clzdi2.S"),
439-
("__clzsi2", "arm/clzsi2.S"),
440434
("__divmodsi4", "arm/divmodsi4.S"),
441435
("__divsi3", "arm/divsi3.S"),
442436
("__modsi3", "arm/modsi3.S"),
@@ -572,9 +566,6 @@ mod c {
572566
}
573567
}
574568
sources.remove(&to_remove);
575-
576-
// But use some generic implementations where possible
577-
sources.extend(&[("__clzdi2", "clzdi2.c"), ("__clzsi2", "clzsi2.c")])
578569
}
579570

580571
if llvm_target[0] == "thumbv7m" || llvm_target[0] == "thumbv7em" {

src/int/leading_zeros.rs

+54-38
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+
const { 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+
const { 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,55 +98,68 @@ 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+
const { 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+
const { 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

134137
intrinsics! {
135-
#[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 {
138+
/// Returns the number of leading binary zeros in `x`
139+
pub extern "C" fn __clzsi2(x: u32) -> usize {
143140
if cfg!(any(target_arch = "riscv32", target_arch = "riscv64")) {
144-
usize_leading_zeros_riscv(x)
141+
leading_zeros_riscv(x)
142+
} else {
143+
leading_zeros_default(x)
144+
}
145+
}
146+
147+
/// Returns the number of leading binary zeros in `x`
148+
pub extern "C" fn __clzdi2(x: u64) -> usize {
149+
if cfg!(any(target_arch = "riscv32", target_arch = "riscv64")) {
150+
leading_zeros_riscv(x)
151+
} else {
152+
leading_zeros_default(x)
153+
}
154+
}
155+
156+
/// Returns the number of leading binary zeros in `x`
157+
pub extern "C" fn __clzti2(x: u128) -> usize {
158+
let hi = (x >> 64) as u64;
159+
if hi == 0 {
160+
64 + __clzdi2(x as u64)
145161
} else {
146-
usize_leading_zeros_default(x)
162+
__clzdi2(hi)
147163
}
148164
}
149165
}

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

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

95134
#[test]

0 commit comments

Comments
 (0)