1010//! - https://github.com/stillwater-sc/universal/
1111
1212use 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+
242285criterion_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