1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector cos function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2019-2023, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include "mathlib.h"
9*412f47f9SXin Li #include "v_math.h"
10*412f47f9SXin Li
11*412f47f9SXin Li static const struct data
12*412f47f9SXin Li {
13*412f47f9SXin Li float64x2_t poly[7];
14*412f47f9SXin Li float64x2_t range_val, shift, inv_pi, half_pi, pi_1, pi_2, pi_3;
15*412f47f9SXin Li } data = {
16*412f47f9SXin Li /* Worst-case error is 3.3 ulp in [-pi/2, pi/2]. */
17*412f47f9SXin Li .poly = { V2 (-0x1.555555555547bp-3), V2 (0x1.1111111108a4dp-7),
18*412f47f9SXin Li V2 (-0x1.a01a019936f27p-13), V2 (0x1.71de37a97d93ep-19),
19*412f47f9SXin Li V2 (-0x1.ae633919987c6p-26), V2 (0x1.60e277ae07cecp-33),
20*412f47f9SXin Li V2 (-0x1.9e9540300a1p-41) },
21*412f47f9SXin Li .inv_pi = V2 (0x1.45f306dc9c883p-2),
22*412f47f9SXin Li .half_pi = V2 (0x1.921fb54442d18p+0),
23*412f47f9SXin Li .pi_1 = V2 (0x1.921fb54442d18p+1),
24*412f47f9SXin Li .pi_2 = V2 (0x1.1a62633145c06p-53),
25*412f47f9SXin Li .pi_3 = V2 (0x1.c1cd129024e09p-106),
26*412f47f9SXin Li .shift = V2 (0x1.8p52),
27*412f47f9SXin Li .range_val = V2 (0x1p23)
28*412f47f9SXin Li };
29*412f47f9SXin Li
30*412f47f9SXin Li #define C(i) d->poly[i]
31*412f47f9SXin Li
32*412f47f9SXin Li static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,uint64x2_t odd,uint64x2_t cmp)33*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, uint64x2_t odd, uint64x2_t cmp)
34*412f47f9SXin Li {
35*412f47f9SXin Li y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));
36*412f47f9SXin Li return v_call_f64 (cos, x, y, cmp);
37*412f47f9SXin Li }
38*412f47f9SXin Li
V_NAME_D1(cos)39*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (cos) (float64x2_t x)
40*412f47f9SXin Li {
41*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
42*412f47f9SXin Li float64x2_t n, r, r2, r3, r4, t1, t2, t3, y;
43*412f47f9SXin Li uint64x2_t odd, cmp;
44*412f47f9SXin Li
45*412f47f9SXin Li #if WANT_SIMD_EXCEPT
46*412f47f9SXin Li r = vabsq_f64 (x);
47*412f47f9SXin Li cmp = vcgeq_u64 (vreinterpretq_u64_f64 (r),
48*412f47f9SXin Li vreinterpretq_u64_f64 (d->range_val));
49*412f47f9SXin Li if (unlikely (v_any_u64 (cmp)))
50*412f47f9SXin Li /* If fenv exceptions are to be triggered correctly, set any special lanes
51*412f47f9SXin Li to 1 (which is neutral w.r.t. fenv). These lanes will be fixed by
52*412f47f9SXin Li special-case handler later. */
53*412f47f9SXin Li r = vbslq_f64 (cmp, v_f64 (1.0), r);
54*412f47f9SXin Li #else
55*412f47f9SXin Li cmp = vcageq_f64 (x, d->range_val);
56*412f47f9SXin Li r = x;
57*412f47f9SXin Li #endif
58*412f47f9SXin Li
59*412f47f9SXin Li /* n = rint((|x|+pi/2)/pi) - 0.5. */
60*412f47f9SXin Li n = vfmaq_f64 (d->shift, d->inv_pi, vaddq_f64 (r, d->half_pi));
61*412f47f9SXin Li odd = vshlq_n_u64 (vreinterpretq_u64_f64 (n), 63);
62*412f47f9SXin Li n = vsubq_f64 (n, d->shift);
63*412f47f9SXin Li n = vsubq_f64 (n, v_f64 (0.5));
64*412f47f9SXin Li
65*412f47f9SXin Li /* r = |x| - n*pi (range reduction into -pi/2 .. pi/2). */
66*412f47f9SXin Li r = vfmsq_f64 (r, d->pi_1, n);
67*412f47f9SXin Li r = vfmsq_f64 (r, d->pi_2, n);
68*412f47f9SXin Li r = vfmsq_f64 (r, d->pi_3, n);
69*412f47f9SXin Li
70*412f47f9SXin Li /* sin(r) poly approx. */
71*412f47f9SXin Li r2 = vmulq_f64 (r, r);
72*412f47f9SXin Li r3 = vmulq_f64 (r2, r);
73*412f47f9SXin Li r4 = vmulq_f64 (r2, r2);
74*412f47f9SXin Li
75*412f47f9SXin Li t1 = vfmaq_f64 (C (4), C (5), r2);
76*412f47f9SXin Li t2 = vfmaq_f64 (C (2), C (3), r2);
77*412f47f9SXin Li t3 = vfmaq_f64 (C (0), C (1), r2);
78*412f47f9SXin Li
79*412f47f9SXin Li y = vfmaq_f64 (t1, C (6), r4);
80*412f47f9SXin Li y = vfmaq_f64 (t2, y, r4);
81*412f47f9SXin Li y = vfmaq_f64 (t3, y, r4);
82*412f47f9SXin Li y = vfmaq_f64 (r, y, r3);
83*412f47f9SXin Li
84*412f47f9SXin Li if (unlikely (v_any_u64 (cmp)))
85*412f47f9SXin Li return special_case (x, y, odd, cmp);
86*412f47f9SXin Li return vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));
87*412f47f9SXin Li }
88