Use an external crate for fast approximate math functions.

This commit is contained in:
Nathan Vegdahl 2020-12-26 10:03:35 +09:00
parent 4cfc051260
commit 6586b38dc9
5 changed files with 344 additions and 352 deletions

621
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -36,6 +36,7 @@ rustc-serialize = "0.3"
scoped_threadpool = "0.1" scoped_threadpool = "0.1"
time = "0.1" time = "0.1"
glam = {git="https://github.com/bitshifter/glam-rs.git", rev="0f314f99", default-features=false, features=["approx"]} glam = {git="https://github.com/bitshifter/glam-rs.git", rev="0f314f99", default-features=false, features=["approx"]}
fastapprox = "0.3"
# Local crate dependencies # Local crate dependencies
[dependencies.bvh_order] [dependencies.bvh_order]

View File

@ -1,6 +1,6 @@
## Psychopath ## Psychopath
With the exception of files under `psychoblend/` and `sub_crates/`, and any code explicitly excepted in code comments, this project is licensed under the GPLv3 as follows: With the exception of files under `psychoblend/` and `sub_crates/`, this project is licensed under the GPLv3 as follows:
Copyright (c) 2020 Nathan Vegdahl Copyright (c) 2020 Nathan Vegdahl

View File

@ -1,19 +1,19 @@
Copyright <YEAR> <COPYRIGHT HOLDER> Copyright (c) 2020 Nathan Vegdahl
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a copy
copy of this software and associated documentation files (the "Software"), of this software and associated documentation files (the "Software"), to deal
to deal in the Software without restriction, including without limitation in the Software without restriction, including without limitation the rights
the rights to use, copy, modify, merge, publish, distribute, sublicense, to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
and/or sell copies of the Software, and to permit persons to whom the copies of the Software, and to permit persons to whom the Software is
Software is furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in all
all copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
DEALINGS IN THE SOFTWARE. SOFTWARE.

View File

@ -188,64 +188,34 @@ pub fn probit(n: f32, width: f32) -> f32 {
x as f32 * width x as f32 * width
} }
//----------------------------------------------------------------
// Adapted to Rust from https://code.google.com/archive/p/fastapprox/
pub fn fast_ln(x: f32) -> f32 { pub fn fast_ln(x: f32) -> f32 {
use std::mem::transmute_copy; fastapprox::fast::ln(x)
let mut y = unsafe { transmute_copy::<f32, u32>(&x) as f32 };
y *= 8.262_958_288_192_749_0e-8;
y - 87.989_971_088
} }
pub fn fast_pow2(p: f32) -> f32 { pub fn fast_pow2(p: f32) -> f32 {
use std::mem::transmute_copy; fastapprox::fast::pow2(p)
let offset: f32 = if p < 0.0 { 1.0 } else { 0.0 };
let clipp: f32 = if p < -126.0 { -126.0 } else { p };
let w: i32 = clipp as i32;
let z: f32 = clipp - w as f32 + offset;
let i: u32 = ((1 << 23) as f32
* (clipp + 121.274_057_5 + 27.728_023_3 / (4.842_525_68 - z) - 1.490_129_07 * z))
as u32;
unsafe { transmute_copy::<u32, f32>(&i) }
} }
pub fn fast_log2(x: f32) -> f32 { pub fn fast_log2(x: f32) -> f32 {
use std::mem::transmute_copy; fastapprox::fast::log2(x)
let xi = unsafe { transmute_copy::<f32, u32>(&x) };
let y = xi as f32 * 1.192_092_895_507_812_5e-7;
let mx = unsafe { transmute_copy::<u32, f32>(&((xi & 0x007F_FFFF) | 0x3f00_0000)) };
y - 124.225_514_99 - 1.498_030_302 * mx - 1.725_879_99 / (0.352_088_706_8 + mx)
} }
pub fn fast_exp(p: f32) -> f32 { pub fn fast_exp(p: f32) -> f32 {
fast_pow2(f32::consts::LOG2_E * p) fastapprox::fast::exp(p)
} }
pub fn fast_pow(x: f32, p: f32) -> f32 { pub fn fast_pow(x: f32, p: f32) -> f32 {
fast_pow2(p * fast_log2(x)) fastapprox::fast::pow(x, p)
} }
pub fn faster_pow2(p: f32) -> f32 { pub fn faster_pow2(p: f32) -> f32 {
use std::mem::transmute_copy; fastapprox::faster::pow2(p)
let clipp: f32 = if p < -126.0 { -126.0 } else { p };
let i: u32 = ((1 << 23) as f32 * (clipp + 126.942_695_04)) as u32;
unsafe { transmute_copy::<u32, f32>(&i) }
} }
pub fn faster_exp(p: f32) -> f32 { pub fn faster_exp(p: f32) -> f32 {
faster_pow2(f32::consts::LOG2_E * p) fastapprox::faster::exp(p)
} }
// End of adapted code
//---------------------------------------------------------------- //----------------------------------------------------------------
#[cfg(test)] #[cfg(test)]