1*412f47f9SXin Li /*
2*412f47f9SXin Li * Helper for single-precision routines which calculate exp(x) - 1 and do not
3*412f47f9SXin Li * need special-case handling
4*412f47f9SXin Li *
5*412f47f9SXin Li * Copyright (c) 2022-2024, 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_V_EXPM1F_INLINE_H
10*412f47f9SXin Li #define PL_MATH_V_EXPM1F_INLINE_H
11*412f47f9SXin Li
12*412f47f9SXin Li #include "v_math.h"
13*412f47f9SXin Li #include "math_config.h"
14*412f47f9SXin Li #include "poly_advsimd_f32.h"
15*412f47f9SXin Li
16*412f47f9SXin Li struct v_expm1f_data
17*412f47f9SXin Li {
18*412f47f9SXin Li float32x4_t poly[5];
19*412f47f9SXin Li float invln2_and_ln2[4];
20*412f47f9SXin Li float32x4_t shift;
21*412f47f9SXin Li int32x4_t exponent_bias;
22*412f47f9SXin Li };
23*412f47f9SXin Li
24*412f47f9SXin Li /* Coefficients generated using fpminimax with degree=5 in [-log(2)/2,
25*412f47f9SXin Li log(2)/2]. Exponent bias is asuint(1.0f).
26*412f47f9SXin Li invln2_and_ln2 Stores constants: invln2, ln2_lo, ln2_hi, 0. */
27*412f47f9SXin Li #define V_EXPM1F_DATA \
28*412f47f9SXin Li { \
29*412f47f9SXin Li .poly = { V4 (0x1.fffffep-2), V4 (0x1.5554aep-3), V4 (0x1.555736p-5), \
30*412f47f9SXin Li V4 (0x1.12287cp-7), V4 (0x1.6b55a2p-10) }, \
31*412f47f9SXin Li .shift = V4 (0x1.8p23f), .exponent_bias = V4 (0x3f800000), \
32*412f47f9SXin Li .invln2_and_ln2 = { 0x1.715476p+0f, 0x1.62e4p-1f, 0x1.7f7d1cp-20f, 0 }, \
33*412f47f9SXin Li }
34*412f47f9SXin Li
35*412f47f9SXin Li static inline float32x4_t
expm1f_inline(float32x4_t x,const struct v_expm1f_data * d)36*412f47f9SXin Li expm1f_inline (float32x4_t x, const struct v_expm1f_data *d)
37*412f47f9SXin Li {
38*412f47f9SXin Li /* Helper routine for calculating exp(x) - 1.
39*412f47f9SXin Li Copied from v_expm1f_1u6.c, with all special-case handling removed - the
40*412f47f9SXin Li calling routine should handle special values if required. */
41*412f47f9SXin Li
42*412f47f9SXin Li /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */
43*412f47f9SXin Li float32x4_t invln2_and_ln2 = vld1q_f32 (d->invln2_and_ln2);
44*412f47f9SXin Li float32x4_t j
45*412f47f9SXin Li = vsubq_f32 (vfmaq_laneq_f32 (d->shift, x, invln2_and_ln2, 0), d->shift);
46*412f47f9SXin Li int32x4_t i = vcvtq_s32_f32 (j);
47*412f47f9SXin Li float32x4_t f = vfmsq_laneq_f32 (x, j, invln2_and_ln2, 1);
48*412f47f9SXin Li f = vfmsq_laneq_f32 (f, j, invln2_and_ln2, 2);
49*412f47f9SXin Li
50*412f47f9SXin Li /* Approximate expm1(f) with polynomial P, expm1(f) ~= f + f^2 * P(f).
51*412f47f9SXin Li Uses Estrin scheme, where the main _ZGVnN4v_expm1f routine uses
52*412f47f9SXin Li Horner. */
53*412f47f9SXin Li float32x4_t f2 = vmulq_f32 (f, f);
54*412f47f9SXin Li float32x4_t f4 = vmulq_f32 (f2, f2);
55*412f47f9SXin Li float32x4_t p = v_estrin_4_f32 (f, f2, f4, d->poly);
56*412f47f9SXin Li p = vfmaq_f32 (f, f2, p);
57*412f47f9SXin Li
58*412f47f9SXin Li /* t = 2^i. */
59*412f47f9SXin Li int32x4_t u = vaddq_s32 (vshlq_n_s32 (i, 23), d->exponent_bias);
60*412f47f9SXin Li float32x4_t t = vreinterpretq_f32_s32 (u);
61*412f47f9SXin Li /* expm1(x) ~= p * t + (t - 1). */
62*412f47f9SXin Li return vfmaq_f32 (vsubq_f32 (t, v_f32 (1.0f)), p, t);
63*412f47f9SXin Li }
64*412f47f9SXin Li
65*412f47f9SXin Li #endif // PL_MATH_V_EXPM1F_INLINE_H
66