use crate::exponents::exp2f::EXP2F_TABLE;
use crate::exponents::expf::{ExpfBackend, GenericExpfBackend};
#[inline(always)]
fn exp2m1f_gen<B: ExpfBackend>(x: f32, backend: B) -> f32 {
let x_u = x.to_bits();
let x_abs = x_u & 0x7fff_ffffu32;
if x_abs >= 0x4300_0000u32 || x_abs <= 0x3d00_0000u32 {
if x_abs <= 0x3d00_0000u32 {
const C: [u64; 6] = [
0x3fe62e42fefa39f3,
0x3fcebfbdff82c57b,
0x3fac6b08d6f2d7aa,
0x3f83b2ab6fc92f5d,
0x3f55d897cfe27125,
0x3f243090e61e6af1,
];
let xd = x as f64;
let xsq = xd * xd;
let c0 = backend.fma(xd, f64::from_bits(C[1]), f64::from_bits(C[0]));
let c1 = backend.fma(xd, f64::from_bits(C[3]), f64::from_bits(C[2]));
let c2 = backend.fma(xd, f64::from_bits(C[5]), f64::from_bits(C[4]));
let p = backend.polyeval3(xsq, c0, c1, c2);
return (p * xd) as f32;
}
if x.is_sign_positive() || x.is_nan() {
return x + f32::INFINITY;
}
}
if x <= -25.0 {
if x.is_infinite() {
return -1.0;
}
if x.is_nan() {
return x;
}
return -1.0;
}
let xd = x as f64;
let kf = backend.roundf(x * 64.0);
let k = unsafe { kf.to_int_unchecked::<i32>() }; let dx = backend.fma(f64::from_bits(0xbf90000000000000), kf as f64, xd);
const TABLE_BITS: u32 = 6;
const TABLE_MASK: u64 = (1u64 << TABLE_BITS) - 1;
let exp_hi: i64 = ((k >> TABLE_BITS) as i64).wrapping_shl(52);
let mh_bits = (EXP2F_TABLE[((k as u64) & TABLE_MASK) as usize] as i64).wrapping_add(exp_hi);
let mh = f64::from_bits(mh_bits as u64);
const C: [u64; 5] = [
0x3fe62e42fefa39ef,
0x3fcebfbdff8131c4,
0x3fac6b08d7061695,
0x3f83b2b1bee74b2a,
0x3f55d88091198529,
];
let dx_sq = dx * dx;
let c1 = backend.fma(dx, f64::from_bits(C[0]), 1.0);
let c2 = backend.fma(dx, f64::from_bits(C[2]), f64::from_bits(C[1]));
let c3 = backend.fma(dx, f64::from_bits(C[4]), f64::from_bits(C[3]));
let p = backend.polyeval3(dx_sq, c1, c2, c3);
backend.fma(p, mh, -1.) as f32
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx", enable = "fma")]
unsafe fn exp2m1f_fma_impl(x: f32) -> f32 {
use crate::exponents::expf::FmaBackend;
exp2m1f_gen(x, FmaBackend {})
}
#[inline]
pub fn f_exp2m1f(x: f32) -> f32 {
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
exp2m1f_gen(x, GenericExpfBackend {})
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
use std::sync::OnceLock;
static EXECUTOR: OnceLock<unsafe fn(f32) -> f32> = OnceLock::new();
let q = EXECUTOR.get_or_init(|| {
if std::arch::is_x86_feature_detected!("avx")
&& std::arch::is_x86_feature_detected!("fma")
{
exp2m1f_fma_impl
} else {
fn def_exp2f(x: f32) -> f32 {
exp2m1f_gen(x, GenericExpfBackend {})
}
def_exp2f
}
});
unsafe { q(x) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exp2m1f() {
assert!(f_exp2m1f(f32::from_bits(0x7fc0_0000)).is_nan());
assert_eq!(f_exp2m1f(0.432423), 0.34949815);
assert_eq!(f_exp2m1f(-4.), -0.9375);
assert_eq!(f_exp2m1f(5.43122), 42.14795);
assert_eq!(f_exp2m1f(4.), 15.0);
assert_eq!(f_exp2m1f(3.), 7.);
assert_eq!(f_exp2m1f(0.1), 0.07177346);
assert_eq!(f_exp2m1f(0.0543432432), 0.038386293);
assert!(f_exp2m1f(f32::NAN).is_nan());
assert_eq!(f_exp2m1f(f32::INFINITY), f32::INFINITY);
assert_eq!(f_exp2m1f(f32::NEG_INFINITY), -1.0);
}
}