Move benchmarks into its own crate

This commit is contained in:
gnzlbg
2019-07-02 08:22:03 +02:00
parent 04a276fb39
commit f0d518231c
5 changed files with 25 additions and 12 deletions
+11
View File
@@ -0,0 +1,11 @@
[package]
name = "libm-bench"
version = "0.1.0"
authors = ["Gonzalo Brito Gadeschi <gonzalobg88@gmail.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
[dependencies]
libm = { path = "../.." }
rand = "0.6.5"
paste = "0.1.5"
+115
View File
@@ -0,0 +1,115 @@
#![feature(test)]
extern crate test;
use rand::Rng;
use test::Bencher;
macro_rules! unary {
($($func:ident),*) => ($(
paste::item! {
#[bench]
pub fn [<$func>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f64>();
bh.iter(|| test::black_box(libm::[<$func>](x)))
}
#[bench]
pub fn [<$func f>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f32>();
bh.iter(|| test::black_box(libm::[<$func f>](x)))
}
}
)*);
}
macro_rules! binary {
($($func:ident),*) => ($(
paste::item! {
#[bench]
pub fn [<$func>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f64>();
let y = rng.gen::<f64>();
bh.iter(|| test::black_box(libm::[<$func>](x, y)))
}
#[bench]
pub fn [<$func f>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f32>();
let y = rng.gen::<f32>();
bh.iter(|| test::black_box(libm::[<$func f>](x, y)))
}
}
)*);
($($func:ident);*) => ($(
paste::item! {
#[bench]
pub fn [<$func>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f64>();
let n = rng.gen::<i32>();
bh.iter(|| test::black_box(libm::[<$func>](x, n)))
}
#[bench]
pub fn [<$func f>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f32>();
let n = rng.gen::<i32>();
bh.iter(|| test::black_box(libm::[<$func f>](x, n)))
}
}
)*);
}
macro_rules! trinary {
($($func:ident),*) => ($(
paste::item! {
#[bench]
pub fn [<$func>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f64>();
let y = rng.gen::<f64>();
let z = rng.gen::<f64>();
bh.iter(|| test::black_box(libm::[<$func>](x, y, z)))
}
#[bench]
pub fn [<$func f>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let x = rng.gen::<f32>();
let y = rng.gen::<f32>();
let z = rng.gen::<f32>();
bh.iter(|| test::black_box(libm::[<$func f>](x, y, z)))
}
}
)*);
}
macro_rules! bessel {
($($func:ident),*) => ($(
paste::item! {
#[bench]
pub fn [<$func>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let mut n = rng.gen::<i32>();
n &= 0xffff;
let x = rng.gen::<f64>();
bh.iter(|| test::black_box(libm::[<$func>](n, x)))
}
#[bench]
pub fn [<$func f>](bh: &mut Bencher) {
let mut rng = rand::thread_rng();
let mut n = rng.gen::<i32>();
n &= 0xffff;
let x = rng.gen::<f32>();
bh.iter(|| test::black_box(libm::[<$func f>](n, x)))
}
}
)*);
}
unary!(
acos, acosh, asin, atan, cbrt, ceil, cos, cosh, erf, exp, exp2, exp10, expm1, fabs, floor, j0,
j1, lgamma, log, log1p, log2, log10, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc, y0, y1
);
binary!(atan2, copysign, fdim, fmax, fmin, fmod, hypot, pow);
trinary!(fma);
bessel!(jn, yn);
binary!(ldexp; scalbn);