Skip to main content

elem_simd_math/f64x/
mod.rs

1//! Vector helpers for the `f64` kernels.
2//!
3//! Ported from `sleef-rs` rev `3671d46` (`src/f64x.rs`, `src/f64x/constants.rs`) and the `doubled`
4//! crate v0.3.3. See the crate-level [`crate::common`] docs for the translation table.
5//!
6//! Every helper takes the SIMD token as its first argument. `<S as Simd>::f64s` is not
7//! invertible in argument position, so a signature whose only vector argument is that projection
8//! cannot be inferred from a call; passing the token first makes every call site inferrable
9//! without a turbofish.
10
11pub mod activation;
12pub mod constants;
13pub mod special;
14pub mod u05;
15pub mod u10;
16pub mod u15;
17pub mod u35;
18
19use fearless_simd::prelude::*;
20use fearless_simd::Simd;
21
22pub use crate::common::{
23    mla, mul_sub, neg_mul_add, poly10, poly11, poly12, poly13, poly14, poly15, poly16, poly17,
24    poly18, poly19, poly2, poly20, poly21, poly3, poly4, poly5, poly6, poly7, poly8, poly9,
25};
26use crate::common::{F64s, I64s, M64s, U64s};
27use crate::constants::*;
28use crate::doubled::f64::{
29    add_as_doubled, add_v, mul_as_doubled, mul_v, normalize, recip_as_doubled, scale,
30    v_add_checked, DD,
31};
32
33use constants::REMPITABDP;
34pub use constants::{splat, splat_bits, splat_int};
35
36/// Reinterprets an `f64` vector as its bit pattern.
37#[inline(always)]
38pub fn to_bits<S: Simd>(_simd: S, x: F64s<S>) -> U64s<S> {
39    x.bitcast()
40}
41
42/// Reinterprets a bit pattern as an `f64` vector.
43#[inline(always)]
44pub fn from_bits<S: Simd>(_simd: S, x: U64s<S>) -> F64s<S> {
45    x.bitcast()
46}
47
48/// Reinterprets a bit pattern as a signed integer vector of the same lane count.
49#[inline(always)]
50pub fn bits_to_int<S: Simd>(_simd: S, x: U64s<S>) -> I64s<S> {
51    x.bitcast()
52}
53
54/// Reinterprets a signed integer vector as a bit pattern.
55#[inline(always)]
56pub fn int_to_bits<S: Simd>(_simd: S, x: I64s<S>) -> U64s<S> {
57    x.bitcast()
58}
59
60/// Sign bit of `x`, as a bit pattern.
61#[inline(always)]
62pub fn sign_bit<S: Simd>(simd: S, x: F64s<S>) -> U64s<S> {
63    to_bits(simd, x) & splat_bits(simd, (-0.0f64).to_bits())
64}
65
66/// `1.0` carrying the sign of `x`.
67#[inline(always)]
68pub fn sign<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
69    from_bits(simd, sign_bit(simd, x) ^ splat_bits(simd, 1.0f64.to_bits()))
70}
71
72/// `x` with the sign bit of `y` applied.
73#[inline(always)]
74pub fn mul_sign<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
75    from_bits(simd, to_bits(simd, x) ^ sign_bit(simd, y))
76}
77
78/// `x` with the sign bit of `y` set.
79#[inline(always)]
80pub fn or_sign<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
81    from_bits(simd, to_bits(simd, x) | sign_bit(simd, y))
82}
83
84/// `x` with the sign of `y`.
85#[inline(always)]
86pub fn copy_sign<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
87    from_bits(
88        simd,
89        (splat_bits(simd, !(-0.0f64).to_bits()) & to_bits(simd, x)) ^ sign_bit(simd, y),
90    )
91}
92
93/// Whether `x` is exactly `-0.0`.
94#[inline(always)]
95pub fn is_neg_zero<S: Simd>(simd: S, x: F64s<S>) -> M64s<S> {
96    to_bits(simd, x).simd_eq(splat_bits(simd, (-0.0f64).to_bits()))
97}
98
99/// Round to nearest integer, ties to even.
100#[inline(always)]
101pub fn rint<S: Simd>(simd: S, d: F64s<S>) -> F64s<S> {
102    let c = mul_sign(simd, splat(simd, D1_52), d);
103    d.abs()
104        .simd_gt(splat(simd, D1_52))
105        .select(d, or_sign(simd, (d + c) - c, d))
106}
107
108/// Truncate towards zero.
109#[inline(always)]
110pub fn trunc<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
111    let mut fr =
112        x - splat(simd, D1_31) * trunci(simd, x * splat(simd, 1.0 / D1_31)).to_float::<F64s<S>>();
113    fr -= trunci(simd, fr).to_float::<F64s<S>>();
114    (x.is_infinite() | x.abs().simd_ge(splat(simd, D1_52))).select(x, copy_sign(simd, x - fr, x))
115}
116
117/// Round towards negative infinity.
118#[inline(always)]
119pub fn floor<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
120    let mut fr =
121        x - splat(simd, D1_31) * trunci(simd, x * splat(simd, 1.0 / D1_31)).to_float::<F64s<S>>();
122    fr -= trunci(simd, fr).to_float::<F64s<S>>();
123    fr = fr.simd_lt(splat(simd, 0.)).select(fr + splat(simd, 1.), fr);
124    (x.is_infinite() | x.abs().simd_ge(splat(simd, D1_52))).select(x, copy_sign(simd, x - fr, x))
125}
126
127/// Round towards positive infinity.
128#[inline(always)]
129pub fn ceil<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
130    let mut fr =
131        x - splat(simd, D1_31) * trunci(simd, x * splat(simd, 1.0 / D1_31)).to_float::<F64s<S>>();
132    fr -= trunci(simd, fr).to_float::<F64s<S>>();
133    fr = fr.simd_le(splat(simd, 0.)).select(fr, fr - splat(simd, 1.));
134    (x.is_infinite() | x.abs().simd_ge(splat(simd, D1_52))).select(x, copy_sign(simd, x - fr, x))
135}
136
137/// Round away from zero.
138#[inline(always)]
139pub fn round<S: Simd>(simd: S, d: F64s<S>) -> F64s<S> {
140    let mut x = d + splat(simd, 0.5);
141    let mut fr =
142        x - splat(simd, D1_31) * trunci(simd, x * splat(simd, 1.0 / D1_31)).to_float::<F64s<S>>();
143    fr -= trunci(simd, fr).to_float::<F64s<S>>();
144    x = (x.simd_le(splat(simd, 0.)) & fr.simd_eq(splat(simd, 0.))).select(x - splat(simd, 1.), x);
145    fr = fr.simd_lt(splat(simd, 0.)).select(fr + splat(simd, 1.), fr);
146    x = d
147        .simd_eq(splat(simd, 0.499_999_999_999_999_944_49))
148        .select(splat(simd, 0.), x);
149    (d.is_infinite() | d.abs().simd_ge(splat(simd, D1_52))).select(d, copy_sign(simd, x - fr, d))
150}
151
152/// Truncate to an integer vector.
153#[inline(always)]
154pub fn trunci<S: Simd>(_simd: S, x: F64s<S>) -> I64s<S> {
155    x.trunc().to_int::<I64s<S>>()
156}
157
158/// Round to an integer vector, ties to even.
159#[inline(always)]
160pub fn roundi<S: Simd>(_simd: S, x: F64s<S>) -> I64s<S> {
161    x.round_ties_even().to_int::<I64s<S>>()
162}
163
164/// Whether `x` is integral.
165#[inline(always)]
166pub fn is_integer<S: Simd>(simd: S, x: F64s<S>) -> M64s<S> {
167    let mut t = trunc(simd, x * splat(simd, 1.0 / D1_31));
168    t = mla(simd, splat(simd, -D1_31), t, x);
169    trunc(simd, t).simd_eq(t) | x.abs().simd_gt(splat(simd, D1_53))
170}
171
172/// Whether `x` is an odd integer.
173#[inline(always)]
174pub fn is_odd<S: Simd>(simd: S, x: F64s<S>) -> M64s<S> {
175    let mut t = trunc(simd, x * splat(simd, 1.0 / D1_31));
176    t = mla(simd, splat(simd, -D1_31), t, x);
177    (trunci(simd, t) & splat_int(simd, 1)).simd_eq(splat_int(simd, 1))
178        & x.abs().simd_lt(splat(simd, D1_53))
179}
180
181/// Selects between two scalars broadcast to vectors.
182#[inline(always)]
183pub fn select_splat<S: Simd>(simd: S, mask: M64s<S>, if_true: f64, if_false: f64) -> F64s<S> {
184    mask.select(splat(simd, if_true), splat(simd, if_false))
185}
186
187/// Selects between three scalar values using two masks.
188#[inline(always)]
189pub fn select3<S: Simd>(simd: S, o0: M64s<S>, o1: M64s<S>, d0: f64, d1: f64, d2: f64) -> F64s<S> {
190    o0.select(splat(simd, d0), select_splat(simd, o1, d1, d2))
191}
192
193/// Selects between four scalar values using three masks.
194#[allow(clippy::too_many_arguments)]
195#[inline(always)]
196pub fn select4<S: Simd>(
197    simd: S,
198    o0: M64s<S>,
199    o1: M64s<S>,
200    o2: M64s<S>,
201    d0: f64,
202    d1: f64,
203    d2: f64,
204    d3: f64,
205) -> F64s<S> {
206    o0.select(
207        splat(simd, d0),
208        o1.select(splat(simd, d1), select_splat(simd, o2, d2, d3)),
209    )
210}
211
212/// Selects between two double-double values using a mask.
213#[inline(always)]
214pub fn select_doubled<S: Simd>(_simd: S, mask: M64s<S>, l: DD<S>, r: DD<S>) -> DD<S> {
215    DD::new(mask.select(l.0, r.0), mask.select(l.1, r.1))
216}
217
218/// `2^q` for an integer vector `q`.
219#[inline(always)]
220pub fn pow2i<S: Simd>(simd: S, q: I64s<S>) -> F64s<S> {
221    from_bits(simd, int_to_bits(simd, (splat_int(simd, 0x3ff) + q) << 52))
222}
223
224/// `d * 2^e`, split into two half-range multiplications.
225#[inline(always)]
226pub fn ldexp2k<S: Simd>(simd: S, d: F64s<S>, e: I64s<S>) -> F64s<S> {
227    let e1 = e >> 1;
228    d * pow2i(simd, e1) * pow2i(simd, e - e1)
229}
230
231/// `d * 2^q` by adding directly to the exponent field.
232#[inline(always)]
233pub fn ldexp3k<S: Simd>(simd: S, d: F64s<S>, q: I64s<S>) -> F64s<S> {
234    from_bits(simd, to_bits(simd, d) + int_to_bits(simd, q << 52))
235}
236
237/// The exponent of `d`, without subnormal handling.
238#[inline(always)]
239pub fn ilogb2k<S: Simd>(simd: S, d: F64s<S>) -> I64s<S> {
240    bits_to_int(simd, (to_bits(simd, d) >> 52) & splat_bits(simd, 0x7ff)) - splat_int(simd, 0x3ff)
241}
242
243/// The exponent of `d`, scaling subnormals into range first.
244#[inline(always)]
245pub fn ilogbk<S: Simd>(simd: S, mut d: F64s<S>) -> I64s<S> {
246    let o = d.simd_lt(splat(simd, 4.909_093_465_297_726_6_e-91));
247    d = o.select(splat(simd, 2.037_035_976_334_486_e90) * d, d);
248    let q = bits_to_int(simd, (to_bits(simd, d) >> 52) & splat_bits(simd, 0x7ff));
249    q - o.select(splat_int(simd, 300 + 0x3ff), splat_int(simd, 0x3ff))
250}
251
252/// `x * 2^q` over the full exponent range, including subnormals.
253#[inline(always)]
254pub fn ldexpk<S: Simd>(simd: S, x: F64s<S>, q: I64s<S>) -> F64s<S> {
255    let mut m = q >> 31;
256    m = (((m + q) >> 9) - m) << 7;
257    let q = q - (m << 2);
258    m = splat_int(simd, 0x3ff) + m;
259    m = (!splat_int(simd, 0).simd_gt(m)).select(splat_int(simd, -1), splat_int(simd, 0)) & m;
260    m = m
261        .simd_gt(splat_int(simd, 0x7ff))
262        .select(splat_int(simd, 0x7ff), m);
263    let y = from_bits(simd, int_to_bits(simd, m) << 52);
264    x * y * y * y * y * pow2i(simd, q)
265}
266
267/// Multiply by an integral power of two.
268#[inline(always)]
269pub fn ldexp<S: Simd>(simd: S, x: F64s<S>, q: I64s<S>) -> F64s<S> {
270    ldexpk(simd, x, q)
271}
272
273/// The exponent of `d` as an integer vector.
274#[inline(always)]
275pub fn ilogb<S: Simd>(simd: S, d: F64s<S>) -> I64s<S> {
276    let mut e = ilogbk(simd, d.abs()).to_float();
277    e = d
278        .simd_eq(splat(simd, 0.))
279        .select(splat(simd, SLEEF_FP_ILOGB0 as f64), e);
280    e = d.is_nan().select(splat(simd, SLEEF_FP_ILOGBNAN as f64), e);
281    e = d.is_infinite().select(splat(simd, f64::MAX), e);
282    roundi(simd, e)
283}
284
285/// Picks `m` with the sign of `d` for infinite `d`, otherwise `d`.
286#[inline(always)]
287pub fn visinf2<S: Simd>(simd: S, d: F64s<S>, m: F64s<S>) -> F64s<S> {
288    from_bits(
289        simd,
290        d.is_infinite()
291            .select(splat_bits(simd, u64::MAX), splat_bits(simd, 0))
292            & ((to_bits(simd, d) & sign_bit(simd, d)) | to_bits(simd, m)),
293    )
294}
295
296/// Maximum, treating a NaN in `y` as `x`.
297#[inline(always)]
298pub fn fmax<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
299    let _ = simd;
300    y.is_nan().select(x, x.max(y))
301}
302
303/// Minimum, treating a NaN in `y` as `x`.
304#[inline(always)]
305pub fn fmin<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
306    let _ = simd;
307    y.is_nan().select(x, x.min(y))
308}
309
310/// Positive difference.
311#[inline(always)]
312pub fn fdim<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
313    let ret = x - y;
314    (ret.simd_lt(splat(simd, 0.)) | x.simd_eq(y)).select(splat(simd, 0.), ret)
315}
316
317/// `v + d`, matching upstream's `F64x + Doubled<F64x>`.
318#[inline(always)]
319pub fn v_add_dd<S: Simd>(_simd: S, v: F64s<S>, d: DD<S>) -> DD<S> {
320    let t = add_as_doubled::<S>(v, d.0);
321    DD::new(t.0, t.1 + d.1)
322}
323
324/// Absolute value.
325#[inline(always)]
326pub fn fabs<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
327    let _ = simd;
328    x.abs()
329}
330
331/// The next representable value after `x` in the direction of `y`.
332#[inline(always)]
333pub fn nextafter<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
334    let x = x
335        .simd_eq(splat(simd, 0.))
336        .select(mul_sign(simd, splat(simd, 0.), y), x);
337    let xi2 = bits_to_int(simd, to_bits(simd, x));
338    let c = x.is_sign_negative() ^ y.simd_ge(x);
339
340    let xi2 = c.select(-(xi2 ^ splat_int(simd, i64::MIN)), xi2);
341    let xi2 = x.simd_ne(y).select(xi2 - splat_int(simd, 1), xi2);
342    let xi2 = c.select(-(xi2 ^ splat_int(simd, i64::MIN)), xi2);
343
344    let mut ret = from_bits(simd, int_to_bits(simd, xi2));
345
346    ret = (ret.simd_eq(splat(simd, 0.)) & x.simd_ne(splat(simd, 0.)))
347        .select(mul_sign(simd, splat(simd, 0.), x), ret);
348    ret = (x.simd_eq(splat(simd, 0.)) & y.simd_eq(splat(simd, 0.))).select(y, ret);
349    (x.is_nan() | y.is_nan()).select(splat(simd, f64::NAN), ret)
350}
351
352/// `sqrt(x)` as a double-double.
353#[inline(always)]
354pub fn sqrt_as_doubled<S: Simd>(simd: S, x: F64s<S>) -> DD<S> {
355    let t = x.sqrt();
356    scale(
357        (DD::new(x, splat(simd, 0.0)) + mul_as_doubled(t, t)) * recip_as_doubled(t),
358        splat(simd, 0.5),
359    )
360}
361
362/// Removes the integer part of `4 * x`, returning the fraction and the integer part.
363#[inline(always)]
364pub fn rempisub<S: Simd>(simd: S, x: F64s<S>) -> (F64s<S>, I64s<S>) {
365    let c = mul_sign(simd, splat(simd, D1_52), x);
366    let rint4x = (splat(simd, 4.) * x)
367        .abs()
368        .simd_gt(splat(simd, D1_52))
369        .select(
370            splat(simd, 4.) * x,
371            or_sign(simd, mla(simd, splat(simd, 4.), x, c) - c, x),
372        );
373    let rintx = x
374        .abs()
375        .simd_gt(splat(simd, D1_52))
376        .select(x, or_sign(simd, (x + c) - c, x));
377
378    let fr = mla(simd, splat(simd, -0.25), rint4x, x);
379    let vi = trunci(simd, mla(simd, splat(simd, -4.), rintx, rint4x));
380    (fr, vi)
381}
382
383/// Reduces `a` modulo `pi/2` using the Payne–Hanek tables.
384#[inline(always)]
385pub fn rempi<S: Simd>(simd: S, mut a: F64s<S>) -> (DD<S>, I64s<S>) {
386    let mut ex = ilogb2k(simd, a);
387    ex -= splat_int(simd, 55);
388    let mut q = ex
389        .simd_gt(splat_int(simd, 700 - 55))
390        .select(splat_int(simd, -1), splat_int(simd, 0))
391        & splat_int(simd, -64);
392    a = ldexp3k(simd, a, q);
393    ex = !(ex >> 31) & ex;
394    ex <<= 2;
395    let mut x = mul_as_doubled(a, from_slice_offset(simd, REMPITABDP.as_slice(), ex));
396    let (did, dii) = rempisub(simd, x.0);
397    q = dii;
398    x.0 = did;
399    x = normalize(x);
400    let mut y = mul_as_doubled(a, from_slice_offset(simd, &REMPITABDP[1..], ex));
401    x += y;
402    let (did, dii) = rempisub(simd, x.0);
403    q += dii;
404    x.0 = did;
405    x = normalize(x);
406    y = DD::new(
407        from_slice_offset(simd, &REMPITABDP[2..], ex),
408        from_slice_offset(simd, &REMPITABDP[3..], ex),
409    );
410    y = mul_v(y, a);
411    x += y;
412    x = normalize(x);
413    x = mul_v(x, splat(simd, D_PI.0 * 2.)) + mul_v(x, splat(simd, D_PI.1 * 2.));
414    let o = a.abs().simd_lt(splat(simd, 0.7));
415    x = DD::new(
416        o.select(a, x.0),
417        from_bits(
418            simd,
419            o.select(splat_bits(simd, 0), splat_bits(simd, u64::MAX)) & to_bits(simd, x.1),
420        ),
421    );
422    (x, q)
423}
424
425/// Gathers one lane per element of `vi` from `table`.
426#[inline(always)]
427pub fn from_slice_offset<S: Simd>(simd: S, table: &[f64], vi: I64s<S>) -> F64s<S> {
428    let idx = vi.to_array();
429    let mut values = F64s::<S>::splat(simd, 0.0).to_array();
430    for (slot, &i) in values.as_mut().iter_mut().zip(idx.as_ref().iter()) {
431        // Upstream reads through an unchecked gather, so an infinite argument, whose exponent
432        // overflows the Payne-Hanek index, reads past the end of the table there. Only the
433        // in-range part is meaningful and the caller masks the result for non-finite arguments,
434        // so clamping keeps the in-domain values untouched.
435        *slot = table[(i as usize).min(table.len() - 1)];
436    }
437    F64s::<S>::load_array(simd, values)
438}
439
440/// `x` rounded to an integer towards zero, for a non-negative `x` that is already integral.
441#[inline(always)]
442fn trunc_positive<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
443    let mut fr = mla(
444        simd,
445        splat(simd, -D1_31),
446        trunci(simd, x * splat(simd, 1.0 / D1_31)).to_float::<F64s<S>>(),
447        x,
448    );
449    fr -= trunci(simd, fr).to_float::<F64s<S>>();
450    x.abs().simd_ge(splat(simd, D1_52)).select(x, x - fr)
451}
452
453/// `x` with its last mantissa bit cleared, used by `toward0`.
454#[inline(always)]
455fn toward0<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
456    let t = from_bits(
457        simd,
458        to_bits(simd, x) + int_to_bits(simd, splat_int(simd, -1)),
459    );
460    x.simd_eq(splat(simd, 0.)).select(splat(simd, 0.), t)
461}
462
463/// Floating-point remainder, truncated towards zero.
464///
465/// The error bound of the returned value is `0.5 ULP`.
466#[inline(always)]
467pub fn fmod<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
468    let n = x.abs();
469    let d = y.abs();
470    let s = splat(simd, 1.);
471    let o = d.simd_lt(splat(simd, f64::MIN_POSITIVE));
472    let n = o.select(n * splat(simd, D1_54), n);
473    let d = o.select(d * splat(simd, D1_54), d);
474    let s = o.select(s * splat(simd, 1. / D1_54), s);
475    let rd = toward0(simd, splat(simd, 1.) / d);
476    let mut r: DD<S> = DD::new(n, splat(simd, 0.));
477
478    for _ in 0..21 {
479        // ceil(log2(DBL_MAX) / 52) iterations are sufficient.
480        let r0: F64s<S> = r.0;
481        let mut q = trunc_positive(simd, toward0(simd, r0) * rd);
482        // The product above is rounded up on the fused path, so the quotient has to be made
483        // conservative before the loop relies on it; upstream clears the low bit here for exactly
484        // that reason, and without it the loop can overshoot and return a negative remainder.
485        q = from_bits(simd, to_bits(simd, q) & splat_bits(simd, u64::MAX - 1));
486        let big = (splat(simd, 3.) * d).simd_gt(r0) & r0.simd_ge(d);
487        q = big.select(splat(simd, 2.), q);
488        let mid = (d + d).simd_gt(r0) & r0.simd_ge(d);
489        q = mid.select(splat(simd, 1.), q);
490        r = normalize(r + mul_v(-mul_as_doubled(splat(simd, 1.), q), d));
491        if r.0.simd_lt(d).all_true() {
492            break;
493        }
494    }
495
496    let mut ret = r.0 * s;
497    ret = r.0.simd_eq(d).select(splat(simd, 0.), ret);
498
499    ret = mul_sign(simd, ret, x);
500
501    ret = n.simd_lt(d).select(x, ret);
502    d.simd_eq(splat(simd, 0.))
503        .select(splat(simd, f64::NAN), ret)
504}
505
506/// IEEE remainder, rounded to nearest even quotient.
507///
508/// The error bound of the returned value is `0.5 ULP`.
509#[inline(always)]
510pub fn remainder<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
511    let mut n = x.abs();
512    let mut d = y.abs();
513    let mut s = splat(simd, 1.);
514    let o = d.simd_lt(splat(simd, f64::MIN_POSITIVE * 2.));
515    n = o.select(n * splat(simd, D1_54), n);
516    d = o.select(d * splat(simd, D1_54), d);
517    s = o.select(s * splat(simd, 1. / D1_54), s);
518    let rd = splat(simd, 1.) / d;
519    let mut r: DD<S> = DD::new(n, splat(simd, 0.));
520    let mut qisodd = S::mask64s::splat(simd, false);
521
522    for _ in 0..21 {
523        // ceil(log2(DBL_MAX) / 52) iterations are sufficient.
524        let mut q = rint(simd, r.0 * rd);
525        // As in `fmod`: the fused product can round up, and `q` has to stay conservative.
526        q = from_bits(simd, to_bits(simd, q) & splat_bits(simd, u64::MAX - 1));
527        q =
528            r.0.abs()
529                .simd_lt(d * splat(simd, 1.5))
530                .select(mul_sign(simd, splat(simd, 1.), r.0), q);
531        q = (r.0.abs().simd_lt(d * splat(simd, 0.5))
532            | (!qisodd & r.0.abs().simd_eq(d * splat(simd, 0.5))))
533        .select(splat(simd, 0.), q);
534        if q.simd_eq(splat(simd, 0.)).all_true() {
535            break;
536        }
537        q = (q * (-d))
538            .is_infinite()
539            .select(q - mul_sign(simd, splat(simd, 1.), r.0), q);
540        qisodd ^= is_odd(simd, q);
541        r = normalize(r + mul_v(mul_as_doubled(q, splat(simd, -1.)), d));
542    }
543
544    let mut ret = r.0 * s;
545    ret = mul_sign(simd, ret, x);
546    ret = y
547        .is_infinite()
548        .select(x.is_infinite().select(splat(simd, f64::NAN), x), ret);
549    d.simd_eq(splat(simd, 0.))
550        .select(splat(simd, f64::NAN), ret)
551}
552
553/// Splits `x` into its fractional and integral parts.
554///
555/// The returned pair is `(fractional, integral)`.
556#[inline(always)]
557pub fn modf<S: Simd>(simd: S, x: F64s<S>) -> (F64s<S>, F64s<S>) {
558    let mut fr =
559        x - splat(simd, D1_31) * trunci(simd, x * splat(simd, 1.0 / D1_31)).to_float::<F64s<S>>();
560    fr -= trunci(simd, fr).to_float::<F64s<S>>();
561    fr = x
562        .abs()
563        .simd_gt(splat(simd, D1_52))
564        .select(splat(simd, 0.), fr);
565
566    (copy_sign(simd, fr, x), copy_sign(simd, x - fr, x))
567}
568
569/// Fractional component of a floating-point number, as returned by `frexp`.
570#[inline(always)]
571pub fn frfrexp<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
572    let x = x
573        .abs()
574        .simd_lt(splat(simd, f64::MIN_POSITIVE))
575        .select(x * splat(simd, D1_63), x);
576
577    let mut xm = to_bits(simd, x);
578    xm &= splat_bits(simd, 0x_800f_ffff_ffff_ffff);
579    xm |= splat_bits(simd, 0x_3fe0_0000_u64 << 32);
580
581    let ret = from_bits(simd, xm);
582
583    let ret = x
584        .is_infinite()
585        .select(mul_sign(simd, splat(simd, f64::INFINITY), x), ret);
586    x.simd_eq(splat(simd, 0.)).select(x, ret)
587}
588
589/// Exponent of a floating-point number, as returned by `frexp`.
590#[inline(always)]
591pub fn expfrexp<S: Simd>(simd: S, x: F64s<S>) -> I64s<S> {
592    let x = x
593        .abs()
594        .simd_lt(splat(simd, f64::MIN_POSITIVE))
595        .select(x * splat(simd, D1_63), x);
596
597    let ret = (bits_to_int(simd, to_bits(simd, x) >> 52) & splat_int(simd, 0x7ff))
598        - splat_int(simd, 0x3fe);
599
600    (x.simd_eq(splat(simd, 0.)) | x.is_nan() | x.is_infinite()).select(splat_int(simd, 0), ret)
601}
602
603/// Fused multiply-add: `x * y + z` with a single rounding.
604///
605/// The error bound of the returned value is `max(0.500_01 ULP, f64::MIN_POSITIVE)`. Upstream
606/// returns infinity with the correct sign when the magnitude of the exact result exceeds
607/// `1e+300`, and this port keeps that behaviour.
608#[inline(always)]
609pub fn fma<S: Simd>(_simd: S, x: F64s<S>, y: F64s<S>, z: F64s<S>) -> F64s<S> {
610    // `mul_add_precise` rounds the exact product-plus-add once on every level, which is exactly
611    // what this function promises; upstream emulates it by hand only because `mul_add` is not
612    // guaranteed to be fused.
613    x.mul_add_precise(y, z)
614}