//! The few number functions the voice needs, written here because `core` //! has no sine, exponential or square root without the standard library. //! //! Each brings its argument into a small range where a short polynomial //! is accurate to about the precision of an `f32`, then undoes the step: //! the sine folds any angle onto a quarter turn, the exponential splits off //! a power of two that it puts straight into the number's exponent bits. //! The filters call them once per 5 ms frame, never per sample, so they //! need to be right more than they need to be fast. use core::f32::consts::{FRAC_PI_2, LOG2_E, PI, TAU}; /// The largest whole number not above `x`. pub fn floor(x: f32) -> f32 { // An `f32` this large is already whole, and the cast would lose it. if !(-8_388_608.0..=8_388_608.0).contains(&x) { return x; } let t = x as i32 as f32; if t > x { t - 1.0 } else { t } } /// The sine of `x` radians. pub fn sin(x: f32) -> f32 { if !x.is_finite() { return 0.0; } // Onto [-π, π], then onto [-π/2, π/2] by the sine's symmetry there. let mut r = x - floor(x / TAU + 0.5) * TAU; if r > FRAC_PI_2 { r = PI - r; } else if r < -FRAC_PI_2 { r = -PI - r; } // The Taylor series to the 11th power: within 6e-8 on a quarter turn. let r2 = r * r; let p = 1.0 / 362_880.0 - r2 / 39_916_800.0; let p = 1.0 / 120.0 - r2 * (1.0 / 5040.0 - r2 * p); r * (1.0 - r2 * (1.0 / 6.0 - r2 * p)) } /// The cosine of `x` radians. pub fn cos(x: f32) -> f32 { sin(x + FRAC_PI_2) } /// e to the power `x`. pub fn exp(x: f32) -> f32 { if x.is_nan() { return 0.0; } if x > 88.0 { return f32::MAX; } if x < -87.0 { return 0.0; } // x = n·ln 2 + r with |r| ≤ ln 2 / 2, so e^x = 2^n · e^r. ln 2 is // taken in two parts, the first with its low bits clear, so that // n times it is exact and r keeps its precision for large x. const LN_2_HIGH: f32 = f32::from_bits(0x3f31_7200); const LN_2_LOW: f32 = 1.428_606_8e-6; let n = floor(x * LOG2_E + 0.5); let r = (x - n * LN_2_HIGH) - n * LN_2_LOW; let mut p = 1.0 + r / 8.0; p = 1.0 + r / 7.0 * p; p = 1.0 + r / 6.0 * p; p = 1.0 + r / 5.0 * p; p = 1.0 + r / 4.0 * p; p = 1.0 + r / 3.0 * p; p = 1.0 + r / 2.0 * p; p = 1.0 + r * p; // 2^n made directly: n + 127 in the exponent field, which the range // check above keeps within 1..=254. let two_n = f32::from_bits(((n as i32 + 127) as u32) << 23); p * two_n } /// The square root of `x`; 0 for anything not above 0. pub fn sqrt(x: f32) -> f32 { if x <= 0.0 || x.is_nan() { return 0.0; } if x == f32::INFINITY { return x; } // Halving the exponent bits gives a guess within a few per cent; // Newton's step doubles the correct digits each time. let mut y = f32::from_bits((x.to_bits() >> 1) + 0x1fbd_1df5); for _ in 0..4 { y = 0.5 * (y + x / y); } y } #[cfg(test)] mod tests { extern crate std; use super::*; fn near(a: f32, b: f64, within: f64) -> bool { (f64::from(a) - b).abs() <= within } #[test] fn floor_rounds_down() { assert_eq!(floor(2.5), 2.0); assert_eq!(floor(-2.5), -3.0); assert_eq!(floor(-3.0), -3.0); assert_eq!(floor(0.0), 0.0); assert_eq!(floor(1e20), 1e20); } #[test] fn sine_and_cosine_match_the_tables() { for i in -400..=400 { let x = i as f32 * 0.05; let want = f64::from(x).sin(); assert!(near(sin(x), want, 2e-6), "sin({x}) = {} not {want}", sin(x)); let want = f64::from(x).cos(); assert!(near(cos(x), want, 2e-6), "cos({x}) = {} not {want}", cos(x)); } assert!(near(sin(PI / 6.0), 0.5, 1e-6)); assert!(near(cos(PI / 3.0), 0.5, 1e-6)); assert!(near(sin(1000.0), 1000f64.sin(), 1e-4)); assert_eq!(sin(f32::NAN), 0.0); } #[test] fn exponential_matches_the_tables() { for i in -200..=200 { let x = i as f32 * 0.4; let want = f64::from(x).exp(); let got = f64::from(exp(x)); assert!((got - want).abs() <= want * 1e-6, "exp({x}) = {got} not {want}"); } assert!(near(exp(1.0), core::f64::consts::E, 1e-6)); assert_eq!(exp(0.0), 1.0); assert_eq!(exp(-100.0), 0.0); assert_eq!(exp(100.0), f32::MAX); } #[test] fn square_root_matches_the_tables() { for x in [1e-6f32, 0.01, 0.5, 1.0, 2.0, 9.0, 1234.5, 1e12] { let want = f64::from(x).sqrt(); let got = f64::from(sqrt(x)); assert!((got - want).abs() <= want * 1e-6, "sqrt({x}) = {got} not {want}"); } assert_eq!(sqrt(-4.0), 0.0); assert_eq!(sqrt(0.0), 0.0); } }