remove trig.rs (no longer used)
This commit is contained in:
parent
5c97379057
commit
6e2b5c3d2a
@ -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
|
||||
|
||||
@ -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);
|
||||
@ -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
|
||||
55
src/trig.rs
55
src/trig.rs
@ -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<f32> = 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<f32> = x.iter().map(|v| v.cos()).collect();
|
||||
let appr: Vec<f32> = 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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user