xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_exp10f_1u5.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-precision SVE 2^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 "sv_math.h"
9*412f47f9SXin Li #include "include/mathlib.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 /* For x < -SpecialBound, the result is subnormal and not handled correctly by
15*412f47f9SXin Li    FEXPA.  */
16*412f47f9SXin Li #define SpecialBound 37.9
17*412f47f9SXin Li 
18*412f47f9SXin Li static const struct data
19*412f47f9SXin Li {
20*412f47f9SXin Li   float poly[5];
21*412f47f9SXin Li   float shift, log10_2, log2_10_hi, log2_10_lo, special_bound;
22*412f47f9SXin Li } data = {
23*412f47f9SXin Li   /* Coefficients generated using Remez algorithm with minimisation of relative
24*412f47f9SXin Li      error.
25*412f47f9SXin Li      rel error: 0x1.89dafa3p-24
26*412f47f9SXin Li      abs error: 0x1.167d55p-23 in [-log10(2)/2, log10(2)/2]
27*412f47f9SXin Li      maxerr: 0.52 +0.5 ulp.  */
28*412f47f9SXin Li   .poly = { 0x1.26bb16p+1f, 0x1.5350d2p+1f, 0x1.04744ap+1f, 0x1.2d8176p+0f,
29*412f47f9SXin Li 	    0x1.12b41ap-1f },
30*412f47f9SXin Li   /* 1.5*2^17 + 127, a shift value suitable for FEXPA.  */
31*412f47f9SXin Li   .shift = 0x1.903f8p17f,
32*412f47f9SXin Li   .log10_2 = 0x1.a934fp+1,
33*412f47f9SXin Li   .log2_10_hi = 0x1.344136p-2,
34*412f47f9SXin Li   .log2_10_lo = -0x1.ec10cp-27,
35*412f47f9SXin Li   .special_bound = SpecialBound,
36*412f47f9SXin Li };
37*412f47f9SXin Li 
38*412f47f9SXin Li static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t special)39*412f47f9SXin Li special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
40*412f47f9SXin Li {
41*412f47f9SXin Li   return sv_call_f32 (exp10f, x, y, special);
42*412f47f9SXin Li }
43*412f47f9SXin Li 
44*412f47f9SXin Li /* Single-precision SVE exp10f routine. Implements the same algorithm
45*412f47f9SXin Li    as AdvSIMD exp10f.
46*412f47f9SXin Li    Worst case error is 1.02 ULPs.
47*412f47f9SXin Li    _ZGVsMxv_exp10f(-0x1.040488p-4) got 0x1.ba5f9ep-1
48*412f47f9SXin Li 				  want 0x1.ba5f9cp-1.  */
SV_NAME_F1(exp10)49*412f47f9SXin Li svfloat32_t SV_NAME_F1 (exp10) (svfloat32_t x, const svbool_t pg)
50*412f47f9SXin Li {
51*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
52*412f47f9SXin Li   /* exp10(x) = 2^(n/N) * 10^r = 2^n * (1 + poly (r)),
53*412f47f9SXin Li      with poly(r) in [1/sqrt(2), sqrt(2)] and
54*412f47f9SXin Li      x = r + n * log10(2) / N, with r in [-log10(2)/2N, log10(2)/2N].  */
55*412f47f9SXin Li 
56*412f47f9SXin Li   /* Load some constants in quad-word chunks to minimise memory access (last
57*412f47f9SXin Li      lane is wasted).  */
58*412f47f9SXin Li   svfloat32_t log10_2_and_inv = svld1rq (svptrue_b32 (), &d->log10_2);
59*412f47f9SXin Li 
60*412f47f9SXin Li   /* n = round(x/(log10(2)/N)).  */
61*412f47f9SXin Li   svfloat32_t shift = sv_f32 (d->shift);
62*412f47f9SXin Li   svfloat32_t z = svmla_lane (shift, x, log10_2_and_inv, 0);
63*412f47f9SXin Li   svfloat32_t n = svsub_x (pg, z, shift);
64*412f47f9SXin Li 
65*412f47f9SXin Li   /* r = x - n*log10(2)/N.  */
66*412f47f9SXin Li   svfloat32_t r = svmls_lane (x, n, log10_2_and_inv, 1);
67*412f47f9SXin Li   r = svmls_lane (r, n, log10_2_and_inv, 2);
68*412f47f9SXin Li 
69*412f47f9SXin Li   svbool_t special = svacgt (pg, x, d->special_bound);
70*412f47f9SXin Li   svfloat32_t scale = svexpa (svreinterpret_u32 (z));
71*412f47f9SXin Li 
72*412f47f9SXin Li   /* Polynomial evaluation: poly(r) ~ exp10(r)-1.  */
73*412f47f9SXin Li   svfloat32_t r2 = svmul_x (pg, r, r);
74*412f47f9SXin Li   svfloat32_t poly
75*412f47f9SXin Li       = svmla_x (pg, svmul_x (pg, r, d->poly[0]),
76*412f47f9SXin Li 		 sv_pairwise_poly_3_f32_x (pg, r, r2, d->poly + 1), r2);
77*412f47f9SXin Li 
78*412f47f9SXin Li   if (unlikely (svptest_any (pg, special)))
79*412f47f9SXin Li     return special_case (x, svmla_x (pg, scale, scale, poly), special);
80*412f47f9SXin Li 
81*412f47f9SXin Li   return svmla_x (pg, scale, scale, poly);
82*412f47f9SXin Li }
83*412f47f9SXin Li 
84*412f47f9SXin Li PL_SIG (SV, F, 1, exp10, -9.9, 9.9)
85*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (exp10), 0.52)
86*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp10), 0, SpecialBound, 50000)
87*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp10), SpecialBound, inf, 50000)
88