#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
use super::common::{
asin_coefficients, atan_coefficients, cbrt_constants, exp_coefficients, exp2_coefficients,
hyperbolic_breakpoints, inv_hyperbolic_breakpoints, log_coefficients, trig_coefficients,
};
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn exp_f32(x: __m512) -> __m512 {
use exp_coefficients::*;
let xc = _mm512_max_ps(_mm512_set1_ps(MIN_F32), x);
let xc = _mm512_min_ps(_mm512_set1_ps(MAX_F32), xc);
let y = _mm512_mul_ps(xc, _mm512_set1_ps(std::f32::consts::LOG2_E));
let n = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm512_fnmadd_ps(n, _mm512_set1_ps(LN2_HI_F32), xc);
let r = _mm512_fnmadd_ps(n, _mm512_set1_ps(LN2_LO_F32), r);
let mut poly = _mm512_set1_ps(C7_F32);
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C6_F32));
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C5_F32));
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C4_F32));
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C3_F32));
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C2_F32));
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C1_F32));
poly = _mm512_fmadd_ps(poly, r, _mm512_set1_ps(C0_F32));
let n_i32 = _mm512_cvtps_epi32(n);
let n_hi = _mm512_srai_epi32::<1>(n_i32);
let n_lo = _mm512_sub_epi32(n_i32, n_hi);
let bias = _mm512_set1_epi32(127);
let p_hi = _mm512_castsi512_ps(_mm512_slli_epi32::<23>(_mm512_add_epi32(n_hi, bias)));
let p_lo = _mm512_castsi512_ps(_mm512_slli_epi32::<23>(_mm512_add_epi32(n_lo, bias)));
_mm512_mul_ps(_mm512_mul_ps(poly, p_hi), p_lo)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn exp_f64(x: __m512d) -> __m512d {
use exp_coefficients::*;
let log2e = _mm512_set1_pd(std::f64::consts::LOG2_E);
let ln2_hi = _mm512_set1_pd(LN2_HI_F64);
let ln2_lo = _mm512_set1_pd(LN2_LO_F64);
let c0 = _mm512_set1_pd(C0_F64);
let c1 = _mm512_set1_pd(C1_F64);
let c2 = _mm512_set1_pd(C2_F64);
let c3 = _mm512_set1_pd(C3_F64);
let c4 = _mm512_set1_pd(C4_F64);
let c5 = _mm512_set1_pd(C5_F64);
let c6 = _mm512_set1_pd(C6_F64);
let c7 = _mm512_set1_pd(C7_F64);
let c8 = _mm512_set1_pd(C8_F64);
let c9 = _mm512_set1_pd(C9_F64);
let c10 = _mm512_set1_pd(C10_F64);
let c11 = _mm512_set1_pd(C11_F64);
let c12 = _mm512_set1_pd(C12_F64);
let c13 = _mm512_set1_pd(C13_F64);
let xc = _mm512_max_pd(_mm512_set1_pd(MIN_F64), x);
let xc = _mm512_min_pd(_mm512_set1_pd(MAX_F64), xc);
let y = _mm512_mul_pd(xc, log2e);
let n = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm512_fnmadd_pd(n, ln2_hi, xc);
let r = _mm512_fnmadd_pd(n, ln2_lo, r);
let mut poly = c13;
poly = _mm512_fmadd_pd(poly, r, c12);
poly = _mm512_fmadd_pd(poly, r, c11);
poly = _mm512_fmadd_pd(poly, r, c10);
poly = _mm512_fmadd_pd(poly, r, c9);
poly = _mm512_fmadd_pd(poly, r, c8);
poly = _mm512_fmadd_pd(poly, r, c7);
poly = _mm512_fmadd_pd(poly, r, c6);
poly = _mm512_fmadd_pd(poly, r, c5);
poly = _mm512_fmadd_pd(poly, r, c4);
poly = _mm512_fmadd_pd(poly, r, c3);
poly = _mm512_fmadd_pd(poly, r, c2);
poly = _mm512_fmadd_pd(poly, r, c1);
poly = _mm512_fmadd_pd(poly, r, c0);
let n_hi = _mm512_roundscale_pd::<{ _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC }>(_mm512_mul_pd(
n,
_mm512_set1_pd(0.5),
));
let n_lo = _mm512_sub_pd(n, n_hi);
let bias = _mm512_set1_epi64(1023);
let p_hi = _mm512_castsi512_pd(_mm512_slli_epi64::<52>(_mm512_add_epi64(
_mm512_cvtpd_epi64(n_hi),
bias,
)));
let p_lo = _mm512_castsi512_pd(_mm512_slli_epi64::<52>(_mm512_add_epi64(
_mm512_cvtpd_epi64(n_lo),
bias,
)));
_mm512_mul_pd(_mm512_mul_pd(poly, p_hi), p_lo)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn tanh_f32(x: __m512) -> __m512 {
let a = _mm512_abs_ps(x);
let u = expm1_f32(_mm512_add_ps(a, a));
let d = _mm512_div_ps(u, _mm512_add_ps(u, _mm512_set1_ps(2.0)));
let is_inf = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(u, _mm512_set1_ps(f32::INFINITY));
let d = _mm512_mask_blend_ps(is_inf, d, _mm512_set1_ps(1.0));
copy_sign_f32(d, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn copy_sign_f32(magnitude: __m512, source: __m512) -> __m512 {
let sign_bit = _mm512_set1_epi32(0x8000_0000u32 as i32);
let sign = _mm512_and_si512(_mm512_castps_si512(source), sign_bit);
_mm512_castsi512_ps(_mm512_or_si512(_mm512_castps_si512(magnitude), sign))
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn tanh_f64(x: __m512d) -> __m512d {
let a = _mm512_abs_pd(x);
let u = expm1_f64(_mm512_add_pd(a, a));
let d = _mm512_div_pd(u, _mm512_add_pd(u, _mm512_set1_pd(2.0)));
let is_inf = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(u, _mm512_set1_pd(f64::INFINITY));
let d = _mm512_mask_blend_pd(is_inf, d, _mm512_set1_pd(1.0));
copy_sign_f64(d, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn copy_sign_f64(magnitude: __m512d, source: __m512d) -> __m512d {
let sign_bit = _mm512_set1_epi64(0x8000_0000_0000_0000u64 as i64);
let sign = _mm512_and_si512(_mm512_castpd_si512(source), sign_bit);
_mm512_castsi512_pd(_mm512_or_si512(_mm512_castpd_si512(magnitude), sign))
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn log_reduce_f32(x: __m512) -> (__m512, __m512) {
use log_coefficients::*;
let one = _mm512_set1_ps(1.0);
let two = _mm512_set1_ps(2.0);
let half = _mm512_set1_ps(0.5);
let sqrt2 = _mm512_set1_ps(std::f32::consts::SQRT_2);
let is_sub = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(x, _mm512_set1_ps(f32::MIN_POSITIVE));
let x_norm = _mm512_mask_mul_ps(x, is_sub, x, _mm512_set1_ps(SUBNORMAL_SCALE_F32));
let x_bits = _mm512_castps_si512(x_norm);
let exp_raw = _mm512_srli_epi32::<23>(x_bits);
let exp_unbiased = _mm512_sub_epi32(exp_raw, _mm512_set1_epi32(EXP_BIAS_F32));
let mut n = _mm512_cvtepi32_ps(exp_unbiased);
let mantissa_mask = _mm512_set1_epi32(MANTISSA_MASK_F32);
let exp_zero = _mm512_set1_epi32(EXP_ZERO_F32);
let m_bits = _mm512_or_si512(_mm512_and_si512(x_bits, mantissa_mask), exp_zero);
let mut m = _mm512_castsi512_ps(m_bits);
let need_adjust = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(m, sqrt2);
m = _mm512_mask_mul_ps(m, need_adjust, m, half);
n = _mm512_mask_add_ps(n, need_adjust, n, one);
let n = _mm512_mask_add_ps(n, is_sub, n, _mm512_set1_ps(SUBNORMAL_SHIFT_F32));
let f = _mm512_sub_ps(m, one);
let s = _mm512_div_ps(f, _mm512_add_ps(two, f));
let z = _mm512_mul_ps(s, s);
let r = _mm512_mul_ps(
z,
_mm512_fmadd_ps(
z,
_mm512_fmadd_ps(
z,
_mm512_fmadd_ps(z, _mm512_set1_ps(LG4_F32), _mm512_set1_ps(LG3_F32)),
_mm512_set1_ps(LG2_F32),
),
_mm512_set1_ps(LG1_F32),
),
);
let hfsq = _mm512_mul_ps(half, _mm512_mul_ps(f, f));
let logm = _mm512_sub_ps(
f,
_mm512_sub_ps(hfsq, _mm512_mul_ps(s, _mm512_add_ps(hfsq, r))),
);
(n, logm)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn log_special_f32(x: __m512, r: __m512) -> __m512 {
let zero = _mm512_setzero_ps();
let not_positive = _mm512_cmp_ps_mask::<_CMP_NGT_UQ>(x, zero);
let is_zero = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, zero);
let is_inf = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, _mm512_set1_ps(f32::INFINITY));
let out = _mm512_mask_blend_ps(not_positive, r, _mm512_set1_ps(f32::NAN));
let out = _mm512_mask_blend_ps(is_zero, out, _mm512_set1_ps(f32::NEG_INFINITY));
_mm512_mask_blend_ps(is_inf, out, _mm512_set1_ps(f32::INFINITY))
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log_f32(x: __m512) -> __m512 {
use log_coefficients::{LN2_HI_F32, LN2_LO_F32};
let (n, logm) = log_reduce_f32(x);
let lo = _mm512_fmadd_ps(n, _mm512_set1_ps(LN2_LO_F32), logm);
let r = _mm512_fmadd_ps(n, _mm512_set1_ps(LN2_HI_F32), lo);
log_special_f32(x, r)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
unsafe fn log_reduce_f64(x: __m512d) -> (__m512d, __m512d) {
use log_coefficients::*;
let one = _mm512_set1_pd(1.0);
let two = _mm512_set1_pd(2.0);
let half = _mm512_set1_pd(0.5);
let sqrt2 = _mm512_set1_pd(std::f64::consts::SQRT_2);
let is_sub = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(x, _mm512_set1_pd(f64::MIN_POSITIVE));
let x_norm = _mm512_mask_mul_pd(x, is_sub, x, _mm512_set1_pd(SUBNORMAL_SCALE_F64));
let n_shift = _mm512_mask_blend_pd(
is_sub,
_mm512_setzero_pd(),
_mm512_set1_pd(SUBNORMAL_SHIFT_F64),
);
let x_bits = _mm512_castpd_si512(x_norm);
let exp_raw = _mm512_srli_epi64::<52>(x_bits);
let exp_unbiased = _mm512_sub_epi64(exp_raw, _mm512_set1_epi64(EXP_BIAS_F64));
let mut n = _mm512_cvtepi64_pd(exp_unbiased);
let mantissa_mask = _mm512_set1_epi64(MANTISSA_MASK_F64 as i64);
let exp_zero = _mm512_set1_epi64(EXP_ZERO_F64 as i64);
let m_bits = _mm512_or_si512(_mm512_and_si512(x_bits, mantissa_mask), exp_zero);
let mut m = _mm512_castsi512_pd(m_bits);
let need_adjust = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(m, sqrt2);
m = _mm512_mask_mul_pd(m, need_adjust, m, half);
n = _mm512_mask_add_pd(n, need_adjust, n, one);
n = _mm512_add_pd(n, n_shift);
let f = _mm512_sub_pd(m, one);
let s = _mm512_div_pd(f, _mm512_add_pd(two, f));
let z = _mm512_mul_pd(s, s);
let w = _mm512_mul_pd(z, z);
let t1 = _mm512_mul_pd(
w,
_mm512_fmadd_pd(
w,
_mm512_fmadd_pd(w, _mm512_set1_pd(LG6_F64), _mm512_set1_pd(LG4_F64)),
_mm512_set1_pd(LG2_F64),
),
);
let t2 = _mm512_mul_pd(
z,
_mm512_fmadd_pd(
w,
_mm512_fmadd_pd(
w,
_mm512_fmadd_pd(w, _mm512_set1_pd(LG7_F64), _mm512_set1_pd(LG5_F64)),
_mm512_set1_pd(LG3_F64),
),
_mm512_set1_pd(LG1_F64),
),
);
let r = _mm512_add_pd(t2, t1);
let hfsq = _mm512_mul_pd(half, _mm512_mul_pd(f, f));
let logm = _mm512_sub_pd(
f,
_mm512_sub_pd(hfsq, _mm512_mul_pd(s, _mm512_add_pd(hfsq, r))),
);
(n, logm)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn log_special_f64(x: __m512d, r: __m512d) -> __m512d {
let zero = _mm512_setzero_pd();
let not_positive = _mm512_cmp_pd_mask::<_CMP_NGT_UQ>(x, zero);
let is_zero = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, zero);
let is_inf = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, _mm512_set1_pd(f64::INFINITY));
let out = _mm512_mask_blend_pd(not_positive, r, _mm512_set1_pd(f64::NAN));
let out = _mm512_mask_blend_pd(is_zero, out, _mm512_set1_pd(f64::NEG_INFINITY));
_mm512_mask_blend_pd(is_inf, out, _mm512_set1_pd(f64::INFINITY))
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn log_f64(x: __m512d) -> __m512d {
use log_coefficients::{LN2_HI_F64, LN2_LO_F64};
let (n, logm) = log_reduce_f64(x);
let lo = _mm512_fmadd_pd(n, _mm512_set1_pd(LN2_LO_F64), logm);
let r = _mm512_fmadd_pd(n, _mm512_set1_pd(LN2_HI_F64), lo);
log_special_f64(x, r)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn trig_reduce_f32(x: __m512) -> (__m512i, __m512) {
use trig_coefficients::{PIO2_1_F32, PIO2_2_F32, PIO2_3_F32};
let j = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(
_mm512_mul_ps(x, _mm512_set1_ps(std::f32::consts::FRAC_2_PI)),
);
let y = _mm512_fnmadd_ps(j, _mm512_set1_ps(PIO2_1_F32), x);
let y = _mm512_fnmadd_ps(j, _mm512_set1_ps(PIO2_2_F32), y);
let y = _mm512_fnmadd_ps(j, _mm512_set1_ps(PIO2_3_F32), y);
(_mm512_cvtps_epi32(j), y)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn sin_kernel_f32(y: __m512) -> __m512 {
use trig_coefficients::{SIN0_F32, SIN1_F32, SIN2_F32};
let z = _mm512_mul_ps(y, y);
let mut p = _mm512_set1_ps(SIN0_F32);
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(SIN1_F32));
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(SIN2_F32));
_mm512_fmadd_ps(_mm512_mul_ps(z, y), p, y)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn cos_kernel_f32(y: __m512) -> __m512 {
use trig_coefficients::{COS0_F32, COS1_F32, COS2_F32};
let one = _mm512_set1_ps(1.0);
let z = _mm512_mul_ps(y, y);
let mut p = _mm512_set1_ps(COS0_F32);
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(COS1_F32));
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(COS2_F32));
let r = _mm512_mul_ps(_mm512_mul_ps(z, z), p);
let hz = _mm512_mul_ps(_mm512_set1_ps(0.5), z);
let w = _mm512_sub_ps(one, hz);
let correction = _mm512_sub_ps(_mm512_sub_ps(one, w), hz);
_mm512_add_ps(w, _mm512_add_ps(correction, r))
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn sin_quadrant_f32(x: __m512, offset: i32) -> __m512 {
let (j, y) = trig_reduce_f32(x);
let sin_y = sin_kernel_f32(y);
let cos_y = cos_kernel_f32(y);
let j_mod_4 = _mm512_and_si512(
_mm512_add_epi32(j, _mm512_set1_epi32(offset)),
_mm512_set1_epi32(3),
);
let use_cos_mask = _mm512_cmpeq_epi32_mask(
_mm512_and_si512(j_mod_4, _mm512_set1_epi32(1)),
_mm512_set1_epi32(1),
);
let negate_mask = _mm512_cmpeq_epi32_mask(
_mm512_and_si512(j_mod_4, _mm512_set1_epi32(2)),
_mm512_set1_epi32(2),
);
let result = _mm512_mask_blend_ps(use_cos_mask, sin_y, cos_y);
let negated = _mm512_sub_ps(_mm512_setzero_ps(), result);
_mm512_mask_blend_ps(negate_mask, result, negated)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn sin_f32(x: __m512) -> __m512 {
let r = sin_quadrant_f32(x, 0);
let is_zero = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, _mm512_setzero_ps());
_mm512_mask_blend_ps(is_zero, r, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn trig_reduce_f64(x: __m512d) -> (__m512d, __m512d) {
use trig_coefficients::{PIO2_1_F64, PIO2_2_F64, PIO2_3_F64, PIO2_3T_F64};
let j = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(
_mm512_mul_pd(x, _mm512_set1_pd(std::f64::consts::FRAC_2_PI)),
);
let y = _mm512_fnmadd_pd(j, _mm512_set1_pd(PIO2_1_F64), x);
let y = _mm512_fnmadd_pd(j, _mm512_set1_pd(PIO2_2_F64), y);
let y = _mm512_fnmadd_pd(j, _mm512_set1_pd(PIO2_3_F64), y);
let y = _mm512_fnmadd_pd(j, _mm512_set1_pd(PIO2_3T_F64), y);
(j, y)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn sin_kernel_f64(y: __m512d) -> __m512d {
use trig_coefficients::{SIN1_F64, SIN2_F64, SIN3_F64, SIN4_F64, SIN5_F64, SIN6_F64};
let z = _mm512_mul_pd(y, y);
let mut p = _mm512_set1_pd(SIN6_F64);
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(SIN5_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(SIN4_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(SIN3_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(SIN2_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(SIN1_F64));
_mm512_fmadd_pd(_mm512_mul_pd(z, y), p, y)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn cos_kernel_f64(y: __m512d) -> __m512d {
use trig_coefficients::{COS1_F64, COS2_F64, COS3_F64, COS4_F64, COS5_F64, COS6_F64};
let one = _mm512_set1_pd(1.0);
let z = _mm512_mul_pd(y, y);
let mut p = _mm512_set1_pd(COS6_F64);
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(COS5_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(COS4_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(COS3_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(COS2_F64));
p = _mm512_fmadd_pd(p, z, _mm512_set1_pd(COS1_F64));
let r = _mm512_mul_pd(_mm512_mul_pd(z, z), p);
let hz = _mm512_mul_pd(_mm512_set1_pd(0.5), z);
let w = _mm512_sub_pd(one, hz);
let correction = _mm512_sub_pd(_mm512_sub_pd(one, w), hz);
_mm512_add_pd(w, _mm512_add_pd(correction, r))
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
unsafe fn sin_quadrant_f64(x: __m512d, offset: i64) -> __m512d {
let (j, y) = trig_reduce_f64(x);
let sin_y = sin_kernel_f64(y);
let cos_y = cos_kernel_f64(y);
let j_i64 = _mm512_add_epi64(_mm512_cvtpd_epi64(j), _mm512_set1_epi64(offset));
let j_mod_4 = _mm512_and_si512(j_i64, _mm512_set1_epi64(3));
let use_cos_mask = _mm512_cmpeq_epi64_mask(
_mm512_and_si512(j_mod_4, _mm512_set1_epi64(1)),
_mm512_set1_epi64(1),
);
let negate_mask = _mm512_cmpeq_epi64_mask(
_mm512_and_si512(j_mod_4, _mm512_set1_epi64(2)),
_mm512_set1_epi64(2),
);
let result = _mm512_mask_blend_pd(use_cos_mask, sin_y, cos_y);
let negated = _mm512_sub_pd(_mm512_setzero_pd(), result);
_mm512_mask_blend_pd(negate_mask, result, negated)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn sin_f64(x: __m512d) -> __m512d {
let r = sin_quadrant_f64(x, 0);
let is_zero = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, _mm512_setzero_pd());
_mm512_mask_blend_pd(is_zero, r, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cos_f32(x: __m512) -> __m512 {
sin_quadrant_f32(x, 1)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn cos_f64(x: __m512d) -> __m512d {
sin_quadrant_f64(x, 1)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn tan_f32(x: __m512) -> __m512 {
let (j, y) = trig_reduce_f32(x);
let sin_y = sin_kernel_f32(y);
let cos_y = cos_kernel_f32(y);
let odd_mask = _mm512_cmpeq_epi32_mask(
_mm512_and_si512(j, _mm512_set1_epi32(1)),
_mm512_set1_epi32(1),
);
let neg_cos = _mm512_sub_ps(_mm512_setzero_ps(), cos_y);
let num = _mm512_mask_blend_ps(odd_mask, sin_y, neg_cos);
let den = _mm512_mask_blend_ps(odd_mask, cos_y, sin_y);
let r = _mm512_div_ps(num, den);
let is_zero = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, _mm512_setzero_ps());
_mm512_mask_blend_ps(is_zero, r, x)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn tan_f64(x: __m512d) -> __m512d {
let (j, y) = trig_reduce_f64(x);
let sin_y = sin_kernel_f64(y);
let cos_y = cos_kernel_f64(y);
let j_i64 = _mm512_cvtpd_epi64(j);
let use_cot_mask = _mm512_cmpeq_epi64_mask(
_mm512_and_si512(j_i64, _mm512_set1_epi64(1)),
_mm512_set1_epi64(1),
);
let neg_cos_y = _mm512_sub_pd(_mm512_setzero_pd(), cos_y);
let num = _mm512_mask_blend_pd(use_cot_mask, sin_y, neg_cos_y);
let den = _mm512_mask_blend_pd(use_cot_mask, cos_y, sin_y);
let r = _mm512_div_pd(num, den);
let is_zero = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, _mm512_setzero_pd());
_mm512_mask_blend_pd(is_zero, r, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atan_f32(x: __m512) -> __m512 {
use atan_coefficients::*;
let one = _mm512_set1_ps(1.0);
let sign = _mm512_and_si512(
_mm512_castps_si512(x),
_mm512_castps_si512(_mm512_set1_ps(-0.0)),
);
let ax = _mm512_abs_ps(x);
let mut t = _mm512_div_ps(_mm512_set1_ps(-1.0), ax);
let mut hi = _mm512_set1_ps(ATAN_HI1_F32);
let mut lo = _mm512_set1_ps(ATAN_LO1_F32);
let in1 = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(ax, _mm512_set1_ps(BREAK1_F32));
let t_mid = _mm512_div_ps(_mm512_sub_ps(ax, one), _mm512_add_ps(ax, one));
t = _mm512_mask_blend_ps(in1, t, t_mid);
hi = _mm512_mask_blend_ps(in1, hi, _mm512_set1_ps(ATAN_HI0_F32));
lo = _mm512_mask_blend_ps(in1, lo, _mm512_set1_ps(ATAN_LO0_F32));
let zero = _mm512_setzero_ps();
let in0 = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(ax, _mm512_set1_ps(BREAK0_F32));
t = _mm512_mask_blend_ps(in0, t, ax);
hi = _mm512_mask_blend_ps(in0, hi, zero);
lo = _mm512_mask_blend_ps(in0, lo, zero);
let z = _mm512_mul_ps(t, t);
let mut p = _mm512_set1_ps(AT0_F32);
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(AT1_F32));
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(AT2_F32));
p = _mm512_fmadd_ps(p, z, _mm512_set1_ps(AT3_F32));
let poly = _mm512_fmadd_ps(_mm512_mul_ps(z, t), p, t);
let result = _mm512_add_ps(hi, _mm512_add_ps(lo, poly));
_mm512_castsi512_ps(_mm512_or_si512(_mm512_castps_si512(result), sign))
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atan_f64(x: __m512d) -> __m512d {
use atan_coefficients::*;
let one = _mm512_set1_pd(1.0);
let zero = _mm512_setzero_pd();
let ax = _mm512_abs_pd(x);
let neg_mask = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(x, zero);
let mut c = _mm512_set1_pd(1.5);
let mut hi = _mm512_set1_pd(ATAN_HI2_F64);
let mut lo = _mm512_set1_pd(ATAN_LO2_F64);
let in2 = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(ax, _mm512_set1_pd(BREAK2_F64));
c = _mm512_mask_blend_pd(in2, c, one);
hi = _mm512_mask_blend_pd(in2, hi, _mm512_set1_pd(ATAN_HI1_F64));
lo = _mm512_mask_blend_pd(in2, lo, _mm512_set1_pd(ATAN_LO1_F64));
let in1 = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(ax, _mm512_set1_pd(BREAK1_F64));
c = _mm512_mask_blend_pd(in1, c, _mm512_set1_pd(0.5));
hi = _mm512_mask_blend_pd(in1, hi, _mm512_set1_pd(ATAN_HI0_F64));
lo = _mm512_mask_blend_pd(in1, lo, _mm512_set1_pd(ATAN_LO0_F64));
let in0 = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(ax, _mm512_set1_pd(BREAK0_F64));
c = _mm512_mask_blend_pd(in0, c, zero);
hi = _mm512_mask_blend_pd(in0, hi, zero);
lo = _mm512_mask_blend_pd(in0, lo, zero);
let big = _mm512_cmp_pd_mask::<_CMP_GE_OQ>(ax, _mm512_set1_pd(BREAK3_F64));
hi = _mm512_mask_blend_pd(big, hi, _mm512_set1_pd(ATAN_HI3_F64));
lo = _mm512_mask_blend_pd(big, lo, _mm512_set1_pd(ATAN_LO3_F64));
let num = _mm512_mask_blend_pd(big, _mm512_sub_pd(ax, c), _mm512_set1_pd(-1.0));
let den = _mm512_mask_blend_pd(big, _mm512_fmadd_pd(c, ax, one), ax);
let t = _mm512_div_pd(num, den);
let z = _mm512_mul_pd(t, t);
let w = _mm512_mul_pd(z, z);
let mut s1 = _mm512_set1_pd(AT10_F64);
s1 = _mm512_fmadd_pd(s1, w, _mm512_set1_pd(AT8_F64));
s1 = _mm512_fmadd_pd(s1, w, _mm512_set1_pd(AT6_F64));
s1 = _mm512_fmadd_pd(s1, w, _mm512_set1_pd(AT4_F64));
s1 = _mm512_fmadd_pd(s1, w, _mm512_set1_pd(AT2_F64));
s1 = _mm512_fmadd_pd(s1, w, _mm512_set1_pd(AT0_F64));
s1 = _mm512_mul_pd(s1, z);
let mut s2 = _mm512_set1_pd(AT9_F64);
s2 = _mm512_fmadd_pd(s2, w, _mm512_set1_pd(AT7_F64));
s2 = _mm512_fmadd_pd(s2, w, _mm512_set1_pd(AT5_F64));
s2 = _mm512_fmadd_pd(s2, w, _mm512_set1_pd(AT3_F64));
s2 = _mm512_fmadd_pd(s2, w, _mm512_set1_pd(AT1_F64));
s2 = _mm512_mul_pd(s2, w);
let poly = _mm512_mul_pd(t, _mm512_add_pd(s1, s2));
let result = _mm512_sub_pd(hi, _mm512_sub_pd(_mm512_sub_pd(poly, lo), t));
let neg_result = _mm512_sub_pd(zero, result);
_mm512_mask_blend_pd(neg_mask, result, neg_result)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn rsqrt_f32(x: __m512) -> __m512 {
let approx = _mm512_rsqrt14_ps(x);
let half = _mm512_set1_ps(0.5);
let three = _mm512_set1_ps(3.0);
let x_approx2 = _mm512_mul_ps(x, _mm512_mul_ps(approx, approx));
let factor = _mm512_sub_ps(three, x_approx2);
_mm512_mul_ps(half, _mm512_mul_ps(approx, factor))
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn rsqrt_f64(x: __m512d) -> __m512d {
let sqrt_x = _mm512_sqrt_pd(x);
_mm512_div_pd(_mm512_set1_pd(1.0), sqrt_x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn exp2_f32(x: __m512) -> __m512 {
use exp_coefficients::{C0_F32, C1_F32, C2_F32, C3_F32, C4_F32, C5_F32, C6_F32, C7_F32};
use exp2_coefficients::{MAX_F32, MIN_F32};
let xc = _mm512_max_ps(_mm512_set1_ps(MIN_F32), x);
let xc = _mm512_min_ps(_mm512_set1_ps(MAX_F32), xc);
let n = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(xc);
let r = _mm512_sub_ps(xc, n);
let rl = _mm512_mul_ps(r, _mm512_set1_ps(std::f32::consts::LN_2));
let mut poly = _mm512_set1_ps(C7_F32);
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C6_F32));
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C5_F32));
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C4_F32));
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C3_F32));
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C2_F32));
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C1_F32));
poly = _mm512_fmadd_ps(poly, rl, _mm512_set1_ps(C0_F32));
let n_i32 = _mm512_cvtps_epi32(n);
let n_hi = _mm512_srai_epi32::<1>(n_i32);
let n_lo = _mm512_sub_epi32(n_i32, n_hi);
let bias = _mm512_set1_epi32(127);
let p_hi = _mm512_castsi512_ps(_mm512_slli_epi32::<23>(_mm512_add_epi32(n_hi, bias)));
let p_lo = _mm512_castsi512_ps(_mm512_slli_epi32::<23>(_mm512_add_epi32(n_lo, bias)));
_mm512_mul_ps(_mm512_mul_ps(poly, p_hi), p_lo)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn exp2_f64(x: __m512d) -> __m512d {
use exp2_coefficients::*;
let xc = _mm512_max_pd(x, _mm512_set1_pd(MIN_F64));
let xc = _mm512_min_pd(xc, _mm512_set1_pd(MAX_F64));
let n = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(xc);
let r = _mm512_sub_pd(xc, n);
let mut poly = _mm512_set1_pd(C13_F64);
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C12_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C11_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C10_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C9_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C8_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C7_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C6_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C5_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C4_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C3_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C2_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C1_F64));
poly = _mm512_fmadd_pd(poly, r, _mm512_set1_pd(C0_F64));
let n_hi = _mm512_roundscale_pd::<{ _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC }>(_mm512_mul_pd(
n,
_mm512_set1_pd(0.5),
));
let n_lo = _mm512_sub_pd(n, n_hi);
let bias = _mm512_set1_epi64(1023);
let p_hi = _mm512_castsi512_pd(_mm512_slli_epi64::<52>(_mm512_add_epi64(
_mm512_cvtpd_epi64(n_hi),
bias,
)));
let p_lo = _mm512_castsi512_pd(_mm512_slli_epi64::<52>(_mm512_add_epi64(
_mm512_cvtpd_epi64(n_lo),
bias,
)));
let out = _mm512_mul_pd(_mm512_mul_pd(poly, p_hi), p_lo);
_mm512_mask_blend_pd(_mm512_cmp_pd_mask::<_CMP_UNORD_Q>(x, x), out, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn expm1_f32(x: __m512) -> __m512 {
use exp_coefficients::*;
let xc = _mm512_max_ps(_mm512_set1_ps(EXPM1_MIN_F32), x);
let xc = _mm512_min_ps(_mm512_set1_ps(EXPM1_MAX_F32), xc);
let y = _mm512_mul_ps(xc, _mm512_set1_ps(std::f32::consts::LOG2_E));
let n = _mm512_roundscale_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm512_fnmadd_ps(n, _mm512_set1_ps(LN2_HI_F32), xc);
let r = _mm512_fnmadd_ps(n, _mm512_set1_ps(LN2_LO_F32), r);
let mut q = _mm512_set1_ps(C7_F32);
q = _mm512_fmadd_ps(q, r, _mm512_set1_ps(C6_F32));
q = _mm512_fmadd_ps(q, r, _mm512_set1_ps(C5_F32));
q = _mm512_fmadd_ps(q, r, _mm512_set1_ps(C4_F32));
q = _mm512_fmadd_ps(q, r, _mm512_set1_ps(C3_F32));
q = _mm512_fmadd_ps(q, r, _mm512_set1_ps(C2_F32));
let e = _mm512_fmadd_ps(_mm512_mul_ps(r, r), q, r);
let n_i32 = _mm512_cvtps_epi32(n);
let bias = _mm512_set1_epi32(127 - 1);
let t = _mm512_castsi512_ps(_mm512_slli_epi32::<23>(_mm512_add_epi32(n_i32, bias)));
let out = _mm512_mul_ps(
_mm512_set1_ps(2.0),
_mm512_fmadd_ps(t, e, _mm512_sub_ps(t, _mm512_set1_ps(0.5))),
);
let zero = _mm512_setzero_ps();
let out = _mm512_mask_blend_ps(_mm512_cmp_ps_mask::<_CMP_EQ_OQ>(n, zero), out, e);
_mm512_mask_blend_ps(_mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, zero), out, x)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn expm1_f64(x: __m512d) -> __m512d {
use exp_coefficients::*;
let xc = _mm512_max_pd(x, _mm512_set1_pd(EXPM1_MIN_F64));
let xc = _mm512_min_pd(xc, _mm512_set1_pd(EXPM1_MAX_F64));
let y = _mm512_mul_pd(xc, _mm512_set1_pd(std::f64::consts::LOG2_E));
let n = _mm512_roundscale_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm512_fnmadd_pd(n, _mm512_set1_pd(LN2_HI_F64), xc);
let r = _mm512_fnmadd_pd(n, _mm512_set1_pd(LN2_LO_F64), r);
let mut q = _mm512_set1_pd(C13_F64);
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C12_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C11_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C10_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C9_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C8_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C7_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C6_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C5_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C4_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C3_F64));
q = _mm512_fmadd_pd(q, r, _mm512_set1_pd(C2_F64));
let e = _mm512_fmadd_pd(_mm512_mul_pd(r, r), q, r);
let bias = _mm512_set1_epi64(1023 - 1);
let t = _mm512_castsi512_pd(_mm512_slli_epi64::<52>(_mm512_add_epi64(
_mm512_cvtpd_epi64(n),
bias,
)));
let out = _mm512_mul_pd(
_mm512_set1_pd(2.0),
_mm512_fmadd_pd(t, e, _mm512_sub_pd(t, _mm512_set1_pd(0.5))),
);
let no_scale = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(n, _mm512_setzero_pd());
let out = _mm512_mask_blend_pd(no_scale, out, e);
let is_zero = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, _mm512_setzero_pd());
let out = _mm512_mask_blend_pd(is_zero, out, x);
_mm512_mask_blend_pd(_mm512_cmp_pd_mask::<_CMP_UNORD_Q>(x, x), out, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log2_f32(x: __m512) -> __m512 {
let (n, logm) = log_reduce_f32(x);
let r = _mm512_fmadd_ps(logm, _mm512_set1_ps(std::f32::consts::LOG2_E), n);
log_special_f32(x, r)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn log2_f64(x: __m512d) -> __m512d {
let (n, logm) = log_reduce_f64(x);
let r = _mm512_fmadd_pd(logm, _mm512_set1_pd(std::f64::consts::LOG2_E), n);
log_special_f64(x, r)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log10_f32(x: __m512) -> __m512 {
let (n, logm) = log_reduce_f32(x);
let scaled = _mm512_mul_ps(logm, _mm512_set1_ps(std::f32::consts::LOG10_E));
let r = _mm512_fmadd_ps(n, _mm512_set1_ps(std::f32::consts::LOG10_2), scaled);
log_special_f32(x, r)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn log10_f64(x: __m512d) -> __m512d {
let (n, logm) = log_reduce_f64(x);
let scaled = _mm512_mul_pd(logm, _mm512_set1_pd(std::f64::consts::LOG10_E));
let r = _mm512_fmadd_pd(n, _mm512_set1_pd(std::f64::consts::LOG10_2), scaled);
log_special_f64(x, r)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn log1p_f32(x: __m512) -> __m512 {
let one = _mm512_set1_ps(1.0);
let u = _mm512_add_ps(one, x);
let c_small = _mm512_sub_ps(x, _mm512_sub_ps(u, one));
let c_large = _mm512_sub_ps(one, _mm512_sub_ps(u, x));
let x_leads = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(_mm512_abs_ps(x), one);
let c = _mm512_mask_blend_ps(x_leads, c_large, c_small);
let r = _mm512_add_ps(log_f32(u), _mm512_div_ps(c, u));
let is_unit = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(u, one);
let out = _mm512_mask_blend_ps(is_unit, r, x);
let is_neg_one = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, _mm512_set1_ps(-1.0));
let is_inf = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(x, _mm512_set1_ps(f32::INFINITY));
let out = _mm512_mask_blend_ps(is_neg_one, out, _mm512_set1_ps(f32::NEG_INFINITY));
_mm512_mask_blend_ps(is_inf, out, _mm512_set1_ps(f32::INFINITY))
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn log1p_f64(x: __m512d) -> __m512d {
let one = _mm512_set1_pd(1.0);
let u = _mm512_add_pd(one, x);
let c_small = _mm512_sub_pd(x, _mm512_sub_pd(u, one));
let c_large = _mm512_sub_pd(one, _mm512_sub_pd(u, x));
let x_leads = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(_mm512_abs_pd(x), one);
let c = _mm512_mask_blend_pd(x_leads, c_large, c_small);
let r = _mm512_add_pd(log_f64(u), _mm512_div_pd(c, u));
let is_unit = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(u, one);
let out = _mm512_mask_blend_pd(is_unit, r, x);
let is_neg_one = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, _mm512_set1_pd(-1.0));
let is_inf = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(x, _mm512_set1_pd(f64::INFINITY));
let out = _mm512_mask_blend_pd(is_neg_one, out, _mm512_set1_pd(f64::NEG_INFINITY));
_mm512_mask_blend_pd(is_inf, out, _mm512_set1_pd(f64::INFINITY))
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn sinh_f32(x: __m512) -> __m512 {
let one = _mm512_set1_ps(1.0);
let half = _mm512_set1_ps(0.5);
let a = _mm512_abs_ps(x);
let u = expm1_f32(a);
let d = _mm512_div_ps(u, _mm512_add_ps(one, u));
let is_inf = _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(u, _mm512_set1_ps(f32::INFINITY));
let d = _mm512_mask_blend_ps(is_inf, d, one);
let s = _mm512_mul_ps(half, _mm512_add_ps(u, d));
let t = exp_f32(_mm512_mul_ps(half, a));
let far = _mm512_mul_ps(_mm512_mul_ps(half, t), t);
let big = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(a, _mm512_set1_ps(hyperbolic_breakpoints::BIG_F32));
let s = _mm512_mask_blend_ps(big, s, far);
copy_sign_f32(s, x)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn sinh_f64(x: __m512d) -> __m512d {
let one = _mm512_set1_pd(1.0);
let a = _mm512_abs_pd(x);
let u = expm1_f64(a);
let d = _mm512_div_pd(u, _mm512_add_pd(one, u));
let is_inf = _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(u, _mm512_set1_pd(f64::INFINITY));
let d = _mm512_mask_blend_pd(is_inf, d, one);
let s = _mm512_mul_pd(_mm512_set1_pd(0.5), _mm512_add_pd(u, d));
let half = _mm512_set1_pd(0.5);
let t = exp_f64(_mm512_mul_pd(half, a));
let far = _mm512_mul_pd(_mm512_mul_pd(half, t), t);
let big = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(a, _mm512_set1_pd(hyperbolic_breakpoints::BIG_F64));
let s = _mm512_mask_blend_pd(big, s, far);
copy_sign_f64(s, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cosh_f32(x: __m512) -> __m512 {
let half = _mm512_set1_ps(0.5);
let a = _mm512_abs_ps(x);
let exp_x = exp_f32(x);
let exp_neg_x = exp_f32(_mm512_sub_ps(_mm512_setzero_ps(), x));
let near = _mm512_mul_ps(half, _mm512_add_ps(exp_x, exp_neg_x));
let t = exp_f32(_mm512_mul_ps(half, a));
let far = _mm512_mul_ps(_mm512_mul_ps(half, t), t);
let big = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(a, _mm512_set1_ps(hyperbolic_breakpoints::BIG_F32));
_mm512_mask_blend_ps(big, near, far)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn cosh_f64(x: __m512d) -> __m512d {
let half = _mm512_set1_pd(0.5);
let a = _mm512_abs_pd(x);
let exp_x = exp_f64(x);
let exp_neg_x = exp_f64(_mm512_sub_pd(_mm512_setzero_pd(), x));
let near = _mm512_mul_pd(half, _mm512_add_pd(exp_x, exp_neg_x));
let t = exp_f64(_mm512_mul_pd(half, a));
let far = _mm512_mul_pd(_mm512_mul_pd(half, t), t);
let big = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(a, _mm512_set1_pd(hyperbolic_breakpoints::BIG_F64));
_mm512_mask_blend_pd(big, near, far)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asinh_f32(x: __m512) -> __m512 {
use inv_hyperbolic_breakpoints::{BIG_F32, NEAR_F32};
let one = _mm512_set1_ps(1.0);
let a = _mm512_abs_ps(x);
let t = _mm512_mul_ps(a, a);
let root = _mm512_sqrt_ps(_mm512_add_ps(t, one));
let near = log1p_f32(_mm512_add_ps(a, _mm512_div_ps(t, _mm512_add_ps(one, root))));
let mid = log_f32(_mm512_fmadd_ps(
_mm512_set1_ps(2.0),
a,
_mm512_div_ps(one, _mm512_add_ps(root, a)),
));
let far = _mm512_add_ps(log_f32(a), _mm512_set1_ps(std::f32::consts::LN_2));
let is_mid = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(a, _mm512_set1_ps(NEAR_F32));
let is_far = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(a, _mm512_set1_ps(BIG_F32));
let r = _mm512_mask_blend_ps(is_mid, near, mid);
let r = _mm512_mask_blend_ps(is_far, r, far);
copy_sign_f32(r, x)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn asinh_f64(x: __m512d) -> __m512d {
use inv_hyperbolic_breakpoints::{BIG_F64, NEAR_F64};
let one = _mm512_set1_pd(1.0);
let a = _mm512_abs_pd(x);
let t = _mm512_mul_pd(a, a);
let root = _mm512_sqrt_pd(_mm512_add_pd(t, one));
let near = log1p_f64(_mm512_add_pd(a, _mm512_div_pd(t, _mm512_add_pd(one, root))));
let mid = log_f64(_mm512_fmadd_pd(
_mm512_set1_pd(2.0),
a,
_mm512_div_pd(one, _mm512_add_pd(root, a)),
));
let far = _mm512_add_pd(log_f64(a), _mm512_set1_pd(std::f64::consts::LN_2));
let is_mid = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(a, _mm512_set1_pd(NEAR_F64));
let is_far = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(a, _mm512_set1_pd(BIG_F64));
let r = _mm512_mask_blend_pd(is_mid, near, mid);
let r = _mm512_mask_blend_pd(is_far, r, far);
copy_sign_f64(r, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acosh_f32(x: __m512) -> __m512 {
use inv_hyperbolic_breakpoints::{BIG_F32, NEAR_F32};
let one = _mm512_set1_ps(1.0);
let two = _mm512_set1_ps(2.0);
let t = _mm512_sub_ps(x, one);
let disc = _mm512_sqrt_ps(_mm512_fmadd_ps(t, t, _mm512_mul_ps(two, t)));
let near = log1p_f32(_mm512_add_ps(t, disc));
let root = _mm512_sqrt_ps(_mm512_fmsub_ps(x, x, one));
let mid = log_f32(_mm512_fmsub_ps(
two,
x,
_mm512_div_ps(one, _mm512_add_ps(x, root)),
));
let far = _mm512_add_ps(log_f32(x), _mm512_set1_ps(std::f32::consts::LN_2));
let is_mid = _mm512_cmp_ps_mask::<_CMP_GE_OQ>(x, _mm512_set1_ps(NEAR_F32));
let is_far = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(x, _mm512_set1_ps(BIG_F32));
let r = _mm512_mask_blend_ps(is_mid, near, mid);
let r = _mm512_mask_blend_ps(is_far, r, far);
let below = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(x, one);
_mm512_mask_blend_ps(below, r, _mm512_set1_ps(f32::NAN))
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn acosh_f64(x: __m512d) -> __m512d {
use inv_hyperbolic_breakpoints::{BIG_F64, NEAR_F64};
let one = _mm512_set1_pd(1.0);
let two = _mm512_set1_pd(2.0);
let t = _mm512_sub_pd(x, one);
let disc = _mm512_sqrt_pd(_mm512_fmadd_pd(t, t, _mm512_mul_pd(two, t)));
let near = log1p_f64(_mm512_add_pd(t, disc));
let root = _mm512_sqrt_pd(_mm512_fmsub_pd(x, x, one));
let mid = log_f64(_mm512_fmsub_pd(
two,
x,
_mm512_div_pd(one, _mm512_add_pd(x, root)),
));
let far = _mm512_add_pd(log_f64(x), _mm512_set1_pd(std::f64::consts::LN_2));
let is_mid = _mm512_cmp_pd_mask::<_CMP_GE_OQ>(x, _mm512_set1_pd(NEAR_F64));
let is_far = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(x, _mm512_set1_pd(BIG_F64));
let r = _mm512_mask_blend_pd(is_mid, near, mid);
let r = _mm512_mask_blend_pd(is_far, r, far);
let below = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(x, one);
_mm512_mask_blend_pd(below, r, _mm512_set1_pd(f64::NAN))
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn atanh_f32(x: __m512) -> __m512 {
use inv_hyperbolic_breakpoints::ATANH_SPLIT_F32;
let one = _mm512_set1_ps(1.0);
let a = _mm512_abs_ps(x);
let t = _mm512_add_ps(a, a);
let den = _mm512_sub_ps(one, a);
let small = log1p_f32(_mm512_add_ps(t, _mm512_div_ps(_mm512_mul_ps(t, a), den)));
let large = log1p_f32(_mm512_div_ps(t, den));
let is_small = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(a, _mm512_set1_ps(ATANH_SPLIT_F32));
let picked = _mm512_mask_blend_ps(is_small, large, small);
let r = _mm512_mul_ps(_mm512_set1_ps(0.5), picked);
copy_sign_f32(r, x)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn atanh_f64(x: __m512d) -> __m512d {
use inv_hyperbolic_breakpoints::ATANH_SPLIT_F64;
let one = _mm512_set1_pd(1.0);
let a = _mm512_abs_pd(x);
let t = _mm512_add_pd(a, a);
let den = _mm512_sub_pd(one, a);
let small = log1p_f64(_mm512_add_pd(t, _mm512_div_pd(_mm512_mul_pd(t, a), den)));
let large = log1p_f64(_mm512_div_pd(t, den));
let is_small = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(a, _mm512_set1_pd(ATANH_SPLIT_F64));
let picked = _mm512_mask_blend_pd(is_small, large, small);
let r = _mm512_mul_pd(_mm512_set1_pd(0.5), picked);
copy_sign_f64(r, x)
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn asin_r_f32(t: __m512) -> __m512 {
use asin_coefficients::*;
let mut p = _mm512_set1_ps(PS0_F32);
p = _mm512_fmadd_ps(p, t, _mm512_set1_ps(PS1_F32));
p = _mm512_fmadd_ps(p, t, _mm512_set1_ps(PS2_F32));
p = _mm512_fmadd_ps(p, t, _mm512_set1_ps(PS3_F32));
p = _mm512_fmadd_ps(p, t, _mm512_set1_ps(PS4_F32));
_mm512_mul_ps(t, p)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asin_f32(x: __m512) -> __m512 {
use asin_coefficients::*;
let one = _mm512_set1_ps(1.0);
let half = _mm512_set1_ps(HALF_F32);
let sign = _mm512_and_si512(
_mm512_castps_si512(x),
_mm512_castps_si512(_mm512_set1_ps(-0.0)),
);
let ax = _mm512_abs_ps(x);
let small = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm512_mul_ps(_mm512_sub_ps(one, ax), half);
let t = _mm512_mask_blend_ps(small, t_refl, _mm512_mul_ps(ax, ax));
let s = _mm512_sqrt_ps(t);
let v = _mm512_mask_blend_ps(small, s, ax);
let w = _mm512_fmadd_ps(v, asin_r_f32(t), v);
let two_w = _mm512_add_ps(w, w);
let res_refl = _mm512_sub_ps(
_mm512_set1_ps(PIO2_HI_F32),
_mm512_sub_ps(two_w, _mm512_set1_ps(PIO2_LO_F32)),
);
let result = _mm512_mask_blend_ps(small, res_refl, w);
_mm512_castsi512_ps(_mm512_or_si512(_mm512_castps_si512(result), sign))
}
#[target_feature(enable = "avx512f")]
#[inline]
unsafe fn asin_r_f64(t: __m512d) -> __m512d {
use asin_coefficients::*;
let mut p = _mm512_set1_pd(PS5_F64);
p = _mm512_fmadd_pd(p, t, _mm512_set1_pd(PS4_F64));
p = _mm512_fmadd_pd(p, t, _mm512_set1_pd(PS3_F64));
p = _mm512_fmadd_pd(p, t, _mm512_set1_pd(PS2_F64));
p = _mm512_fmadd_pd(p, t, _mm512_set1_pd(PS1_F64));
p = _mm512_fmadd_pd(p, t, _mm512_set1_pd(PS0_F64));
p = _mm512_mul_pd(p, t);
let mut q = _mm512_set1_pd(QS4_F64);
q = _mm512_fmadd_pd(q, t, _mm512_set1_pd(QS3_F64));
q = _mm512_fmadd_pd(q, t, _mm512_set1_pd(QS2_F64));
q = _mm512_fmadd_pd(q, t, _mm512_set1_pd(QS1_F64));
q = _mm512_fmadd_pd(q, t, _mm512_set1_pd(1.0));
_mm512_div_pd(p, q)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn asin_f64(x: __m512d) -> __m512d {
use asin_coefficients::*;
let one = _mm512_set1_pd(1.0);
let zero = _mm512_setzero_pd();
let half = _mm512_set1_pd(HALF_F64);
let ax = _mm512_abs_pd(x);
let neg_mask = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(x, zero);
let small = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm512_mul_pd(_mm512_sub_pd(one, ax), half);
let t = _mm512_mask_blend_pd(small, t_refl, _mm512_mul_pd(ax, ax));
let r = asin_r_f64(t);
let s = _mm512_sqrt_pd(t);
let res_small = _mm512_fmadd_pd(ax, r, ax);
let s_sr = _mm512_fmadd_pd(s, r, s);
let two_s = _mm512_add_pd(s_sr, s_sr);
let res_refl = _mm512_sub_pd(
_mm512_set1_pd(PIO2_HI_F64),
_mm512_sub_pd(two_s, _mm512_set1_pd(PIO2_LO_F64)),
);
let result = _mm512_mask_blend_pd(small, res_refl, res_small);
let neg_result = _mm512_sub_pd(zero, result);
_mm512_mask_blend_pd(neg_mask, result, neg_result)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acos_f32(x: __m512) -> __m512 {
use asin_coefficients::*;
let one = _mm512_set1_ps(1.0);
let half = _mm512_set1_ps(HALF_F32);
let pio2_lo = _mm512_set1_ps(PIO2_LO_F32);
let ax = _mm512_abs_ps(x);
let small = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm512_mul_ps(_mm512_sub_ps(one, ax), half);
let t = _mm512_mask_blend_ps(small, t_refl, _mm512_mul_ps(x, x));
let q = asin_r_f32(t);
let s = _mm512_sqrt_ps(t);
let px = _mm512_mul_ps(x, q);
let res_small = _mm512_sub_ps(
_mm512_set1_ps(PIO2_HI_F32),
_mm512_add_ps(x, _mm512_sub_ps(px, pio2_lo)),
);
let ps = _mm512_mul_ps(s, q);
let s_sr = _mm512_add_ps(s, ps);
let res_pos = _mm512_add_ps(s_sr, s_sr);
let w = _mm512_add_ps(s, _mm512_sub_ps(ps, pio2_lo));
let res_neg = _mm512_sub_ps(_mm512_set1_ps(PI_HI_F32), _mm512_add_ps(w, w));
let positive = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(x, _mm512_setzero_ps());
let res_refl = _mm512_mask_blend_ps(positive, res_neg, res_pos);
_mm512_mask_blend_ps(small, res_refl, res_small)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn acos_f64(x: __m512d) -> __m512d {
use asin_coefficients::*;
let one = _mm512_set1_pd(1.0);
let zero = _mm512_setzero_pd();
let half = _mm512_set1_pd(HALF_F64);
let pio2_lo = _mm512_set1_pd(PIO2_LO_F64);
let ax = _mm512_abs_pd(x);
let small = _mm512_cmp_pd_mask::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm512_mul_pd(_mm512_sub_pd(one, ax), half);
let t = _mm512_mask_blend_pd(small, t_refl, _mm512_mul_pd(ax, ax));
let r = asin_r_f64(t);
let s = _mm512_sqrt_pd(t);
let res_small = _mm512_sub_pd(
_mm512_set1_pd(PIO2_HI_F64),
_mm512_add_pd(x, _mm512_sub_pd(_mm512_mul_pd(x, r), pio2_lo)),
);
let s_sr = _mm512_fmadd_pd(s, r, s);
let res_pos = _mm512_add_pd(s_sr, s_sr);
let w = _mm512_add_pd(s, _mm512_sub_pd(_mm512_mul_pd(s, r), pio2_lo));
let res_neg = _mm512_sub_pd(_mm512_set1_pd(PI_HI_F64), _mm512_add_pd(w, w));
let positive = _mm512_cmp_pd_mask::<_CMP_GT_OQ>(x, zero);
let res_refl = _mm512_mask_blend_pd(positive, res_neg, res_pos);
_mm512_mask_blend_pd(small, res_refl, res_small)
}
#[target_feature(enable = "avx512f")]
#[inline]
pub unsafe fn cbrt_f32(x: __m512) -> __m512 {
use cbrt_constants::*;
let sign_bit = _mm512_set1_epi32(0x8000_0000u32 as i32);
let x_bits = _mm512_castps_si512(x);
let sign = _mm512_and_si512(x_bits, sign_bit);
let abs_x = _mm512_castsi512_ps(_mm512_andnot_si512(sign_bit, x_bits));
let one = _mm512_set1_ps(1.0);
let big = _mm512_cmp_ps_mask::<_CMP_GE_OQ>(abs_x, _mm512_set1_ps(BIG_F32));
let small = _mm512_cmp_ps_mask::<_CMP_LT_OQ>(abs_x, _mm512_set1_ps(SMALL_F32));
let s_in = _mm512_mask_blend_ps(small, one, _mm512_set1_ps(SCALE_UP_F32));
let s_in = _mm512_mask_blend_ps(big, s_in, _mm512_set1_ps(SCALE_DOWN_F32));
let s_out = _mm512_mask_blend_ps(small, one, _mm512_set1_ps(UNSCALE_DOWN_F32));
let s_out = _mm512_mask_blend_ps(big, s_out, _mm512_set1_ps(UNSCALE_UP_F32));
let a = _mm512_mul_ps(abs_x, s_in);
let a_bits = _mm512_cvtepi32_ps(_mm512_castps_si512(a));
let q = _mm512_fmadd_ps(a_bits, _mm512_set1_ps(1.0 / 3.0), _mm512_set1_ps(B1_F32));
let t = _mm512_castsi512_ps(_mm512_cvtps_epi32(q));
let r = _mm512_mul_ps(_mm512_mul_ps(t, t), t);
let num = _mm512_add_ps(_mm512_add_ps(a, a), r);
let den = _mm512_add_ps(_mm512_add_ps(a, r), r);
let t = _mm512_mul_ps(t, _mm512_div_ps(num, den));
let r = _mm512_mul_ps(_mm512_mul_ps(t, t), t);
let num = _mm512_add_ps(_mm512_add_ps(a, a), r);
let den = _mm512_add_ps(_mm512_add_ps(a, r), r);
let t = _mm512_mul_ps(t, _mm512_div_ps(num, den));
let scaled = _mm512_castps_si512(_mm512_mul_ps(t, s_out));
let out = _mm512_castsi512_ps(_mm512_or_si512(scaled, sign));
let zero = _mm512_setzero_ps();
let ordinary = _mm512_cmp_ps_mask::<_CMP_GT_OQ>(abs_x, zero)
& _mm512_cmp_ps_mask::<_CMP_LT_OQ>(abs_x, _mm512_set1_ps(f32::INFINITY));
_mm512_mask_blend_ps(ordinary, x, out)
}
#[target_feature(enable = "avx512f", enable = "avx512dq")]
#[inline]
pub unsafe fn cbrt_f64(x: __m512d) -> __m512d {
let sign_bit = _mm512_set1_epi64(0x8000_0000_0000_0000u64 as i64);
let x_bits = _mm512_castpd_si512(x);
let sign = _mm512_and_si512(x_bits, sign_bit);
let abs_x = _mm512_castsi512_pd(_mm512_andnot_si512(sign_bit, x_bits));
let one_third = _mm512_set1_pd(1.0 / 3.0);
let log_x = log_f64(abs_x);
let guess = exp_f64(_mm512_mul_pd(log_x, one_third));
let two = _mm512_set1_pd(2.0);
let three = _mm512_set1_pd(3.0);
let y = guess;
let y2 = _mm512_mul_pd(y, y);
let y_new = _mm512_div_pd(_mm512_fmadd_pd(two, y, _mm512_div_pd(abs_x, y2)), three);
let y2 = _mm512_mul_pd(y_new, y_new);
let result = _mm512_div_pd(_mm512_fmadd_pd(two, y_new, _mm512_div_pd(abs_x, y2)), three);
_mm512_castsi512_pd(_mm512_or_si512(_mm512_castpd_si512(result), sign))
}