1 /* 2 * Single-precision SVE sin(x) function. 3 * 4 * Copyright (c) 2019-2023, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include "sv_math.h" 9 #include "pl_sig.h" 10 #include "pl_test.h" 11 12 static const struct data 13 { 14 float poly[4]; 15 /* Pi-related values to be loaded as one quad-word and used with 16 svmla_lane. */ 17 float negpi1, negpi2, negpi3, invpi; 18 float shift; 19 } data = { 20 .poly = { 21 /* Non-zero coefficients from the degree 9 Taylor series expansion of 22 sin. */ 23 -0x1.555548p-3f, 0x1.110df4p-7f, -0x1.9f42eap-13f, 0x1.5b2e76p-19f 24 }, 25 .negpi1 = -0x1.921fb6p+1f, 26 .negpi2 = 0x1.777a5cp-24f, 27 .negpi3 = 0x1.ee59dap-49f, 28 .invpi = 0x1.45f306p-2f, 29 .shift = 0x1.8p+23f 30 }; 31 32 #define RangeVal 0x49800000 /* asuint32 (0x1p20f). */ 33 #define C(i) sv_f32 (d->poly[i]) 34 35 static svfloat32_t NOINLINE 36 special_case (svfloat32_t x, svfloat32_t y, svbool_t cmp) 37 { 38 return sv_call_f32 (sinf, x, y, cmp); 39 } 40 41 /* A fast SVE implementation of sinf. 42 Maximum error: 1.89 ULPs. 43 This maximum error is achieved at multiple values in [-2^18, 2^18] 44 but one example is: 45 SV_NAME_F1 (sin)(0x1.9247a4p+0) got 0x1.fffff6p-1 want 0x1.fffffap-1. */ 46 svfloat32_t SV_NAME_F1 (sin) (svfloat32_t x, const svbool_t pg) 47 { 48 const struct data *d = ptr_barrier (&data); 49 50 svfloat32_t ax = svabs_x (pg, x); 51 svuint32_t sign 52 = sveor_x (pg, svreinterpret_u32 (x), svreinterpret_u32 (ax)); 53 svbool_t cmp = svcmpge (pg, svreinterpret_u32 (ax), RangeVal); 54 55 /* pi_vals are a quad-word of helper values - the first 3 elements contain 56 -pi in extended precision, the last contains 1 / pi. */ 57 svfloat32_t pi_vals = svld1rq (svptrue_b32 (), &d->negpi1); 58 59 /* n = rint(|x|/pi). */ 60 svfloat32_t n = svmla_lane (sv_f32 (d->shift), ax, pi_vals, 3); 61 svuint32_t odd = svlsl_x (pg, svreinterpret_u32 (n), 31); 62 n = svsub_x (pg, n, d->shift); 63 64 /* r = |x| - n*pi (range reduction into -pi/2 .. pi/2). */ 65 svfloat32_t r; 66 r = svmla_lane (ax, n, pi_vals, 0); 67 r = svmla_lane (r, n, pi_vals, 1); 68 r = svmla_lane (r, n, pi_vals, 2); 69 70 /* sin(r) approx using a degree 9 polynomial from the Taylor series 71 expansion. Note that only the odd terms of this are non-zero. */ 72 svfloat32_t r2 = svmul_x (pg, r, r); 73 svfloat32_t y; 74 y = svmla_x (pg, C (2), r2, C (3)); 75 y = svmla_x (pg, C (1), r2, y); 76 y = svmla_x (pg, C (0), r2, y); 77 y = svmla_x (pg, r, r, svmul_x (pg, y, r2)); 78 79 /* sign = y^sign^odd. */ 80 sign = sveor_x (pg, sign, odd); 81 82 if (unlikely (svptest_any (pg, cmp))) 83 return special_case (x, 84 svreinterpret_f32 (sveor_x ( 85 svnot_z (pg, cmp), svreinterpret_u32 (y), sign)), 86 cmp); 87 return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (y), sign)); 88 } 89 90 PL_SIG (SV, F, 1, sin, -3.1, 3.1) 91 PL_TEST_ULP (SV_NAME_F1 (sin), 1.40) 92 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (sin), 0, 0x1p23, 1000000) 93 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (sin), 0x1p23, inf, 10000) 94