xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_cospi_3u2.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision SVE cospi(x) 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 "sv_math.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "pl_test.h"
12*412f47f9SXin Li #include "poly_sve_f64.h"
13*412f47f9SXin Li 
14*412f47f9SXin Li static const struct data
15*412f47f9SXin Li {
16*412f47f9SXin Li   double poly[10];
17*412f47f9SXin Li   double 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 = { 0x1.921fb54442d184p1, -0x1.4abbce625be53p2, 0x1.466bc6775ab16p1,
22*412f47f9SXin Li 	    -0x1.32d2cce62dc33p-1, 0x1.507834891188ep-4, -0x1.e30750a28c88ep-8,
23*412f47f9SXin Li 	    0x1.e8f48308acda4p-12, -0x1.6fc0032b3c29fp-16,
24*412f47f9SXin Li 	    0x1.af86ae521260bp-21, -0x1.012a9870eeb7dp-25 },
25*412f47f9SXin Li   .range_val = 0x1p53,
26*412f47f9SXin Li };
27*412f47f9SXin Li 
28*412f47f9SXin Li /* A fast SVE implementation of cospi.
29*412f47f9SXin Li    Maximum error 3.20 ULP:
30*412f47f9SXin Li    _ZGVsMxv_cospi(0x1.f18ba32c63159p-6) got 0x1.fdabf595f9763p-1
31*412f47f9SXin Li 				       want 0x1.fdabf595f9766p-1.  */
SV_NAME_D1(cospi)32*412f47f9SXin Li svfloat64_t SV_NAME_D1 (cospi) (svfloat64_t x, const svbool_t pg)
33*412f47f9SXin Li {
34*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
35*412f47f9SXin Li 
36*412f47f9SXin Li   /* Using cospi(x) = sinpi(0.5 - x)
37*412f47f9SXin Li      range reduction and offset into sinpi range -1/2 .. 1/2
38*412f47f9SXin Li      r = 0.5 - |x - rint(x)|.  */
39*412f47f9SXin Li   svfloat64_t n = svrinta_x (pg, x);
40*412f47f9SXin Li   svfloat64_t r = svsub_x (pg, x, n);
41*412f47f9SXin Li   r = svsub_x (pg, sv_f64 (0.5), svabs_x (pg, r));
42*412f47f9SXin Li 
43*412f47f9SXin Li   /* Result should be negated based on if n is odd or not.
44*412f47f9SXin Li      If ax >= 2^53, the result will always be positive.  */
45*412f47f9SXin Li   svbool_t cmp = svaclt (pg, x, d->range_val);
46*412f47f9SXin Li   svuint64_t intn = svreinterpret_u64 (svcvt_s64_z (pg, n));
47*412f47f9SXin Li   svuint64_t sign = svlsl_z (cmp, intn, 63);
48*412f47f9SXin Li 
49*412f47f9SXin Li   /* y = sin(r).  */
50*412f47f9SXin Li   svfloat64_t r2 = svmul_x (pg, r, r);
51*412f47f9SXin Li   svfloat64_t r4 = svmul_x (pg, r2, r2);
52*412f47f9SXin Li   svfloat64_t y = sv_pw_horner_9_f64_x (pg, r2, r4, d->poly);
53*412f47f9SXin Li   y = svmul_x (pg, y, r);
54*412f47f9SXin Li 
55*412f47f9SXin Li   return svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (y), sign));
56*412f47f9SXin Li }
57*412f47f9SXin Li 
58*412f47f9SXin Li PL_SIG (SV, D, 1, cospi, -0.9, 0.9)
59*412f47f9SXin Li PL_TEST_ULP (SV_NAME_D1 (cospi), 2.71)
60*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (cospi), 0, 0x1p-63, 5000)
61*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (cospi), 0x1p-63, 0.5, 10000)
62*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (cospi), 0.5, 0x1p51, 10000)
63*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (cospi), 0x1p51, inf, 100000)
64