macro_rules! dd_family {
(
$(#[$meta:meta])*
mod $modname:ident, vector = $vec:ident, elem = $elem:ty
) => {
$(#[$meta])*
pub mod $modname {
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub};
use fearless_simd::prelude::*;
use fearless_simd::{ExtractToken, Simd};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DD<S: Simd>(pub S::$vec, pub S::$vec);
impl<S: Simd> DD<S> {
#[inline(always)]
pub const fn new(hi: S::$vec, lo: S::$vec) -> Self {
Self(hi, lo)
}
#[inline(always)]
pub fn splat(simd: S, value: ($elem, $elem)) -> Self {
Self(
S::$vec::splat(simd, value.0),
S::$vec::splat(simd, value.1),
)
}
}
#[inline(always)]
pub fn normalize<S: Simd>(d: DD<S>) -> DD<S> {
let s0 = d.0 + d.1;
DD::new(s0, d.0 - s0 + d.1)
}
#[inline(always)]
pub fn scale<S: Simd>(d: DD<S>, other: S::$vec) -> DD<S> {
DD::new(d.0 * other, d.1 * other)
}
#[inline(always)]
pub fn add_checked<S: Simd>(d: DD<S>, other: DD<S>) -> DD<S> {
let r0 = d.0 + other.0;
DD::new(r0, d.0 - r0 + other.0 + d.1 + other.1)
}
#[inline(always)]
pub fn add_checked_v<S: Simd>(d: DD<S>, other: S::$vec) -> DD<S> {
let r0 = d.0 + other;
DD::new(r0, d.0 - r0 + other + d.1)
}
#[inline(always)]
pub fn sub_checked<S: Simd>(d: DD<S>, other: DD<S>) -> DD<S> {
let r0 = d.0 - other.0;
DD::new(r0, d.0 - r0 - other.0 + d.1 - other.1)
}
#[inline(always)]
pub fn square<S: Simd>(d: DD<S>) -> DD<S> {
let r0 = d.0 * d.0;
DD::new(r0, (d.0 + d.0).mul_add_precise(d.1, d.0.mul_add_precise(d.0, -r0)))
}
#[inline(always)]
pub fn square_as_f<S: Simd>(d: DD<S>) -> S::$vec {
d.0.mul_add_precise(d.0, d.0 * d.1 + d.0 * d.1)
}
#[inline(always)]
pub fn mul_as_f<S: Simd>(d: DD<S>, other: DD<S>) -> S::$vec {
d.0.mul_add_precise(other.0, d.1.mul_add_precise(other.0, d.0 * other.1))
}
#[inline(always)]
pub fn recip<S: Simd>(d: DD<S>) -> DD<S> {
let one = S::$vec::splat(d.0.token(), 1.0 as $elem);
let q0 = one / d.0;
DD::new(
q0,
q0 * (-d.1).mul_add_precise(q0, (-d.0).mul_add_precise(q0, one)),
)
}
#[inline(always)]
pub fn sqrt<S: Simd>(d: DD<S>) -> DD<S> {
let simd = d.0.token();
let t = (d.0 + d.1).sqrt();
scale(
(d + mul_as_doubled(t, t)) * recip_as_doubled(t),
S::$vec::splat(simd, 0.5 as $elem),
)
}
#[inline(always)]
pub fn mul_as_doubled<S: Simd>(x: S::$vec, y: S::$vec) -> DD<S> {
let r0 = x * y;
DD::new(r0, x.mul_add_precise(y, -r0))
}
#[inline(always)]
pub fn recip_as_doubled<S: Simd>(x: S::$vec) -> DD<S> {
let one = S::$vec::splat(x.token(), 1.0 as $elem);
let q0 = one / x;
DD::new(q0, q0 * (-x).mul_add_precise(q0, one))
}
#[inline(always)]
pub fn add_as_doubled<S: Simd>(x: S::$vec, y: S::$vec) -> DD<S> {
let r0 = x + y;
let v = r0 - x;
DD::new(r0, x - (r0 - v) + (y - v))
}
#[inline(always)]
pub fn add_checked_as_doubled<S: Simd>(x: S::$vec, y: S::$vec) -> DD<S> {
let r0 = x + y;
DD::new(r0, x - r0 + y)
}
#[inline(always)]
pub fn add_v<S: Simd>(d: DD<S>, v: S::$vec) -> DD<S> {
let r0 = d.0 + v;
let x = r0 - d.0;
DD::new(r0, d.0 - (r0 - x) + (v - x) + d.1)
}
#[inline(always)]
pub fn mul_v<S: Simd>(d: DD<S>, v: S::$vec) -> DD<S> {
let r0 = d.0 * v;
DD::new(r0, d.1.mul_add_precise(v, d.0.mul_add_precise(v, -r0)))
}
#[inline(always)]
pub fn v_add_checked<S: Simd>(v: S::$vec, other: DD<S>) -> DD<S> {
let r0 = v + other.0;
DD::new(r0, v - r0 + other.0 + other.1)
}
#[inline(always)]
pub fn add_assign_v<S: Simd>(d: &mut DD<S>, other: S::$vec) {
*d += DD::new(other, S::$vec::splat(other.token(), 0.0 as $elem));
}
#[inline(always)]
pub fn mul_assign_v<S: Simd>(d: &mut DD<S>, other: S::$vec) {
*d *= DD::new(other, S::$vec::splat(other.token(), 0.0 as $elem));
}
impl<S: Simd> Add for DD<S> {
type Output = Self;
#[inline(always)]
fn add(self, other: Self) -> Self {
let r0 = self.0 + other.0;
let v = r0 - self.0;
DD::new(r0, self.0 - (r0 - v) + (other.0 - v) + (self.1 + other.1))
}
}
impl<S: Simd> Neg for DD<S> {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self {
DD::new(-self.0, -self.1)
}
}
impl<S: Simd> Mul for DD<S> {
type Output = Self;
#[inline(always)]
fn mul(self, other: Self) -> Self {
let r0 = self.0 * other.0;
DD::new(
r0,
self.0.mul_add_precise(
other.1,
self.1.mul_add_precise(other.0, self.0.mul_add_precise(other.0, -r0)),
),
)
}
}
impl<S: Simd> Sub for DD<S> {
type Output = Self;
#[inline(always)]
fn sub(self, other: Self) -> Self {
self + (-other)
}
}
impl<S: Simd> Div for DD<S> {
type Output = Self;
#[inline(always)]
fn div(self, other: Self) -> Self {
let one = S::$vec::splat(self.0.token(), 1.0 as $elem);
let t = one / other.0;
let q0 = self.0 * t;
let u = t.mul_add_precise(self.0, -q0);
let mut q1 = (-other.1).mul_add_precise(t, (-other.0).mul_add_precise(t, one));
q1 = q0.mul_add_precise(q1, self.1.mul_add_precise(t, u));
DD::new(q0, q1)
}
}
impl<S: Simd> AddAssign for DD<S> {
#[inline(always)]
fn add_assign(&mut self, other: Self) {
*self = *self + other;
}
}
impl<S: Simd> MulAssign for DD<S> {
#[inline(always)]
fn mul_assign(&mut self, other: Self) {
*self = *self * other;
}
}
}
};
}
dd_family! {
mod f64,
vector = f64s,
elem = f64
}
dd_family! {
mod f32,
vector = f32s,
elem = f32
}