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