elem_simd_math/doubled.rs
1//! Double-double ("double-float") arithmetic over SIMD vectors.
2//!
3//! Ported from the `doubled` crate v0.3.3 (`src/lib.rs`, `src/f64x.rs`, `src/f32x.rs`) as used by
4//! `sleef-rs` rev `3671d46`. Only the operations the ported kernels call are kept.
5//!
6//! The `doubled` crate is MIT OR Apache-2.0, Copyright (c) 2018 Jorge Aparicio; see
7//! `THIRD-PARTY-LICENSES`.
8//!
9//! # Fused formulation
10//!
11//! Upstream selects between an FMA-based and a split-based formulation at compile time
12//! (`cfg(target_feature = "fma")`). This port always uses the FMA-based one, evaluated with
13//! [`fearless_simd::SimdFloat::mul_add_precise`], which rounds the exact product-plus-add once on
14//! every level. Plain `mul_add` must not be substituted: on SSE2 and on WebAssembly without
15//! `relaxed-simd` it is `a * b + c`, which makes the residual below exactly zero and collapses
16//! the whole layer to single precision. The split-based formulation was measurably slower on the
17//! double-double-heavy kernels without being more accurate.
18//!
19//! # Shape
20//!
21//! Extension traits cannot be implemented for `S::f64s` (the projection is not a local type), so
22//! upstream's method-style extension traits (`MulAsDoubled`, …) become free functions here, and
23//! the operators are implemented on the local [`f64::DD`] type.
24//!
25//! Every function is defined once by the `dd_family!` macro, which is instantiated per element
26//! type, so the two families cannot drift apart.
27
28macro_rules! dd_family {
29 (
30 $(#[$meta:meta])*
31 mod $modname:ident, vector = $vec:ident, elem = $elem:ty
32 ) => {
33 $(#[$meta])*
34 pub mod $modname {
35 use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub};
36
37 use fearless_simd::prelude::*;
38 use fearless_simd::{ExtractToken, Simd};
39
40 /// A value carried as an unevaluated sum of two vectors `(hi, lo)`.
41 ///
42 /// `hi` holds the leading part and `lo` the rounding error, so the pair represents
43 /// `hi + lo` to roughly twice the significand width of the element type.
44 #[derive(Clone, Copy, Debug, PartialEq)]
45 pub struct DD<S: Simd>(pub S::$vec, pub S::$vec);
46
47 impl<S: Simd> DD<S> {
48 /// Pairs `hi` and `lo` without normalizing or ordering them.
49 #[inline(always)]
50 pub const fn new(hi: S::$vec, lo: S::$vec) -> Self {
51 Self(hi, lo)
52 }
53
54 /// Broadcasts a scalar double-double.
55 #[inline(always)]
56 pub fn splat(simd: S, value: ($elem, $elem)) -> Self {
57 Self(
58 S::$vec::splat(simd, value.0),
59 S::$vec::splat(simd, value.1),
60 )
61 }
62 }
63
64 /// Re-establishes `|hi| >= |lo|` after an approximate addition.
65 #[inline(always)]
66 pub fn normalize<S: Simd>(d: DD<S>) -> DD<S> {
67 let s0 = d.0 + d.1;
68 DD::new(s0, d.0 - s0 + d.1)
69 }
70
71 /// Multiplies both components by `other`.
72 #[inline(always)]
73 pub fn scale<S: Simd>(d: DD<S>, other: S::$vec) -> DD<S> {
74 DD::new(d.0 * other, d.1 * other)
75 }
76
77 /// Adds `other`, assuming `|self| >= |other|`.
78 #[inline(always)]
79 pub fn add_checked<S: Simd>(d: DD<S>, other: DD<S>) -> DD<S> {
80 let r0 = d.0 + other.0;
81 DD::new(r0, d.0 - r0 + other.0 + d.1 + other.1)
82 }
83
84 /// Adds `other`, assuming `|self| >= |other|`.
85 #[inline(always)]
86 pub fn add_checked_v<S: Simd>(d: DD<S>, other: S::$vec) -> DD<S> {
87 let r0 = d.0 + other;
88 DD::new(r0, d.0 - r0 + other + d.1)
89 }
90
91 /// Subtracts `other`, assuming `|self| >= |other|`.
92 #[inline(always)]
93 pub fn sub_checked<S: Simd>(d: DD<S>, other: DD<S>) -> DD<S> {
94 let r0 = d.0 - other.0;
95 DD::new(r0, d.0 - r0 - other.0 + d.1 - other.1)
96 }
97
98 /// Squares the represented value.
99 #[inline(always)]
100 pub fn square<S: Simd>(d: DD<S>) -> DD<S> {
101 let r0 = d.0 * d.0;
102 DD::new(r0, (d.0 + d.0).mul_add_precise(d.1, d.0.mul_add_precise(d.0, -r0)))
103 }
104
105 /// Squares and collapses to a single vector.
106 #[inline(always)]
107 pub fn square_as_f<S: Simd>(d: DD<S>) -> S::$vec {
108 d.0.mul_add_precise(d.0, d.0 * d.1 + d.0 * d.1)
109 }
110
111 /// Multiplies and collapses to a single vector.
112 #[inline(always)]
113 pub fn mul_as_f<S: Simd>(d: DD<S>, other: DD<S>) -> S::$vec {
114 d.0.mul_add_precise(other.0, d.1.mul_add_precise(other.0, d.0 * other.1))
115 }
116
117 /// Reciprocal of the represented value.
118 #[inline(always)]
119 pub fn recip<S: Simd>(d: DD<S>) -> DD<S> {
120 let one = S::$vec::splat(d.0.token(), 1.0 as $elem);
121 let q0 = one / d.0;
122 DD::new(
123 q0,
124 q0 * (-d.1).mul_add_precise(q0, (-d.0).mul_add_precise(q0, one)),
125 )
126 }
127
128 /// Square root of the represented value.
129 #[inline(always)]
130 pub fn sqrt<S: Simd>(d: DD<S>) -> DD<S> {
131 let simd = d.0.token();
132 let t = (d.0 + d.1).sqrt();
133 scale(
134 (d + mul_as_doubled(t, t)) * recip_as_doubled(t),
135 S::$vec::splat(simd, 0.5 as $elem),
136 )
137 }
138
139 /// The exact product `x * y` split into two components.
140 #[inline(always)]
141 pub fn mul_as_doubled<S: Simd>(x: S::$vec, y: S::$vec) -> DD<S> {
142 let r0 = x * y;
143 DD::new(r0, x.mul_add_precise(y, -r0))
144 }
145
146 /// Reciprocal of a vector as an unevaluated sum.
147 #[inline(always)]
148 pub fn recip_as_doubled<S: Simd>(x: S::$vec) -> DD<S> {
149 let one = S::$vec::splat(x.token(), 1.0 as $elem);
150 let q0 = one / x;
151 DD::new(q0, q0 * (-x).mul_add_precise(q0, one))
152 }
153
154 /// The exact sum `x + y` split into two components.
155 #[inline(always)]
156 pub fn add_as_doubled<S: Simd>(x: S::$vec, y: S::$vec) -> DD<S> {
157 let r0 = x + y;
158 let v = r0 - x;
159 DD::new(r0, x - (r0 - v) + (y - v))
160 }
161
162 /// The exact sum `x + y` split into two components, assuming `|x| >= |y|`.
163 #[inline(always)]
164 pub fn add_checked_as_doubled<S: Simd>(x: S::$vec, y: S::$vec) -> DD<S> {
165 let r0 = x + y;
166 DD::new(r0, x - r0 + y)
167 }
168
169 /// `d + v`, without normalizing.
170 #[inline(always)]
171 pub fn add_v<S: Simd>(d: DD<S>, v: S::$vec) -> DD<S> {
172 let r0 = d.0 + v;
173 let x = r0 - d.0;
174 DD::new(r0, d.0 - (r0 - x) + (v - x) + d.1)
175 }
176
177 /// `d * v`, without normalizing.
178 #[inline(always)]
179 pub fn mul_v<S: Simd>(d: DD<S>, v: S::$vec) -> DD<S> {
180 let r0 = d.0 * v;
181 DD::new(r0, d.1.mul_add_precise(v, d.0.mul_add_precise(v, -r0)))
182 }
183
184 /// `v + d`, matching upstream's `T + Doubled<T>`.
185 #[inline(always)]
186 pub fn v_add_checked<S: Simd>(v: S::$vec, other: DD<S>) -> DD<S> {
187 let r0 = v + other.0;
188 DD::new(r0, v - r0 + other.0 + other.1)
189 }
190
191 /// Adds a vector in place, without normalizing.
192 #[inline(always)]
193 pub fn add_assign_v<S: Simd>(d: &mut DD<S>, other: S::$vec) {
194 *d += DD::new(other, S::$vec::splat(other.token(), 0.0 as $elem));
195 }
196
197 /// Multiplies by a vector in place, without normalizing.
198 #[inline(always)]
199 pub fn mul_assign_v<S: Simd>(d: &mut DD<S>, other: S::$vec) {
200 *d *= DD::new(other, S::$vec::splat(other.token(), 0.0 as $elem));
201 }
202
203 impl<S: Simd> Add for DD<S> {
204 type Output = Self;
205 #[inline(always)]
206 fn add(self, other: Self) -> Self {
207 let r0 = self.0 + other.0;
208 let v = r0 - self.0;
209 DD::new(r0, self.0 - (r0 - v) + (other.0 - v) + (self.1 + other.1))
210 }
211 }
212
213
214 impl<S: Simd> Neg for DD<S> {
215 type Output = Self;
216 #[inline(always)]
217 fn neg(self) -> Self {
218 DD::new(-self.0, -self.1)
219 }
220 }
221
222 impl<S: Simd> Mul for DD<S> {
223 type Output = Self;
224 #[inline(always)]
225 fn mul(self, other: Self) -> Self {
226 let r0 = self.0 * other.0;
227 DD::new(
228 r0,
229 self.0.mul_add_precise(
230 other.1,
231 self.1.mul_add_precise(other.0, self.0.mul_add_precise(other.0, -r0)),
232 ),
233 )
234 }
235 }
236
237
238 impl<S: Simd> Sub for DD<S> {
239 type Output = Self;
240 #[inline(always)]
241 fn sub(self, other: Self) -> Self {
242 self + (-other)
243 }
244 }
245
246 impl<S: Simd> Div for DD<S> {
247 type Output = Self;
248 #[inline(always)]
249 fn div(self, other: Self) -> Self {
250 let one = S::$vec::splat(self.0.token(), 1.0 as $elem);
251 let t = one / other.0;
252
253 let q0 = self.0 * t;
254 let u = t.mul_add_precise(self.0, -q0);
255 let mut q1 = (-other.1).mul_add_precise(t, (-other.0).mul_add_precise(t, one));
256 q1 = q0.mul_add_precise(q1, self.1.mul_add_precise(t, u));
257
258 DD::new(q0, q1)
259 }
260 }
261
262 impl<S: Simd> AddAssign for DD<S> {
263 #[inline(always)]
264 fn add_assign(&mut self, other: Self) {
265 *self = *self + other;
266 }
267 }
268
269
270 impl<S: Simd> MulAssign for DD<S> {
271 #[inline(always)]
272 fn mul_assign(&mut self, other: Self) {
273 *self = *self * other;
274 }
275 }
276
277 }
278 };
279}
280
281dd_family! {
282 /// Double-double arithmetic over native-width `f64` vectors.
283 mod f64,
284 vector = f64s,
285 elem = f64
286}
287
288dd_family! {
289 /// Double-double arithmetic over native-width `f32` vectors.
290 mod f32,
291 vector = f32s,
292 elem = f32
293}