Skip to content

Commit 61c8582

Browse files
committed
Benchmark quire add in vs_external (results preliminary!)
1 parent 9c38c2b commit 61c8582

File tree

3 files changed

+463
-384
lines changed

3 files changed

+463
-384
lines changed

benches/vs_external/cerlane_softposit.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ pub struct posit32_t { v: u32 }
1212
#[derive(Clone, Copy)]
1313
pub struct posit64_t { v: u64 }
1414

15+
#[repr(C)]
16+
#[derive(Clone, Copy)]
17+
#[derive(Default)]
18+
pub struct quire32_t { v: [u64; 8] }
19+
1520
#[link(name = "softposit")]
1621
unsafe extern "C" {
1722
pub fn p32_add(x: posit32_t, y: posit32_t) -> posit32_t;
@@ -25,6 +30,14 @@ unsafe extern "C" {
2530

2631
pub fn p32_sqrt(x: posit32_t) -> posit32_t;
2732
// pub fn p64_sqrt(x: posit64_t) -> posit64_t;
33+
34+
fn q32_fdp_add(q: quire32_t, x: posit32_t, y: posit32_t) -> quire32_t;
35+
}
36+
37+
#[inline]
38+
pub unsafe fn q32_add(q: quire32_t, x: posit32_t) -> quire32_t {
39+
let unit = posit32_t::from(p32::ONE);
40+
unsafe { q32_fdp_add(q, x, unit) }
2841
}
2942

3043
impl From<p32> for posit32_t { fn from(x: p32) -> Self { posit32_t { v: x.to_bits() as u32 } } }

benches/vs_external/main.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! - https://github.com/stillwater-sc/universal/
1111
1212
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
13-
use fast_posit::{p32, p64};
13+
use fast_posit::{p32, p64, q32};
1414

1515
//
1616

@@ -74,6 +74,23 @@ fn bench_1ary<T: Copy, U: From<T>>(
7474
}));
7575
}
7676

77+
/// Benchmark a 1-arg function that accumulates into another type.
78+
fn bench_1ary_accumulate<T: Copy, A: Clone, U: From<T>>(
79+
g: &mut criterion::BenchmarkGroup<'_, criterion::measurement::WallTime>,
80+
name: &str,
81+
initial: A,
82+
data: &[T],
83+
mut f: impl FnMut(&mut A, U),
84+
) {
85+
g.throughput(Throughput::Elements(data.len() as u64));
86+
g.bench_function(name, |b| b.iter(|| {
87+
let mut accum = black_box(initial.clone());
88+
for &x in data {
89+
let _ = black_box(f(&mut accum, black_box(U::from(x))));
90+
}
91+
}));
92+
}
93+
7794
//
7895

7996
/// Benchmark this number of operations
@@ -169,7 +186,7 @@ fn add_64(c: &mut Criterion) {
169186
bench_2ary(&mut g, "berkeley-softfloat", &data_float, |x, y| unsafe { berkeley_softfloat::f64_add(x, y) });
170187

171188
/*#[cfg(feature = "cerlane-softposit")]
172-
let _ = bench_2ary(&mut g, "cerlane-softposit", &data_posit, |x, y| unsafe { cerlane_softposit::p64_add(x, y) };*/
189+
bench_2ary(&mut g, "cerlane-softposit", &data_posit, |x, y| unsafe { cerlane_softposit::p64_add(x, y) };*/
173190

174191
#[cfg(feature = "stillwater-softposit")]
175192
bench_2ary(&mut g, "stillwater-softposit", &data_posit, |x, y| unsafe { stillwater_softposit::posit64_addp64(x, y) });
@@ -189,7 +206,7 @@ fn mul_64(c: &mut Criterion) {
189206
bench_2ary(&mut g, "berkeley-softfloat", &data_float, |x, y| unsafe { berkeley_softfloat::f64_mul(x, y) });
190207

191208
/*#[cfg(feature = "cerlane-softposit")]
192-
let _ = bench_2ary(&mut g, "cerlane-softposit", &data_posit, |x, y| unsafe { cerlane_softposit::p64_mul(x, y) };*/
209+
bench_2ary(&mut g, "cerlane-softposit", &data_posit, |x, y| unsafe { cerlane_softposit::p64_mul(x, y) };*/
193210

194211
#[cfg(feature = "stillwater-softposit")]
195212
bench_2ary(&mut g, "stillwater-softposit", &data_posit, |x, y| unsafe { stillwater_softposit::posit64_mulp64(x, y) });
@@ -209,7 +226,7 @@ fn div_64(c: &mut Criterion) {
209226
bench_2ary(&mut g, "berkeley-softfloat", &data_float, |x, y| unsafe { berkeley_softfloat::f64_div(x, y) });
210227

211228
/*#[cfg(feature = "cerlane-softposit")]
212-
let _ = bench_2ary(&mut g, "cerlane-softposit", &data_posit, |x, y| unsafe { cerlane_softposit::p64_div(x, y) };*/
229+
bench_2ary(&mut g, "cerlane-softposit", &data_posit, |x, y| unsafe { cerlane_softposit::p64_div(x, y) };*/
213230

214231
#[cfg(feature = "stillwater-softposit")]
215232
bench_2ary(&mut g, "stillwater-softposit", &data_posit, |x, y| unsafe { stillwater_softposit::posit64_divp64(x, y) });
@@ -229,7 +246,7 @@ fn sqrt_64(c: &mut Criterion) {
229246
bench_1ary(&mut g, "berkeley-softfloat", &data_float, |x| unsafe { berkeley_softfloat::f64_sqrt(x) });
230247

231248
/*#[cfg(feature = "cerlane-softposit")]
232-
let _ = bench_1ary(&mut g, "cerlane-softposit", &data_posit, |x| unsafe { cerlane_softposit::p64_sqrt(x) });*/
249+
bench_1ary(&mut g, "cerlane-softposit", &data_posit, |x| unsafe { cerlane_softposit::p64_sqrt(x) });*/
233250

234251
#[cfg(feature = "stillwater-softposit")]
235252
bench_1ary(&mut g, "stillwater-softposit", &data_posit, |x| unsafe { stillwater_softposit::posit64_sqrt(x) });
@@ -239,6 +256,32 @@ fn sqrt_64(c: &mut Criterion) {
239256
g.finish();
240257
}
241258

259+
/// Generate arrays of random posits and benchmark our impl and external impls in summing them all
260+
/// into a quire.
261+
fn quire_add_32(c: &mut Criterion) {
262+
let (data_posit, _) = make_data_32(LEN);
263+
let mut g = c.benchmark_group("quire_add_32");
264+
265+
#[cfg(feature = "cerlane-softposit")]
266+
bench_1ary_accumulate(
267+
&mut g,
268+
"cerlane-softposit",
269+
cerlane_softposit::quire32_t::default(),
270+
&data_posit,
271+
|q, x| unsafe { *q = cerlane_softposit::q32_add(*q, x) },
272+
);
273+
274+
bench_1ary_accumulate(
275+
&mut g,
276+
"posit",
277+
q32::ZERO,
278+
&data_posit,
279+
|q: &mut q32, x: p32| *q += x,
280+
);
281+
282+
g.finish();
283+
}
284+
242285
criterion_group!(add,
243286
add_32,
244287
add_64,
@@ -259,4 +302,8 @@ criterion_group!(sqrt,
259302
sqrt_64,
260303
);
261304

262-
criterion_main!(add, mul, div, sqrt);
305+
criterion_group!(quire,
306+
quire_add_32,
307+
);
308+
309+
criterion_main!(add, mul, div, sqrt, quire);

0 commit comments

Comments
 (0)