elem-simd-math 0.1.3

Vectorized elementwise math kernels for f32/f64 over stable-Rust SIMD (port of SLEEF via sleef-rs, on fearless_simd).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! `f32` kernels with a 0.5 ULP error bound (SLEEF's `u05` tier).
//!
//! Ported from `sleef-rs` rev `3671d46` (`src/f32x/u05.rs`). The private `sinpifk` / `cospifk`
//! helpers live in upstream's `src/f32x.rs`; they are carried here because only this tier uses
//! them.

use super::*;
use crate::doubled::f32::{add_v, mul_as_doubled, mul_v, square, DD};

/// `sin(pi * d)` for a reduced argument, kept as a double-double.
#[inline(always)]
fn sinpifk<S: Simd>(simd: S, d: F32s<S>) -> DD<S> {
    let u = d * splat(simd, 4.);
    let mut q = trunci(simd, u);
    q = (q
        + (bits_to_int(simd, int_to_bits(simd, q) >> splat_bits(simd, 31)) ^ splat_int(simd, 1)))
        & splat_int(simd, !1);
    let o = (q & splat_int(simd, 2)).simd_eq(splat_int(simd, 2));

    let s = u - q.to_float::<F32s<S>>();
    let t = s;
    let s = s * s;
    let s2 = mul_as_doubled::<S>(t, t);

    let mut u = mla(
        simd,
        select_splat(simd, o, -0.243_061_180_1_e-7, 0.309_384_205_4_e-6),
        s,
        select_splat(simd, o, 0.359_057_708_e-5, -0.365_730_738_8_e-4),
    );
    u = mla(
        simd,
        u,
        s,
        select_splat(simd, o, -0.325_991_772_1_e-3, 0.249_039_358_5_e-2),
    );

    let mut x = v_add_dd(
        simd,
        u * s,
        select_doubled(
            simd,
            o,
            DD::new(
                splat(simd, 0.015_854_343_771_934_509_277),
                splat(simd, 4.494_005_135_403_224_281_1_e-10),
            ),
            DD::new(
                splat(simd, -0.080_745_510_756_969_451_904),
                splat(simd, -1.337_366_533_907_693_625_8_e-9),
            ),
        ),
    );
    x = s2 * x
        + select_doubled(
            simd,
            o,
            DD::new(
                splat(simd, -0.308_425_128_459_930_419_92),
                splat(simd, -9.072_833_903_073_392_227_7_e-9),
            ),
            DD::new(
                splat(simd, 0.785_398_185_253_143_310_55),
                splat(simd, -2.185_733_861_756_648_485_5_e-8),
            ),
        );

    x *= select_doubled(simd, o, s2, DD::new(t, splat(simd, 0.)));
    x = select_doubled(simd, o, add_v(x, splat(simd, 1.)), x);

    let o = (q & splat_int(simd, 4)).simd_eq(splat_int(simd, 4));
    x.0 = from_bits(
        simd,
        o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            & splat_bits(simd, (-0.0f32).to_bits())
            ^ to_bits(simd, x.0),
    );
    x.1 = from_bits(
        simd,
        o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            & splat_bits(simd, (-0.0f32).to_bits())
            ^ to_bits(simd, x.1),
    );

    x
}

/// Evaluates `sin(pi * d)` and `cos(pi * d)` for a given `d` simultaneously.
///
/// Evaluates the sine and cosine functions of `pi * d` at a time, and stores the two values in a
/// tuple. The error bound of the returned values is `max(0.506 ULP, f32::MIN_POSITIVE)` for `d`
/// in `[-1e+7, 1e+7]`. If `d` is a finite value out of this range, an arbitrary value within
/// `[-1, 1]` is returned. If `d` is a `NaN` or infinity, a `NaN` is returned.
#[inline(always)]
pub fn sincospi<S: Simd>(simd: S, d: F32s<S>) -> (F32s<S>, F32s<S>) {
    let u = d * splat(simd, 4.);
    let mut q = trunci(simd, u);
    q = (q
        + (bits_to_int(simd, int_to_bits(simd, q) >> splat_bits(simd, 31)) ^ splat_int(simd, 1)))
        & splat_int(simd, !1);
    let s = u - q.to_float::<F32s<S>>();

    let t = s;
    let s = s * s;
    let s2 = mul_as_doubled::<S>(t, t);

    //

    let mut u = splat(simd, 0.309_384_205_4_e-6);
    u = mla(simd, u, s, splat(simd, -0.365_730_738_8_e-4));
    u = mla(simd, u, s, splat(simd, 0.249_039_358_5_e-2));
    let mut x = v_add_dd(
        simd,
        u * s,
        DD::new(
            splat(simd, -0.080_745_510_756_969_451_904),
            splat(simd, -1.337_366_533_907_693_625_8_e-9),
        ),
    );
    x = s2 * x
        + DD::new(
            splat(simd, 0.785_398_185_253_143_310_55),
            splat(simd, -2.185_733_861_756_648_485_5_e-8),
        );

    x = mul_v(x, t);
    let rx = x.0 + x.1;

    let rx = is_neg_zero(simd, d).select(splat(simd, -0.0), rx);

    //

    let mut u = splat(simd, -0.243_061_180_1_e-7);
    u = mla(simd, u, s, splat(simd, 0.359_057_708_e-5));
    u = mla(simd, u, s, splat(simd, -0.325_991_772_1_e-3));
    let mut x = v_add_dd(
        simd,
        u * s,
        DD::new(
            splat(simd, 0.015_854_343_771_934_509_277),
            splat(simd, 4.494_005_135_403_224_281_1_e-10),
        ),
    );
    x = s2 * x
        + DD::new(
            splat(simd, -0.308_425_128_459_930_419_92),
            splat(simd, -9.072_833_903_073_392_227_7_e-9),
        );

    x = add_v(x * s2, splat(simd, 1.));
    let ry = x.0 + x.1;

    //

    let o = (q & splat_int(simd, 2)).simd_eq(splat_int(simd, 0));
    let mut rsin = o.select(rx, ry);
    let mut rcos = o.select(ry, rx);

    let o = (q & splat_int(simd, 4)).simd_eq(splat_int(simd, 4));
    rsin = from_bits(
        simd,
        o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            & splat_bits(simd, (-0.0f32).to_bits())
            ^ to_bits(simd, rsin),
    );

    let o = ((q + splat_int(simd, 2)) & splat_int(simd, 4)).simd_eq(splat_int(simd, 4));
    rcos = from_bits(
        simd,
        o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            & splat_bits(simd, (-0.0f32).to_bits())
            ^ to_bits(simd, rcos),
    );

    let o = d.abs().simd_gt(splat(simd, 1e+7));
    rsin = from_bits(
        simd,
        (!o).select(splat_bits(simd, u32::MAX), splat_bits(simd, 0)) & to_bits(simd, rsin),
    );
    rcos = from_bits(
        simd,
        (!o).select(splat_bits(simd, u32::MAX), splat_bits(simd, 0)) & to_bits(simd, rcos),
    );

    let o = d.is_infinite();
    rsin = from_bits(
        simd,
        o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0)) | to_bits(simd, rsin),
    );
    rcos = from_bits(
        simd,
        o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0)) | to_bits(simd, rcos),
    );

    (rsin, rcos)
}

