From 6e2b5c3d2a301f17669760d5154c04d8f16b26a5 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 29 Mar 2021 19:29:39 +0000 Subject: [PATCH] remove trig.rs (no longer used) --- Cargo.toml | 4 ---- benches/trig.rs | 16 -------------- src/lib.rs | 1 - src/trig.rs | 55 ------------------------------------------------- 4 files changed, 76 deletions(-) delete mode 100644 benches/trig.rs delete mode 100644 src/trig.rs diff --git a/Cargo.toml b/Cargo.toml index 9dfdc2a..9317f8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,10 +19,6 @@ criterion = "0.3.4" [patch.crates-io] rayon = {git = "https://github.com/rayon-rs/rayon.git"} #for using git version of rayon -[[bench]] -name = "trig" -harness = false - [profile.dev] codegen-units = 1 opt-level = 3 diff --git a/benches/trig.rs b/benches/trig.rs deleted file mode 100644 index 2537d3a..0000000 --- a/benches/trig.rs +++ /dev/null @@ -1,16 +0,0 @@ -use physarum::trig; - -use criterion::{black_box, criterion_group, criterion_main, Criterion}; - -fn bench_cos_appr(c: &mut Criterion) { - c.bench_function("Approximate cosine", |b| { - b.iter(|| trig::cos(black_box(1.0))) - }); -} - -fn bench_cos_exact(c: &mut Criterion) { - c.bench_function("Exact cosine", |b| b.iter(|| f32::cos(black_box(1.0)))); -} - -criterion_group!(benches, bench_cos_appr, bench_cos_exact); -criterion_main!(benches); diff --git a/src/lib.rs b/src/lib.rs index c743b7b..4709868 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,5 @@ mod blur; mod grid; pub mod model; mod palette; -pub mod trig; // for benchmarking mod util; mod imgdata; // for storing image data \ No newline at end of file diff --git a/src/trig.rs b/src/trig.rs deleted file mode 100644 index eb5046a..0000000 --- a/src/trig.rs +++ /dev/null @@ -1,55 +0,0 @@ -/// From https://bits.stephan-brumme.com/absFloat.html -#[inline(always)] -fn abs(x: f32) -> f32 { - f32::from_bits(x.to_bits() & 0x7FFF_FFFF) -} - -/// Branchless floor implementation -#[inline(always)] -fn floor(x: f32) -> f32 { - let mut x_trunc = (x as i32) as f32; - x_trunc -= (x < x_trunc) as i32 as f32; - x_trunc -} - -/// Approximates `cos(x)` in radians with the maximum error of `0.002` -/// https://stackoverflow.com/posts/28050328/revisions -pub fn cos(mut x: f32) -> f32 { - const ALPHA: f32 = 0.5 * std::f32::consts::FRAC_1_PI; - x *= ALPHA; - x -= 0.25_f32 + floor(x + 0.25_f32); - x *= 16.0_f32 * (abs(x) - 0.5_f32); - x += 0.225_f32 * x * (abs(x) - 1.0_f32); - x -} - -/// Approximates `sin(x)` in radians with the maximum error of `0.002` -pub fn sin(x: f32) -> f32 { - cos(x - std::f32::consts::FRAC_PI_2) -} - -#[cfg(test)] -mod tests { - use super::*; - use itertools::repeat_n; - - #[test] - fn test_cos() { - let n_points = 1000; - let x: Vec = repeat_n(std::f32::consts::TAU / (n_points - 1) as f32, n_points) - .enumerate() - .map(|(i, delta)| i as f32 * delta) - .collect(); - - let exact: Vec = x.iter().map(|v| v.cos()).collect(); - let appr: Vec = x.iter().map(|v| cos(*v)).collect(); - - let mut max_error = 0.0_f32; - for (y_exact, y_appr) in exact.iter().zip(&appr) { - max_error = (y_exact - y_appr).abs().max(max_error); - } - - // The error bound is even better! - assert!(max_error <= 0.0011); - } -}