use crate::dd::{quick_two_sum, DD};
use crate::dv::{self, DV};
use crate::dw;
use crate::od::{alg, OD};
use crate::ov::OV;
use crate::qd::QD;
use crate::qv::QV;
use crate::simd::Simd;
use crate::v8::{M8, V8};
use std::ops::Neg;
#[rustfmt::skip]
pub(crate) mod tables;
pub(crate) mod fast_reduce;
pub mod mutate {
pub const REDUCE_TAIL: u32 = 1;
pub const REDUCE_PI_SHORT: u32 = 2;
pub const SIN_SHORT: u32 = 3;
pub const COS_SHORT: u32 = 4;
pub const TAN_DIV_SHORT: u32 = 5;
pub const LANE_TERMS: u32 = 6;
pub const LANE_QUADRANT: u32 = 7;
pub const LANE_NAN: u32 = 8;
pub const LANE_POW2: u32 = 9;
pub const REDUCE_FAST_UNCHECKED: u32 = 10;
pub const REDUCE_FAST_EPS: u32 = 11;
pub const REDUCE_FAST_PI: u32 = 12;
pub const N: u32 = 12;
pub const NAMES: [&str; 13] = [
"none",
"reduction: last part of r dropped",
"reduction: pi/2 cut to 64 (n + 1) + 1 bits",
"sin r: two terms fewer",
"cos r: two terms fewer",
"tan: divisor's last part dropped",
"lanes: Horner's mask from lane 0's terms",
"lanes: lane 0's quadrant on every lane",
"lanes: NaN lanes from the lanes' arithmetic, NaN signs flipped",
"lanes: lane 0's powers of two on every lane",
"fast reduction: test skipped",
"fast reduction: test with eps = 0",
"fast reduction: pi/2 without its last two digits",
];
#[cfg(feature = "mutate")]
thread_local!(static ID: std::cell::Cell<u32> = const { std::cell::Cell::new(0) });
#[cfg(feature = "mutate")]
pub fn set(id: u32) {
ID.with(|m| m.set(id));
}
#[cfg(feature = "mutate")]
#[inline(always)]
pub fn on(id: u32) -> bool {
ID.with(|m| m.get() == id)
}
#[cfg(not(feature = "mutate"))]
#[inline(always)]
pub fn on(_id: u32) -> bool {
false
}
}
pub const LIMIT: f64 = f64::from_bits((1023 + 52) << 52); #[allow(clippy::approx_constant)]
const R_TH: f64 = 0.7854;
const L: usize = 20;
#[derive(Clone, Copy)]
struct Acc([u64; L]);
impl Acc {
#[inline(always)]
fn add_at(&mut self, w: usize, v: u128) {
let mut c = v;
let mut i = w;
while c != 0 && i < L {
let t = self.0[i] as u128 + (c as u64) as u128;
self.0[i] = t as u64;
c = (c >> 64) + (t >> 64);
i += 1;
}
}
#[inline(always)]
fn sub_at(&mut self, w: usize, v: u128) {
let mut c = v;
let mut i = w;
while c != 0 && i < L {
let (s, b) = self.0[i].overflowing_sub(c as u64);
self.0[i] = s;
c = (c >> 64) + b as u128;
i += 1;
}
}
#[inline(always)]
fn add_f64(&mut self, x: f64) {
if x == 0.0 {
return;
}
let b = x.to_bits();
let e = ((b >> 52) & 0x7ff) as i32;
let frac = b & ((1u64 << 52) - 1);
let (m, ex) = if e == 0 {
(frac, -1074)
} else {
(frac | (1u64 << 52), e - 1075)
};
let pos = (ex + tables::F as i32) as u32;
let v = (m as u128) << (pos % 64);
if b >> 63 == 0 {
self.add_at((pos / 64) as usize, v);
} else {
self.sub_at((pos / 64) as usize, v);
}
}
#[inline(always)]
fn sub_kp(&mut self, k: i64, n: usize) {
if k == 0 {
return;
}
let a = k.unsigned_abs() as u128;
let cut = if mutate::on(mutate::REDUCE_PI_SHORT) {
L - 1 - (n + 1)
} else {
0
};
let mut mc: u128 = 0;
let mut bc = false;
for i in 0..L {
let p = if i < cut { 0 } else { tables::P[i] };
let t = a * p as u128 + mc;
let pl = t as u64;
mc = t >> 64;
if k > 0 {
let (s1, b1) = self.0[i].overflowing_sub(pl);
let (s2, b2) = s1.overflowing_sub(bc as u64);
self.0[i] = s2;
bc = b1 | b2;
} else {
let (s1, c1) = self.0[i].overflowing_add(pl);
let (s2, c2) = s1.overflowing_add(bc as u64);
self.0[i] = s2;
bc = c1 | c2;
}
}
}
fn round(&self) -> f64 {
let neg = self.0[L - 1] >> 63 == 1;
let mut m = self.0;
if neg {
let mut c = true;
for v in m.iter_mut() {
let (s, o) = (!*v).overflowing_add(c as u64);
*v = s;
c = o;
}
}
let Some(top) = m.iter().rposition(|&v| v != 0) else {
return 0.0;
};
let h = 64 * top + 63 - m[top].leading_zeros() as usize;
let sub = tables::F as usize - 1074;
let lsb = if h >= sub + 52 { h - 52 } else { sub };
let (w, s) = ((lsb - 1) / 64, (lsb - 1) % 64);
let win = if s > 0 && w + 1 < L {
(m[w] >> s) | (m[w + 1] << (64 - s))
} else {
m[w] >> s
};
let (mut mant, half) = (win >> 1, win & 1 == 1);
let sticky = m[w] & ((1u64 << s) - 1) != 0 || m[..w].iter().any(|&v| v != 0);
if half && (sticky || mant & 1 == 1) {
mant += 1;
}
let mut be = lsb as i64 - tables::F as i64 + 1075;
if mant == 1u64 << 53 {
mant = 1u64 << 52;
be += 1;
}
let bits = if mant < 1u64 << 52 {
mant
} else {
((be as u64) << 52) | (mant & ((1u64 << 52) - 1))
};
let v = f64::from_bits(bits);
if neg {
-v
} else {
v
}
}
fn greedy<const N: usize>(mut self) -> [f64; N] {
let mut x = [0.0; N];
for xi in x.iter_mut() {
let v = self.round();
if v == 0.0 {
break;
}
*xi = v;
self.add_f64(-v);
}
if mutate::on(mutate::REDUCE_TAIL) {
x[N - 1] = 0.0;
}
x
}
}
#[inline(always)]
pub(crate) fn reduce<const N: usize, const L: usize>(x: &[f64; N]) -> (u32, [f64; N]) {
let k = (x[0] * tables::TWO_OVER_PI).round();
if k == 0.0 {
return (0, *x);
}
reduce_fast_k::<N, L>(x, k)
}
#[inline(never)]
fn reduce_fast_k<const N: usize, const L: usize>(x: &[f64; N], k: f64) -> (u32, [f64; N]) {
if !mutate::on(mutate::REDUCE_PI_SHORT) {
let (k, r, ok) = fast_reduce::fast::<f64, N, L>(x, k);
if ok {
return ((k as i64).rem_euclid(4) as u32, r);
}
}
reduce_k(x, k)
}
#[inline(always)]
pub(crate) fn reduce_exact<const N: usize>(x: &[f64; N]) -> (u32, [f64; N]) {
let k = (x[0] * tables::TWO_OVER_PI).round();
if k == 0.0 {
return (0, *x);
}
reduce_k(x, k)
}
#[inline(never)]
fn reduce_k<const N: usize>(x: &[f64; N], mut k: f64) -> (u32, [f64; N]) {
let mut acc = Acc([0; L]);
for &v in x {
acc.add_f64(v);
}
acc.sub_kp(k as i64, N);
let r0 = acc.round();
if r0.abs() > R_TH {
let k2 = (r0 * tables::TWO_OVER_PI).round();
acc.sub_kp(k2 as i64, N);
k += k2;
}
((k as i64).rem_euclid(4) as u32, acc.greedy::<N>())
}
pub(crate) trait Ext: Copy + Neg<Output = Self> {
const TINY: f64;
const KS: &'static [u8];
const KC: &'static [u8];
type M: Copy;
type K: Copy;
type F: Copy;
fn lead(self) -> Self::F;
fn finite(self) -> Self::M;
fn coef(c: &[f64; 8]) -> Self;
fn zero() -> Self;
fn one() -> Self;
fn nan() -> Self;
fn mul(self, b: Self) -> Self;
fn add(self, b: Self) -> Self;
fn one_plus(self) -> Self;
fn div(self, b: Self) -> Self;
fn reduce(self) -> (Self::K, Self);
fn drop_last(self) -> Self;
fn scale(self, p: Self::F) -> Self;
fn sel(m: Self::M, a: Self, b: Self) -> Self;
fn early(self) -> (Self::M, Self::M);
fn buckets(self) -> (Self::M, Self::K);
fn mask(b: bool) -> Self::M;
fn all(m: Self::M) -> bool;
fn any(m: Self::M) -> bool;
fn or(m: Self::M, n: Self::M) -> Self::M;
fn quadrant(q: Self::K, f: fn(usize) -> bool) -> Self::M;
fn terms(c: Self::M, j: Self::K) -> Self::K;
fn kmax(k: Self::K) -> usize;
fn active(k: Self::K, i: usize) -> Self::M;
fn lead_coef(c: Self::M, k: Self::K) -> Self;
fn pow2_j(j: Self::K, m: i32) -> Self::F;
fn unit_where(u: Self::M, p: Self::F) -> Self::F;
}
#[inline(always)]
fn bucket(r0: f64) -> usize {
(1022 - ((r0.to_bits() >> 52) & 0x7ff) as i64).max(0) as usize
}
#[inline(always)]
fn pow2(k: i32) -> f64 {
f64::from_bits(((1023 + k) as u64) << 52)
}
#[inline(always)]
fn horner<T: Ext>(z: T, k: T::K, lead: T, c: impl Fn(usize) -> T) -> T {
let mut p = lead;
for i in (1..T::kmax(k) - 1).rev() {
let m = T::active(k, i);
if T::all(m) {
p = z.mul(p).add(c(i));
} else {
p = T::sel(m, z.mul(p).add(c(i)), p);
}
}
p
}
#[inline(always)]
fn terms(t: &[u8], j: usize, short: u32) -> usize {
let k = t[j] as usize;
if mutate::on(short) {
k.saturating_sub(2).max(2)
} else {
k
}
}
#[inline(always)]
fn sin_cos_r<T: Ext>(r: T, z: T, tiny: T::M, j: T::K, c: T::M) -> T {
let small = T::sel(c, T::one(), r);
if T::all(tiny) {
return small;
}
let (all_c, no_c) = (T::all(c), !T::any(c));
let k = T::terms(c, j);
let p = horner(z, k, T::lead_coef(c, k), |i| {
if all_c {
T::coef(&tables::COS[i])
} else if no_c {
T::coef(&tables::SIN[i])
} else {
T::sel(c, T::coef(&tables::COS[i]), T::coef(&tables::SIN[i]))
}
});
let zp = z.mul(p);
let s = if all_c {
zp
} else {
let r1 = r.scale(T::pow2_j(j, 1));
r1.add(r1.mul(zp)).scale(T::pow2_j(j, -1))
};
let v = if no_c { s } else { T::sel(c, zp.one_plus(), s) };
T::sel(tiny, small, v)
}
struct Early<T: Ext> {
skip: T::M,
sin: T,
cos: T,
x: T,
}
#[inline(always)]
fn early<T: Ext>(x: T) -> Early<T> {
let (nan, tiny) = x.early();
let skip = T::or(nan, tiny);
Early {
skip,
sin: T::sel(nan, T::nan(), x),
cos: T::sel(nan, T::nan(), T::one()),
x: T::sel(skip, T::zero(), x),
}
}
#[inline(always)]
fn odd(q: usize) -> bool {
q % 2 == 1
}
#[inline(always)]
fn sin_neg(q: usize) -> bool {
q >= 2
}
#[inline(always)]
fn cos_neg(q: usize) -> bool {
q == 1 || q == 2
}
#[inline(always)]
fn sin<T: Ext>(x: T) -> T {
let e = early(x);
if T::all(e.skip) {
return e.sin;
}
let (q, r) = e.x.reduce();
let z = r.mul(r);
let (tiny, j) = r.buckets();
let v = sin_cos_r(r, z, tiny, j, T::quadrant(q, odd));
T::sel(e.skip, e.sin, T::sel(T::quadrant(q, sin_neg), -v, v))
}
#[inline(always)]
fn cos<T: Ext>(x: T) -> T {
let e = early(x);
if T::all(e.skip) {
return e.cos;
}
let (q, r) = e.x.reduce();
let z = r.mul(r);
let (tiny, j) = r.buckets();
let v = sin_cos_r(r, z, tiny, j, T::quadrant(q, |q| !odd(q)));
T::sel(e.skip, e.cos, T::sel(T::quadrant(q, cos_neg), -v, v))
}
#[inline(always)]
fn sin_cos<T: Ext>(x: T) -> (T, T) {
let e = early(x);
if T::all(e.skip) {
return (e.sin, e.cos);
}
let (q, r) = e.x.reduce();
let z = r.mul(r);
let (tiny, j) = r.buckets();
let (s, c) = (
sin_cos_r(r, z, tiny, j, T::mask(false)),
sin_cos_r(r, z, tiny, j, T::mask(true)),
);
let odd = T::quadrant(q, odd);
let (a, b) = (T::sel(odd, c, s), T::sel(odd, s, c));
let a = T::sel(T::quadrant(q, sin_neg), -a, a);
let b = T::sel(T::quadrant(q, cos_neg), -b, b);
(T::sel(e.skip, e.sin, a), T::sel(e.skip, e.cos, b))
}
#[inline(always)]
fn tan<T: Ext>(x: T) -> T {
let e = early(x);
if T::all(e.skip) {
return e.sin;
}
let (q, r) = e.x.reduce();
let z = r.mul(r);
let (tiny, j) = r.buckets();
let (s, c) = (
sin_cos_r(r, z, tiny, j, T::mask(false)),
sin_cos_r(r, z, tiny, j, T::mask(true)),
);
let short = |d: T| {
if mutate::on(mutate::TAN_DIV_SHORT) {
d.drop_last()
} else {
d
}
};
let odd = T::quadrant(q, odd);
let (num, den) = (T::sel(odd, c, s), short(T::sel(odd, s, c)));
let unit = T::or(odd, tiny);
let t = if T::all(unit) {
num.div(den)
} else {
let (up, down) = (T::pow2_j(j, 3), T::pow2_j(j, -3));
num.scale(T::unit_where(unit, up))
.div(den)
.scale(T::unit_where(unit, down))
};
T::sel(e.skip, e.sin, T::sel(odd, -t, t))
}
macro_rules! one_lane {
() => {
type M = bool;
type K = usize;
type F = f64;
#[inline(always)]
fn sel(m: bool, a: Self, b: Self) -> Self {
if m {
a
} else {
b
}
}
#[inline(always)]
fn early(self) -> (bool, bool) {
let a = self.lead().abs();
let nan = !self.finite() || a > LIMIT;
(nan, !nan && a < Self::TINY)
}
#[inline(always)]
fn buckets(self) -> (bool, usize) {
let a = self.lead().abs();
if a < Self::TINY {
(true, Self::KS.len() - 1)
} else {
(false, bucket(a))
}
}
#[inline(always)]
fn mask(b: bool) -> bool {
b
}
#[inline(always)]
fn all(m: bool) -> bool {
m
}
#[inline(always)]
fn any(m: bool) -> bool {
m
}
#[inline(always)]
fn or(m: bool, n: bool) -> bool {
m || n
}
#[inline(always)]
fn quadrant(q: usize, f: fn(usize) -> bool) -> bool {
f(q)
}
#[inline(always)]
fn terms(c: bool, j: usize) -> usize {
if c {
terms(Self::KC, j, mutate::COS_SHORT)
} else {
terms(Self::KS, j, mutate::SIN_SHORT)
}
}
#[inline(always)]
fn kmax(k: usize) -> usize {
k
}
#[inline(always)]
fn active(_k: usize, _i: usize) -> bool {
true
}
#[inline(always)]
fn lead_coef(c: bool, k: usize) -> Self {
Self::coef(if c {
&tables::COS[k - 1]
} else {
&tables::SIN[k - 1]
})
}
#[inline(always)]
fn pow2_j(j: usize, m: i32) -> f64 {
pow2(m * (j as i32 + 1))
}
#[inline(always)]
fn unit_where(u: bool, p: f64) -> f64 {
if u {
1.0
} else {
p
}
}
};
}
macro_rules! eight_lanes {
() => {
type M = M8<S>;
type K = V8<S>;
type F = V8<S>;
#[inline(always)]
fn early(self) -> (M8<S>, M8<S>) {
let a = self.lead().abs();
let nan = self.finite().not().or(a.gt(V8::splat(LIMIT)));
(nan, nan.not().and(V8::splat(Self::TINY).gt(a)))
}
#[inline(always)]
fn buckets(self) -> (M8<S>, V8<S>) {
let a = self.lead().abs();
let j = a.to_array().map(|v| {
if v < Self::TINY {
(Self::KS.len() - 1) as f64
} else {
bucket(v) as f64
}
});
(V8::splat(Self::TINY).gt(a), V8::from_array(j))
}
#[inline(always)]
fn mask(b: bool) -> M8<S> {
let z = V8::zero();
if b {
z.eq(z)
} else {
z.ne(z)
}
}
#[inline(always)]
fn all(m: M8<S>) -> bool {
m.bits() == 0xff
}
#[inline(always)]
fn any(m: M8<S>) -> bool {
m.bits() != 0
}
#[inline(always)]
fn or(m: M8<S>, n: M8<S>) -> M8<S> {
m.or(n)
}
#[inline(always)]
fn quadrant(q: V8<S>, f: fn(usize) -> bool) -> M8<S> {
let q = q.to_array();
let lane0 = mutate::on(mutate::LANE_QUADRANT);
let set = q.map(|v| {
let v = if lane0 { q[0] } else { v };
f(v as usize) as u8 as f64
});
V8::from_array(set).eq(V8::splat(1.0))
}
#[inline(always)]
fn terms(c: M8<S>, j: V8<S>) -> V8<S> {
let (c, j) = (c.bits(), j.to_array());
V8::from_array(std::array::from_fn(|l| {
let j = j[l] as usize;
if c >> l & 1 == 1 {
terms(Self::KC, j, mutate::COS_SHORT) as f64
} else {
terms(Self::KS, j, mutate::SIN_SHORT) as f64
}
}))
}
#[inline(always)]
fn kmax(k: V8<S>) -> usize {
k.hmax() as usize
}
#[inline(always)]
fn active(k: V8<S>, i: usize) -> M8<S> {
let k = if mutate::on(mutate::LANE_TERMS) {
k.bcast(0)
} else {
k
};
k.ge(V8::splat((i + 2) as f64))
}
#[inline(always)]
fn lead_coef(c: M8<S>, k: V8<S>) -> Self {
let (c, k) = (c.bits(), k.to_array());
Self::rows(std::array::from_fn(|l| {
let t = if c >> l & 1 == 1 {
&tables::COS
} else {
&tables::SIN
};
&t[k[l] as usize - 1]
}))
}
#[inline(always)]
fn pow2_j(j: V8<S>, m: i32) -> V8<S> {
let j = j.to_array();
let lane0 = mutate::on(mutate::LANE_POW2);
V8::from_array(j.map(|v| {
let v = if lane0 { j[0] } else { v };
pow2(m * (v as i32 + 1))
}))
}
#[inline(always)]
fn unit_where(u: M8<S>, p: V8<S>) -> V8<S> {
u.sel(V8::splat(1.0), p)
}
};
}
impl Ext for DD {
const TINY: f64 = f64::from_bits((1023 - 53) << 52); const KS: &'static [u8] = &tables::KS2;
const KC: &'static [u8] = &tables::KC2;
one_lane!();
fn lead(self) -> f64 {
self.hi
}
fn finite(self) -> bool {
self.hi.is_finite() && self.lo.is_finite()
}
fn coef(c: &[f64; 8]) -> DD {
DD { hi: c[0], lo: c[1] }
}
fn zero() -> DD {
DD::ZERO
}
fn one() -> DD {
DD::ONE
}
fn nan() -> DD {
DD::from_f64(f64::NAN)
}
fn mul(self, b: DD) -> DD {
let (hi, lo) = dw::dw_times_dw3((self.hi, self.lo), (b.hi, b.lo));
DD { hi, lo }
}
fn add(self, b: DD) -> DD {
self + b
}
fn one_plus(self) -> DD {
let (hi, lo) = dw::dw_plus_fp((self.hi, self.lo), 1.0);
DD { hi, lo }
}
fn div(self, b: DD) -> DD {
self.fast_div(b)
}
#[inline(always)]
fn reduce(self) -> (usize, DD) {
let (q, r) = reduce::<2, { fast_reduce::limbs(2) }>(&[self.hi, self.lo]);
let (hi, lo) = quick_two_sum(r[0], r[1]);
(q as usize, DD { hi, lo })
}
fn drop_last(self) -> DD {
DD::from_f64(self.hi)
}
fn scale(self, p: f64) -> DD {
self.mul_pwr2(p)
}
}
impl Ext for QD {
const TINY: f64 = f64::from_bits((1023 - 106) << 52); const KS: &'static [u8] = &tables::KS4;
const KC: &'static [u8] = &tables::KC4;
one_lane!();
fn lead(self) -> f64 {
self.x[0]
}
fn finite(self) -> bool {
self.x.iter().all(|v| v.is_finite())
}
fn coef(c: &[f64; 8]) -> QD {
QD {
x: [c[0], c[1], c[2], c[3]],
}
}
fn zero() -> QD {
QD::ZERO
}
fn one() -> QD {
QD::ONE
}
fn nan() -> QD {
QD::from_f64(f64::NAN)
}
fn mul(self, b: QD) -> QD {
self * b
}
fn add(self, b: QD) -> QD {
self + b
}
fn one_plus(self) -> QD {
self.add_f(1.0)
}
fn div(self, b: QD) -> QD {
self / b
}
#[inline(always)]
fn reduce(self) -> (usize, QD) {
let (q, x) = reduce::<4, { fast_reduce::limbs(4) }>(&self.x);
(q as usize, QD { x })
}
fn drop_last(self) -> QD {
QD {
x: [self.x[0], self.x[1], self.x[2], 0.0],
}
}
fn scale(self, p: f64) -> QD {
self.mul_pwr2(p)
}
}
impl Ext for OD {
const TINY: f64 = f64::from_bits((1023 - 212) << 52); const KS: &'static [u8] = &tables::KS8;
const KC: &'static [u8] = &tables::KC8;
one_lane!();
fn lead(self) -> f64 {
self.x[0]
}
fn finite(self) -> bool {
self.x.iter().all(|v| v.is_finite())
}
fn coef(c: &[f64; 8]) -> OD {
OD { x: *c }
}
fn zero() -> OD {
OD::ZERO
}
fn one() -> OD {
OD::ONE
}
fn nan() -> OD {
OD::from_f64(f64::NAN)
}
fn mul(self, b: OD) -> OD {
self * b
}
fn add(self, b: OD) -> OD {
self + b
}
fn one_plus(self) -> OD {
self.add_f(1.0)
}
fn div(self, b: OD) -> OD {
self / b
}
#[inline(always)]
fn reduce(self) -> (usize, OD) {
let (q, x) = reduce::<8, { fast_reduce::limbs(8) }>(&self.x);
(q as usize, OD { x })
}
fn drop_last(self) -> OD {
let mut x = self.x;
x[7] = 0.0;
OD { x }
}
fn scale(self, p: f64) -> OD {
self.mul_pwr2(p)
}
}
macro_rules! trig_methods {
($t:ty, $sc:literal, $tan:literal) => {
impl $t {
#[doc = concat!("sin x, within ", $sc, " relative and an absolute 5 |k| 2^-1217 < 2^-1163 for x on the ",
"ladder with |x_0| <= 2^52 (see `trig` for the hypotheses): a reduction by pi/2 to 1216 fraction bits ",
"with the bits of exact integer arithmetic, and a Taylor series. NaN for NaN, the infinities and |x_0| > 2^52; a ",
"zero gives itself.")]
pub fn sin(self) -> $t {
sin(self)
}
#[doc = concat!("cos x, within ", $sc, " relative, as `sin`; one for a zero.")]
pub fn cos(self) -> $t {
cos(self)
}
#[doc = concat!("tan x = sin x / cos x from one reduction, within ", $tan, " and 5 |k| 2^-1217 / ",
"|sin x cos x| relative, as `sin`; a zero gives itself.")]
pub fn tan(self) -> $t {
tan(self)
}
pub fn sin_cos(self) -> ($t, $t) {
sin_cos(self)
}
}
};
}
trig_methods!(DD, "7.85 u^2 (u = 2^-53, proven)", "28.5 u^2 (proven)");
trig_methods!(QD, "32 u^4 (u = 2^-53, tested)", "64 u^4 (tested)");
trig_methods!(OD, "3.17 u^8 (u = 2^-53, proven)", "6.42 u^8 (proven)");
#[inline(always)]
pub(crate) fn reduce_lanes<S: Simd, const N: usize, const L: usize>(c: &[V8<S>; N]) -> (V8<S>, [V8<S>; N]) {
let t = c[0].mul(V8::splat(tables::TWO_OVER_PI));
let k0 = V8::splat(0.5).gt(t.abs());
if k0.bits() == 0xff {
return (V8::zero(), *c);
}
if (!k0.bits()).count_ones() <= 3 {
let (mut q, mut r) = (V8::zero(), *c);
scalar_lanes(c, !k0.bits(), &mut q, &mut r, reduce::<N, L>);
return (q, r);
}
let (k, f, ok) = fast_reduce::fast::<V8<S>, N, L>(c, fast_reduce::round_away(t));
let mut q = k0.sel(V8::zero(), fast_reduce::mod4(k));
let mut r = *c;
for i in 0..N {
r[i] = k0.sel(c[i], f[i]);
}
let left = if mutate::on(mutate::REDUCE_PI_SHORT) {
!k0.bits()
} else {
!(ok.bits() | k0.bits())
};
if left != 0 {
scalar_lanes(c, left, &mut q, &mut r, reduce_exact::<N>);
}
(q, r)
}
#[inline(always)]
fn scalar_lanes<S: Simd, const N: usize>(
x: &[V8<S>; N],
m: u8,
q: &mut V8<S>,
r: &mut [V8<S>; N],
f: impl Fn(&[f64; N]) -> (u32, [f64; N]),
) {
let a = x.map(|v| v.to_array());
let mut qa = q.to_array();
let mut ra = r.map(|v| v.to_array());
for l in 0..8 {
if m >> l & 1 == 1 {
let (ql, rl) = f(&std::array::from_fn(|i| a[i][l]));
qa[l] = ql as f64;
for i in 0..N {
ra[i][l] = rl[i];
}
}
}
*q = V8::from_array(qa);
*r = ra.map(V8::from_array);
}
#[inline(always)]
fn finite_lanes<S: Simd>(c: &[V8<S>]) -> M8<S> {
let inf = V8::splat(f64::INFINITY);
let mut m = inf.gt(c[0].abs());
for v in &c[1..] {
m = m.and(inf.gt(v.abs()));
}
m
}
impl<S: Simd> DV<S> {
#[inline(always)]
fn rows(r: [&[f64; 8]; 8]) -> Self {
DV {
hi: V8::from_array(r.map(|c| c[0])),
lo: V8::from_array(r.map(|c| c[1])),
}
}
}
impl<S: Simd> Ext for DV<S> {
const TINY: f64 = <DD as Ext>::TINY;
const KS: &'static [u8] = <DD as Ext>::KS;
const KC: &'static [u8] = <DD as Ext>::KC;
eight_lanes!();
#[inline(always)]
fn lead(self) -> V8<S> {
self.hi
}
#[inline(always)]
fn finite(self) -> M8<S> {
finite_lanes(&[self.hi, self.lo])
}
#[inline(always)]
fn coef(c: &[f64; 8]) -> Self {
DV::splat(DD::coef(c))
}
#[inline(always)]
fn zero() -> Self {
DV::zero()
}
#[inline(always)]
fn one() -> Self {
DV::splat(DD::one())
}
#[inline(always)]
fn nan() -> Self {
DV::splat(DD::nan())
}
#[inline(always)]
fn mul(self, b: Self) -> Self {
let (hi, lo) = dw::dw_times_dw3((self.hi, self.lo), (b.hi, b.lo));
DV { hi, lo }
}
#[inline(always)]
fn add(self, b: Self) -> Self {
self + b
}
#[inline(always)]
fn one_plus(self) -> Self {
let (hi, lo) = dw::dw_plus_fp((self.hi, self.lo), V8::splat(1.0));
DV { hi, lo }
}
#[inline(always)]
fn div(self, b: Self) -> Self {
self.fast_div(b)
}
#[inline(always)]
fn reduce(self) -> (V8<S>, Self) {
let (q, [h, l]) = reduce_lanes::<S, 2, { fast_reduce::limbs(2) }>(&[self.hi, self.lo]);
let (hi, lo) = dv::quick_two_sum(h, l);
(q, DV { hi, lo })
}
#[inline(always)]
fn drop_last(self) -> Self {
DV {
hi: self.hi,
lo: V8::zero(),
}
}
#[inline(always)]
fn scale(self, p: V8<S>) -> Self {
DV {
hi: self.hi.mul(p),
lo: self.lo.mul(p),
}
}
#[inline(always)]
fn sel(m: M8<S>, a: Self, b: Self) -> Self {
DV {
hi: m.sel(a.hi, b.hi),
lo: m.sel(a.lo, b.lo),
}
}
}
impl<S: Simd> QV<S> {
#[inline(always)]
fn rows(r: [&[f64; 8]; 8]) -> Self {
let mut c = [V8::zero(); 4];
for i in 0..4 {
c[i] = V8::from_array(r.map(|c| c[i]));
}
QV { c }
}
}
impl<S: Simd> Ext for QV<S> {
const TINY: f64 = <QD as Ext>::TINY;
const KS: &'static [u8] = <QD as Ext>::KS;
const KC: &'static [u8] = <QD as Ext>::KC;
eight_lanes!();
#[inline(always)]
fn lead(self) -> V8<S> {
self.c[0]
}
#[inline(always)]
fn finite(self) -> M8<S> {
finite_lanes(&self.c)
}
#[inline(always)]
fn coef(c: &[f64; 8]) -> Self {
QV::splat(QD::coef(c))
}
#[inline(always)]
fn zero() -> Self {
QV::zero()
}
#[inline(always)]
fn one() -> Self {
QV::splat(QD::one())
}
#[inline(always)]
fn nan() -> Self {
QV::splat(QD::nan())
}
#[inline(always)]
fn mul(self, b: Self) -> Self {
self * b
}
#[inline(always)]
fn add(self, b: Self) -> Self {
self + b
}
#[inline(always)]
fn one_plus(self) -> Self {
self.add_f(1.0)
}
#[inline(always)]
fn div(self, b: Self) -> Self {
self / b
}
#[inline(always)]
fn reduce(self) -> (V8<S>, Self) {
let (q, c) = reduce_lanes::<S, 4, { fast_reduce::limbs(4) }>(&self.c);
(q, QV { c })
}
#[inline(always)]
fn drop_last(self) -> Self {
let mut c = self.c;
c[3] = V8::zero();
QV { c }
}
#[inline(always)]
fn scale(self, p: V8<S>) -> Self {
let mut c = self.c;
for v in c.iter_mut() {
*v = v.mul(p);
}
QV { c }
}
#[inline(always)]
fn sel(m: M8<S>, a: Self, b: Self) -> Self {
let mut c = b.c;
for i in 0..4 {
c[i] = m.sel(a.c[i], b.c[i]);
}
QV { c }
}
}
impl<S: Simd> OV<S> {
#[inline(always)]
fn rows(r: [&[f64; 8]; 8]) -> Self {
let mut c = [V8::zero(); 8];
for i in 0..8 {
c[i] = V8::from_array(r.map(|c| c[i]));
}
OV { c }
}
}
impl<S: Simd> Ext for OV<S> {
const TINY: f64 = <OD as Ext>::TINY;
const KS: &'static [u8] = <OD as Ext>::KS;
const KC: &'static [u8] = <OD as Ext>::KC;
eight_lanes!();
#[inline(always)]
fn lead(self) -> V8<S> {
self.c[0]
}
#[inline(always)]
fn finite(self) -> M8<S> {
finite_lanes(&self.c)
}
#[inline(always)]
fn coef(c: &[f64; 8]) -> Self {
OV::splat(OD::coef(c))
}
#[inline(always)]
fn zero() -> Self {
OV::zero()
}
#[inline(always)]
fn one() -> Self {
OV::splat(OD::one())
}
#[inline(always)]
fn nan() -> Self {
OV::splat(OD::nan())
}
#[inline(always)]
fn mul(self, b: Self) -> Self {
self * b
}
#[inline(always)]
fn add(self, b: Self) -> Self {
self + b
}
#[inline(always)]
fn one_plus(self) -> Self {
self.add_f(1.0)
}
#[inline(always)]
fn div(self, b: Self) -> Self {
self / b
}
#[inline(always)]
fn reduce(self) -> (V8<S>, Self) {
let (q, c) = reduce_lanes::<S, 8, { fast_reduce::limbs(8) }>(&self.c);
(q, OV { c })
}
#[inline(always)]
fn drop_last(self) -> Self {
let mut c = self.c;
c[7] = V8::zero();
OV { c }
}
#[inline(always)]
fn scale(self, p: V8<S>) -> Self {
OV {
c: alg::mul_pwr2(&self.c, p),
}
}
#[inline(always)]
fn sel(m: M8<S>, a: Self, b: Self) -> Self {
let mut c = b.c;
for i in 0..8 {
c[i] = m.sel(a.c[i], b.c[i]);
}
OV { c }
}
}
trait Scalars: Sized {
type Scalar: Copy;
fn nan_lanes(self) -> u8;
fn flip_nans(self) -> Self;
fn lane_of(self, j: usize) -> Self::Scalar;
fn with_lane(self, j: usize, v: Self::Scalar) -> Self;
}
macro_rules! scalars {
($t:ident, $s:ident, $n:literal, $parts:ident, $from:ident, $sparts:ident) => {
impl<S: Simd> Scalars for $t<S> {
type Scalar = $s;
#[inline(always)]
fn nan_lanes(self) -> u8 {
let parts = self.$parts();
let mut m = parts[0].ne(parts[0]);
for x in &parts[1..] {
m = m.or(x.ne(*x));
}
m.bits()
}
fn flip_nans(self) -> Self {
$t::$from(self.$parts().map(|x| x.ne(x).sel(x.neg(), x)))
}
fn lane_of(self, j: usize) -> $s {
self.lane(j)
}
fn with_lane(self, j: usize, value: $s) -> Self {
let mut a: [[f64; 8]; $n] = self.$parts().map(|x| x.to_array());
let w = $sparts(value);
for i in 0..$n {
a[i][j] = w[i];
}
$t::$from(a.map(V8::from_array))
}
}
};
}
impl<S: Simd> DV<S> {
#[inline(always)]
fn parts_v(self) -> [V8<S>; 2] {
[self.hi, self.lo]
}
fn from_parts_v(c: [V8<S>; 2]) -> Self {
DV { hi: c[0], lo: c[1] }
}
}
impl<S: Simd> QV<S> {
#[inline(always)]
fn parts_v(self) -> [V8<S>; 4] {
self.c
}
fn from_parts_v(c: [V8<S>; 4]) -> Self {
QV { c }
}
}
impl<S: Simd> OV<S> {
#[inline(always)]
fn parts_v(self) -> [V8<S>; 8] {
self.c
}
fn from_parts_v(c: [V8<S>; 8]) -> Self {
OV { c }
}
}
fn dd_parts(x: DD) -> [f64; 2] {
[x.hi, x.lo]
}
fn qd_parts(x: QD) -> [f64; 4] {
x.x
}
fn od_parts(x: OD) -> [f64; 8] {
x.x
}
scalars!(DV, DD, 2, parts_v, from_parts_v, dd_parts);
scalars!(QV, QD, 4, parts_v, from_parts_v, qd_parts);
scalars!(OV, OD, 8, parts_v, from_parts_v, od_parts);
#[inline(always)]
fn scalar_nans<T: Scalars + Copy>(x: T, v: T, f: fn(T::Scalar) -> T::Scalar) -> T {
let m = v.nan_lanes();
if m == 0 {
return v;
}
if mutate::on(mutate::LANE_NAN) {
return v.flip_nans();
}
let f = std::hint::black_box(f);
let mut v = v;
for j in 0..8 {
if m >> j & 1 == 1 {
v = v.with_lane(j, f(x.lane_of(j)));
}
}
v
}
macro_rules! trig_lane_methods {
($t:ident, $s:ident) => {
impl<S: Simd> $t<S> {
#[doc = concat!("sin x on every lane, each lane bitwise `", stringify!($s), "::sin` of its value, ",
"with its bounds (see `trig`)")]
#[inline]
pub fn sin(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, sin(self), $s::sin),
)
}
#[doc = concat!("cos x on every lane, each lane bitwise `", stringify!($s), "::cos` of its value")]
#[inline]
pub fn cos(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, cos(self), $s::cos),
)
}
#[doc = concat!("tan x on every lane, each lane bitwise `", stringify!($s), "::tan` of its value. ",
"`x.tan()` on this type is this function; the tangent of `dual::Lane` is `Lane::tan(x)`.")]
#[inline]
pub fn tan(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, tan(self), $s::tan),
)
}
#[inline]
pub fn sin_cos(self) -> (Self, Self) {
S::vectorize(
#[inline(always)]
|| {
let (s, c) = sin_cos(self);
let m = s.nan_lanes() | c.nan_lanes();
if m == 0 {
return (s, c);
}
if mutate::on(mutate::LANE_NAN) {
return (s.flip_nans(), c.flip_nans());
}
let f = std::hint::black_box($s::sin_cos as fn($s) -> ($s, $s));
let (mut s, mut c) = (s, c);
for j in 0..8 {
if m >> j & 1 == 1 {
let (a, b) = f(self.lane_of(j));
s = s.with_lane(j, a);
c = c.with_lane(j, b);
}
}
(s, c)
},
)
}
}
};
}
trig_lane_methods!(DV, DD);
trig_lane_methods!(QV, QD);
trig_lane_methods!(OV, OD);