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