xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_cospif_2u6.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-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_f32.h"
13*412f47f9SXin Li 
14*412f47f9SXin Li static const struct data
15*412f47f9SXin Li {
16*412f47f9SXin Li   float poly[6];
17*412f47f9SXin Li   float range_val;
18*412f47f9SXin Li } data = {
19*412f47f9SXin Li   /* Taylor series coefficents for sin(pi * x).  */
20*412f47f9SXin Li   .poly = { 0x1.921fb6p1f, -0x1.4abbcep2f, 0x1.466bc6p1f, -0x1.32d2ccp-1f,
21*412f47f9SXin Li 	    0x1.50783p-4f, -0x1.e30750p-8f },
22*412f47f9SXin Li   .range_val = 0x1p31f,
23*412f47f9SXin Li };
24*412f47f9SXin Li 
25*412f47f9SXin Li /* A fast SVE implementation of cospif.
26*412f47f9SXin Li    Maximum error: 2.60 ULP:
27*412f47f9SXin Li    _ZGVsMxv_cospif(+/-0x1.cae664p-4) got 0x1.e09c9ep-1
28*412f47f9SXin Li 				    want 0x1.e09c98p-1.  */
SV_NAME_F1(cospi)29*412f47f9SXin Li svfloat32_t SV_NAME_F1 (cospi) (svfloat32_t x, const svbool_t pg)
30*412f47f9SXin Li {
31*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
32*412f47f9SXin Li 
33*412f47f9SXin Li   /* Using cospi(x) = sinpi(0.5 - x)
34*412f47f9SXin Li      range reduction and offset into sinpi range -1/2 .. 1/2
35*412f47f9SXin Li      r = 0.5 - |x - rint(x)|.  */
36*412f47f9SXin Li   svfloat32_t n = svrinta_x (pg, x);
37*412f47f9SXin Li   svfloat32_t r = svsub_x (pg, x, n);
38*412f47f9SXin Li   r = svsub_x (pg, sv_f32 (0.5f), svabs_x (pg, r));
39*412f47f9SXin Li 
40*412f47f9SXin Li   /* Result should be negated based on if n is odd or not.
41*412f47f9SXin Li      If ax >= 2^31, the result will always be positive.  */
42*412f47f9SXin Li   svbool_t cmp = svaclt (pg, x, d->range_val);
43*412f47f9SXin Li   svuint32_t intn = svreinterpret_u32 (svcvt_s32_x (pg, n));
44*412f47f9SXin Li   svuint32_t sign = svlsl_z (cmp, intn, 31);
45*412f47f9SXin Li 
46*412f47f9SXin Li   /* y = sin(r).  */
47*412f47f9SXin Li   svfloat32_t r2 = svmul_x (pg, r, r);
48*412f47f9SXin Li   svfloat32_t y = sv_horner_5_f32_x (pg, r2, d->poly);
49*412f47f9SXin Li   y = svmul_x (pg, y, r);
50*412f47f9SXin Li 
51*412f47f9SXin Li   return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (y), sign));
52*412f47f9SXin Li }
53*412f47f9SXin Li 
54*412f47f9SXin Li PL_SIG (SV, F, 1, cospi, -0.9, 0.9)
55*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (cospi), 2.08)
56*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cospi), 0, 0x1p-31, 5000)
57*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cospi), 0x1p-31, 0.5, 10000)
58*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cospi), 0.5, 0x1p31f, 10000)
59*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cospi), 0x1p31f, inf, 10000)
60