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