numr 0.11.0

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Inverse trig family (asin, acos, atan) dispatch.
//! Same threshold/ISA-selection logic as every unary family (`dispatch.rs`).

use super::super::{SimdLevel, detect_simd};
use super::dispatch::{SIMD_THRESHOLD, is_simd_supported};
use super::{unary_scalar_f32, unary_scalar_f64};
use crate::ops::UnaryOp;

/// Dispatch an inverse trig op (Asin/Acos/Atan) for f32.
///
/// # Safety
/// - `a` and `out` must be valid pointers to `len` elements
#[inline]
pub(super) unsafe fn inverse_trig_f32(op: UnaryOp, a: *const f32, out: *mut f32, len: usize) {
    let level = detect_simd();
    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar || !is_simd_supported(op) {
        return unsafe { unary_scalar_f32(op, a, out, len) };
    }
    unsafe {
        #[cfg(target_arch = "x86_64")]
        match level {
            SimdLevel::Avx512 => super::x86_64::avx512::unary_f32(op, a, out, len),
            SimdLevel::Avx2Fma => super::x86_64::avx2::unary_f32(op, a, out, len),
            _ => unary_scalar_f32(op, a, out, len),
        }
        #[cfg(target_arch = "aarch64")]
        match level {
            SimdLevel::Neon | SimdLevel::NeonFp16 => {
                super::aarch64::neon::unary_f32(op, a, out, len)
            }
            _ => unary_scalar_f32(op, a, out, len),
        }
        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        unary_scalar_f32(op, a, out, len);
    }
}

