Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 7d0b45b

Browse files
committed
Add simple icount benchmarks for u256 operations
1 parent 3440603 commit 7d0b45b

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

libm/crates/libm-test/benches/icount.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Benchmarks that use `iai-cachegrind` to be reasonably CI-stable.
22
33
use std::hint::black_box;
4+
use std::ops::Shr;
45

56
use iai_callgrind::{library_benchmark, library_benchmark_group, main};
7+
use libm::support::{HInt, u256};
68
use libm_test::gen::spaced;
79
use libm_test::{CheckBasis, CheckCtx, GeneratorKind, MathOp, OpRustArgs, TupleCall, op};
810

@@ -51,8 +53,107 @@ libm_macros::for_each_function! {
5153
callback: icount_benches,
5254
}
5355

56+
fn setup_u128_mul() -> Vec<(u128, u128)> {
57+
let step = u128::MAX / 300;
58+
let mut x = 0u128;
59+
let mut y = 0u128;
60+
let mut v = Vec::new();
61+
62+
loop {
63+
'inner: loop {
64+
match y.checked_add(step) {
65+
Some(new) => y = new,
66+
None => break 'inner,
67+
}
68+
69+
v.push((x, y))
70+
}
71+
72+
match x.checked_add(step) {
73+
Some(new) => x = new,
74+
None => break,
75+
}
76+
}
77+
78+
v
79+
}
80+
81+
/*
82+
fn setup_u256_add() -> Vec<(u256, u256)> {
83+
let mut v = Vec::new();
84+
for (x, y) in setup_u128_mul() {
85+
// square the u128 inputs to cover most of the u256 range
86+
v.push((x.widen_mul(x), y.widen_mul(y)));
87+
}
88+
// Doesn't get covered by `u128:MAX^2`
89+
v.push((u256::MAX, u256::MAX));
90+
v
91+
}
92+
*/
93+
94+
fn setup_u256_shift() -> Vec<(u256, u32)> {
95+
let mut v = Vec::new();
96+
97+
for (x, _) in setup_u128_mul() {
98+
let x2 = x.widen_mul(x);
99+
for y in 0u32..256 {
100+
v.push((x2, y));
101+
}
102+
}
103+
104+
v
105+
}
106+
107+
#[library_benchmark]
108+
#[bench::linspace(setup_u128_mul())]
109+
fn icount_bench_u128_widen_mul(cases: Vec<(u128, u128)>) {
110+
let f = black_box(u128::zero_widen_mul);
111+
for (x, y) in cases.iter().copied() {
112+
f(x, y);
113+
}
114+
}
115+
116+
library_benchmark_group!(
117+
name = icount_bench_u128_widen_mul_group;
118+
benchmarks = icount_bench_u128_widen_mul
119+
);
120+
121+
/* Not yet implemented
122+
#[library_benchmark]
123+
#[bench::linspace(setup_u256_add())]
124+
fn icount_bench_u256_add(cases: Vec<(u256, u256)>) {
125+
let f = black_box(u256::add);
126+
for (x, y) in cases.iter().copied() {
127+
f(x, y);
128+
}
129+
}
130+
131+
library_benchmark_group!(
132+
name = icount_bench_u256_add_group;
133+
benchmarks = icount_bench_u256_add
134+
);
135+
*/
136+
137+
#[library_benchmark]
138+
#[bench::linspace(setup_u256_shift())]
139+
fn icount_bench_u256_shr(cases: Vec<(u256, u32)>) {
140+
let f = black_box(u256::shr);
141+
for (x, y) in cases.iter().copied() {
142+
f(x, y);
143+
}
144+
}
145+
146+
library_benchmark_group!(
147+
name = icount_bench_u256_shr_group;
148+
benchmarks = icount_bench_u256_shr
149+
);
150+
54151
main!(
55152
library_benchmark_groups =
153+
// u256-related benchmarks
154+
icount_bench_u128_widen_mul_group,
155+
// icount_bench_u256_add_group,
156+
icount_bench_u256_shr_group,
56157
// verify-apilist-start
57158
// verify-sorted-start
58159
icount_bench_acos_group,

libm/src/math/support/big.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const U128_LO_MASK: u128 = u64::MAX as u128;
2020
pub struct u256(pub [u64; 4]);
2121

2222
impl u256 {
23-
#[cfg(test)]
23+
#[allow(unused)]
2424
pub const MAX: Self = Self([u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
2525

2626
/// Reinterpret as a signed integer

libm/src/math/support/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod float_traits;
55
pub mod hex_float;
66
mod int_traits;
77

8+
#[allow(unused_imports)]
9+
pub use big::{i256, u256};
810
#[allow(unused_imports)]
911
pub use float_traits::{DFloat, Float, HFloat, IntTy};
1012
pub(crate) use float_traits::{f32_from_bits, f64_from_bits};

0 commit comments

Comments
 (0)