xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sinpif.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1 /*
2  * Single-precision SVE sinpi(x) function.
3  *
4  * Copyright (c) 2023-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "mathlib.h"
10 #include "test_sig.h"
11 #include "test_defs.h"
12 #include "sv_poly_f32.h"
13 
14 static const struct data
15 {
16   float poly[6], range_val;
17 } data = {
18   /* Taylor series coefficents for sin(pi * x).  */
19   .poly = { 0x1.921fb6p1f, -0x1.4abbcep2f, 0x1.466bc6p1f, -0x1.32d2ccp-1f,
20 	    0x1.50783p-4f, -0x1.e30750p-8f },
21   .range_val = 0x1p31,
22 };
23 
24 /* A fast SVE implementation of sinpif.
25    Maximum error 2.48 ULP:
26    _ZGVsMxv_sinpif(0x1.d062b6p-2) got 0x1.fa8c06p-1
27 				 want 0x1.fa8c02p-1.  */
SV_NAME_F1(sinpi)28 svfloat32_t SV_NAME_F1 (sinpi) (svfloat32_t x, const svbool_t pg)
29 {
30   const struct data *d = ptr_barrier (&data);
31 
32   /* range reduction into -1/2 .. 1/2
33      with n = rint(x) and r = r - n.  */
34   svfloat32_t n = svrinta_x (pg, x);
35   svfloat32_t r = svsub_x (pg, x, n);
36 
37   /* Result should be negated based on if n is odd or not.  */
38   svbool_t cmp = svaclt (pg, x, d->range_val);
39   svuint32_t intn = svreinterpret_u32 (svcvt_s32_z (pg, n));
40   svuint32_t sign = svlsl_z (cmp, intn, 31);
41 
42   /* y = sin(r).  */
43   svfloat32_t r2 = svmul_x (pg, r, r);
44   svfloat32_t y = sv_horner_5_f32_x (pg, r2, d->poly);
45   y = svmul_x (pg, y, r);
46 
47   return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (y), sign));
48 }
49 
50 #if WANT_TRIGPI_TESTS
51 TEST_ULP (SV_NAME_F1 (sinpi), 1.99)
52 TEST_DISABLE_FENV (SV_NAME_F1 (sinpi))
53 TEST_SYM_INTERVAL (SV_NAME_F1 (sinpi), 0, 0x1p-31, 5000)
54 TEST_SYM_INTERVAL (SV_NAME_F1 (sinpi), 0x1p-31, 0.5, 10000)
55 TEST_SYM_INTERVAL (SV_NAME_F1 (sinpi), 0.5, 0x1p22f, 10000)
56 TEST_SYM_INTERVAL (SV_NAME_F1 (sinpi), 0x1p22f, inf, 10000)
57 #endif
58 CLOSE_SVE_ATTR
59