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