xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_cospi_3u1.c (revision 38b3683592d4c20a74f52a6e8e29368e6fa61858)
1 /*
2  * Double-precision vector cospi function.
3  *
4  * Copyright (c) 2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "mathlib.h"
9 #include "v_math.h"
10 #include "poly_advsimd_f64.h"
11 #include "pl_sig.h"
12 #include "pl_test.h"
13 
14 static const struct data
15 {
16   float64x2_t poly[10];
17   float64x2_t range_val;
18 } data = {
19   /* Polynomial coefficients generated using Remez algorithm,
20      see sinpi.sollya for details.  */
21   .poly = { V2 (0x1.921fb54442d184p1), V2 (-0x1.4abbce625be53p2),
22 	    V2 (0x1.466bc6775ab16p1), V2 (-0x1.32d2cce62dc33p-1),
23 	    V2 (0x1.507834891188ep-4), V2 (-0x1.e30750a28c88ep-8),
24 	    V2 (0x1.e8f48308acda4p-12), V2 (-0x1.6fc0032b3c29fp-16),
25 	    V2 (0x1.af86ae521260bp-21), V2 (-0x1.012a9870eeb7dp-25) },
26   .range_val = V2 (0x1p63),
27 };
28 
29 static float64x2_t VPCS_ATTR NOINLINE
30 special_case (float64x2_t x, float64x2_t y, uint64x2_t odd, uint64x2_t cmp)
31 {
32   /* Fall back to scalar code.  */
33   y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));
34   return v_call_f64 (cospi, x, y, cmp);
35 }
36 
37 /* Approximation for vector double-precision cospi(x).
38    Maximum Error 3.06 ULP:
39   _ZGVnN2v_cospi(0x1.7dd4c0b03cc66p-5) got 0x1.fa854babfb6bep-1
40 				      want 0x1.fa854babfb6c1p-1.  */
41 float64x2_t VPCS_ATTR V_NAME_D1 (cospi) (float64x2_t x)
42 {
43   const struct data *d = ptr_barrier (&data);
44 
45 #if WANT_SIMD_EXCEPT
46   float64x2_t r = vabsq_f64 (x);
47   uint64x2_t cmp = vcaleq_f64 (v_f64 (0x1p64), x);
48 
49   /* When WANT_SIMD_EXCEPT = 1, special lanes should be zero'd
50      to avoid them overflowing and throwing exceptions.  */
51   r = v_zerofy_f64 (r, cmp);
52   uint64x2_t odd = vshlq_n_u64 (vcvtnq_u64_f64 (r), 63);
53 
54 #else
55   float64x2_t r = x;
56   uint64x2_t cmp = vcageq_f64 (r, d->range_val);
57   uint64x2_t odd
58       = vshlq_n_u64 (vreinterpretq_u64_s64 (vcvtaq_s64_f64 (r)), 63);
59 
60 #endif
61 
62   r = vsubq_f64 (r, vrndaq_f64 (r));
63 
64   /* cospi(x) = sinpi(0.5 - abs(x)) for values -1/2 .. 1/2.  */
65   r = vsubq_f64 (v_f64 (0.5), vabsq_f64 (r));
66 
67   /* y = sin(r).  */
68   float64x2_t r2 = vmulq_f64 (r, r);
69   float64x2_t r4 = vmulq_f64 (r2, r2);
70   float64x2_t y = vmulq_f64 (v_pw_horner_9_f64 (r2, r4, d->poly), r);
71 
72   /* Fallback to scalar.  */
73   if (unlikely (v_any_u64 (cmp)))
74     return special_case (x, y, odd, cmp);
75 
76   /* Reintroduce the sign bit for inputs which round to odd.  */
77   return vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));
78 }
79 
80 PL_SIG (V, D, 1, cospi, -0.9, 0.9)
81 PL_TEST_ULP (V_NAME_D1 (cospi), 2.56)
82 PL_TEST_EXPECT_FENV (V_NAME_D1 (cospi), WANT_SIMD_EXCEPT)
83 PL_TEST_SYM_INTERVAL (V_NAME_D1 (cospi), 0, 0x1p-63, 5000)
84 PL_TEST_SYM_INTERVAL (V_NAME_D1 (cospi), 0x1p-63, 0.5, 10000)
85 PL_TEST_SYM_INTERVAL (V_NAME_D1 (cospi), 0.5, 0x1p51, 10000)
86 PL_TEST_SYM_INTERVAL (V_NAME_D1 (cospi), 0x1p51, inf, 10000)
87