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