/// f64 counterpart of [`inverse_trig_f32`].
///
/// # Safety
/// - `a` and `out` must be valid pointers to `len` elements
#[inline]
pub(super) unsafe fn inverse_trig_f64(op: UnaryOp, a: *const f64, out: *mut f64, len: usize) {
    let level = detect_simd();
    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar || !is_simd_supported(op) {
        return unsafe { unary_scalar_f64(op, a, out, len) };
    }
    unsafe {
        #[cfg(target_arch = "x86_64")]
        match level {
            SimdLevel::Avx512 => super::x86_64::avx512::unary_f64(op, a, out, len),
            SimdLevel::Avx2Fma => super::x86_64::avx2::unary_f64(op, a, out, len),
            _ => unary_scalar_f64(op, a, out, len),
        }
        #[cfg(target_arch = "aarch64")]
        match level {
            SimdLevel::Neon | SimdLevel::NeonFp16 => {
                super::aarch64::neon::unary_f64(op, a, out, len)
            }
            _ => unary_scalar_f64(op, a, out, len),
        }
        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        unary_scalar_f64(op, a, out, len);
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_support::{fill_range_f32, fill_sweep_f64, rel_err_f32, rel_err_f64};
    use super::*;

    #[test]
    fn test_unary_asin_f64_double_precision() {
        const LEN: usize = 2048;
        let mut a: Vec<f64> = Vec::with_capacity(LEN);

        // 1/sqrt(2) is where asin crosses from the direct series to the
        // reflection in every naive atan-based formulation, so a wrong branch
        // shows up here first. 0.5 is this implementation's own branch point.
        for &b in &[std::f64::consts::FRAC_1_SQRT_2, 0.5, 1.0] {
            for k in -100i32..=100 {
                let d = b + (k as f64) * 1e-12;
                if d <= 1.0 {
                    a.push(d);
                    a.push(-d);
                }
            }
        }
        fill_sweep_f64(&mut a, LEN, 1.0);

        let mut out = vec![0.0f64; LEN];
        unsafe { inverse_trig_f64(UnaryOp::Asin, a.as_ptr(), out.as_mut_ptr(), LEN) }

        for i in 0..LEN {
            let expected = a[i].asin();
            let rel_err = rel_err_f64(out[i], expected);
            assert!(
                rel_err < 1e-14,
                "asin({}) = {}, expected {}, rel_err = {} at index {}",
                a[i],
                out[i],
                expected,
                rel_err,
                i
            );
        }
    }

    #[test]
    fn test_unary_acos_f64_double_precision() {
        const LEN: usize = 2048;
        let mut a: Vec<f64> = Vec::with_capacity(LEN);

        for &b in &[std::f64::consts::FRAC_1_SQRT_2, 0.5, 1.0] {
            for k in -100i32..=100 {
                let d = b + (k as f64) * 1e-12;
                if d <= 1.0 {
                    a.push(d);
                    a.push(-d);
                }
            }
        }
        fill_sweep_f64(&mut a, LEN, 1.0);

        let mut out = vec![0.0f64; LEN];
        unsafe { inverse_trig_f64(UnaryOp::Acos, a.as_ptr(), out.as_mut_ptr(), LEN) }

        for i in 0..LEN {
            let expected = a[i].acos();
            let rel_err = rel_err_f64(out[i], expected);
            assert!(
                rel_err < 1e-14,
                "acos({}) = {}, expected {}, rel_err = {} at index {}",
                a[i],
                out[i],
                expected,
                rel_err,
                i
            );
        }
    }

    #[test]
    fn test_unary_atan_f64_double_precision() {
        const LEN: usize = 2048;
        let mut a: Vec<f64> = Vec::with_capacity(LEN);

        // Every reduction breakpoint, from both sides. |x| = 1 is the boundary
        // of the naive reciprocal reduction and the worst point of a truncated
        // Gregory series.
        for &b in &[0.4375f64, 0.6875, 1.0, 1.1875, 2.4375] {
            for k in -40i32..=40 {
                let d = b + (k as f64) * 1e-12;
                a.push(d);
                a.push(-d);
            }
        }
        // Magnitudes far past the last breakpoint, where t = -1/|x| is used.
        for k in 0..100 {
            let d = 10.0f64.powi(k % 25 + 2);
            a.push(d);
            a.push(-d);
        }
        fill_sweep_f64(&mut a, LEN, 8.0);

        let mut out = vec![0.0f64; LEN];
        unsafe { inverse_trig_f64(UnaryOp::Atan, a.as_ptr(), out.as_mut_ptr(), LEN) }

        for i in 0..LEN {
            let expected = a[i].atan();
            let rel_err = rel_err_f64(out[i], expected);
            assert!(
                rel_err < 1e-14,
                "atan({}) = {}, expected {}, rel_err = {} at index {}",
                a[i],
                out[i],
                expected,
                rel_err,
                i
            );
        }
    }

    #[test]
    fn test_unary_inverse_trig_f64_domain_edges() {
        const LEN: usize = 2048;

        // ±1 are exact endpoints; beyond them asin/acos are undefined.
        let asin_edges = [1.0f64, -1.0, 0.0, -0.0, 1.5, -1.5, f64::INFINITY, f64::NAN];
        let a: Vec<f64> = (0..LEN).map(|i| asin_edges[i % asin_edges.len()]).collect();

        let mut out = vec![0.0f64; LEN];
        unsafe { inverse_trig_f64(UnaryOp::Asin, a.as_ptr(), out.as_mut_ptr(), LEN) }
        for i in 0..LEN {
            let expected = a[i].asin();
            if expected.is_nan() {
                assert!(out[i].is_nan(), "asin({}) = {}, expected NaN", a[i], out[i]);
            } else {
                assert!(
                    rel_err_f64(out[i], expected) < 1e-14,
                    "asin({}) = {}, expected {}",
                    a[i],
                    out[i],
                    expected
                );
            }
        }

        unsafe { inverse_trig_f64(UnaryOp::Acos, a.as_ptr(), out.as_mut_ptr(), LEN) }
        for i in 0..LEN {
            let expected = a[i].acos();
            if expected.is_nan() {
                assert!(out[i].is_nan(), "acos({}) = {}, expected NaN", a[i], out[i]);
            } else {
                assert!(
                    rel_err_f64(out[i], expected) < 1e-14,
                    "acos({}) = {}, expected {}",
                    a[i],
                    out[i],
                    expected
                );
            }
        }

        // atan is defined everywhere; ±inf must saturate to ±pi/2.
        let atan_edges = [
            f64::INFINITY,
            f64::NEG_INFINITY,
            0.0f64,
            1.0,
            -1.0,
            1e308,
            -1e308,
            f64::NAN,
        ];
        let a: Vec<f64> = (0..LEN).map(|i| atan_edges[i % atan_edges.len()]).collect();
        unsafe { inverse_trig_f64(UnaryOp::Atan, a.as_ptr(), out.as_mut_ptr(), LEN) }
        for i in 0..LEN {
            if a[i].is_nan() {
                assert!(out[i].is_nan(), "atan(NaN) = {}", out[i]);
            } else {
                let expected = a[i].atan();
                assert!(
                    rel_err_f64(out[i], expected) < 1e-14,
                    "atan({}) = {}, expected {}",
                    a[i],
                    out[i],
                    expected
                );
            }
        }
    }

    /// Probe points for f32 asin/acos: the branch points, the endpoints, and a
    /// sweep of the whole domain.
    fn inverse_trig_probe_points_f32(len: usize) -> Vec<f32> {
        let mut a: Vec<f32> = Vec::with_capacity(len);

        // 1/sqrt(2) is where asin crosses from the direct series to the
        // reflection in any atan-based formulation, because atan's argument
        // reaches exactly 1 there. 0.5 is this implementation's own branch
        // point, and ±1 is the endpoint the reflection has to land on exactly.
        for &b in &[std::f32::consts::FRAC_1_SQRT_2, 0.5, 1.0] {
            for k in -80i32..=80 {
                let d = b + (k as f32) * 1e-7;
                if d <= 1.0 {
                    a.push(d);
                    a.push(-d);
                }
            }
        }

        fill_range_f32(&mut a, len, -1.0, 1.0);
        a
    }

    /// f32 asin must reach single precision over [-1, 1].
    ///
    /// Composing it as `atan(x / sqrt(1 - x²))` sends atan's argument to 1 at
    /// |x| = 1/sqrt(2), the slowest-converging point of the Gregory series it
    /// used to call, worth 4.5e-2 relative.
    #[test]
    fn test_unary_asin_f32_single_precision() {
        const LEN: usize = 2048;
        let a = inverse_trig_probe_points_f32(LEN);

        let mut out = vec![0.0f32; LEN];
        unsafe { inverse_trig_f32(UnaryOp::Asin, a.as_ptr(), out.as_mut_ptr(), LEN) }

        for i in 0..LEN {
            let expected = (a[i] as f64).asin() as f32;
            let rel_err = rel_err_f32(out[i], expected);
            assert!(
                rel_err < 1e-6,
                "asin({}) = {}, expected {}, rel_err = {} at index {}",
                a[i],
                out[i],
                expected,
                rel_err,
                i
            );
        }
    }

    /// f32 acos must reach single precision over [-1, 1].
    ///
    /// `π/2 - asin(x)` inherits every defect of asin and adds cancellation as
    /// x approaches 1, where the result is the difference that vanishes.
    #[test]
    fn test_unary_acos_f32_single_precision() {
        const LEN: usize = 2048;
        let a = inverse_trig_probe_points_f32(LEN);

        let mut out = vec![0.0f32; LEN];
        unsafe { inverse_trig_f32(UnaryOp::Acos, a.as_ptr(), out.as_mut_ptr(), LEN) }

        for i in 0..LEN {
            let expected = (a[i] as f64).acos() as f32;
            let rel_err = rel_err_f32(out[i], expected);
            assert!(
                rel_err < 1e-6,
                "acos({}) = {}, expected {}, rel_err = {} at index {}",
                a[i],
                out[i],
                expected,
                rel_err,
                i
            );
        }
    }

    /// f32 atan must reach single precision for every finite input.
    ///
    /// The Gregory series it used to evaluate on [0, 1] converges like
    /// 1/(2n+3) at the boundary: seven terms leave ~4e-2 relative error, which
    /// is where the old peak sat.
    #[test]
    fn test_unary_atan_f32_single_precision() {
        const LEN: usize = 2048;
        let mut a: Vec<f32> = Vec::with_capacity(LEN);

        // |x| = 1 is the old reduction boundary; the two tan(π/8) and tan(3π/8)
        // values are the new ones.
        for &b in &[1.0f32, 0.414_213_56, 2.414_213_6, 0.989_011] {
            for k in -60i32..=60 {
                let d = b * (1.0 + (k as f32) * 1e-6);
                a.push(d);
                a.push(-d);
            }
        }

        // Many magnitudes: atan has to hold from the smallest normal up to the
        // point where the result is π/2 to the last bit.
        for k in -35i32..=35 {
            let d = 10f32.powi(k);
            a.push(d);
            a.push(-d);
        }

        fill_range_f32(&mut a, LEN, -20.0, 20.0);

        let mut out = vec![0.0f32; LEN];
        unsafe { inverse_trig_f32(UnaryOp::Atan, a.as_ptr(), out.as_mut_ptr(), LEN) }

        for i in 0..LEN {
            let expected = (a[i] as f64).atan() as f32;
            let rel_err = rel_err_f32(out[i], expected);
            assert!(
                rel_err < 1e-6,
                "atan({}) = {}, expected {}, rel_err = {} at index {}",
                a[i],
                out[i],
                expected,
                rel_err,
                i
            );
        }
    }

    #[test]
    fn test_unary_inverse_trig_f32_domain_edges() {
        const LEN: usize = 2048;

        // asin/acos are NaN outside [-1, 1] and exact at the endpoints; atan
        // saturates to ±π/2 at infinity.
        let edges = [
            0.0f32,
            -0.0,
            1.0,
            -1.0,
            0.5,
            -0.5,
            1.000_001,
            -1.000_001,
            2.0,
            f32::INFINITY,
            f32::NEG_INFINITY,
            f32::NAN,
        ];
        let a: Vec<f32> = (0..LEN).map(|i| edges[i % edges.len()]).collect();
        let mut out = vec![0.0f32; LEN];

        for (op, reference) in [
            (UnaryOp::Asin, f64::asin as fn(f64) -> f64),
            (UnaryOp::Acos, f64::acos as fn(f64) -> f64),
            (UnaryOp::Atan, f64::atan as fn(f64) -> f64),
        ] {
            unsafe { inverse_trig_f32(op, a.as_ptr(), out.as_mut_ptr(), LEN) }
            for i in 0..LEN {
                let expected = reference(a[i] as f64) as f32;
                if expected.is_nan() {
                    assert!(
                        out[i].is_nan(),
                        "{:?}({}) = {}, expected NaN",
                        op,
                        a[i],
                        out[i]
                    );
                    continue;
                }
                assert!(
                    rel_err_f32(out[i], expected) < 1e-6,
                    "{:?}({}) = {}, expected {}",
                    op,
                    a[i],
                    out[i],
                    expected
                );
            }
        }
    }
}