diff --git a/src/math.rs b/src/math.rs index ab674af..5ce76b8 100644 --- a/src/math.rs +++ b/src/math.rs @@ -142,10 +142,24 @@ pub fn fast_pow2(p: f32) -> f32 { unsafe { transmute_copy::(&i) } } +pub fn fast_log2(x: f32) -> f32 { + use std::mem::transmute_copy; + + let xi = unsafe { transmute_copy::(&x) }; + let y = xi as f32 * 1.1920928955078125e-7; + let mx = unsafe { transmute_copy::(&((xi & 0x007FFFFF) | 0x3f000000)) }; + + return y - 124.22551499 - 1.498030302 * mx - 1.72587999 / (0.3520887068 + mx); +} + pub fn fast_exp(p: f32) -> f32 { fast_pow2(1.442695040 * p) } +pub fn fast_pow(x: f32, p: f32) -> f32 { + fast_pow2(p * fast_log2(x)) +} + pub fn faster_pow2(p: f32) -> f32 { use std::mem::transmute_copy; @@ -159,6 +173,7 @@ pub fn faster_exp(p: f32) -> f32 { faster_pow2(1.442695040 * p) } + // End of adapted code //----------------------------------------------------------------