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