/// Evaluates `sin(pi * d)` for a given `d`.
///
/// The error bound of the returned value is `max(0.506 ULP, f32::MIN_POSITIVE)` for `d` in
/// `[-1e+7, 1e+7]`. If `d` is a finite value out of this range, an arbitrary value within
/// `[-1, 1]` is returned. If `d` is a `NaN` or infinity, a `NaN` is returned.
#[inline(always)]
pub fn sinpi<S: Simd>(simd: S, d: F32s<S>) -> F32s<S> {
    let x = sinpifk(simd, d);
    let mut r = x.0 + x.1;

    r = is_neg_zero(simd, d).select(splat(simd, -0.0), r);
    r = from_bits(
        simd,
        (!d.abs().simd_gt(splat(simd, TRIGRANGEMAX4_F)))
            .select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            & to_bits(simd, r),
    );
    from_bits(
        simd,
        d.is_infinite()
            .select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            | to_bits(simd, r),
    )
}

/// `cos(pi * d)` for a reduced argument, kept as a double-double.
#[inline(always)]
fn cospifk<S: Simd>(simd: S, d: F32s<S>) -> DD<S> {
    let u = d * splat(simd, 4.);
    let mut q = trunci(simd, u);
    q = (q
        + (bits_to_int(simd, int_to_bits(simd, q) >> splat_bits(simd, 31)) ^ splat_int(simd, 1)))
        & splat_int(simd, !1);
    let o = (q & splat_int(simd, 2)).simd_eq(splat_int(simd, 0));

    let s = u - q.to_float::<F32s<S>>();
    let t = s;
    let s = s * s;
    let s2 = mul_as_doubled::<S>(t, t);

    let mut u = mla(
        simd,
        select_splat(simd, o, -0.243_061_180_1_e-7, 0.309_384_205_4_e-6),
        s,
        select_splat(simd, o, 0.359_057_708_e-5, -0.365_730_738_8_e-4),
    );
    u = mla(
        simd,
        u,
        s,
        select_splat(simd, o, -0.325_991_772_1_e-3, 0.249_039_358_5_e-2),
    );

    let mut x = v_add_dd(
        simd,
        u * s,
        select_doubled(
            simd,
            o,
            DD::new(
                splat(simd, 0.015_854_343_771_934_509_277),
                splat(simd, 4.494_005_135_403_224_281_1_e-10),
            ),
            DD::new(
                splat(simd, -0.080_745_510_756_969_451_904),
                splat(simd, -1.337_366_533_907_693_625_8_e-9),
            ),
        ),
    );
    x = s2 * x
        + select_doubled(
            simd,
            o,
            DD::new(
                splat(simd, -0.308_425_128_459_930_419_92),
                splat(simd, -9.072_833_903_073_392_227_7_e-9),
            ),
            DD::new(
                splat(simd, 0.785_398_185_253_143_310_55),
                splat(simd, -2.185_733_861_756_648_485_5_e-8),
            ),
        );

    x *= select_doubled(simd, o, s2, DD::new(t, splat(simd, 0.)));
    x = select_doubled(simd, o, add_v(x, splat(simd, 1.)), x);

    let o = ((q + splat_int(simd, 2)) & splat_int(simd, 4)).simd_eq(splat_int(simd, 4));
    x = DD::new(
        from_bits(
            simd,
            o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
                & splat_bits(simd, (-0.0f32).to_bits())
                ^ to_bits(simd, x.0),
        ),
        from_bits(
            simd,
            o.select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
                & splat_bits(simd, (-0.0f32).to_bits())
                ^ to_bits(simd, x.1),
        ),
    );

    x
}

