use core::f32;
#[cfg(feature = "fastmath")]
pub fn cbrtf(x: f32) -> f32 {
const B1: u32 = 709_958_130;
let mut r: f64;
let mut t: f64;
let mut ui: u32 = x.to_bits();
let mut hx: u32 = ui & 0x7FFF_FFFF;
hx = hx / 3 + B1;
ui &= 0x8000_0000;
ui |= hx;
t = f64::from(f32::from_bits(ui));
r = t * t * t;
t = t * (f64::from(x) + f64::from(x) + r) / (f64::from(x) + r + r);
r = t * t * t;
t = t * (f64::from(x) + f64::from(x) + r) / (f64::from(x) + r + r);
t as f32
}
#[cfg(feature = "fastmath")]
pub fn powf(x: f32, y: f32) -> f32 {
exp2(log2(x) * y)
}
#[cfg(feature = "fastmath")]
fn exp2(x: f32) -> f32 {
let x = x.clamp(-126.99999, 129.0);
let ipart: i32 = unsafe { (x - 0.5).to_int_unchecked() };
let fpart = x - ipart as f32;
let expi = f32::from_bits(((ipart + 127_i32) << 23_i32) as u32);
let expf = poly5(
fpart,
9.999_999_4E-1,
6.931_531E-1,
2.401_536_1E-1,
5.582_631_8E-2,
8.989_34E-3,
1.877_576_7E-3,
);
expi * expf
}
#[cfg(feature = "fastmath")]
fn log2(x: f32) -> f32 {
let expmask = 0x7F80_0000_i32;
let mantmask = 0x007F_FFFF_i32;
let one_bits = 1_f32.to_bits() as i32;
let x_bits = x.to_bits() as i32;
let exp = (((x_bits & expmask) >> 23_i32) - 127_i32) as f32;
let mant = f32::from_bits(((x_bits & mantmask) | one_bits) as u32);
let polynomial = poly5(
mant,
3.115_79,
-3.324_199,
2.598_845_2,
-1.231_530_3,
3.182_133_7E-1,
-3.443_600_6E-2,
);
let polynomial = polynomial * (mant - 1.0);
polynomial + exp
}
#[cfg(feature = "fastmath")]
#[inline(always)]
fn poly5(x: f32, c0: f32, c1: f32, c2: f32, c3: f32, c4: f32, c5: f32) -> f32 {
x.mul_add(poly4(x, c1, c2, c3, c4, c5), c0)
}
#[cfg(feature = "fastmath")]
#[inline(always)]
fn poly4(x: f32, c0: f32, c1: f32, c2: f32, c3: f32, c4: f32) -> f32 {
x.mul_add(poly3(x, c1, c2, c3, c4), c0)
}
#[cfg(feature = "fastmath")]
#[inline(always)]
fn poly3(x: f32, c0: f32, c1: f32, c2: f32, c3: f32) -> f32 {
x.mul_add(poly2(x, c1, c2, c3), c0)
}
#[cfg(feature = "fastmath")]
#[inline(always)]
fn poly2(x: f32, c0: f32, c1: f32, c2: f32) -> f32 {
x.mul_add(poly1(x, c1, c2), c0)
}
#[cfg(feature = "fastmath")]
#[inline(always)]
fn poly1(x: f32, c0: f32, c1: f32) -> f32 {
x.mul_add(poly0(x, c1), c0)
}
#[cfg(feature = "fastmath")]
#[inline(always)]
const fn poly0(_x: f32, c0: f32) -> f32 {
c0
}
#[cfg(not(feature = "fastmath"))]
#[inline(always)]
pub fn cbrtf(x: f32) -> f32 {
x.cbrt()
}
#[cfg(not(feature = "fastmath"))]
#[inline(always)]
pub fn powf(x: f32, y: f32) -> f32 {
x.powf(y)
}