#[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 = "avx2", enable = "fma")]
#[inline]
pub unsafe fn exp_f32(x: __m256) -> __m256 {
use exp_coefficients::*;
let xc = _mm256_max_ps(_mm256_set1_ps(MIN_F32), x);
let xc = _mm256_min_ps(_mm256_set1_ps(MAX_F32), xc);
let y = _mm256_mul_ps(xc, _mm256_set1_ps(std::f32::consts::LOG2_E));
let n = _mm256_round_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm256_fnmadd_ps(n, _mm256_set1_ps(LN2_HI_F32), xc);
let r = _mm256_fnmadd_ps(n, _mm256_set1_ps(LN2_LO_F32), r);
let mut poly = _mm256_set1_ps(C7_F32);
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C6_F32));
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C5_F32));
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C4_F32));
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C3_F32));
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C2_F32));
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C1_F32));
poly = _mm256_fmadd_ps(poly, r, _mm256_set1_ps(C0_F32));
let n_i32 = _mm256_cvtps_epi32(n);
let n_hi = _mm256_srai_epi32::<1>(n_i32);
let n_lo = _mm256_sub_epi32(n_i32, n_hi);
let bias = _mm256_set1_epi32(127);
let p_hi = _mm256_castsi256_ps(_mm256_slli_epi32::<23>(_mm256_add_epi32(n_hi, bias)));
let p_lo = _mm256_castsi256_ps(_mm256_slli_epi32::<23>(_mm256_add_epi32(n_lo, bias)));
_mm256_mul_ps(_mm256_mul_ps(poly, p_hi), p_lo)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn exp_f64(x: __m256d) -> __m256d {
use exp_coefficients::*;
let log2e = _mm256_set1_pd(std::f64::consts::LOG2_E);
let ln2_hi = _mm256_set1_pd(LN2_HI_F64);
let ln2_lo = _mm256_set1_pd(LN2_LO_F64);
let c0 = _mm256_set1_pd(C0_F64);
let c1 = _mm256_set1_pd(C1_F64);
let c2 = _mm256_set1_pd(C2_F64);
let c3 = _mm256_set1_pd(C3_F64);
let c4 = _mm256_set1_pd(C4_F64);
let c5 = _mm256_set1_pd(C5_F64);
let c6 = _mm256_set1_pd(C6_F64);
let c7 = _mm256_set1_pd(C7_F64);
let c8 = _mm256_set1_pd(C8_F64);
let c9 = _mm256_set1_pd(C9_F64);
let c10 = _mm256_set1_pd(C10_F64);
let c11 = _mm256_set1_pd(C11_F64);
let c12 = _mm256_set1_pd(C12_F64);
let c13 = _mm256_set1_pd(C13_F64);
let xc = _mm256_max_pd(_mm256_set1_pd(MIN_F64), x);
let xc = _mm256_min_pd(_mm256_set1_pd(MAX_F64), xc);
let y = _mm256_mul_pd(xc, log2e);
let n = _mm256_round_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm256_fnmadd_pd(n, ln2_hi, xc);
let r = _mm256_fnmadd_pd(n, ln2_lo, r);
let mut poly = c13;
poly = _mm256_fmadd_pd(poly, r, c12);
poly = _mm256_fmadd_pd(poly, r, c11);
poly = _mm256_fmadd_pd(poly, r, c10);
poly = _mm256_fmadd_pd(poly, r, c9);
poly = _mm256_fmadd_pd(poly, r, c8);
poly = _mm256_fmadd_pd(poly, r, c7);
poly = _mm256_fmadd_pd(poly, r, c6);
poly = _mm256_fmadd_pd(poly, r, c5);
poly = _mm256_fmadd_pd(poly, r, c4);
poly = _mm256_fmadd_pd(poly, r, c3);
poly = _mm256_fmadd_pd(poly, r, c2);
poly = _mm256_fmadd_pd(poly, r, c1);
poly = _mm256_fmadd_pd(poly, r, c0);
let mut result = [0.0f64; 4];
let mut n_arr = [0.0f64; 4];
let mut poly_arr = [0.0f64; 4];
_mm256_storeu_pd(n_arr.as_mut_ptr(), n);
_mm256_storeu_pd(poly_arr.as_mut_ptr(), poly);
for i in 0..4 {
let n_i = n_arr[i] as i64;
let hi = n_i / 2;
let lo = n_i - hi;
let p_hi = f64::from_bits(((hi + 1023) as u64) << 52);
let p_lo = f64::from_bits(((lo + 1023) as u64) << 52);
result[i] = (poly_arr[i] * p_hi) * p_lo;
}
_mm256_loadu_pd(result.as_ptr())
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn tanh_f32(x: __m256) -> __m256 {
let sign_mask = _mm256_set1_ps(-0.0);
let a = _mm256_andnot_ps(sign_mask, x);
let u = expm1_f32(_mm256_add_ps(a, a));
let d = _mm256_div_ps(u, _mm256_add_ps(u, _mm256_set1_ps(2.0)));
let is_inf = _mm256_cmp_ps::<_CMP_EQ_OQ>(u, _mm256_set1_ps(f32::INFINITY));
let d = _mm256_blendv_ps(d, _mm256_set1_ps(1.0), is_inf);
_mm256_or_ps(d, _mm256_and_ps(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn tanh_f64(x: __m256d) -> __m256d {
let sign_mask = _mm256_set1_pd(-0.0);
let a = _mm256_andnot_pd(sign_mask, x);
let u = expm1_f64(_mm256_add_pd(a, a));
let d = _mm256_div_pd(u, _mm256_add_pd(u, _mm256_set1_pd(2.0)));
let is_inf = _mm256_cmp_pd::<_CMP_EQ_OQ>(u, _mm256_set1_pd(f64::INFINITY));
let d = _mm256_blendv_pd(d, _mm256_set1_pd(1.0), is_inf);
_mm256_or_pd(d, _mm256_and_pd(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn log_reduce_f32(x: __m256) -> (__m256, __m256) {
use log_coefficients::*;
let one = _mm256_set1_ps(1.0);
let two = _mm256_set1_ps(2.0);
let half = _mm256_set1_ps(0.5);
let sqrt2 = _mm256_set1_ps(std::f32::consts::SQRT_2);
let is_sub = _mm256_cmp_ps::<_CMP_LT_OQ>(x, _mm256_set1_ps(f32::MIN_POSITIVE));
let scaled = _mm256_mul_ps(x, _mm256_set1_ps(SUBNORMAL_SCALE_F32));
let x_norm = _mm256_blendv_ps(x, scaled, is_sub);
let n_shift = _mm256_blendv_ps(
_mm256_setzero_ps(),
_mm256_set1_ps(SUBNORMAL_SHIFT_F32),
is_sub,
);
let x_bits = _mm256_castps_si256(x_norm);
let exp_raw = _mm256_srli_epi32::<23>(x_bits);
let exp_unbiased = _mm256_sub_epi32(exp_raw, _mm256_set1_epi32(EXP_BIAS_F32));
let mut n = _mm256_cvtepi32_ps(exp_unbiased);
let mantissa_mask = _mm256_set1_epi32(MANTISSA_MASK_F32);
let exp_zero = _mm256_set1_epi32(EXP_ZERO_F32);
let m_bits = _mm256_or_si256(_mm256_and_si256(x_bits, mantissa_mask), exp_zero);
let mut m = _mm256_castsi256_ps(m_bits);
let need_adjust = _mm256_cmp_ps::<_CMP_GT_OQ>(m, sqrt2);
m = _mm256_blendv_ps(m, _mm256_mul_ps(m, half), need_adjust);
n = _mm256_blendv_ps(n, _mm256_add_ps(n, one), need_adjust);
let n = _mm256_add_ps(n, n_shift);
let f = _mm256_sub_ps(m, one);
let s = _mm256_div_ps(f, _mm256_add_ps(two, f));
let z = _mm256_mul_ps(s, s);
let r = _mm256_mul_ps(
z,
_mm256_fmadd_ps(
z,
_mm256_fmadd_ps(
z,
_mm256_fmadd_ps(z, _mm256_set1_ps(LG4_F32), _mm256_set1_ps(LG3_F32)),
_mm256_set1_ps(LG2_F32),
),
_mm256_set1_ps(LG1_F32),
),
);
let hfsq = _mm256_mul_ps(half, _mm256_mul_ps(f, f));
let logm = _mm256_sub_ps(
f,
_mm256_sub_ps(hfsq, _mm256_mul_ps(s, _mm256_add_ps(hfsq, r))),
);
(n, logm)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn log_special_f32(x: __m256, r: __m256) -> __m256 {
let zero = _mm256_setzero_ps();
let not_positive = _mm256_cmp_ps::<_CMP_NGT_UQ>(x, zero);
let is_zero = _mm256_cmp_ps::<_CMP_EQ_OQ>(x, zero);
let is_inf = _mm256_cmp_ps::<_CMP_EQ_OQ>(x, _mm256_set1_ps(f32::INFINITY));
let out = _mm256_blendv_ps(r, _mm256_set1_ps(f32::NAN), not_positive);
let out = _mm256_blendv_ps(out, _mm256_set1_ps(f32::NEG_INFINITY), is_zero);
_mm256_blendv_ps(out, _mm256_set1_ps(f32::INFINITY), is_inf)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log_f32(x: __m256) -> __m256 {
use log_coefficients::{LN2_HI_F32, LN2_LO_F32};
let (n, logm) = log_reduce_f32(x);
let lo = _mm256_fmadd_ps(n, _mm256_set1_ps(LN2_LO_F32), logm);
let r = _mm256_fmadd_ps(n, _mm256_set1_ps(LN2_HI_F32), lo);
log_special_f32(x, r)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn log_reduce_f64(x: __m256d) -> (__m256d, __m256d) {
use log_coefficients::*;
let one = _mm256_set1_pd(1.0);
let two = _mm256_set1_pd(2.0);
let half = _mm256_set1_pd(0.5);
let sqrt2_val = std::f64::consts::SQRT_2;
let is_sub = _mm256_cmp_pd::<_CMP_LT_OQ>(x, _mm256_set1_pd(f64::MIN_POSITIVE));
let scaled = _mm256_mul_pd(x, _mm256_set1_pd(SUBNORMAL_SCALE_F64));
let x_norm = _mm256_blendv_pd(x, scaled, is_sub);
let n_shift = _mm256_blendv_pd(
_mm256_setzero_pd(),
_mm256_set1_pd(SUBNORMAL_SHIFT_F64),
is_sub,
);
let x_bits = _mm256_castpd_si256(x_norm);
let exp_raw = _mm256_srli_epi64::<52>(x_bits);
let mantissa_mask = _mm256_set1_epi64x(MANTISSA_MASK_F64 as i64);
let exp_zero = _mm256_set1_epi64x(EXP_ZERO_F64 as i64);
let m_bits = _mm256_or_si256(_mm256_and_si256(x_bits, mantissa_mask), exp_zero);
let m_initial = _mm256_castsi256_pd(m_bits);
let mut m_arr = [0.0f64; 4];
let mut exp_arr = [0i64; 4];
_mm256_storeu_pd(m_arr.as_mut_ptr(), m_initial);
_mm256_storeu_si256(exp_arr.as_mut_ptr() as *mut __m256i, exp_raw);
let mut n_arr = [0.0f64; 4];
for i in 0..4 {
let mut exp_unbiased = exp_arr[i] - EXP_BIAS_F64;
let mut m = m_arr[i];
if m > sqrt2_val {
m *= 0.5;
exp_unbiased += 1;
}
n_arr[i] = exp_unbiased as f64;
m_arr[i] = m;
}
let n = _mm256_add_pd(_mm256_loadu_pd(n_arr.as_ptr()), n_shift);
let m = _mm256_loadu_pd(m_arr.as_ptr());
let f = _mm256_sub_pd(m, one);
let s = _mm256_div_pd(f, _mm256_add_pd(two, f));
let z = _mm256_mul_pd(s, s);
let w = _mm256_mul_pd(z, z);
let t1 = _mm256_mul_pd(
w,
_mm256_fmadd_pd(
w,
_mm256_fmadd_pd(w, _mm256_set1_pd(LG6_F64), _mm256_set1_pd(LG4_F64)),
_mm256_set1_pd(LG2_F64),
),
);
let t2 = _mm256_mul_pd(
z,
_mm256_fmadd_pd(
w,
_mm256_fmadd_pd(
w,
_mm256_fmadd_pd(w, _mm256_set1_pd(LG7_F64), _mm256_set1_pd(LG5_F64)),
_mm256_set1_pd(LG3_F64),
),
_mm256_set1_pd(LG1_F64),
),
);
let r = _mm256_add_pd(t2, t1);
let hfsq = _mm256_mul_pd(half, _mm256_mul_pd(f, f));
let logm = _mm256_sub_pd(
f,
_mm256_sub_pd(hfsq, _mm256_mul_pd(s, _mm256_add_pd(hfsq, r))),
);
(n, logm)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn log_special_f64(x: __m256d, r: __m256d) -> __m256d {
let zero = _mm256_setzero_pd();
let not_positive = _mm256_cmp_pd::<_CMP_NGT_UQ>(x, zero);
let is_zero = _mm256_cmp_pd::<_CMP_EQ_OQ>(x, zero);
let is_inf = _mm256_cmp_pd::<_CMP_EQ_OQ>(x, _mm256_set1_pd(f64::INFINITY));
let out = _mm256_blendv_pd(r, _mm256_set1_pd(f64::NAN), not_positive);
let out = _mm256_blendv_pd(out, _mm256_set1_pd(f64::NEG_INFINITY), is_zero);
_mm256_blendv_pd(out, _mm256_set1_pd(f64::INFINITY), is_inf)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log_f64(x: __m256d) -> __m256d {
use log_coefficients::{LN2_HI_F64, LN2_LO_F64};
let (n, logm) = log_reduce_f64(x);
let lo = _mm256_fmadd_pd(n, _mm256_set1_pd(LN2_LO_F64), logm);
let r = _mm256_fmadd_pd(n, _mm256_set1_pd(LN2_HI_F64), lo);
log_special_f64(x, r)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn trig_reduce_f32(x: __m256) -> (__m256i, __m256) {
use trig_coefficients::{PIO2_1_F32, PIO2_2_F32, PIO2_3_F32};
let j = _mm256_round_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(_mm256_mul_ps(
x,
_mm256_set1_ps(std::f32::consts::FRAC_2_PI),
));
let y = _mm256_fnmadd_ps(j, _mm256_set1_ps(PIO2_1_F32), x);
let y = _mm256_fnmadd_ps(j, _mm256_set1_ps(PIO2_2_F32), y);
let y = _mm256_fnmadd_ps(j, _mm256_set1_ps(PIO2_3_F32), y);
(_mm256_cvtps_epi32(j), y)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn sin_kernel_f32(y: __m256) -> __m256 {
use trig_coefficients::{SIN0_F32, SIN1_F32, SIN2_F32};
let z = _mm256_mul_ps(y, y);
let mut p = _mm256_set1_ps(SIN0_F32);
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(SIN1_F32));
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(SIN2_F32));
_mm256_fmadd_ps(_mm256_mul_ps(z, y), p, y)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn cos_kernel_f32(y: __m256) -> __m256 {
use trig_coefficients::{COS0_F32, COS1_F32, COS2_F32};
let one = _mm256_set1_ps(1.0);
let z = _mm256_mul_ps(y, y);
let mut p = _mm256_set1_ps(COS0_F32);
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(COS1_F32));
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(COS2_F32));
let r = _mm256_mul_ps(_mm256_mul_ps(z, z), p);
let hz = _mm256_mul_ps(_mm256_set1_ps(0.5), z);
let w = _mm256_sub_ps(one, hz);
let correction = _mm256_sub_ps(_mm256_sub_ps(one, w), hz);
_mm256_add_ps(w, _mm256_add_ps(correction, r))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn sin_quadrant_f32(x: __m256, offset: i32) -> __m256 {
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 = _mm256_and_si256(
_mm256_add_epi32(j, _mm256_set1_epi32(offset)),
_mm256_set1_epi32(3),
);
let use_cos_mask = _mm256_castsi256_ps(_mm256_cmpeq_epi32(
_mm256_and_si256(j_mod_4, _mm256_set1_epi32(1)),
_mm256_set1_epi32(1),
));
let negate_mask = _mm256_castsi256_ps(_mm256_cmpeq_epi32(
_mm256_and_si256(j_mod_4, _mm256_set1_epi32(2)),
_mm256_set1_epi32(2),
));
let result = _mm256_blendv_ps(sin_y, cos_y, use_cos_mask);
let negated = _mm256_xor_ps(result, _mm256_set1_ps(-0.0));
_mm256_blendv_ps(result, negated, negate_mask)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn sin_f32(x: __m256) -> __m256 {
let r = sin_quadrant_f32(x, 0);
let is_zero = _mm256_cmp_ps::<_CMP_EQ_OQ>(x, _mm256_setzero_ps());
_mm256_blendv_ps(r, x, is_zero)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn trig_reduce_f64(x: __m256d) -> (__m256d, __m256d) {
use trig_coefficients::{PIO2_1_F64, PIO2_2_F64, PIO2_3_F64, PIO2_3T_F64};
let j = _mm256_round_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(_mm256_mul_pd(
x,
_mm256_set1_pd(std::f64::consts::FRAC_2_PI),
));
let y = _mm256_fnmadd_pd(j, _mm256_set1_pd(PIO2_1_F64), x);
let y = _mm256_fnmadd_pd(j, _mm256_set1_pd(PIO2_2_F64), y);
let y = _mm256_fnmadd_pd(j, _mm256_set1_pd(PIO2_3_F64), y);
let y = _mm256_fnmadd_pd(j, _mm256_set1_pd(PIO2_3T_F64), y);
(j, y)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn sin_kernel_f64(y: __m256d) -> __m256d {
use trig_coefficients::{SIN1_F64, SIN2_F64, SIN3_F64, SIN4_F64, SIN5_F64, SIN6_F64};
let z = _mm256_mul_pd(y, y);
let mut p = _mm256_set1_pd(SIN6_F64);
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(SIN5_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(SIN4_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(SIN3_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(SIN2_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(SIN1_F64));
_mm256_fmadd_pd(_mm256_mul_pd(z, y), p, y)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn cos_kernel_f64(y: __m256d) -> __m256d {
use trig_coefficients::{COS1_F64, COS2_F64, COS3_F64, COS4_F64, COS5_F64, COS6_F64};
let one = _mm256_set1_pd(1.0);
let z = _mm256_mul_pd(y, y);
let mut p = _mm256_set1_pd(COS6_F64);
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(COS5_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(COS4_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(COS3_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(COS2_F64));
p = _mm256_fmadd_pd(p, z, _mm256_set1_pd(COS1_F64));
let r = _mm256_mul_pd(_mm256_mul_pd(z, z), p);
let hz = _mm256_mul_pd(_mm256_set1_pd(0.5), z);
let w = _mm256_sub_pd(one, hz);
let correction = _mm256_sub_pd(_mm256_sub_pd(one, w), hz);
_mm256_add_pd(w, _mm256_add_pd(correction, r))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn sin_quadrant_f64(x: __m256d, offset: i32) -> __m256d {
let (j, y) = trig_reduce_f64(x);
let sin_y = sin_kernel_f64(y);
let cos_y = cos_kernel_f64(y);
let mut j_arr = [0.0f64; 4];
let mut sin_arr = [0.0f64; 4];
let mut cos_arr = [0.0f64; 4];
_mm256_storeu_pd(j_arr.as_mut_ptr(), j);
_mm256_storeu_pd(sin_arr.as_mut_ptr(), sin_y);
_mm256_storeu_pd(cos_arr.as_mut_ptr(), cos_y);
let mut result = [0.0f64; 4];
for i in 0..4 {
let quadrant = (j_arr[i] as i32).wrapping_add(offset) & 3;
result[i] = match quadrant {
0 => sin_arr[i],
1 => cos_arr[i],
2 => -sin_arr[i],
_ => -cos_arr[i],
};
}
_mm256_loadu_pd(result.as_ptr())
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn sin_f64(x: __m256d) -> __m256d {
let r = sin_quadrant_f64(x, 0);
let is_zero = _mm256_cmp_pd::<_CMP_EQ_OQ>(x, _mm256_setzero_pd());
_mm256_blendv_pd(r, x, is_zero)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn cos_f32(x: __m256) -> __m256 {
sin_quadrant_f32(x, 1)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn cos_f64(x: __m256d) -> __m256d {
sin_quadrant_f64(x, 1)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn tan_f32(x: __m256) -> __m256 {
let (j, y) = trig_reduce_f32(x);
let sin_y = sin_kernel_f32(y);
let cos_y = cos_kernel_f32(y);
let odd_mask = _mm256_castsi256_ps(_mm256_cmpeq_epi32(
_mm256_and_si256(j, _mm256_set1_epi32(1)),
_mm256_set1_epi32(1),
));
let neg_cos = _mm256_xor_ps(cos_y, _mm256_set1_ps(-0.0));
let num = _mm256_blendv_ps(sin_y, neg_cos, odd_mask);
let den = _mm256_blendv_ps(cos_y, sin_y, odd_mask);
let r = _mm256_div_ps(num, den);
let is_zero = _mm256_cmp_ps::<_CMP_EQ_OQ>(x, _mm256_setzero_ps());
_mm256_blendv_ps(r, x, is_zero)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn tan_f64(x: __m256d) -> __m256d {
let (j, y) = trig_reduce_f64(x);
let sin_y = sin_kernel_f64(y);
let cos_y = cos_kernel_f64(y);
let mut j_arr = [0.0f64; 4];
let mut sin_arr = [0.0f64; 4];
let mut cos_arr = [0.0f64; 4];
_mm256_storeu_pd(j_arr.as_mut_ptr(), j);
_mm256_storeu_pd(sin_arr.as_mut_ptr(), sin_y);
_mm256_storeu_pd(cos_arr.as_mut_ptr(), cos_y);
let mut num = [0.0f64; 4];
let mut den = [0.0f64; 4];
for i in 0..4 {
if (j_arr[i] as i32) & 1 == 1 {
num[i] = -cos_arr[i];
den[i] = sin_arr[i];
} else {
num[i] = sin_arr[i];
den[i] = cos_arr[i];
}
}
let r = _mm256_div_pd(_mm256_loadu_pd(num.as_ptr()), _mm256_loadu_pd(den.as_ptr()));
let is_zero = _mm256_cmp_pd::<_CMP_EQ_OQ>(x, _mm256_setzero_pd());
_mm256_blendv_pd(r, x, is_zero)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn atan_f32(x: __m256) -> __m256 {
use atan_coefficients::*;
let one = _mm256_set1_ps(1.0);
let sign_mask = _mm256_set1_ps(-0.0);
let sign = _mm256_and_ps(x, sign_mask);
let ax = _mm256_andnot_ps(sign_mask, x);
let mut t = _mm256_div_ps(_mm256_set1_ps(-1.0), ax);
let mut hi = _mm256_set1_ps(ATAN_HI1_F32);
let mut lo = _mm256_set1_ps(ATAN_LO1_F32);
let in1 = _mm256_cmp_ps::<_CMP_LT_OQ>(ax, _mm256_set1_ps(BREAK1_F32));
let t_mid = _mm256_div_ps(_mm256_sub_ps(ax, one), _mm256_add_ps(ax, one));
t = _mm256_blendv_ps(t, t_mid, in1);
hi = _mm256_blendv_ps(hi, _mm256_set1_ps(ATAN_HI0_F32), in1);
lo = _mm256_blendv_ps(lo, _mm256_set1_ps(ATAN_LO0_F32), in1);
let zero = _mm256_setzero_ps();
let in0 = _mm256_cmp_ps::<_CMP_LT_OQ>(ax, _mm256_set1_ps(BREAK0_F32));
t = _mm256_blendv_ps(t, ax, in0);
hi = _mm256_blendv_ps(hi, zero, in0);
lo = _mm256_blendv_ps(lo, zero, in0);
let z = _mm256_mul_ps(t, t);
let mut p = _mm256_set1_ps(AT0_F32);
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(AT1_F32));
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(AT2_F32));
p = _mm256_fmadd_ps(p, z, _mm256_set1_ps(AT3_F32));
let poly = _mm256_fmadd_ps(_mm256_mul_ps(z, t), p, t);
let result = _mm256_add_ps(hi, _mm256_add_ps(lo, poly));
_mm256_or_ps(result, sign)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn atan_f64(x: __m256d) -> __m256d {
use atan_coefficients::*;
let one = _mm256_set1_pd(1.0);
let sign_mask = _mm256_set1_pd(-0.0);
let sign = _mm256_and_pd(x, sign_mask);
let ax = _mm256_andnot_pd(sign_mask, x);
let mut c = _mm256_set1_pd(1.5);
let mut hi = _mm256_set1_pd(ATAN_HI2_F64);
let mut lo = _mm256_set1_pd(ATAN_LO2_F64);
let in2 = _mm256_cmp_pd::<_CMP_LT_OQ>(ax, _mm256_set1_pd(BREAK2_F64));
c = _mm256_blendv_pd(c, one, in2);
hi = _mm256_blendv_pd(hi, _mm256_set1_pd(ATAN_HI1_F64), in2);
lo = _mm256_blendv_pd(lo, _mm256_set1_pd(ATAN_LO1_F64), in2);
let in1 = _mm256_cmp_pd::<_CMP_LT_OQ>(ax, _mm256_set1_pd(BREAK1_F64));
c = _mm256_blendv_pd(c, _mm256_set1_pd(0.5), in1);
hi = _mm256_blendv_pd(hi, _mm256_set1_pd(ATAN_HI0_F64), in1);
lo = _mm256_blendv_pd(lo, _mm256_set1_pd(ATAN_LO0_F64), in1);
let zero = _mm256_setzero_pd();
let in0 = _mm256_cmp_pd::<_CMP_LT_OQ>(ax, _mm256_set1_pd(BREAK0_F64));
c = _mm256_blendv_pd(c, zero, in0);
hi = _mm256_blendv_pd(hi, zero, in0);
lo = _mm256_blendv_pd(lo, zero, in0);
let big = _mm256_cmp_pd::<_CMP_GE_OQ>(ax, _mm256_set1_pd(BREAK3_F64));
hi = _mm256_blendv_pd(hi, _mm256_set1_pd(ATAN_HI3_F64), big);
lo = _mm256_blendv_pd(lo, _mm256_set1_pd(ATAN_LO3_F64), big);
let num = _mm256_blendv_pd(_mm256_sub_pd(ax, c), _mm256_set1_pd(-1.0), big);
let den = _mm256_blendv_pd(_mm256_fmadd_pd(c, ax, one), ax, big);
let t = _mm256_div_pd(num, den);
let z = _mm256_mul_pd(t, t);
let w = _mm256_mul_pd(z, z);
let mut s1 = _mm256_set1_pd(AT10_F64);
s1 = _mm256_fmadd_pd(s1, w, _mm256_set1_pd(AT8_F64));
s1 = _mm256_fmadd_pd(s1, w, _mm256_set1_pd(AT6_F64));
s1 = _mm256_fmadd_pd(s1, w, _mm256_set1_pd(AT4_F64));
s1 = _mm256_fmadd_pd(s1, w, _mm256_set1_pd(AT2_F64));
s1 = _mm256_fmadd_pd(s1, w, _mm256_set1_pd(AT0_F64));
s1 = _mm256_mul_pd(s1, z);
let mut s2 = _mm256_set1_pd(AT9_F64);
s2 = _mm256_fmadd_pd(s2, w, _mm256_set1_pd(AT7_F64));
s2 = _mm256_fmadd_pd(s2, w, _mm256_set1_pd(AT5_F64));
s2 = _mm256_fmadd_pd(s2, w, _mm256_set1_pd(AT3_F64));
s2 = _mm256_fmadd_pd(s2, w, _mm256_set1_pd(AT1_F64));
s2 = _mm256_mul_pd(s2, w);
let poly = _mm256_mul_pd(t, _mm256_add_pd(s1, s2));
let result = _mm256_sub_pd(hi, _mm256_sub_pd(_mm256_sub_pd(poly, lo), t));
_mm256_or_pd(result, sign)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn hmax_f32(v: __m256) -> f32 {
let high = _mm256_extractf128_ps(v, 1);
let low = _mm256_castps256_ps128(v);
let max128 = _mm_max_ps(low, high);
let shuf = _mm_movehdup_ps(max128);
let max64 = _mm_max_ps(max128, shuf);
let shuf2 = _mm_movehl_ps(max64, max64);
let max32 = _mm_max_ss(max64, shuf2);
_mm_cvtss_f32(max32)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn hmax_f64(v: __m256d) -> f64 {
let high = _mm256_extractf128_pd(v, 1);
let low = _mm256_castpd256_pd128(v);
let max128 = _mm_max_pd(low, high);
let shuf = _mm_unpackhi_pd(max128, max128);
let max64 = _mm_max_sd(max128, shuf);
_mm_cvtsd_f64(max64)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn hsum_f32(v: __m256) -> f32 {
let high = _mm256_extractf128_ps(v, 1);
let low = _mm256_castps256_ps128(v);
let sum128 = _mm_add_ps(low, high);
let shuf = _mm_movehdup_ps(sum128);
let sum64 = _mm_add_ps(sum128, shuf);
let shuf2 = _mm_movehl_ps(sum64, sum64);
let sum32 = _mm_add_ss(sum64, shuf2);
_mm_cvtss_f32(sum32)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn hsum_f64(v: __m256d) -> f64 {
let high = _mm256_extractf128_pd(v, 1);
let low = _mm256_castpd256_pd128(v);
let sum128 = _mm_add_pd(low, high);
let shuf = _mm_unpackhi_pd(sum128, sum128);
let sum64 = _mm_add_sd(sum128, shuf);
_mm_cvtsd_f64(sum64)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn rsqrt_f32(x: __m256) -> __m256 {
let approx = _mm256_rsqrt_ps(x);
let half = _mm256_set1_ps(0.5);
let three = _mm256_set1_ps(3.0);
let x_approx2 = _mm256_mul_ps(x, _mm256_mul_ps(approx, approx));
let factor = _mm256_sub_ps(three, x_approx2);
_mm256_mul_ps(half, _mm256_mul_ps(approx, factor))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn rsqrt_f64(x: __m256d) -> __m256d {
let sqrt_x = _mm256_sqrt_pd(x);
_mm256_div_pd(_mm256_set1_pd(1.0), sqrt_x)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn exp2_f32(x: __m256) -> __m256 {
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 = _mm256_max_ps(_mm256_set1_ps(MIN_F32), x);
let xc = _mm256_min_ps(_mm256_set1_ps(MAX_F32), xc);
let n = _mm256_round_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(xc);
let r = _mm256_sub_ps(xc, n);
let rl = _mm256_mul_ps(r, _mm256_set1_ps(std::f32::consts::LN_2));
let mut poly = _mm256_set1_ps(C7_F32);
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C6_F32));
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C5_F32));
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C4_F32));
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C3_F32));
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C2_F32));
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C1_F32));
poly = _mm256_fmadd_ps(poly, rl, _mm256_set1_ps(C0_F32));
let n_i32 = _mm256_cvtps_epi32(n);
let n_hi = _mm256_srai_epi32::<1>(n_i32);
let n_lo = _mm256_sub_epi32(n_i32, n_hi);
let bias = _mm256_set1_epi32(127);
let p_hi = _mm256_castsi256_ps(_mm256_slli_epi32::<23>(_mm256_add_epi32(n_hi, bias)));
let p_lo = _mm256_castsi256_ps(_mm256_slli_epi32::<23>(_mm256_add_epi32(n_lo, bias)));
_mm256_mul_ps(_mm256_mul_ps(poly, p_hi), p_lo)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn exp2_f64(x: __m256d) -> __m256d {
use exp2_coefficients::*;
let xc = _mm256_max_pd(x, _mm256_set1_pd(MIN_F64));
let xc = _mm256_min_pd(xc, _mm256_set1_pd(MAX_F64));
let n = _mm256_round_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(xc);
let r = _mm256_sub_pd(xc, n);
let mut poly = _mm256_set1_pd(C13_F64);
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C12_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C11_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C10_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C9_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C8_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C7_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C6_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C5_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C4_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C3_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C2_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C1_F64));
poly = _mm256_fmadd_pd(poly, r, _mm256_set1_pd(C0_F64));
let mut scaled = [0.0f64; 4];
let mut n_arr = [0.0f64; 4];
let mut poly_arr = [0.0f64; 4];
_mm256_storeu_pd(n_arr.as_mut_ptr(), n);
_mm256_storeu_pd(poly_arr.as_mut_ptr(), poly);
for i in 0..4 {
let n_i = n_arr[i] as i64;
let hi = n_i / 2;
let lo = n_i - hi;
let p_hi = f64::from_bits(((hi + 1023) as u64) << 52);
let p_lo = f64::from_bits(((lo + 1023) as u64) << 52);
scaled[i] = (poly_arr[i] * p_hi) * p_lo;
}
let out = _mm256_loadu_pd(scaled.as_ptr());
_mm256_blendv_pd(x, out, _mm256_cmp_pd::<_CMP_ORD_Q>(x, x))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn expm1_f32(x: __m256) -> __m256 {
use exp_coefficients::*;
let xc = _mm256_max_ps(_mm256_set1_ps(EXPM1_MIN_F32), x);
let xc = _mm256_min_ps(_mm256_set1_ps(EXPM1_MAX_F32), xc);
let y = _mm256_mul_ps(xc, _mm256_set1_ps(std::f32::consts::LOG2_E));
let n = _mm256_round_ps::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm256_fnmadd_ps(n, _mm256_set1_ps(LN2_HI_F32), xc);
let r = _mm256_fnmadd_ps(n, _mm256_set1_ps(LN2_LO_F32), r);
let mut q = _mm256_set1_ps(C7_F32);
q = _mm256_fmadd_ps(q, r, _mm256_set1_ps(C6_F32));
q = _mm256_fmadd_ps(q, r, _mm256_set1_ps(C5_F32));
q = _mm256_fmadd_ps(q, r, _mm256_set1_ps(C4_F32));
q = _mm256_fmadd_ps(q, r, _mm256_set1_ps(C3_F32));
q = _mm256_fmadd_ps(q, r, _mm256_set1_ps(C2_F32));
let e = _mm256_fmadd_ps(_mm256_mul_ps(r, r), q, r);
let n_i32 = _mm256_cvtps_epi32(n);
let bias = _mm256_set1_epi32(127 - 1);
let t = _mm256_castsi256_ps(_mm256_slli_epi32::<23>(_mm256_add_epi32(n_i32, bias)));
let out = _mm256_mul_ps(
_mm256_set1_ps(2.0),
_mm256_fmadd_ps(t, e, _mm256_sub_ps(t, _mm256_set1_ps(0.5))),
);
let out = _mm256_blendv_ps(out, e, _mm256_cmp_ps::<_CMP_EQ_OQ>(n, _mm256_setzero_ps()));
_mm256_blendv_ps(out, x, _mm256_cmp_ps::<_CMP_EQ_OQ>(x, _mm256_setzero_ps()))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn expm1_f64(x: __m256d) -> __m256d {
use exp_coefficients::*;
let xc = _mm256_max_pd(x, _mm256_set1_pd(EXPM1_MIN_F64));
let xc = _mm256_min_pd(xc, _mm256_set1_pd(EXPM1_MAX_F64));
let y = _mm256_mul_pd(xc, _mm256_set1_pd(std::f64::consts::LOG2_E));
let n = _mm256_round_pd::<{ _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC }>(y);
let r = _mm256_fnmadd_pd(n, _mm256_set1_pd(LN2_HI_F64), xc);
let r = _mm256_fnmadd_pd(n, _mm256_set1_pd(LN2_LO_F64), r);
let mut q = _mm256_set1_pd(C13_F64);
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C12_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C11_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C10_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C9_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C8_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C7_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C6_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C5_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C4_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C3_F64));
q = _mm256_fmadd_pd(q, r, _mm256_set1_pd(C2_F64));
let e = _mm256_fmadd_pd(_mm256_mul_pd(r, r), q, r);
let mut scaled = [0.0f64; 4];
let mut n_arr = [0.0f64; 4];
let mut e_arr = [0.0f64; 4];
_mm256_storeu_pd(n_arr.as_mut_ptr(), n);
_mm256_storeu_pd(e_arr.as_mut_ptr(), e);
for i in 0..4 {
let m = n_arr[i] as i64 - 1;
let t = f64::from_bits(((m + 1023) as u64) << 52);
scaled[i] = 2.0 * t.mul_add(e_arr[i], t - 0.5);
}
let out = _mm256_loadu_pd(scaled.as_ptr());
let out = _mm256_blendv_pd(out, e, _mm256_cmp_pd::<_CMP_EQ_OQ>(n, _mm256_setzero_pd()));
let out = _mm256_blendv_pd(out, x, _mm256_cmp_pd::<_CMP_EQ_OQ>(x, _mm256_setzero_pd()));
_mm256_blendv_pd(x, out, _mm256_cmp_pd::<_CMP_ORD_Q>(x, x))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log2_f32(x: __m256) -> __m256 {
let (n, logm) = log_reduce_f32(x);
let r = _mm256_fmadd_ps(logm, _mm256_set1_ps(std::f32::consts::LOG2_E), n);
log_special_f32(x, r)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log2_f64(x: __m256d) -> __m256d {
let (n, logm) = log_reduce_f64(x);
let r = _mm256_fmadd_pd(logm, _mm256_set1_pd(std::f64::consts::LOG2_E), n);
log_special_f64(x, r)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log10_f32(x: __m256) -> __m256 {
let (n, logm) = log_reduce_f32(x);
let scaled = _mm256_mul_ps(logm, _mm256_set1_ps(std::f32::consts::LOG10_E));
let r = _mm256_fmadd_ps(n, _mm256_set1_ps(std::f32::consts::LOG10_2), scaled);
log_special_f32(x, r)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log10_f64(x: __m256d) -> __m256d {
let (n, logm) = log_reduce_f64(x);
let scaled = _mm256_mul_pd(logm, _mm256_set1_pd(std::f64::consts::LOG10_E));
let r = _mm256_fmadd_pd(n, _mm256_set1_pd(std::f64::consts::LOG10_2), scaled);
log_special_f64(x, r)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log1p_f32(x: __m256) -> __m256 {
let one = _mm256_set1_ps(1.0);
let u = _mm256_add_ps(one, x);
let c_small = _mm256_sub_ps(x, _mm256_sub_ps(u, one));
let c_large = _mm256_sub_ps(one, _mm256_sub_ps(u, x));
let abs_x = _mm256_andnot_ps(_mm256_set1_ps(-0.0), x);
let x_leads = _mm256_cmp_ps::<_CMP_LT_OQ>(abs_x, one);
let c = _mm256_blendv_ps(c_large, c_small, x_leads);
let r = _mm256_add_ps(log_f32(u), _mm256_div_ps(c, u));
let is_unit = _mm256_cmp_ps::<_CMP_EQ_OQ>(u, one);
let out = _mm256_blendv_ps(r, x, is_unit);
let is_neg_one = _mm256_cmp_ps::<_CMP_EQ_OQ>(x, _mm256_set1_ps(-1.0));
let is_inf = _mm256_cmp_ps::<_CMP_EQ_OQ>(x, _mm256_set1_ps(f32::INFINITY));
let out = _mm256_blendv_ps(out, _mm256_set1_ps(f32::NEG_INFINITY), is_neg_one);
_mm256_blendv_ps(out, _mm256_set1_ps(f32::INFINITY), is_inf)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn log1p_f64(x: __m256d) -> __m256d {
let one = _mm256_set1_pd(1.0);
let u = _mm256_add_pd(one, x);
let c_small = _mm256_sub_pd(x, _mm256_sub_pd(u, one));
let c_large = _mm256_sub_pd(one, _mm256_sub_pd(u, x));
let abs_x = _mm256_andnot_pd(_mm256_set1_pd(-0.0), x);
let x_leads = _mm256_cmp_pd::<_CMP_LT_OQ>(abs_x, one);
let c = _mm256_blendv_pd(c_large, c_small, x_leads);
let r = _mm256_add_pd(log_f64(u), _mm256_div_pd(c, u));
let is_unit = _mm256_cmp_pd::<_CMP_EQ_OQ>(u, one);
let out = _mm256_blendv_pd(r, x, is_unit);
let is_neg_one = _mm256_cmp_pd::<_CMP_EQ_OQ>(x, _mm256_set1_pd(-1.0));
let is_inf = _mm256_cmp_pd::<_CMP_EQ_OQ>(x, _mm256_set1_pd(f64::INFINITY));
let out = _mm256_blendv_pd(out, _mm256_set1_pd(f64::NEG_INFINITY), is_neg_one);
_mm256_blendv_pd(out, _mm256_set1_pd(f64::INFINITY), is_inf)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn sinh_f32(x: __m256) -> __m256 {
let sign_mask = _mm256_set1_ps(-0.0);
let one = _mm256_set1_ps(1.0);
let half = _mm256_set1_ps(0.5);
let a = _mm256_andnot_ps(sign_mask, x);
let u = expm1_f32(a);
let d = _mm256_div_ps(u, _mm256_add_ps(one, u));
let is_inf = _mm256_cmp_ps::<_CMP_EQ_OQ>(u, _mm256_set1_ps(f32::INFINITY));
let d = _mm256_blendv_ps(d, one, is_inf);
let s = _mm256_mul_ps(half, _mm256_add_ps(u, d));
let t = exp_f32(_mm256_mul_ps(half, a));
let far = _mm256_mul_ps(_mm256_mul_ps(half, t), t);
let big = _mm256_cmp_ps::<_CMP_GT_OQ>(a, _mm256_set1_ps(hyperbolic_breakpoints::BIG_F32));
let s = _mm256_blendv_ps(s, far, big);
_mm256_or_ps(s, _mm256_and_ps(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn sinh_f64(x: __m256d) -> __m256d {
let sign_mask = _mm256_set1_pd(-0.0);
let one = _mm256_set1_pd(1.0);
let a = _mm256_andnot_pd(sign_mask, x);
let u = expm1_f64(a);
let d = _mm256_div_pd(u, _mm256_add_pd(one, u));
let is_inf = _mm256_cmp_pd::<_CMP_EQ_OQ>(u, _mm256_set1_pd(f64::INFINITY));
let d = _mm256_blendv_pd(d, one, is_inf);
let s = _mm256_mul_pd(_mm256_set1_pd(0.5), _mm256_add_pd(u, d));
let half = _mm256_set1_pd(0.5);
let t = exp_f64(_mm256_mul_pd(half, a));
let far = _mm256_mul_pd(_mm256_mul_pd(half, t), t);
let big = _mm256_cmp_pd::<_CMP_GT_OQ>(a, _mm256_set1_pd(hyperbolic_breakpoints::BIG_F64));
let s = _mm256_blendv_pd(s, far, big);
_mm256_or_pd(s, _mm256_and_pd(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn cosh_f32(x: __m256) -> __m256 {
let half = _mm256_set1_ps(0.5);
let a = _mm256_andnot_ps(_mm256_set1_ps(-0.0), x);
let exp_x = exp_f32(x);
let exp_neg_x = exp_f32(_mm256_sub_ps(_mm256_setzero_ps(), x));
let near = _mm256_mul_ps(half, _mm256_add_ps(exp_x, exp_neg_x));
let t = exp_f32(_mm256_mul_ps(half, a));
let far = _mm256_mul_ps(_mm256_mul_ps(half, t), t);
let big = _mm256_cmp_ps::<_CMP_GT_OQ>(a, _mm256_set1_ps(hyperbolic_breakpoints::BIG_F32));
_mm256_blendv_ps(near, far, big)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn cosh_f64(x: __m256d) -> __m256d {
let half = _mm256_set1_pd(0.5);
let a = _mm256_andnot_pd(_mm256_set1_pd(-0.0), x);
let exp_x = exp_f64(x);
let exp_neg_x = exp_f64(_mm256_sub_pd(_mm256_setzero_pd(), x));
let near = _mm256_mul_pd(half, _mm256_add_pd(exp_x, exp_neg_x));
let t = exp_f64(_mm256_mul_pd(half, a));
let far = _mm256_mul_pd(_mm256_mul_pd(half, t), t);
let big = _mm256_cmp_pd::<_CMP_GT_OQ>(a, _mm256_set1_pd(hyperbolic_breakpoints::BIG_F64));
_mm256_blendv_pd(near, far, big)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn asinh_f32(x: __m256) -> __m256 {
use inv_hyperbolic_breakpoints::{BIG_F32, NEAR_F32};
let sign_mask = _mm256_set1_ps(-0.0);
let one = _mm256_set1_ps(1.0);
let a = _mm256_andnot_ps(sign_mask, x);
let t = _mm256_mul_ps(a, a);
let root = _mm256_sqrt_ps(_mm256_add_ps(t, one));
let near = log1p_f32(_mm256_add_ps(a, _mm256_div_ps(t, _mm256_add_ps(one, root))));
let mid = log_f32(_mm256_fmadd_ps(
_mm256_set1_ps(2.0),
a,
_mm256_div_ps(one, _mm256_add_ps(root, a)),
));
let far = _mm256_add_ps(log_f32(a), _mm256_set1_ps(std::f32::consts::LN_2));
let r = _mm256_blendv_ps(
near,
mid,
_mm256_cmp_ps::<_CMP_GT_OQ>(a, _mm256_set1_ps(NEAR_F32)),
);
let r = _mm256_blendv_ps(
r,
far,
_mm256_cmp_ps::<_CMP_GT_OQ>(a, _mm256_set1_ps(BIG_F32)),
);
_mm256_or_ps(r, _mm256_and_ps(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn asinh_f64(x: __m256d) -> __m256d {
use inv_hyperbolic_breakpoints::{BIG_F64, NEAR_F64};
let sign_mask = _mm256_set1_pd(-0.0);
let one = _mm256_set1_pd(1.0);
let a = _mm256_andnot_pd(sign_mask, x);
let t = _mm256_mul_pd(a, a);
let root = _mm256_sqrt_pd(_mm256_add_pd(t, one));
let near = log1p_f64(_mm256_add_pd(a, _mm256_div_pd(t, _mm256_add_pd(one, root))));
let mid = log_f64(_mm256_fmadd_pd(
_mm256_set1_pd(2.0),
a,
_mm256_div_pd(one, _mm256_add_pd(root, a)),
));
let far = _mm256_add_pd(log_f64(a), _mm256_set1_pd(std::f64::consts::LN_2));
let r = _mm256_blendv_pd(
near,
mid,
_mm256_cmp_pd::<_CMP_GT_OQ>(a, _mm256_set1_pd(NEAR_F64)),
);
let r = _mm256_blendv_pd(
r,
far,
_mm256_cmp_pd::<_CMP_GT_OQ>(a, _mm256_set1_pd(BIG_F64)),
);
_mm256_or_pd(r, _mm256_and_pd(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn acosh_f32(x: __m256) -> __m256 {
use inv_hyperbolic_breakpoints::{BIG_F32, NEAR_F32};
let one = _mm256_set1_ps(1.0);
let two = _mm256_set1_ps(2.0);
let t = _mm256_sub_ps(x, one);
let disc = _mm256_sqrt_ps(_mm256_fmadd_ps(t, t, _mm256_mul_ps(two, t)));
let near = log1p_f32(_mm256_add_ps(t, disc));
let root = _mm256_sqrt_ps(_mm256_fmsub_ps(x, x, one));
let mid = log_f32(_mm256_fmsub_ps(
two,
x,
_mm256_div_ps(one, _mm256_add_ps(x, root)),
));
let far = _mm256_add_ps(log_f32(x), _mm256_set1_ps(std::f32::consts::LN_2));
let r = _mm256_blendv_ps(
near,
mid,
_mm256_cmp_ps::<_CMP_GE_OQ>(x, _mm256_set1_ps(NEAR_F32)),
);
let r = _mm256_blendv_ps(
r,
far,
_mm256_cmp_ps::<_CMP_GT_OQ>(x, _mm256_set1_ps(BIG_F32)),
);
_mm256_blendv_ps(
r,
_mm256_set1_ps(f32::NAN),
_mm256_cmp_ps::<_CMP_LT_OQ>(x, one),
)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn acosh_f64(x: __m256d) -> __m256d {
use inv_hyperbolic_breakpoints::{BIG_F64, NEAR_F64};
let one = _mm256_set1_pd(1.0);
let two = _mm256_set1_pd(2.0);
let t = _mm256_sub_pd(x, one);
let disc = _mm256_sqrt_pd(_mm256_fmadd_pd(t, t, _mm256_mul_pd(two, t)));
let near = log1p_f64(_mm256_add_pd(t, disc));
let root = _mm256_sqrt_pd(_mm256_fmsub_pd(x, x, one));
let mid = log_f64(_mm256_fmsub_pd(
two,
x,
_mm256_div_pd(one, _mm256_add_pd(x, root)),
));
let far = _mm256_add_pd(log_f64(x), _mm256_set1_pd(std::f64::consts::LN_2));
let r = _mm256_blendv_pd(
near,
mid,
_mm256_cmp_pd::<_CMP_GE_OQ>(x, _mm256_set1_pd(NEAR_F64)),
);
let r = _mm256_blendv_pd(
r,
far,
_mm256_cmp_pd::<_CMP_GT_OQ>(x, _mm256_set1_pd(BIG_F64)),
);
_mm256_blendv_pd(
r,
_mm256_set1_pd(f64::NAN),
_mm256_cmp_pd::<_CMP_LT_OQ>(x, one),
)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn atanh_f32(x: __m256) -> __m256 {
use inv_hyperbolic_breakpoints::ATANH_SPLIT_F32;
let sign_mask = _mm256_set1_ps(-0.0);
let one = _mm256_set1_ps(1.0);
let a = _mm256_andnot_ps(sign_mask, x);
let t = _mm256_add_ps(a, a);
let den = _mm256_sub_ps(one, a);
let small = log1p_f32(_mm256_add_ps(t, _mm256_div_ps(_mm256_mul_ps(t, a), den)));
let large = log1p_f32(_mm256_div_ps(t, den));
let picked = _mm256_blendv_ps(
large,
small,
_mm256_cmp_ps::<_CMP_LT_OQ>(a, _mm256_set1_ps(ATANH_SPLIT_F32)),
);
let r = _mm256_mul_ps(_mm256_set1_ps(0.5), picked);
_mm256_or_ps(r, _mm256_and_ps(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn atanh_f64(x: __m256d) -> __m256d {
use inv_hyperbolic_breakpoints::ATANH_SPLIT_F64;
let sign_mask = _mm256_set1_pd(-0.0);
let one = _mm256_set1_pd(1.0);
let a = _mm256_andnot_pd(sign_mask, x);
let t = _mm256_add_pd(a, a);
let den = _mm256_sub_pd(one, a);
let small = log1p_f64(_mm256_add_pd(t, _mm256_div_pd(_mm256_mul_pd(t, a), den)));
let large = log1p_f64(_mm256_div_pd(t, den));
let picked = _mm256_blendv_pd(
large,
small,
_mm256_cmp_pd::<_CMP_LT_OQ>(a, _mm256_set1_pd(ATANH_SPLIT_F64)),
);
let r = _mm256_mul_pd(_mm256_set1_pd(0.5), picked);
_mm256_or_pd(r, _mm256_and_pd(x, sign_mask))
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn asin_r_f32(t: __m256) -> __m256 {
use asin_coefficients::*;
let mut p = _mm256_set1_ps(PS0_F32);
p = _mm256_fmadd_ps(p, t, _mm256_set1_ps(PS1_F32));
p = _mm256_fmadd_ps(p, t, _mm256_set1_ps(PS2_F32));
p = _mm256_fmadd_ps(p, t, _mm256_set1_ps(PS3_F32));
p = _mm256_fmadd_ps(p, t, _mm256_set1_ps(PS4_F32));
_mm256_mul_ps(t, p)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn asin_f32(x: __m256) -> __m256 {
use asin_coefficients::*;
let one = _mm256_set1_ps(1.0);
let half = _mm256_set1_ps(HALF_F32);
let sign_mask = _mm256_set1_ps(-0.0);
let sign = _mm256_and_ps(x, sign_mask);
let ax = _mm256_andnot_ps(sign_mask, x);
let small = _mm256_cmp_ps::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm256_mul_ps(_mm256_sub_ps(one, ax), half);
let t = _mm256_blendv_ps(t_refl, _mm256_mul_ps(ax, ax), small);
let s = _mm256_sqrt_ps(t);
let v = _mm256_blendv_ps(s, ax, small);
let w = _mm256_fmadd_ps(v, asin_r_f32(t), v);
let two_w = _mm256_add_ps(w, w);
let res_refl = _mm256_sub_ps(
_mm256_set1_ps(PIO2_HI_F32),
_mm256_sub_ps(two_w, _mm256_set1_ps(PIO2_LO_F32)),
);
let result = _mm256_blendv_ps(res_refl, w, small);
_mm256_or_ps(result, sign)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
unsafe fn asin_r_f64(t: __m256d) -> __m256d {
use asin_coefficients::*;
let mut p = _mm256_set1_pd(PS5_F64);
p = _mm256_fmadd_pd(p, t, _mm256_set1_pd(PS4_F64));
p = _mm256_fmadd_pd(p, t, _mm256_set1_pd(PS3_F64));
p = _mm256_fmadd_pd(p, t, _mm256_set1_pd(PS2_F64));
p = _mm256_fmadd_pd(p, t, _mm256_set1_pd(PS1_F64));
p = _mm256_fmadd_pd(p, t, _mm256_set1_pd(PS0_F64));
p = _mm256_mul_pd(p, t);
let mut q = _mm256_set1_pd(QS4_F64);
q = _mm256_fmadd_pd(q, t, _mm256_set1_pd(QS3_F64));
q = _mm256_fmadd_pd(q, t, _mm256_set1_pd(QS2_F64));
q = _mm256_fmadd_pd(q, t, _mm256_set1_pd(QS1_F64));
q = _mm256_fmadd_pd(q, t, _mm256_set1_pd(1.0));
_mm256_div_pd(p, q)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn asin_f64(x: __m256d) -> __m256d {
use asin_coefficients::*;
let one = _mm256_set1_pd(1.0);
let half = _mm256_set1_pd(HALF_F64);
let sign_mask = _mm256_set1_pd(-0.0);
let sign = _mm256_and_pd(x, sign_mask);
let ax = _mm256_andnot_pd(sign_mask, x);
let small = _mm256_cmp_pd::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm256_mul_pd(_mm256_sub_pd(one, ax), half);
let t = _mm256_blendv_pd(t_refl, _mm256_mul_pd(ax, ax), small);
let r = asin_r_f64(t);
let s = _mm256_sqrt_pd(t);
let res_small = _mm256_fmadd_pd(ax, r, ax);
let s_sr = _mm256_fmadd_pd(s, r, s);
let two_s = _mm256_add_pd(s_sr, s_sr);
let res_refl = _mm256_sub_pd(
_mm256_set1_pd(PIO2_HI_F64),
_mm256_sub_pd(two_s, _mm256_set1_pd(PIO2_LO_F64)),
);
let result = _mm256_blendv_pd(res_refl, res_small, small);
_mm256_or_pd(result, sign)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn acos_f32(x: __m256) -> __m256 {
use asin_coefficients::*;
let one = _mm256_set1_ps(1.0);
let half = _mm256_set1_ps(HALF_F32);
let pio2_lo = _mm256_set1_ps(PIO2_LO_F32);
let sign_mask = _mm256_set1_ps(-0.0);
let ax = _mm256_andnot_ps(sign_mask, x);
let small = _mm256_cmp_ps::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm256_mul_ps(_mm256_sub_ps(one, ax), half);
let t = _mm256_blendv_ps(t_refl, _mm256_mul_ps(x, x), small);
let q = asin_r_f32(t);
let s = _mm256_sqrt_ps(t);
let px = _mm256_mul_ps(x, q);
let res_small = _mm256_sub_ps(
_mm256_set1_ps(PIO2_HI_F32),
_mm256_add_ps(x, _mm256_sub_ps(px, pio2_lo)),
);
let ps = _mm256_mul_ps(s, q);
let s_sr = _mm256_add_ps(s, ps);
let res_pos = _mm256_add_ps(s_sr, s_sr);
let w = _mm256_add_ps(s, _mm256_sub_ps(ps, pio2_lo));
let res_neg = _mm256_sub_ps(_mm256_set1_ps(PI_HI_F32), _mm256_add_ps(w, w));
let positive = _mm256_cmp_ps::<_CMP_GT_OQ>(x, _mm256_setzero_ps());
let res_refl = _mm256_blendv_ps(res_neg, res_pos, positive);
_mm256_blendv_ps(res_refl, res_small, small)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn acos_f64(x: __m256d) -> __m256d {
use asin_coefficients::*;
let one = _mm256_set1_pd(1.0);
let half = _mm256_set1_pd(HALF_F64);
let pio2_lo = _mm256_set1_pd(PIO2_LO_F64);
let sign_mask = _mm256_set1_pd(-0.0);
let ax = _mm256_andnot_pd(sign_mask, x);
let small = _mm256_cmp_pd::<_CMP_LT_OQ>(ax, half);
let t_refl = _mm256_mul_pd(_mm256_sub_pd(one, ax), half);
let t = _mm256_blendv_pd(t_refl, _mm256_mul_pd(ax, ax), small);
let r = asin_r_f64(t);
let s = _mm256_sqrt_pd(t);
let res_small = _mm256_sub_pd(
_mm256_set1_pd(PIO2_HI_F64),
_mm256_add_pd(x, _mm256_sub_pd(_mm256_mul_pd(x, r), pio2_lo)),
);
let s_sr = _mm256_fmadd_pd(s, r, s);
let res_pos = _mm256_add_pd(s_sr, s_sr);
let w = _mm256_add_pd(s, _mm256_sub_pd(_mm256_mul_pd(s, r), pio2_lo));
let res_neg = _mm256_sub_pd(_mm256_set1_pd(PI_HI_F64), _mm256_add_pd(w, w));
let positive = _mm256_cmp_pd::<_CMP_GT_OQ>(x, _mm256_setzero_pd());
let res_refl = _mm256_blendv_pd(res_neg, res_pos, positive);
_mm256_blendv_pd(res_refl, res_small, small)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn cbrt_f32(x: __m256) -> __m256 {
use cbrt_constants::*;
let sign_mask = _mm256_set1_ps(-0.0);
let sign = _mm256_and_ps(x, sign_mask);
let abs_x = _mm256_andnot_ps(sign_mask, x);
let one = _mm256_set1_ps(1.0);
let big = _mm256_cmp_ps::<_CMP_GE_OQ>(abs_x, _mm256_set1_ps(BIG_F32));
let small = _mm256_cmp_ps::<_CMP_LT_OQ>(abs_x, _mm256_set1_ps(SMALL_F32));
let s_in = _mm256_blendv_ps(one, _mm256_set1_ps(SCALE_UP_F32), small);
let s_in = _mm256_blendv_ps(s_in, _mm256_set1_ps(SCALE_DOWN_F32), big);
let s_out = _mm256_blendv_ps(one, _mm256_set1_ps(UNSCALE_DOWN_F32), small);
let s_out = _mm256_blendv_ps(s_out, _mm256_set1_ps(UNSCALE_UP_F32), big);
let a = _mm256_mul_ps(abs_x, s_in);
let a_bits = _mm256_cvtepi32_ps(_mm256_castps_si256(a));
let q = _mm256_fmadd_ps(a_bits, _mm256_set1_ps(1.0 / 3.0), _mm256_set1_ps(B1_F32));
let t = _mm256_castsi256_ps(_mm256_cvtps_epi32(q));
let r = _mm256_mul_ps(_mm256_mul_ps(t, t), t);
let num = _mm256_add_ps(_mm256_add_ps(a, a), r);
let den = _mm256_add_ps(_mm256_add_ps(a, r), r);
let t = _mm256_mul_ps(t, _mm256_div_ps(num, den));
let r = _mm256_mul_ps(_mm256_mul_ps(t, t), t);
let num = _mm256_add_ps(_mm256_add_ps(a, a), r);
let den = _mm256_add_ps(_mm256_add_ps(a, r), r);
let t = _mm256_mul_ps(t, _mm256_div_ps(num, den));
let out = _mm256_or_ps(_mm256_mul_ps(t, s_out), sign);
let ordinary = _mm256_and_ps(
_mm256_cmp_ps::<_CMP_GT_OQ>(abs_x, _mm256_setzero_ps()),
_mm256_cmp_ps::<_CMP_LT_OQ>(abs_x, _mm256_set1_ps(f32::INFINITY)),
);
_mm256_blendv_ps(x, out, ordinary)
}
#[target_feature(enable = "avx2", enable = "fma")]
#[inline]
pub unsafe fn cbrt_f64(x: __m256d) -> __m256d {
let sign_mask = _mm256_set1_pd(-0.0);
let sign = _mm256_and_pd(x, sign_mask);
let abs_x = _mm256_andnot_pd(sign_mask, x);
let one_third = _mm256_set1_pd(1.0 / 3.0);
let log_x = log_f64(abs_x);
let guess = exp_f64(_mm256_mul_pd(log_x, one_third));
let two = _mm256_set1_pd(2.0);
let three = _mm256_set1_pd(3.0);
let y = guess;
let y2 = _mm256_mul_pd(y, y);
let y_new = _mm256_div_pd(_mm256_fmadd_pd(two, y, _mm256_div_pd(abs_x, y2)), three);
let y2 = _mm256_mul_pd(y_new, y_new);
let result = _mm256_div_pd(_mm256_fmadd_pd(two, y_new, _mm256_div_pd(abs_x, y2)), three);
_mm256_or_pd(result, sign)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exp_f32_accuracy() {
if !is_x86_feature_detected!("avx2") || !is_x86_feature_detected!("fma") {
return;
}
let inputs: Vec<f32> = (-40..=40).map(|x| x as f32 * 0.1).collect();
let mut outputs = vec![0.0f32; inputs.len()];
unsafe {
for (i, &x) in inputs.iter().enumerate() {
let v = std::arch::x86_64::_mm256_set1_ps(x);
let result = exp_f32(v);
let mut arr = [0.0f32; 8];
std::arch::x86_64::_mm256_storeu_ps(arr.as_mut_ptr(), result);
outputs[i] = arr[0];
}
}
for (i, (&input, &output)) in inputs.iter().zip(outputs.iter()).enumerate() {
let expected = input.exp();
let rel_err = (output - expected).abs() / expected.abs().max(1e-10);
assert!(
rel_err < 1e-5,
"exp({}) = {} (expected {}), rel_err = {} at index {}",
input,
output,
expected,
rel_err,
i
);
}
}
#[test]
fn test_tanh_f32_accuracy() {
if !is_x86_feature_detected!("avx2") || !is_x86_feature_detected!("fma") {
return;
}
let inputs: Vec<f32> = (-30..=30).map(|x| x as f32 * 0.1).collect();
let mut outputs = vec![0.0f32; inputs.len()];
unsafe {
for (i, &x) in inputs.iter().enumerate() {
let v = std::arch::x86_64::_mm256_set1_ps(x);
let result = tanh_f32(v);
let mut arr = [0.0f32; 8];
std::arch::x86_64::_mm256_storeu_ps(arr.as_mut_ptr(), result);
outputs[i] = arr[0];
}
}
for (&input, &output) in inputs.iter().zip(outputs.iter()) {
let expected = input.tanh();
let abs_err = (output - expected).abs();
assert!(
abs_err < 1e-5,
"tanh({}) = {} (expected {}), abs_err = {}",
input,
output,
expected,
abs_err
);
}
}
}