Skip to content

Commit 3a22529

Browse files
committed
Improve __clzsi2 performance
1 parent f3846bc commit 3a22529

File tree

6 files changed

+237
-90
lines changed

6 files changed

+237
-90
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ c = ["cc"]
4747
# Flag this library as the unstable compiler-builtins lib
4848
compiler-builtins = []
4949

50+
# Some functions have alternative implementations only enabled on platforms that are not
51+
# tested by CI, so this flag makes these alternative implementations public for testing.
52+
expose-alt-impls = []
53+
5054
# Generate memory-related intrinsics like memcpy
5155
mem = []
5256

src/int/leading_zeros.rs

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// simplify configuration
2+
#![allow(dead_code)]
3+
4+
// Note: `usize_leading_zeros` happens to produce the correct `usize::leading_zeros(0)`
5+
// value without a explicit zero check. Zero is probably common enough that it could
6+
// warrant adding a zero check at the beginning, but `__clzsi2` has a precondition that
7+
// `x != 0`. Compilers will insert the check for zero in cases where it is needed.
8+
9+
/// Returns the number of leading binary zeros in `x`.
10+
pub fn usize_leading_zeros_default(x: usize) -> usize {
11+
// The basic idea is to test if the higher bits of `x` are zero and bisect the number
12+
// of leading zeros. It is possible for all branches of the bisection to use the same
13+
// code path by conditionally shifting the higher parts down to let the next bisection
14+
// step work on the higher or lower parts of `x`. Instead of starting with `z == 0`
15+
// and adding to the number of zeros, it is slightly faster to start with
16+
// `z == usize::MAX.count_ones()` and subtract from the potential number of zeros,
17+
// because it simplifies the final bisection step.
18+
let mut x = x;
19+
// the number of potential leading zeros
20+
let mut z = usize::MAX.count_ones() as usize;
21+
// a temporary
22+
let mut t: usize;
23+
#[cfg(target_pointer_width = "64")]
24+
{
25+
t = x >> 32;
26+
if t != 0 {
27+
z -= 32;
28+
x = t;
29+
}
30+
}
31+
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
32+
{
33+
t = x >> 16;
34+
if t != 0 {
35+
z -= 16;
36+
x = t;
37+
}
38+
}
39+
t = x >> 8;
40+
if t != 0 {
41+
z -= 8;
42+
x = t;
43+
}
44+
t = x >> 4;
45+
if t != 0 {
46+
z -= 4;
47+
x = t;
48+
}
49+
t = x >> 2;
50+
if t != 0 {
51+
z -= 2;
52+
x = t;
53+
}
54+
// the last two bisections are combined into one conditional
55+
t = x >> 1;
56+
if t != 0 {
57+
z - 2
58+
} else {
59+
z - x
60+
}
61+
62+
// We could potentially save a few cycles by using the LUT trick from
63+
// "https://embeddedgurus.com/state-space/2014/09/
64+
// fast-deterministic-and-portable-counting-leading-zeros/".
65+
// However, 256 bytes for a LUT is too large for embedded use cases. We could remove the
66+
// last 3 bisections and use this 16 byte LUT for the rest of the work:
67+
//const LUT: [u8; 16] = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4];
68+
//z -= LUT[x] as usize;
69+
//z
70+
// However, it ends up generating about the same number of instructions. When benchmarked
71+
// on x86_64, it is slightly faster to use the LUT, but this is probably because of OOO
72+
// execution effects. Changing to using a LUT and branching is risky for smaller cores.
73+
}
74+
75+
// The above method does not compile well on RISC-V, producing code with short branches or
76+
// using an excessively long branchless solution. This method takes advantage of the
77+
// set-if-less-than instruction on RISC-V that allows `(x >= power-of-two) as usize` to be
78+
// branchless.
79+
80+
/// Returns the number of leading binary zeros in `x`.
81+
pub fn usize_leading_zeros_riscv(x: usize) -> usize {
82+
let mut x = x;
83+
// the number of potential leading zeros
84+
let mut z = usize::MAX.count_ones() as usize;
85+
// a temporary
86+
let mut t: usize;
87+
88+
// RISC-V does not have a set-if-greater-than-or-equal instruction and
89+
// `(x >= power-of-two) as usize` will get compiled into two instructions, but this is
90+
// still the most optimal method. A conditional set can only be turned into a single
91+
// immediate instruction if `x` is compared with an immediate `imm` (that can fit into
92+
// 12 bits) like `x < imm` but not `imm < x` (because the immediate is always on the
93+
// right). If we try to save an instruction by using `x < imm` for each bisection, we
94+
// have to shift `x` left and compare with powers of two approaching `usize::MAX + 1`,
95+
// but the immediate will never fit into 12 bits and never save an instruction.
96+
#[cfg(target_pointer_width = "64")]
97+
{
98+
// If the upper 32 bits of `x` are not all 0, `t` is set to `1 << 5`, otherwise `t` is
99+
// set to 0.
100+
t = ((x >= (1 << 32)) as usize) << 5;
101+
// If `t` was set to `1 << 5`, then the upper 32 bits are shifted down for the next step
102+
// to process.
103+
x >>= t;
104+
// If `t` was set to `1 << 5`, then we subtract 32 from the number of potential leading
105+
// zeros
106+
z -= t;
107+
}
108+
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
109+
{
110+
t = ((x >= (1 << 16)) as usize) << 4;
111+
x >>= t;
112+
z -= t;
113+
}
114+
t = ((x >= (1 << 8)) as usize) << 3;
115+
x >>= t;
116+
z -= t;
117+
t = ((x >= (1 << 4)) as usize) << 2;
118+
x >>= t;
119+
z -= t;
120+
t = ((x >= (1 << 2)) as usize) << 1;
121+
x >>= t;
122+
z -= t;
123+
t = (x >= (1 << 1)) as usize;
124+
x >>= t;
125+
z -= t;
126+
// All bits except the LSB are guaranteed to be zero for this final bisection step.
127+
// If `x != 0` then `x == 1` and subtracts one potential zero from `z`.
128+
z - x
129+
}
130+
131+
#[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64")))]
132+
intrinsics! {
133+
#[maybe_use_optimized_c_shim]
134+
#[cfg(any(
135+
target_pointer_width = "16",
136+
target_pointer_width = "32",
137+
target_pointer_width = "64"
138+
))]
139+
/// Returns the number of leading binary zeros in `x`.
140+
pub extern "C" fn __clzsi2(x: usize) -> usize {
141+
usize_leading_zeros_default(x)
142+
}
143+
}
144+
145+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
146+
intrinsics! {
147+
#[maybe_use_optimized_c_shim]
148+
#[cfg(any(
149+
target_pointer_width = "16",
150+
target_pointer_width = "32",
151+
target_pointer_width = "64"
152+
))]
153+
/// Returns the number of leading binary zeros in `x`.
154+
pub extern "C" fn __clzsi2(x: usize) -> usize {
155+
usize_leading_zeros_riscv(x)
156+
}
157+
}

