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