Skip to main content

elem_simd_math/f32x/
mod.rs

1//! Vector helpers for the `f32` kernels.
2//!
3//! Ported from `sleef-rs` rev `3671d46` (`src/f32x.rs`, `src/f32x/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>::f32s` 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 fast;
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::{F32s, I32s, M32s, U32s};
26use crate::constants::*;
27use crate::doubled::f32::{
28    add_as_doubled, mul_as_doubled, mul_v, normalize, recip_as_doubled, scale, DD,
29};
30
31use constants::REMPITABSP;
32pub use constants::{splat, splat_bits, splat_int};
33
34/// Reinterprets an `f32` vector as its bit pattern.
35#[inline(always)]
36pub fn to_bits<S: Simd>(_simd: S, x: F32s<S>) -> U32s<S> {
37    x.bitcast()
38}
39
40/// Reinterprets a bit pattern as an `f32` vector.
41#[inline(always)]
42pub fn from_bits<S: Simd>(_simd: S, x: U32s<S>) -> F32s<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: U32s<S>) -> I32s<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: I32s<S>) -> U32s<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: F32s<S>) -> U32s<S> {
61    to_bits(simd, x) & splat_bits(simd, (-0.0f32).to_bits())
62}
63
64/// `1.0` carrying the sign of `x`.
65#[inline(always)]
66pub fn sign<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
67    from_bits(simd, sign_bit(simd, x) ^ splat_bits(simd, 1.0f32.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: F32s<S>, y: F32s<S>) -> F32s<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: F32s<S>, y: F32s<S>) -> F32s<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: F32s<S>, y: F32s<S>) -> F32s<S> {
85    from_bits(
86        simd,
87        (splat_bits(simd, !(-0.0f32).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: F32s<S>) -> M32s<S> {
94    to_bits(simd, x).simd_eq(splat_bits(simd, (-0.0f32).to_bits()))
95}
96
97/// Round to nearest integer, ties to even.
98#[inline(always)]
99pub fn rint<S: Simd>(simd: S, d: F32s<S>) -> F32s<S> {
100    let c = mul_sign(simd, splat(simd, F1_23), d);
101    d.abs()
102        .simd_gt(splat(simd, F1_23))
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: F32s<S>) -> F32s<S> {
109    let fr = x - trunci(simd, x).to_float::<F32s<S>>();
110    (x.is_infinite() | x.abs().simd_ge(splat(simd, F1_23))).select(x, copy_sign(simd, x - fr, x))
111}
112
113/// Round towards negative infinity.
114#[inline(always)]
115pub fn floor<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
116    let fr = x - trunci(simd, x).to_float::<F32s<S>>();
117    let fr = fr.simd_lt(splat(simd, 0.)).select(fr + splat(simd, 1.), fr);
118    (x.is_infinite() | x.abs().simd_ge(splat(simd, F1_23))).select(x, copy_sign(simd, x - fr, x))
119}
120
121/// Round towards positive infinity.
122#[inline(always)]
123pub fn ceil<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
124    let fr = x - trunci(simd, x).to_float::<F32s<S>>();
125    let fr = fr.simd_le(splat(simd, 0.)).select(fr, fr - splat(simd, 1.));
126    (x.is_infinite() | x.abs().simd_ge(splat(simd, F1_23))).select(x, copy_sign(simd, x - fr, x))
127}
128
129/// Round away from zero.
130#[inline(always)]
131pub fn round<S: Simd>(simd: S, d: F32s<S>) -> F32s<S> {
132    let mut x = d + splat(simd, 0.5);
133    let fr = x - trunci(simd, x).to_float::<F32s<S>>();
134    x = (x.simd_le(splat(simd, 0.)) & fr.simd_eq(splat(simd, 0.))).select(x - splat(simd, 1.), x);
135    let fr = fr.simd_lt(splat(simd, 0.)).select(fr + splat(simd, 1.), fr);
136    x = d
137        .simd_eq(splat(simd, 0.499_999_970_197_677_612_3))
138        .select(splat(simd, 0.), x);
139    (d.is_infinite() | d.abs().simd_ge(splat(simd, F1_23))).select(d, copy_sign(simd, x - fr, d))
140}
141
142/// Truncate to an integer vector.
143#[inline(always)]
144pub fn trunci<S: Simd>(_simd: S, x: F32s<S>) -> I32s<S> {
145    x.trunc().to_int::<I32s<S>>()
146}
147
148/// Round to an integer vector, ties to even.
149#[inline(always)]
150pub fn roundi<S: Simd>(_simd: S, x: F32s<S>) -> I32s<S> {
151    x.round_ties_even().to_int::<I32s<S>>()
152}
153
154/// Whether `x` is integral.
155///
156/// Upstream writes this as `self.trunc().simd_eq(self)`, where `trunc` is its round-to-zero
157/// *conversion*; this port uses the [`trunc`] helper instead. The two agree except for
158/// `|x| >= 2^31`, where upstream's conversion of an out-of-range value says nothing about
159/// integrality while [`trunc`] returns `x` itself, which is the mathematically correct answer
160/// (every `f32` at or above `2^23` is an integer).
161#[inline(always)]
162pub fn is_integer<S: Simd>(simd: S, x: F32s<S>) -> M32s<S> {
163    trunc(simd, x).simd_eq(x)
164}
165
166/// Whether `x` is an odd integer.
167#[inline(always)]
168pub fn is_odd<S: Simd>(simd: S, x: F32s<S>) -> M32s<S> {
169    (trunci(simd, x) & splat_int(simd, 1)).simd_eq(splat_int(simd, 1))
170        & x.abs().simd_lt(splat(simd, F1_24))
171}
172
173/// Selects between two scalars broadcast to vectors.
174#[inline(always)]
175pub fn select_splat<S: Simd>(simd: S, mask: M32s<S>, if_true: f32, if_false: f32) -> F32s<S> {
176    mask.select(splat(simd, if_true), splat(simd, if_false))
177}
178
179/// Selects between three scalar values using two masks.
180#[inline(always)]
181pub fn select3<S: Simd>(simd: S, o0: M32s<S>, o1: M32s<S>, d0: f32, d1: f32, d2: f32) -> F32s<S> {
182    o0.select(splat(simd, d0), select_splat(simd, o1, d1, d2))
183}
184
185/// Selects between four scalar values using three masks.
186#[allow(clippy::too_many_arguments)]
187#[inline(always)]
188pub fn select4<S: Simd>(
189    simd: S,
190    o0: M32s<S>,
191    o1: M32s<S>,
192    o2: M32s<S>,
193    d0: f32,
194    d1: f32,
195    d2: f32,
196    d3: f32,
197) -> F32s<S> {
198    o0.select(
199        splat(simd, d0),
200        o1.select(splat(simd, d1), select_splat(simd, o2, d2, d3)),
201    )
202}
203
204/// Selects between two double-double values using a mask.
205#[inline(always)]
206pub fn select_doubled<S: Simd>(_simd: S, mask: M32s<S>, l: DD<S>, r: DD<S>) -> DD<S> {
207    DD::new(mask.select(l.0, r.0), mask.select(l.1, r.1))
208}
209
210/// `2^q` for an integer vector `q`.
211#[inline(always)]
212pub fn pow2i<S: Simd>(simd: S, q: I32s<S>) -> F32s<S> {
213    from_bits(simd, int_to_bits(simd, (splat_int(simd, 0x7f) + q) << 23))
214}
215
216/// `d * 2^e`, split into two half-range multiplications.
217#[inline(always)]
218pub fn ldexp2k<S: Simd>(simd: S, d: F32s<S>, e: I32s<S>) -> F32s<S> {
219    let e1 = e >> 1;
220    d * pow2i(simd, e1) * pow2i(simd, e - e1)
221}
222
223/// `d * 2^q` by adding directly to the exponent field.
224#[inline(always)]
225pub fn ldexp3k<S: Simd>(simd: S, d: F32s<S>, q: I32s<S>) -> F32s<S> {
226    from_bits(simd, to_bits(simd, d) + int_to_bits(simd, q << 23))
227}
228
229/// The exponent of `d`, without subnormal handling.
230#[inline(always)]
231pub fn ilogb2k<S: Simd>(simd: S, d: F32s<S>) -> I32s<S> {
232    bits_to_int(simd, (to_bits(simd, d) >> 23) & splat_bits(simd, 0xff)) - splat_int(simd, 0x7f)
233}
234
235/// The exponent of `d`, scaling subnormals into range first.
236#[inline(always)]
237pub fn ilogbk<S: Simd>(simd: S, mut d: F32s<S>) -> I32s<S> {
238    let o = d.simd_lt(splat(simd, 5.421_010_862_427_522_e-20));
239    d = o.select(splat(simd, 1.844_674_407_370_955_2_e19) * d, d);
240    let q = bits_to_int(simd, (to_bits(simd, d) >> 23) & splat_bits(simd, 0xff));
241    q - o.select(splat_int(simd, 64 + 0x7f), splat_int(simd, 0x7f))
242}
243
244/// `x * 2^q` over the full exponent range, including subnormals.
245#[inline(always)]
246pub fn ldexpk<S: Simd>(simd: S, x: F32s<S>, q: I32s<S>) -> F32s<S> {
247    let mut m = q >> 31;
248    m = (((m + q) >> 6) - m) << 4;
249    let q = q - (m << 2);
250    m = splat_int(simd, 0x7f) + m;
251    m = (!splat_int(simd, 0).simd_gt(m)).select(splat_int(simd, -1), splat_int(simd, 0)) & m;
252    m = m
253        .simd_gt(splat_int(simd, 0xff))
254        .select(splat_int(simd, 0xff), m);
255    let y = from_bits(simd, int_to_bits(simd, m) << 23);
256    // Upstream multiplies by `u*u*u*u` and only then by the rest; the grouping is observable
257    // when the intermediate overflows, so it is kept.
258    x * (y * y * y * y) * pow2i(simd, q)
259}
260
261/// Multiply by an integral power of two.
262#[inline(always)]
263pub fn ldexp<S: Simd>(simd: S, x: F32s<S>, q: I32s<S>) -> F32s<S> {
264    ldexpk(simd, x, q)
265}
266
267/// The exponent of `d` as an integer vector.
268#[inline(always)]
269pub fn ilogb<S: Simd>(simd: S, d: F32s<S>) -> I32s<S> {
270    let mut e = ilogbk(simd, d.abs());
271    e = d
272        .simd_eq(splat(simd, 0.))
273        .select(splat_int(simd, SLEEF_FP_ILOGB0), e);
274    e = d.is_nan().select(splat_int(simd, SLEEF_FP_ILOGBNAN), e);
275    d.is_infinite().select(splat_int(simd, i32::MAX), e)
276}
277
278/// The value of `m` carrying the sign of an infinite `d`, and zero where `d` is finite.
279#[inline(always)]
280pub fn visinf2<S: Simd>(simd: S, d: F32s<S>, m: F32s<S>) -> F32s<S> {
281    from_bits(
282        simd,
283        d.is_infinite()
284            .select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
285            & ((to_bits(simd, d) & sign_bit(simd, d)) | to_bits(simd, m)),
286    )
287}
288
289/// Maximum, treating a NaN in `y` as `x`.
290#[inline(always)]
291pub fn fmax<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
292    let _ = simd;
293    y.is_nan().select(x, x.max(y))
294}
295
296/// Minimum, treating a NaN in `y` as `x`.
297#[inline(always)]
298pub fn fmin<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
299    let _ = simd;
300    y.is_nan().select(x, x.min(y))
301}
302
303/// Positive difference.
304#[inline(always)]
305pub fn fdim<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
306    let ret = x - y;
307    (ret.simd_lt(splat(simd, 0.)) | x.simd_eq(y)).select(splat(simd, 0.), ret)
308}
309
310/// `v + d`, matching upstream's `F32x + Doubled<F32x>`.
311#[inline(always)]
312pub fn v_add_dd<S: Simd>(_simd: S, v: F32s<S>, d: DD<S>) -> DD<S> {
313    let t = add_as_doubled::<S>(v, d.0);
314    DD::new(t.0, t.1 + d.1)
315}
316
317/// Absolute value.
318#[inline(always)]
319pub fn fabs<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
320    let _ = simd;
321    x.abs()
322}
323
324/// The next representable value after `x` in the direction of `y`.
325#[inline(always)]
326pub fn nextafter<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
327    let x = x
328        .simd_eq(splat(simd, 0.))
329        .select(mul_sign(simd, splat(simd, 0.), y), x);
330    let xi2 = bits_to_int(simd, to_bits(simd, x));
331    let c = x.is_sign_negative() ^ y.simd_ge(x);
332
333    let xi2 = c.select(-(xi2 ^ splat_int(simd, i32::MIN)), xi2);
334    let xi2 = x.simd_ne(y).select(xi2 - splat_int(simd, 1), xi2);
335    let xi2 = c.select(-(xi2 ^ splat_int(simd, i32::MIN)), xi2);
336
337    let mut ret = from_bits(simd, int_to_bits(simd, xi2));
338
339    ret = (ret.simd_eq(splat(simd, 0.)) & x.simd_ne(splat(simd, 0.)))
340        .select(mul_sign(simd, splat(simd, 0.), x), ret);
341    ret = (x.simd_eq(splat(simd, 0.)) & y.simd_eq(splat(simd, 0.))).select(y, ret);
342    (x.is_nan() | y.is_nan()).select(splat(simd, f32::NAN), ret)
343}
344
345/// `sqrt(x)` as a double-double.
346#[inline(always)]
347pub fn sqrt_as_doubled<S: Simd>(simd: S, x: F32s<S>) -> DD<S> {
348    let t = x.sqrt();
349    scale(
350        (DD::new(x, splat(simd, 0.0)) + mul_as_doubled(t, t)) * recip_as_doubled(t),
351        splat(simd, 0.5),
352    )
353}
354
355/// Removes the integer part of `4 * x`, returning the fraction and the integer part.
356#[inline(always)]
357pub fn rempisub<S: Simd>(simd: S, x: F32s<S>) -> (F32s<S>, I32s<S>) {
358    let c = mul_sign(simd, splat(simd, F1_23), x);
359    let rint4x = (splat(simd, 4.) * x)
360        .abs()
361        .simd_gt(splat(simd, F1_23))
362        .select(
363            splat(simd, 4.) * x,
364            or_sign(simd, mla(simd, splat(simd, 4.), x, c) - c, x),
365        );
366    let rintx = x
367        .abs()
368        .simd_gt(splat(simd, F1_23))
369        .select(x, or_sign(simd, (x + c) - c, x));
370
371    let fr = mla(simd, splat(simd, -0.25), rint4x, x);
372    let vi = trunci(simd, mla(simd, splat(simd, -4.), rintx, rint4x));
373    (fr, vi)
374}
375
376/// Reduces `a` modulo `pi/2` using the Payne–Hanek tables.
377#[inline(always)]
378pub fn rempi<S: Simd>(simd: S, mut a: F32s<S>) -> (DD<S>, I32s<S>) {
379    let mut ex = ilogb2k(simd, a);
380    ex -= splat_int(simd, 25);
381    let mut q = ex
382        .simd_gt(splat_int(simd, 90 - 25))
383        .select(splat_int(simd, -1), splat_int(simd, 0))
384        & splat_int(simd, -64);
385    a = ldexp3k(simd, a, q);
386    ex = !(ex >> 31) & ex;
387    ex <<= 2;
388    let mut x = mul_as_doubled(a, from_slice_offset(simd, REMPITABSP.as_slice(), ex));
389    let (did, dii) = rempisub(simd, x.0);
390    q = dii;
391    x.0 = did;
392    x = normalize(x);
393    let mut y = mul_as_doubled(a, from_slice_offset(simd, &REMPITABSP[1..], ex));
394    x += y;
395    let (did, dii) = rempisub(simd, x.0);
396    q += dii;
397    x.0 = did;
398    x = normalize(x);
399    y = DD::new(
400        from_slice_offset(simd, &REMPITABSP[2..], ex),
401        from_slice_offset(simd, &REMPITABSP[3..], ex),
402    );
403    y = mul_v(y, a);
404    x += y;
405    x = normalize(x);
406    x = mul_v(x, splat(simd, D_PI_F.0 * 2.)) + mul_v(x, splat(simd, D_PI_F.1 * 2.));
407    let o = a.abs().simd_lt(splat(simd, 0.7));
408    x = DD::new(
409        o.select(a, x.0),
410        from_bits(
411            simd,
412            o.select(splat_bits(simd, 0), splat_bits(simd, u32::MAX)) & to_bits(simd, x.1),
413        ),
414    );
415    (x, q)
416}
417
418/// Gathers one lane per element of `vi` from `table`.
419#[inline(always)]
420pub fn from_slice_offset<S: Simd>(simd: S, table: &[f32], vi: I32s<S>) -> F32s<S> {
421    let idx = vi.to_array();
422    let mut values = F32s::<S>::splat(simd, 0.0).to_array();
423    for (slot, &i) in values.as_mut().iter_mut().zip(idx.as_ref().iter()) {
424        // Upstream reads through an unchecked gather, so an infinite argument, whose exponent
425        // overflows the Payne-Hanek index, reads past the end of the table there. Only the
426        // in-range part is meaningful and the caller masks the result for non-finite arguments,
427        // so clamping keeps the in-domain values untouched.
428        *slot = table[(i as usize).min(table.len() - 1)];
429    }
430    F32s::<S>::load_array(simd, values)
431}
432
433/// `x` rounded to an integer towards zero, for a non-negative `x` that is already integral.
434#[inline(always)]
435fn trunc_positive<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
436    let fr = x - trunci(simd, x).to_float::<F32s<S>>();
437    x.abs().simd_ge(splat(simd, F1_23)).select(x, x - fr)
438}
439
440/// `x` with its last mantissa bit cleared, used by `toward0`.
441#[inline(always)]
442fn toward0<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
443    let t = from_bits(
444        simd,
445        to_bits(simd, x) + int_to_bits(simd, splat_int(simd, -1)),
446    );
447    x.simd_eq(splat(simd, 0.)).select(splat(simd, 0.), t)
448}
449
450/// Floating-point remainder, truncated towards zero.
451///
452/// The error bound of the returned value is `0.5 ULP`.
453#[inline(always)]
454pub fn fmod<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
455    let n = x.abs();
456    let d = y.abs();
457    let s = splat(simd, 1.);
458    let o = d.simd_lt(splat(simd, f32::MIN_POSITIVE));
459    let n = o.select(n * splat(simd, F1_25), n);
460    let d = o.select(d * splat(simd, F1_25), d);
461    let s = o.select(s * splat(simd, 1. / F1_25), s);
462    let rde = toward0(simd, splat(simd, 1.) / d);
463    let mut r: DD<S> = DD::new(n, splat(simd, 0.));
464
465    for _ in 0..8 {
466        // ceil(log2(FLT_MAX) / 22) + 1 iterations are sufficient.
467        let r0: F32s<S> = r.0;
468        let mut q = trunc_positive(simd, toward0(simd, r0) * rde);
469        let big = (splat(simd, 3.) * d).simd_gt(r0) & r0.simd_ge(d);
470        q = big.select(splat(simd, 2.), q);
471        let mid = (d + d).simd_gt(r0) & r0.simd_ge(d);
472        q = mid.select(splat(simd, 1.), q);
473        r = normalize(r + mul_v(-mul_as_doubled(splat(simd, 1.), q), d));
474        if r.0.simd_lt(d).all_true() {
475            break;
476        }
477    }
478
479    let mut ret = r.0 * s;
480    ret = r.0.simd_eq(d).select(splat(simd, 0.), ret);
481
482    ret = mul_sign(simd, ret, x);
483
484    ret = n.simd_lt(d).select(x, ret);
485    d.simd_eq(splat(simd, 0.))
486        .select(splat(simd, f32::NAN), ret)
487}
488
489/// IEEE remainder, rounded to nearest even quotient.
490///
491/// The error bound of the returned value is `0.5 ULP`.
492#[inline(always)]
493pub fn remainder<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
494    let mut n = x.abs();
495    let mut d = y.abs();
496    let mut s = splat(simd, 1.);
497    let o = d.simd_lt(splat(simd, f32::MIN_POSITIVE * 2.));
498    n = o.select(n * splat(simd, F1_25), n);
499    d = o.select(d * splat(simd, F1_25), d);
500    s = o.select(s * splat(simd, 1. / F1_25), s);
501    let rd = splat(simd, 1.) / d;
502    let mut r: DD<S> = DD::new(n, splat(simd, 0.));
503    let mut qisodd = S::mask32s::splat(simd, false);
504
505    for _ in 0..8 {
506        // ceil(log2(FLT_MAX) / 22) + 1 iterations are sufficient.
507        let mut q = rint(simd, r.0 * rd);
508        q =
509            r.0.abs()
510                .simd_lt(d * splat(simd, 1.5))
511                .select(mul_sign(simd, splat(simd, 1.), r.0), q);
512        q = (r.0.abs().simd_lt(d * splat(simd, 0.5))
513            | (!qisodd & r.0.abs().simd_eq(d * splat(simd, 0.5))))
514        .select(splat(simd, 0.), q);
515        if q.simd_eq(splat(simd, 0.)).all_true() {
516            break;
517        }
518        q = (q * (-d))
519            .is_infinite()
520            .select(q - mul_sign(simd, splat(simd, 1.), r.0), q);
521        qisodd ^= is_odd(simd, q);
522        r = normalize(r + mul_v(mul_as_doubled(q, splat(simd, -1.)), d));
523    }
524
525    let mut ret = r.0 * s;
526    ret = mul_sign(simd, ret, x);
527    ret = y
528        .is_infinite()
529        .select(x.is_infinite().select(splat(simd, f32::NAN), x), ret);
530    d.simd_eq(splat(simd, 0.))
531        .select(splat(simd, f32::NAN), ret)
532}
533
534/// Splits `x` into its fractional and integral parts.
535///
536/// The returned pair is `(fractional, integral)`.
537#[inline(always)]
538pub fn modf<S: Simd>(simd: S, x: F32s<S>) -> (F32s<S>, F32s<S>) {
539    let fr = x - trunci(simd, x).to_float::<F32s<S>>();
540    let fr = x
541        .abs()
542        .simd_gt(splat(simd, F1_23))
543        .select(splat(simd, 0.), fr);
544
545    (copy_sign(simd, fr, x), copy_sign(simd, x - fr, x))
546}
547
548/// Fractional component of a floating-point number, as returned by `frexp`.
549#[inline(always)]
550pub fn frfrexp<S: Simd>(simd: S, x: F32s<S>) -> F32s<S> {
551    let x = x
552        .abs()
553        .simd_lt(splat(simd, f32::MIN_POSITIVE))
554        .select(x * splat(simd, F1_32), x);
555
556    let mut xm = to_bits(simd, x);
557    xm &= splat_bits(simd, 0x_807f_ffff);
558    xm |= splat_bits(simd, 0x_3f00_0000);
559
560    let ret = from_bits(simd, xm);
561
562    let ret = x
563        .is_infinite()
564        .select(mul_sign(simd, splat(simd, f32::INFINITY), x), ret);
565    x.simd_eq(splat(simd, 0.)).select(x, ret)
566}
567
568/// Exponent of a floating-point number, as returned by `frexp`.
569///
570/// Upstream's body is commented out and the function returns zero for every input; this port keeps
571/// that stub rather than replacing it with an unported implementation. No ported kernel calls it.
572#[inline(always)]
573pub fn expfrexp<S: Simd>(simd: S, _x: F32s<S>) -> I32s<S> {
574    splat_int(simd, 0)
575}
576
577/// Fused multiply-add: `x * y + z` with a single rounding.
578///
579/// The error bound of the returned value is `max(0.500_01 ULP, f32::MIN_POSITIVE)`. Upstream's
580/// fallback returns infinity with the correct sign once the magnitude of the exact result exceeds
581/// `1e+33`; this port evaluates the exact product-plus-add with a single rounding instead, which
582/// stays finite wherever the exact result is representable.
583#[inline(always)]
584pub fn fma<S: Simd>(_simd: S, x: F32s<S>, y: F32s<S>, z: F32s<S>) -> F32s<S> {
585    x.mul_add_precise(y, z)
586}