xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/v_expf_inline.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Helper for single-precision routines which calculate exp(ax) and do not
3*f3087befSAndrew Turner  * need special-case handling
4*f3087befSAndrew Turner  *
5*f3087befSAndrew Turner  * Copyright (c) 2019-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_V_EXPF_INLINE_H
10*f3087befSAndrew Turner #define MATH_V_EXPF_INLINE_H
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner #include "v_math.h"
13*f3087befSAndrew Turner 
14*f3087befSAndrew Turner struct v_expf_data
15*f3087befSAndrew Turner {
16*f3087befSAndrew Turner   float ln2_hi, ln2_lo, c0, c2;
17*f3087befSAndrew Turner   float32x4_t inv_ln2, c1, c3, c4;
18*f3087befSAndrew Turner   /* asuint(1.0f).  */
19*f3087befSAndrew Turner   uint32x4_t exponent_bias;
20*f3087befSAndrew Turner };
21*f3087befSAndrew Turner 
22*f3087befSAndrew Turner /* maxerr: 1.45358 +0.5 ulp.  */
23*f3087befSAndrew Turner #define V_EXPF_DATA                                                           \
24*f3087befSAndrew Turner   {                                                                           \
25*f3087befSAndrew Turner     .c0 = 0x1.0e4020p-7f, .c1 = V4 (0x1.573e2ep-5f), .c2 = 0x1.555e66p-3f,    \
26*f3087befSAndrew Turner     .c3 = V4 (0x1.fffdb6p-2f), .c4 = V4 (0x1.ffffecp-1f),                     \
27*f3087befSAndrew Turner     .ln2_hi = 0x1.62e4p-1f, .ln2_lo = 0x1.7f7d1cp-20f,                        \
28*f3087befSAndrew Turner     .inv_ln2 = V4 (0x1.715476p+0f), .exponent_bias = V4 (0x3f800000),         \
29*f3087befSAndrew Turner   }
30*f3087befSAndrew Turner 
31*f3087befSAndrew Turner static inline float32x4_t
v_expf_inline(float32x4_t x,const struct v_expf_data * d)32*f3087befSAndrew Turner v_expf_inline (float32x4_t x, const struct v_expf_data *d)
33*f3087befSAndrew Turner {
34*f3087befSAndrew Turner   /* Helper routine for calculating exp(ax).
35*f3087befSAndrew Turner      Copied from v_expf.c, with all special-case handling removed - the
36*f3087befSAndrew Turner      calling routine should handle special values if required.  */
37*f3087befSAndrew Turner 
38*f3087befSAndrew Turner   /* exp(ax) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
39*f3087befSAndrew Turner      ax = ln2*n + r, with r in [-ln2/2, ln2/2].  */
40*f3087befSAndrew Turner   float32x4_t ax = vabsq_f32 (x);
41*f3087befSAndrew Turner   float32x4_t ln2_c02 = vld1q_f32 (&d->ln2_hi);
42*f3087befSAndrew Turner   float32x4_t n = vrndaq_f32 (vmulq_f32 (ax, d->inv_ln2));
43*f3087befSAndrew Turner   float32x4_t r = vfmsq_laneq_f32 (ax, n, ln2_c02, 0);
44*f3087befSAndrew Turner   r = vfmsq_laneq_f32 (r, n, ln2_c02, 1);
45*f3087befSAndrew Turner   uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtq_s32_f32 (n)), 23);
46*f3087befSAndrew Turner   float32x4_t scale = vreinterpretq_f32_u32 (vaddq_u32 (e, d->exponent_bias));
47*f3087befSAndrew Turner 
48*f3087befSAndrew Turner   /* Custom order-4 Estrin avoids building high order monomial.  */
49*f3087befSAndrew Turner   float32x4_t r2 = vmulq_f32 (r, r);
50*f3087befSAndrew Turner   float32x4_t p = vfmaq_laneq_f32 (d->c1, r, ln2_c02, 2);
51*f3087befSAndrew Turner   float32x4_t q = vfmaq_laneq_f32 (d->c3, r, ln2_c02, 3);
52*f3087befSAndrew Turner   q = vfmaq_f32 (q, p, r2);
53*f3087befSAndrew Turner   p = vmulq_f32 (d->c4, r);
54*f3087befSAndrew Turner   float32x4_t poly = vfmaq_f32 (p, q, r2);
55*f3087befSAndrew Turner   return vfmaq_f32 (scale, poly, scale);
56*f3087befSAndrew Turner }
57*f3087befSAndrew Turner 
58*f3087befSAndrew Turner #endif // MATH_V_EXPF_INLINE_H
59