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