1072a4ba8SAndrew Turner /*
2072a4ba8SAndrew Turner * Single-precision vector e^x function.
3072a4ba8SAndrew Turner *
4072a4ba8SAndrew Turner * Copyright (c) 2019-2023, Arm Limited.
5072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6072a4ba8SAndrew Turner */
7072a4ba8SAndrew Turner
8072a4ba8SAndrew Turner #include "sv_math.h"
9072a4ba8SAndrew Turner #include "pl_sig.h"
10072a4ba8SAndrew Turner #include "pl_test.h"
11072a4ba8SAndrew Turner
12*5a02ffc3SAndrew Turner static const struct data
13072a4ba8SAndrew Turner {
14*5a02ffc3SAndrew Turner float poly[5];
15*5a02ffc3SAndrew Turner float inv_ln2, ln2_hi, ln2_lo, shift, thres;
16*5a02ffc3SAndrew Turner } data = {
17*5a02ffc3SAndrew Turner /* Coefficients copied from the polynomial in AdvSIMD variant, reversed for
18*5a02ffc3SAndrew Turner compatibility with polynomial helpers. */
19*5a02ffc3SAndrew Turner .poly = { 0x1.ffffecp-1f, 0x1.fffdb6p-2f, 0x1.555e66p-3f, 0x1.573e2ep-5f,
20*5a02ffc3SAndrew Turner 0x1.0e4020p-7f },
21*5a02ffc3SAndrew Turner .inv_ln2 = 0x1.715476p+0f,
22*5a02ffc3SAndrew Turner .ln2_hi = 0x1.62e4p-1f,
23*5a02ffc3SAndrew Turner .ln2_lo = 0x1.7f7d1cp-20f,
24*5a02ffc3SAndrew Turner /* 1.5*2^17 + 127. */
25*5a02ffc3SAndrew Turner .shift = 0x1.903f8p17f,
26*5a02ffc3SAndrew Turner /* Roughly 87.3. For x < -Thres, the result is subnormal and not handled
27*5a02ffc3SAndrew Turner correctly by FEXPA. */
28*5a02ffc3SAndrew Turner .thres = 0x1.5d5e2ap+6f,
29*5a02ffc3SAndrew Turner };
30*5a02ffc3SAndrew Turner
31*5a02ffc3SAndrew Turner #define C(i) sv_f32 (d->poly[i])
32*5a02ffc3SAndrew Turner #define ExponentBias 0x3f800000
33*5a02ffc3SAndrew Turner
34*5a02ffc3SAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t special)35*5a02ffc3SAndrew Turner special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
36*5a02ffc3SAndrew Turner {
37072a4ba8SAndrew Turner return sv_call_f32 (expf, x, y, special);
38072a4ba8SAndrew Turner }
39072a4ba8SAndrew Turner
40*5a02ffc3SAndrew Turner /* Optimised single-precision SVE exp function.
41*5a02ffc3SAndrew Turner Worst-case error is 1.04 ulp:
42*5a02ffc3SAndrew Turner SV_NAME_F1 (exp)(0x1.a8eda4p+1) got 0x1.ba74bcp+4
43072a4ba8SAndrew Turner want 0x1.ba74bap+4. */
SV_NAME_F1(exp)44*5a02ffc3SAndrew Turner svfloat32_t SV_NAME_F1 (exp) (svfloat32_t x, const svbool_t pg)
45072a4ba8SAndrew Turner {
46*5a02ffc3SAndrew Turner const struct data *d = ptr_barrier (&data);
47*5a02ffc3SAndrew Turner
48072a4ba8SAndrew Turner /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
49072a4ba8SAndrew Turner x = ln2*n + r, with r in [-ln2/2, ln2/2]. */
50072a4ba8SAndrew Turner
51*5a02ffc3SAndrew Turner /* Load some constants in quad-word chunks to minimise memory access (last
52*5a02ffc3SAndrew Turner lane is wasted). */
53*5a02ffc3SAndrew Turner svfloat32_t invln2_and_ln2 = svld1rq (svptrue_b32 (), &d->inv_ln2);
54*5a02ffc3SAndrew Turner
55072a4ba8SAndrew Turner /* n = round(x/(ln2/N)). */
56*5a02ffc3SAndrew Turner svfloat32_t z = svmla_lane (sv_f32 (d->shift), x, invln2_and_ln2, 0);
57*5a02ffc3SAndrew Turner svfloat32_t n = svsub_x (pg, z, d->shift);
58072a4ba8SAndrew Turner
59072a4ba8SAndrew Turner /* r = x - n*ln2/N. */
60*5a02ffc3SAndrew Turner svfloat32_t r = svmls_lane (x, n, invln2_and_ln2, 1);
61*5a02ffc3SAndrew Turner r = svmls_lane (r, n, invln2_and_ln2, 2);
62072a4ba8SAndrew Turner
63072a4ba8SAndrew Turner /* scale = 2^(n/N). */
64*5a02ffc3SAndrew Turner svbool_t is_special_case = svacgt (pg, x, d->thres);
65*5a02ffc3SAndrew Turner svfloat32_t scale = svexpa (svreinterpret_u32 (z));
66072a4ba8SAndrew Turner
67*5a02ffc3SAndrew Turner /* y = exp(r) - 1 ~= r + C0 r^2 + C1 r^3 + C2 r^4 + C3 r^5 + C4 r^6. */
68*5a02ffc3SAndrew Turner svfloat32_t p12 = svmla_x (pg, C (1), C (2), r);
69*5a02ffc3SAndrew Turner svfloat32_t p34 = svmla_x (pg, C (3), C (4), r);
70*5a02ffc3SAndrew Turner svfloat32_t r2 = svmul_x (pg, r, r);
71*5a02ffc3SAndrew Turner svfloat32_t p14 = svmla_x (pg, p12, p34, r2);
72*5a02ffc3SAndrew Turner svfloat32_t p0 = svmul_x (pg, r, C (0));
73*5a02ffc3SAndrew Turner svfloat32_t poly = svmla_x (pg, p0, r2, p14);
74072a4ba8SAndrew Turner
75072a4ba8SAndrew Turner if (unlikely (svptest_any (pg, is_special_case)))
76*5a02ffc3SAndrew Turner return special_case (x, svmla_x (pg, scale, scale, poly), is_special_case);
77072a4ba8SAndrew Turner
78*5a02ffc3SAndrew Turner return svmla_x (pg, scale, scale, poly);
79072a4ba8SAndrew Turner }
80072a4ba8SAndrew Turner
81072a4ba8SAndrew Turner PL_SIG (SV, F, 1, exp, -9.9, 9.9)
82*5a02ffc3SAndrew Turner PL_TEST_ULP (SV_NAME_F1 (exp), 0.55)
83*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 0, 0x1p-23, 40000)
84*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 0x1p-23, 1, 50000)
85*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 1, 0x1p23, 50000)
86*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 0x1p23, inf, 50000)
87