xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/cos.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1 /*
2  * Double-precision vector cos function.
3  *
4  * Copyright (c) 2019-2024, 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 "test_defs.h"
11 #include "test_sig.h"
12 
13 static const struct data
14 {
15   float64x2_t poly[7];
16   float64x2_t range_val, inv_pi, pi_1, pi_2, pi_3;
17 } data = {
18   /* Worst-case error is 3.3 ulp in [-pi/2, pi/2].  */
19   .poly = { V2 (-0x1.555555555547bp-3), V2 (0x1.1111111108a4dp-7),
20 	    V2 (-0x1.a01a019936f27p-13), V2 (0x1.71de37a97d93ep-19),
21 	    V2 (-0x1.ae633919987c6p-26), V2 (0x1.60e277ae07cecp-33),
22 	    V2 (-0x1.9e9540300a1p-41) },
23   .inv_pi = V2 (0x1.45f306dc9c883p-2),
24   .pi_1 = V2 (0x1.921fb54442d18p+1),
25   .pi_2 = V2 (0x1.1a62633145c06p-53),
26   .pi_3 = V2 (0x1.c1cd129024e09p-106),
27   .range_val = V2 (0x1p23)
28 };
29 
30 #define C(i) d->poly[i]
31 
32 static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,uint64x2_t odd,uint64x2_t cmp)33 special_case (float64x2_t x, float64x2_t y, uint64x2_t odd, uint64x2_t cmp)
34 {
35   y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));
36   return v_call_f64 (cos, x, y, cmp);
37 }
38 
V_NAME_D1(cos)39 float64x2_t VPCS_ATTR V_NAME_D1 (cos) (float64x2_t x)
40 {
41   const struct data *d = ptr_barrier (&data);
42   float64x2_t n, r, r2, r3, r4, t1, t2, t3, y;
43   uint64x2_t odd, cmp;
44 
45 #if WANT_SIMD_EXCEPT
46   r = vabsq_f64 (x);
47   cmp = vcgeq_u64 (vreinterpretq_u64_f64 (r),
48 		   vreinterpretq_u64_f64 (d->range_val));
49   if (unlikely (v_any_u64 (cmp)))
50     /* If fenv exceptions are to be triggered correctly, set any special lanes
51        to 1 (which is neutral w.r.t. fenv). These lanes will be fixed by
52        special-case handler later.  */
53     r = vbslq_f64 (cmp, v_f64 (1.0), r);
54 #else
55   cmp = vcageq_f64 (x, d->range_val);
56   r = x;
57 #endif
58 
59   /* n = rint((|x|+pi/2)/pi) - 0.5.  */
60   n = vrndaq_f64 (vfmaq_f64 (v_f64 (0.5), r, d->inv_pi));
61   odd = vshlq_n_u64 (vreinterpretq_u64_s64 (vcvtq_s64_f64 (n)), 63);
62   n = vsubq_f64 (n, v_f64 (0.5f));
63 
64   /* r = |x| - n*pi  (range reduction into -pi/2 .. pi/2).  */
65   r = vfmsq_f64 (r, d->pi_1, n);
66   r = vfmsq_f64 (r, d->pi_2, n);
67   r = vfmsq_f64 (r, d->pi_3, n);
68 
69   /* sin(r) poly approx.  */
70   r2 = vmulq_f64 (r, r);
71   r3 = vmulq_f64 (r2, r);
72   r4 = vmulq_f64 (r2, r2);
73 
74   t1 = vfmaq_f64 (C (4), C (5), r2);
75   t2 = vfmaq_f64 (C (2), C (3), r2);
76   t3 = vfmaq_f64 (C (0), C (1), r2);
77 
78   y = vfmaq_f64 (t1, C (6), r4);
79   y = vfmaq_f64 (t2, y, r4);
80   y = vfmaq_f64 (t3, y, r4);
81   y = vfmaq_f64 (r, y, r3);
82 
83   if (unlikely (v_any_u64 (cmp)))
84     return special_case (x, y, odd, cmp);
85   return vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));
86 }
87 
88 TEST_SIG (V, D, 1, cos, -3.1, 3.1)
89 TEST_ULP (V_NAME_D1 (cos), 3.0)
90 TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (cos), WANT_SIMD_EXCEPT)
91 TEST_SYM_INTERVAL (V_NAME_D1 (cos), 0, 0x1p23, 500000)
92 TEST_SYM_INTERVAL (V_NAME_D1 (cos), 0x1p23, inf, 10000)
93