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