1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner * SVE helper for single-precision routines which calculate exp(x) - 1 and do
3*5a02ffc3SAndrew Turner * not need special-case handling
4*5a02ffc3SAndrew Turner *
5*5a02ffc3SAndrew Turner * Copyright (c) 2023, Arm Limited.
6*5a02ffc3SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7*5a02ffc3SAndrew Turner */
8*5a02ffc3SAndrew Turner
9*5a02ffc3SAndrew Turner #ifndef PL_MATH_SV_EXPM1F_INLINE_H
10*5a02ffc3SAndrew Turner #define PL_MATH_SV_EXPM1F_INLINE_H
11*5a02ffc3SAndrew Turner
12*5a02ffc3SAndrew Turner #include "sv_math.h"
13*5a02ffc3SAndrew Turner
14*5a02ffc3SAndrew Turner struct sv_expm1f_data
15*5a02ffc3SAndrew Turner {
16*5a02ffc3SAndrew Turner /* These 4 are grouped together so they can be loaded as one quadword, then
17*5a02ffc3SAndrew Turner used with _lane forms of svmla/svmls. */
18*5a02ffc3SAndrew Turner float32_t c2, c4, ln2_hi, ln2_lo;
19*5a02ffc3SAndrew Turner float32_t c0, c1, c3, inv_ln2, shift;
20*5a02ffc3SAndrew Turner };
21*5a02ffc3SAndrew Turner
22*5a02ffc3SAndrew Turner /* Coefficients generated using fpminimax. */
23*5a02ffc3SAndrew Turner #define SV_EXPM1F_DATA \
24*5a02ffc3SAndrew Turner { \
25*5a02ffc3SAndrew Turner .c0 = 0x1.fffffep-2, .c1 = 0x1.5554aep-3, .c2 = 0x1.555736p-5, \
26*5a02ffc3SAndrew Turner .c3 = 0x1.12287cp-7, .c4 = 0x1.6b55a2p-10, \
27*5a02ffc3SAndrew Turner \
28*5a02ffc3SAndrew Turner .shift = 0x1.8p23f, .inv_ln2 = 0x1.715476p+0f, .ln2_hi = 0x1.62e4p-1f, \
29*5a02ffc3SAndrew Turner .ln2_lo = 0x1.7f7d1cp-20f, \
30*5a02ffc3SAndrew Turner }
31*5a02ffc3SAndrew Turner
32*5a02ffc3SAndrew Turner #define C(i) sv_f32 (d->c##i)
33*5a02ffc3SAndrew Turner
34*5a02ffc3SAndrew Turner static inline svfloat32_t
expm1f_inline(svfloat32_t x,svbool_t pg,const struct sv_expm1f_data * d)35*5a02ffc3SAndrew Turner expm1f_inline (svfloat32_t x, svbool_t pg, const struct sv_expm1f_data *d)
36*5a02ffc3SAndrew Turner {
37*5a02ffc3SAndrew Turner /* This vector is reliant on layout of data - it contains constants
38*5a02ffc3SAndrew Turner that can be used with _lane forms of svmla/svmls. Values are:
39*5a02ffc3SAndrew Turner [ coeff_2, coeff_4, ln2_hi, ln2_lo ]. */
40*5a02ffc3SAndrew Turner svfloat32_t lane_constants = svld1rq (svptrue_b32 (), &d->c2);
41*5a02ffc3SAndrew Turner
42*5a02ffc3SAndrew Turner /* Reduce argument to smaller range:
43*5a02ffc3SAndrew Turner Let i = round(x / ln2)
44*5a02ffc3SAndrew Turner and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
45*5a02ffc3SAndrew Turner exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
46*5a02ffc3SAndrew Turner where 2^i is exact because i is an integer. */
47*5a02ffc3SAndrew Turner svfloat32_t j = svmla_x (pg, sv_f32 (d->shift), x, d->inv_ln2);
48*5a02ffc3SAndrew Turner j = svsub_x (pg, j, d->shift);
49*5a02ffc3SAndrew Turner svint32_t i = svcvt_s32_x (pg, j);
50*5a02ffc3SAndrew Turner
51*5a02ffc3SAndrew Turner svfloat32_t f = svmls_lane (x, j, lane_constants, 2);
52*5a02ffc3SAndrew Turner f = svmls_lane (f, j, lane_constants, 3);
53*5a02ffc3SAndrew Turner
54*5a02ffc3SAndrew Turner /* Approximate expm1(f) using polynomial.
55*5a02ffc3SAndrew Turner Taylor expansion for expm1(x) has the form:
56*5a02ffc3SAndrew Turner x + ax^2 + bx^3 + cx^4 ....
57*5a02ffc3SAndrew Turner So we calculate the polynomial P(f) = a + bf + cf^2 + ...
58*5a02ffc3SAndrew Turner and assemble the approximation expm1(f) ~= f + f^2 * P(f). */
59*5a02ffc3SAndrew Turner svfloat32_t p12 = svmla_lane (C (1), f, lane_constants, 0);
60*5a02ffc3SAndrew Turner svfloat32_t p34 = svmla_lane (C (3), f, lane_constants, 1);
61*5a02ffc3SAndrew Turner svfloat32_t f2 = svmul_x (pg, f, f);
62*5a02ffc3SAndrew Turner svfloat32_t p = svmla_x (pg, p12, f2, p34);
63*5a02ffc3SAndrew Turner p = svmla_x (pg, C (0), f, p);
64*5a02ffc3SAndrew Turner p = svmla_x (pg, f, f2, p);
65*5a02ffc3SAndrew Turner
66*5a02ffc3SAndrew Turner /* Assemble the result.
67*5a02ffc3SAndrew Turner expm1(x) ~= 2^i * (p + 1) - 1
68*5a02ffc3SAndrew Turner Let t = 2^i. */
69*5a02ffc3SAndrew Turner svfloat32_t t = svscale_x (pg, sv_f32 (1), i);
70*5a02ffc3SAndrew Turner return svmla_x (pg, svsub_x (pg, t, 1), p, t);
71*5a02ffc3SAndrew Turner }
72*5a02ffc3SAndrew Turner
73*5a02ffc3SAndrew Turner #endif // PL_MATH_SV_EXPM1F_INLINE_H