use std::fmt::Debug; use std::ops::{Div, Neg}; use const_decimal::ScaledInteger; use criterion::measurement::WallTime; use criterion::BenchmarkGroup; use num_traits::ConstOne; use proptest::prelude::{Arbitrary, Strategy}; mod add; mod div; mod mul; mod sub; mod to_f64; fn to_sign(positive: bool) -> I where I: ConstOne + Neg, { match positive { true => I::ONE, false => -I::ONE, } } fn main() { let mut criterion = criterion::Criterion::default().configure_from_args(); // TODO: Our `hi` range for signed integers does not sample negative values. bench_integers::<9, u64>( &mut criterion.benchmark_group("u64_9"), 0..(u32::MAX as u64), // TODO: This upper bound is lower than necessary. ((u32::MAX as u64) - 10u64.pow(9) + 1)..(u64::MAX / 10u64.pow(9)), (u64::MAX / 10u64.pow(9) + 1)..u64::MAX, ); bench_integers::<9, i64>( &mut criterion.benchmark_group("i64_9"), (i32::MIN as i64)..(i32::MAX as i64), (bool::arbitrary(), ((u32::MAX as i64) + 1)..(i64::MAX / 10i64.pow(9))) .prop_map(|(sign, unsigned)| to_sign::(sign) * unsigned), (bool::arbitrary(), (i64::MAX / 10i64.pow(9) + 1)..i64::MAX) .prop_map(|(sign, unsigned)| to_sign::(sign) * unsigned), ); bench_integers::<18, u128>( &mut criterion.benchmark_group("u128_18"), 0..(u64::MAX as u128), // TODO: This upper bound is lower than necessary. ((u64::MAX as u128) - 10u128.pow(18) + 1)..(u128::MAX / 10u128.pow(18)), (u128::MAX / 10u128.pow(18) + 1)..u128::MAX, ); bench_integers::<18, i128>( &mut criterion.benchmark_group("i128_18"), (i64::MIN as i128)..(i64::MAX as i128), (bool::arbitrary(), ((u64::MAX as i128) + 1)..(i128::MAX / 10i128.pow(18))) .prop_map(|(sign, unsigned)| to_sign::(sign) * unsigned), (bool::arbitrary(), (i128::MAX / 10i128.pow(18) + 1)..i128::MAX) .prop_map(|(sign, unsigned)| to_sign::(sign) * unsigned), ); criterion.final_summary(); } fn bench_integers( group: &mut BenchmarkGroup<'_, WallTime>, lo_range: impl Strategy + Clone + Debug, hi_mul_range: impl Strategy + Clone + Debug, hi_div_range: impl Strategy + Clone + Debug, ) where I: ScaledInteger + Arbitrary + Div, { add::bench_all::(group); sub::bench_all::(group); mul::bench_all::(group, lo_range.clone(), hi_mul_range); div::bench_all::(group, lo_range, hi_div_range); to_f64::bench_all::(group); }