use crate::common::{is_integerf, is_odd_integerf};
use crate::polyeval::f_polyeval5;
use crate::sin_cosf::argument_reduction_pi::ArgumentReducerPi;
use crate::sin_cosf::sincosf_eval::{cospif_eval, sinpif_eval};
#[inline(always)]
fn cospif_gen_impl(x: f32) -> f32 {
let x_abs = x.to_bits() & 0x7fff_ffffu32;
let x = f32::from_bits(x_abs);
let xd = x as f64;
if x_abs <= 0x3d80_0000u32 {
if x_abs < 0x38a2_f984u32 {
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
))]
{
use crate::common::f_fmlaf;
return f_fmlaf(x, f32::from_bits(0xb3000000), 1.);
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
)))]
{
use crate::common::f_fmla;
return f_fmla(xd, f64::from_bits(0xbe60000000000000), 1.) as f32;
}
}
let x2 = xd * xd;
let p = f_polyeval5(
x2,
f64::from_bits(0x3ff0000000000000),
f64::from_bits(0xc013bd3cc9be43f7),
f64::from_bits(0x40103c1f08091fe0),
f64::from_bits(0xbff55d3ba3d94835),
f64::from_bits(0x3fce173c2a00e74e),
);
return p as f32;
}
if x_abs >= 0x4b00_0000u32 || is_integerf(x) {
if x_abs >= 0x7f80_0000u32 {
return x + f32::NAN;
}
if x_abs < 0x4b80_0000u32 {
static CF: [f32; 2] = [1., -1.];
return CF[is_odd_integerf(x) as usize];
}
return 1.;
}
let reducer = ArgumentReducerPi { x: x as f64 };
let (y, k) = reducer.reduce_0p25();
(match k & 3 {
0 => cospif_eval(y),
1 => sinpif_eval(-y),
2 => -cospif_eval(y),
_ => sinpif_eval(y),
}) as f32
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx", enable = "fma")]
unsafe fn cospif_fma_impl(x: f32) -> f32 {
let x_abs = x.to_bits() & 0x7fff_ffffu32;
let x = f32::from_bits(x_abs);
let xd = x as f64;
if x_abs <= 0x3d80_0000u32 {
if x_abs < 0x38a2_f984u32 {
return f32::mul_add(x, f32::from_bits(0xb3000000), 1.);
}
let x2 = xd * xd;
use crate::polyeval::d_polyeval5;
let p = d_polyeval5(
x2,
f64::from_bits(0x3ff0000000000000),
f64::from_bits(0xc013bd3cc9be43f7),
f64::from_bits(0x40103c1f08091fe0),
f64::from_bits(0xbff55d3ba3d94835),
f64::from_bits(0x3fce173c2a00e74e),
);
return p as f32;
}
if x_abs >= 0x4b00_0000u32 || x.round_ties_even() == x {
if x_abs >= 0x7f80_0000u32 {
return x + f32::NAN;
}
if x_abs < 0x4b80_0000u32 {
static CF: [f32; 2] = [1., -1.];
let is_odd_integer = is_odd_integerf(x);
return CF[is_odd_integer as usize];
}
return 1.;
}
let reducer = ArgumentReducerPi { x: x as f64 };
let (y, k) = reducer.reduce_0p25_fma();
use crate::sin_cosf::sincosf_eval::{cospif_eval_fma, sinpif_eval_fma};
(match k & 3 {
0 => cospif_eval_fma(y),
1 => sinpif_eval_fma(-y),
2 => -cospif_eval_fma(y),
_ => sinpif_eval_fma(y),
}) as f32
}
#[inline]
pub fn f_cospif(x: f32) -> f32 {
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
cospif_gen_impl(x)
}
#[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")
{
cospif_fma_impl
} else {
cospif_gen_impl
}
});
unsafe { q(x) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_f_cospif() {
assert_eq!(f_cospif(1.), -1.);
assert_eq!(f_cospif(-3.5), 0.0);
assert_eq!(f_cospif(3.), -1.);
assert_eq!(f_cospif(-3.), -1.);
assert_eq!(f_cospif(2.), 1.);
assert_eq!(f_cospif(-2.), 1.);
assert_eq!(f_cospif(115.30706), -0.5696978);
assert!(f_cospif(f32::INFINITY).is_nan());
assert!(f_cospif(f32::NAN).is_nan());
assert!(f_cospif(f32::NEG_INFINITY).is_nan());
}
}