use crate::exponents::exp10f::EXP10F_COEFFS;
use crate::exponents::expf::{ExpfBackend, GenericExpfBackend};
#[cold]
#[inline(always)]
fn exp10m1f_small<B: ExpfBackend>(x: f32, backend: &B) -> f32 {
let dx = x as f64;
let dx_sq = dx * dx;
let c0 = dx * f64::from_bits(EXP10F_COEFFS[0]);
let c1 = backend.fma(
dx,
f64::from_bits(EXP10F_COEFFS[2]),
f64::from_bits(EXP10F_COEFFS[1]),
);
let c2 = backend.fma(
dx,
f64::from_bits(EXP10F_COEFFS[4]),
f64::from_bits(EXP10F_COEFFS[3]),
);
backend.polyeval3(dx_sq, c0, c1, c2) as f32
}
#[inline(always)]
fn exp10m1f_gen<B: ExpfBackend>(x: f32, backend: B) -> f32 {
let x_u = x.to_bits();
let x_abs = x_u & 0x7fff_ffffu32;
if x.is_sign_positive() && x_u >= 0x421a_209bu32 {
return x + f32::INFINITY;
}
if x_abs <= 0x3b9a_209bu32 {
return exp10m1f_small(x, &backend);
}
if x_u >= 0xc0f0d2f1 {
if x.is_infinite() {
return -1.0;
}
if x.is_nan() {
return x;
}
if x_u == 0xc0f0d2f1 {
return f32::from_bits(0xbf7fffff); }
return -1.0;
}
if x_u & 0x800f_ffffu32 == 0 {
match x_u {
0x3f800000u32 => return 9.0, 0x40000000u32 => return 99.0, 0x40400000u32 => return 999.0, 0x40800000u32 => return 9_999.0, 0x40a00000u32 => return 99_999.0, 0x40c00000u32 => return 999_999.0, 0x40e00000u32 => return 9_999_999.0, 0x41000000u32 => return 99_999_999.0, 0x41100000u32 => return 999_999_999.0, 0x41200000u32 => return 9_999_999_999.0, _ => {}
}
}
let rr = crate::exponents::exp10f::exp_b_range_reduc(x, &backend);
let lo_sq = rr.lo * rr.lo;
let c0 = backend.fma(rr.lo, f64::from_bits(EXP10F_COEFFS[0]), 1.0);
let c1 = backend.fma(
rr.lo,
f64::from_bits(EXP10F_COEFFS[2]),
f64::from_bits(EXP10F_COEFFS[1]),
);
let c2 = backend.fma(
rr.lo,
f64::from_bits(EXP10F_COEFFS[4]),
f64::from_bits(EXP10F_COEFFS[3]),
);
let exp10_lo = backend.polyeval3(lo_sq, c0, c1, c2);
backend.fma(exp10_lo, rr.hi, -1.0) as f32
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx", enable = "fma")]
unsafe fn exp10f_fma_impl(x: f32) -> f32 {
use crate::exponents::expf::FmaBackend;
exp10m1f_gen(x, FmaBackend {})
}
#[inline]
pub fn f_exp10m1f(x: f32) -> f32 {
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
exp10m1f_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")
{
exp10f_fma_impl
} else {
fn def_expf(x: f32) -> f32 {
exp10m1f_gen(x, GenericExpfBackend {})
}
def_expf
}
});
unsafe { q(x) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exp10m1f() {
assert_eq!(f_exp10m1f(0.0), 0.0);
assert_eq!(f_exp10m1f(1.0), 9.0);
assert_eq!(f_exp10m1f(1.5), 30.622776);
}
}