1 /*
2 * Single-precision SVE 10^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 #define _GNU_SOURCE
9 #include "sv_math.h"
10 #include "test_sig.h"
11 #include "test_defs.h"
12 #include "sv_poly_f32.h"
13
14 /* For x < -Thres, the result is subnormal and not handled correctly by
15 FEXPA. */
16 #define Thres 37.9
17
18 static const struct data
19 {
20 float log2_10_lo, c0, c2, c4;
21 float c1, c3, log10_2;
22 float shift, log2_10_hi, thres;
23 } data = {
24 /* Coefficients generated using Remez algorithm with minimisation of relative
25 error.
26 rel error: 0x1.89dafa3p-24
27 abs error: 0x1.167d55p-23 in [-log10(2)/2, log10(2)/2]
28 maxerr: 0.52 +0.5 ulp. */
29 .c0 = 0x1.26bb16p+1f,
30 .c1 = 0x1.5350d2p+1f,
31 .c2 = 0x1.04744ap+1f,
32 .c3 = 0x1.2d8176p+0f,
33 .c4 = 0x1.12b41ap-1f,
34 /* 1.5*2^17 + 127, a shift value suitable for FEXPA. */
35 .shift = 0x1.803f8p17f,
36 .log10_2 = 0x1.a934fp+1,
37 .log2_10_hi = 0x1.344136p-2,
38 .log2_10_lo = -0x1.ec10cp-27,
39 .thres = Thres,
40 };
41
42 static inline svfloat32_t
sv_exp10f_inline(svfloat32_t x,const svbool_t pg,const struct data * d)43 sv_exp10f_inline (svfloat32_t x, const svbool_t pg, const struct data *d)
44 {
45 /* exp10(x) = 2^(n/N) * 10^r = 2^n * (1 + poly (r)),
46 with poly(r) in [1/sqrt(2), sqrt(2)] and
47 x = r + n * log10(2) / N, with r in [-log10(2)/2N, log10(2)/2N]. */
48
49 svfloat32_t lane_consts = svld1rq (svptrue_b32 (), &d->log2_10_lo);
50
51 /* n = round(x/(log10(2)/N)). */
52 svfloat32_t shift = sv_f32 (d->shift);
53 svfloat32_t z = svmad_x (pg, sv_f32 (d->log10_2), x, shift);
54 svfloat32_t n = svsub_x (svptrue_b32 (), z, shift);
55
56 /* r = x - n*log10(2)/N. */
57 svfloat32_t r = svmsb_x (pg, sv_f32 (d->log2_10_hi), n, x);
58 r = svmls_lane (r, n, lane_consts, 0);
59
60 svfloat32_t scale = svexpa (svreinterpret_u32 (z));
61
62 /* Polynomial evaluation: poly(r) ~ exp10(r)-1. */
63 svfloat32_t p12 = svmla_lane (sv_f32 (d->c1), r, lane_consts, 2);
64 svfloat32_t p34 = svmla_lane (sv_f32 (d->c3), r, lane_consts, 3);
65 svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);
66 svfloat32_t p14 = svmla_x (pg, p12, p34, r2);
67 svfloat32_t p0 = svmul_lane (r, lane_consts, 1);
68 svfloat32_t poly = svmla_x (pg, p0, r2, p14);
69
70 return svmla_x (pg, scale, scale, poly);
71 }
72
73 static svfloat32_t NOINLINE
special_case(svfloat32_t x,svbool_t special,const struct data * d)74 special_case (svfloat32_t x, svbool_t special, const struct data *d)
75 {
76 return sv_call_f32 (exp10f, x, sv_exp10f_inline (x, svptrue_b32 (), d),
77 special);
78 }
79
80 /* Single-precision SVE exp10f routine. Implements the same algorithm
81 as AdvSIMD exp10f.
82 Worst case error is 1.02 ULPs.
83 _ZGVsMxv_exp10f(-0x1.040488p-4) got 0x1.ba5f9ep-1
84 want 0x1.ba5f9cp-1. */
SV_NAME_F1(exp10)85 svfloat32_t SV_NAME_F1 (exp10) (svfloat32_t x, const svbool_t pg)
86 {
87 const struct data *d = ptr_barrier (&data);
88 svbool_t special = svacgt (pg, x, d->thres);
89 if (unlikely (svptest_any (special, special)))
90 return special_case (x, special, d);
91 return sv_exp10f_inline (x, pg, d);
92 }
93
94 #if WANT_EXP10_TESTS
95 TEST_SIG (SV, F, 1, exp10, -9.9, 9.9)
96 TEST_ULP (SV_NAME_F1 (exp10), 0.52)
97 TEST_DISABLE_FENV (SV_NAME_F1 (exp10))
98 TEST_SYM_INTERVAL (SV_NAME_F1 (exp10), 0, Thres, 50000)
99 TEST_SYM_INTERVAL (SV_NAME_F1 (exp10), Thres, inf, 50000)
100 #endif
101 CLOSE_SVE_ATTR
102