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