src/int/mod.rs

+8-66
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ pub mod sdiv;
1818
pub mod shift;
1919
pub mod udiv;
2020

21+
#[cfg(not(feature = "expose-alt-impls"))]
22+
mod leading_zeros;
23+
24+
#[cfg(feature = "expose-alt-impls")]
25+
pub mod leading_zeros;
26+
27+
pub use self::leading_zeros::__clzsi2;
28+
2129
/// Trait for some basic operations on integers
2230
pub(crate) trait Int:
2331
Copy
@@ -300,69 +308,3 @@ macro_rules! impl_wide_int {
300308

301309
impl_wide_int!(u32, u64, 32);
302310
impl_wide_int!(u64, u128, 64);
303-
304-
intrinsics! {
305-
#[maybe_use_optimized_c_shim]
306-
#[cfg(any(
307-
target_pointer_width = "16",
308-
target_pointer_width = "32",
309-
target_pointer_width = "64"
310-
))]
311-
pub extern "C" fn __clzsi2(x: usize) -> usize {
312-
// TODO: const this? Would require const-if
313-
// Note(Lokathor): the `intrinsics!` macro can't process mut inputs
314-
let mut x = x;
315-
let mut y: usize;
316-
let mut n: usize = {
317-
#[cfg(target_pointer_width = "64")]
318-
{
319-
64
320-
}
321-
#[cfg(target_pointer_width = "32")]
322-
{
323-
32
324-
}
325-
#[cfg(target_pointer_width = "16")]
326-
{
327-
16
328-
}
329-
};
330-
#[cfg(target_pointer_width = "64")]
331-
{
332-
y = x >> 32;
333-
if y != 0 {
334-
n -= 32;
335-
x = y;
336-
}
337-
}
338-
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
339-
{
340-
y = x >> 16;
341-
if y != 0 {
342-
n -= 16;
343-
x = y;
344-
}
345-
}
346-
y = x >> 8;
347-
if y != 0 {
348-
n -= 8;
349-
x = y;
350-
}
351-
y = x >> 4;
352-
if y != 0 {
353-
n -= 4;
354-
x = y;
355-
}
356-
y = x >> 2;
357-
if y != 0 {
358-
n -= 2;
359-
x = y;
360-
}
361-
y = x >> 1;
362-
if y != 0 {
363-
n - 2
364-
} else {
365-
n - x
366-
}
367-
}
368-
}

