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