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