testcrate/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ doctest = false
1111
[build-dependencies]
1212
rand = "0.7"
1313

14+
[dev-dependencies]
15+
# For fuzzing tests we want a deterministic seedable RNG. We also eliminate potential
16+
# problems with system RNGs on the variety of platforms this crate is tested on.
17+
# `xoshiro128**` is used for its quality, size, and speed at generating `u32` shift amounts.
18+
rand_xoshiro = "0.4"
19+
1420
[dependencies.compiler_builtins]
1521
path = ".."
1622
default-features = false
17-
features = ["no-lang-items"]
23+
features = ["no-lang-items", "expose-alt-impls"]
1824

1925
[target.'cfg(all(target_arch = "arm", not(any(target_env = "gnu", target_env = "musl")), target_os = "linux"))'.dev-dependencies]
2026
test = { git = "https://github.com/japaric/utest" }

testcrate/tests/count_leading_zeros.rs

-23
This file was deleted.

testcrate/tests/leading_zeros.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use rand_xoshiro::rand_core::{RngCore, SeedableRng};
2+
use rand_xoshiro::Xoshiro128StarStar;
3+
4+
use compiler_builtins::int::__clzsi2;
5+
use compiler_builtins::int::leading_zeros::{usize_leading_zeros_default, usize_leading_zeros_riscv};
6+
7+
#[test]
8+
fn __clzsi2_test() {
9+
// Binary fuzzer. We cannot just send a random number directly to `__clzsi2()`, because we need
10+
// large sequences of zeros to test. This XORs, ANDs, and ORs random length strings of 1s to
11+
// `x`. ORs insure sequences of ones, ANDs insures sequences of zeros, and XORs are not often
12+
// destructive but add entropy.
13+
let mut rng = Xoshiro128StarStar::seed_from_u64(0);
14+
let mut x = 0usize;
15+
// creates a mask for indexing the bits of the type
16+
let bit_indexing_mask = usize::MAX.count_ones() - 1;
17+
// 10000 iterations is enough to make sure edge cases like single set bits are tested and to go
18+
// through many paths.
19+
for _ in 0..10_000 {
20+
let r0 = bit_indexing_mask & rng.next_u32();
21+
// random length of ones
22+
let ones: usize = !0 >> r0;
23+
let r1 = bit_indexing_mask & rng.next_u32();
24+
// random circular shift
25+
let mask = ones.rotate_left(r1);
26+
match rng.next_u32() % 4 {
27+
0 => x |= mask,
28+
1 => x &= mask,
29+
// both 2 and 3 to make XORs as common as ORs and ANDs combined
30+
_ => x ^= mask,
31+
}
32+
let lz = x.leading_zeros() as usize;
33+
let lz0 = __clzsi2(x);
34+
let lz1 = usize_leading_zeros_default(x);
35+
let lz2 = usize_leading_zeros_riscv(x);
36+
if lz0 != lz {
37+
panic!(
38+
"__clzsi2({}): expected: {}, found: {}",
39+
x,
40+
lz,
41+
lz0,
42+
);
43+
}
44+
if lz1 != lz {
45+
panic!(
46+
"usize_leading_zeros_default({}): expected: {}, found: {}",
47+
x,
48+
lz,
49+
lz1,
50+
);
51+
}
52+
if lz2 != lz {
53+
panic!(
54+
"usize_leading_zeros_riscv({}): expected: {}, found: {}",
55+
x,
56+
lz,
57+
lz2,
58+
);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)