1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-precision vector e^x function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2019-2023, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include "mathlib.h"
9*412f47f9SXin Li #include "v_math.h"
10*412f47f9SXin Li
11*412f47f9SXin Li static const struct data
12*412f47f9SXin Li {
13*412f47f9SXin Li float32x4_t poly[5];
14*412f47f9SXin Li float32x4_t shift, inv_ln2, ln2_hi, ln2_lo;
15*412f47f9SXin Li uint32x4_t exponent_bias;
16*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
17*412f47f9SXin Li float32x4_t special_bound, scale_thresh;
18*412f47f9SXin Li #endif
19*412f47f9SXin Li } data = {
20*412f47f9SXin Li /* maxerr: 1.45358 +0.5 ulp. */
21*412f47f9SXin Li .poly = { V4 (0x1.0e4020p-7f), V4 (0x1.573e2ep-5f), V4 (0x1.555e66p-3f),
22*412f47f9SXin Li V4 (0x1.fffdb6p-2f), V4 (0x1.ffffecp-1f) },
23*412f47f9SXin Li .shift = V4 (0x1.8p23f),
24*412f47f9SXin Li .inv_ln2 = V4 (0x1.715476p+0f),
25*412f47f9SXin Li .ln2_hi = V4 (0x1.62e4p-1f),
26*412f47f9SXin Li .ln2_lo = V4 (0x1.7f7d1cp-20f),
27*412f47f9SXin Li .exponent_bias = V4 (0x3f800000),
28*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
29*412f47f9SXin Li .special_bound = V4 (126.0f),
30*412f47f9SXin Li .scale_thresh = V4 (192.0f),
31*412f47f9SXin Li #endif
32*412f47f9SXin Li };
33*412f47f9SXin Li
34*412f47f9SXin Li #define C(i) d->poly[i]
35*412f47f9SXin Li
36*412f47f9SXin Li #if WANT_SIMD_EXCEPT
37*412f47f9SXin Li
38*412f47f9SXin Li # define TinyBound v_u32 (0x20000000) /* asuint (0x1p-63). */
39*412f47f9SXin Li # define BigBound v_u32 (0x42800000) /* asuint (0x1p6). */
40*412f47f9SXin Li # define SpecialBound v_u32 (0x22800000) /* BigBound - TinyBound. */
41*412f47f9SXin Li
42*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint32x4_t cmp)43*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)
44*412f47f9SXin Li {
45*412f47f9SXin Li /* If fenv exceptions are to be triggered correctly, fall back to the scalar
46*412f47f9SXin Li routine to special lanes. */
47*412f47f9SXin Li return v_call_f32 (expf, x, y, cmp);
48*412f47f9SXin Li }
49*412f47f9SXin Li
50*412f47f9SXin Li #else
51*412f47f9SXin Li
52*412f47f9SXin Li # define SpecialOffset v_u32 (0x82000000)
53*412f47f9SXin Li # define SpecialBias v_u32 (0x7f000000)
54*412f47f9SXin Li
55*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t poly,float32x4_t n,uint32x4_t e,uint32x4_t cmp1,float32x4_t scale,const struct data * d)56*412f47f9SXin Li special_case (float32x4_t poly, float32x4_t n, uint32x4_t e, uint32x4_t cmp1,
57*412f47f9SXin Li float32x4_t scale, const struct data *d)
58*412f47f9SXin Li {
59*412f47f9SXin Li /* 2^n may overflow, break it up into s1*s2. */
60*412f47f9SXin Li uint32x4_t b = vandq_u32 (vclezq_f32 (n), SpecialOffset);
61*412f47f9SXin Li float32x4_t s1 = vreinterpretq_f32_u32 (vaddq_u32 (b, SpecialBias));
62*412f47f9SXin Li float32x4_t s2 = vreinterpretq_f32_u32 (vsubq_u32 (e, b));
63*412f47f9SXin Li uint32x4_t cmp2 = vcagtq_f32 (n, d->scale_thresh);
64*412f47f9SXin Li float32x4_t r2 = vmulq_f32 (s1, s1);
65*412f47f9SXin Li float32x4_t r1 = vmulq_f32 (vfmaq_f32 (s2, poly, s2), s1);
66*412f47f9SXin Li /* Similar to r1 but avoids double rounding in the subnormal range. */
67*412f47f9SXin Li float32x4_t r0 = vfmaq_f32 (scale, poly, scale);
68*412f47f9SXin Li float32x4_t r = vbslq_f32 (cmp1, r1, r0);
69*412f47f9SXin Li return vbslq_f32 (cmp2, r2, r);
70*412f47f9SXin Li }
71*412f47f9SXin Li
72*412f47f9SXin Li #endif
73*412f47f9SXin Li
V_NAME_F1(exp)74*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F1 (exp) (float32x4_t x)
75*412f47f9SXin Li {
76*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
77*412f47f9SXin Li float32x4_t n, r, r2, scale, p, q, poly, z;
78*412f47f9SXin Li uint32x4_t cmp, e;
79*412f47f9SXin Li
80*412f47f9SXin Li #if WANT_SIMD_EXCEPT
81*412f47f9SXin Li /* asuint(x) - TinyBound >= BigBound - TinyBound. */
82*412f47f9SXin Li cmp = vcgeq_u32 (
83*412f47f9SXin Li vsubq_u32 (vandq_u32 (vreinterpretq_u32_f32 (x), v_u32 (0x7fffffff)),
84*412f47f9SXin Li TinyBound),
85*412f47f9SXin Li SpecialBound);
86*412f47f9SXin Li float32x4_t xm = x;
87*412f47f9SXin Li /* If any lanes are special, mask them with 1 and retain a copy of x to allow
88*412f47f9SXin Li special case handler to fix special lanes later. This is only necessary if
89*412f47f9SXin Li fenv exceptions are to be triggered correctly. */
90*412f47f9SXin Li if (unlikely (v_any_u32 (cmp)))
91*412f47f9SXin Li x = vbslq_f32 (cmp, v_f32 (1), x);
92*412f47f9SXin Li #endif
93*412f47f9SXin Li
94*412f47f9SXin Li /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
95*412f47f9SXin Li x = ln2*n + r, with r in [-ln2/2, ln2/2]. */
96*412f47f9SXin Li z = vfmaq_f32 (d->shift, x, d->inv_ln2);
97*412f47f9SXin Li n = vsubq_f32 (z, d->shift);
98*412f47f9SXin Li r = vfmsq_f32 (x, n, d->ln2_hi);
99*412f47f9SXin Li r = vfmsq_f32 (r, n, d->ln2_lo);
100*412f47f9SXin Li e = vshlq_n_u32 (vreinterpretq_u32_f32 (z), 23);
101*412f47f9SXin Li scale = vreinterpretq_f32_u32 (vaddq_u32 (e, d->exponent_bias));
102*412f47f9SXin Li
103*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
104*412f47f9SXin Li cmp = vcagtq_f32 (n, d->special_bound);
105*412f47f9SXin Li #endif
106*412f47f9SXin Li
107*412f47f9SXin Li r2 = vmulq_f32 (r, r);
108*412f47f9SXin Li p = vfmaq_f32 (C (1), C (0), r);
109*412f47f9SXin Li q = vfmaq_f32 (C (3), C (2), r);
110*412f47f9SXin Li q = vfmaq_f32 (q, p, r2);
111*412f47f9SXin Li p = vmulq_f32 (C (4), r);
112*412f47f9SXin Li poly = vfmaq_f32 (p, q, r2);
113*412f47f9SXin Li
114*412f47f9SXin Li if (unlikely (v_any_u32 (cmp)))
115*412f47f9SXin Li #if WANT_SIMD_EXCEPT
116*412f47f9SXin Li return special_case (xm, vfmaq_f32 (scale, poly, scale), cmp);
117*412f47f9SXin Li #else
118*412f47f9SXin Li return special_case (poly, n, e, cmp, scale, d);
119*412f47f9SXin Li #endif
120*412f47f9SXin Li
121*412f47f9SXin Li return vfmaq_f32 (scale, poly, scale);
122*412f47f9SXin Li }
123