/// Evaluates `cos(pi * d)` for a given `d`.
///
/// The error bound of the returned value is `max(0.506 ULP, f32::MIN_POSITIVE)` for `d` in
/// `[-1e+7, 1e+7]`. If `d` is a finite value out of this range, an arbitrary value within
/// `[-1, 1]` is returned. If `d` is a `NaN` or infinity, a `NaN` is returned.
#[inline(always)]
pub fn cospi<S: Simd>(simd: S, d: F32s<S>) -> F32s<S> {
    let x = cospifk(simd, d);
    let r = x.0 + x.1;

    let r = d
        .abs()
        .simd_gt(splat(simd, TRIGRANGEMAX4_F))
        .select(splat(simd, 1.), r);
    from_bits(
        simd,
        d.is_infinite()
            .select(splat_bits(simd, u32::MAX), splat_bits(simd, 0))
            | to_bits(simd, r),
    )
}

/// Square root function.
///
/// The error bound of the returned value is `0.5001 ULP`.
///
/// Upstream selects between two formulations at compile time. This port takes the
/// `cfg!(target_feature = "fma")` branch, whose final correction needs the fused [`mla`];
/// [`crate::f64x::u05::sqrt`] still carries upstream's other branch, and the two files therefore
/// differ in shape at this one function.
#[inline(always)]
pub fn sqrt<S: Simd>(simd: S, d: F32s<S>) -> F32s<S> {
    let d = d.simd_lt(splat(simd, 0.)).select(splat(simd, f32::NAN), d);

    let o = d.simd_lt(splat(simd, 5.293_955_920_339_377_e-23));
    let d = o.select(d * splat(simd, 1.888_946_593_147_858_e+22), d);
    let q = o.select(splat(simd, 7.275_957_614_183_426_e-12), splat(simd, 1.));

    let mut y = from_bits(
        simd,
        int_to_bits(
            simd,
            splat_int(simd, 0x5f3759df)
                - (bits_to_int(simd, to_bits(simd, d)) >> splat_int(simd, 1)),
        ),
    );

    let mut x = d * y;
    let mut w = splat(simd, 0.5) * y;
    y = neg_mul_add(simd, x, w, splat(simd, 0.5));
    x = mla(simd, x, y, x);
    w = mla(simd, w, y, w);
    y = neg_mul_add(simd, x, w, splat(simd, 0.5));
    x = mla(simd, x, y, x);
    w = mla(simd, w, y, w);

    y = neg_mul_add(simd, x, w, splat(simd, 1.5));
    w += w;
    w *= y;
    x = w * d;
    y = mul_sub(simd, w, d, x);
    let mut z = neg_mul_add(simd, w, x, splat(simd, 1.));

    z = neg_mul_add(simd, w, y, z);
    w = splat(simd, 0.5) * x;
    w = mla(simd, w, z, y);
    w += x;

    w *= q;

    w = (d.simd_eq(splat(simd, 0.)) | d.simd_eq(splat(simd, f32::INFINITY))).select(d, w);

    d.simd_lt(splat(simd, 0.)).select(splat(simd, f32::NAN), w)
}

/// 2D Euclidean distance function.
///
/// The error bound of the returned value is `0.5001 ULP`.
#[inline(always)]
pub fn hypot<S: Simd>(simd: S, x: F32s<S>, y: F32s<S>) -> F32s<S> {
    let x = x.abs();
    let y = y.abs();
    let min = x.min(y);
    let n = min;
    let max = x.max(y);
    let d = max;

    let o = max.simd_lt(splat(simd, f32::MIN_POSITIVE));
    let n = o.select(n * splat(simd, F1_24), n);
    let d = o.select(d * splat(simd, F1_24), d);

    let t = DD::new(n, splat(simd, 0.)) / DD::new(d, splat(simd, 0.));
    let t: DD<S> = mul_v(
        crate::doubled::f32::sqrt(add_v(square(t), splat(simd, 1.))),
        max,
    );
    let mut ret = t.0 + t.1;
    ret = ret.is_nan().select(splat(simd, f32::INFINITY), ret);
    ret = min.simd_eq(splat(simd, 0.)).select(max, ret);
    ret = (x.is_nan() | y.is_nan()).select(splat(simd, f32::NAN), ret);
    (x.simd_eq(splat(simd, f32::INFINITY)) | y.simd_eq(splat(simd, f32::INFINITY)))
        .select(splat(simd, f32::INFINITY), ret)
}