2 * Single-precision vector e^x function.
4 * Copyright (c) 2019, Arm Limited.
5 * SPDX-License-Identifier: MIT
12 static const float Poly[] = {
13 /* maxerr: 1.45358 +0.5 ulp. */
20 #define C0 v_f32 (Poly[0])
21 #define C1 v_f32 (Poly[1])
22 #define C2 v_f32 (Poly[2])
23 #define C3 v_f32 (Poly[3])
24 #define C4 v_f32 (Poly[4])
26 #define Shift v_f32 (0x1.8p23f)
27 #define InvLn2 v_f32 (0x1.715476p+0f)
28 #define Ln2hi v_f32 (0x1.62e4p-1f)
29 #define Ln2lo v_f32 (0x1.7f7d1cp-20f)
33 specialcase (v_f32_t poly, v_f32_t n, v_u32_t e, v_f32_t absn, v_u32_t cmp1, v_f32_t scale)
35 /* 2^n may overflow, break it up into s1*s2. */
36 v_u32_t b = v_cond_u32 (n <= v_f32 (0.0f)) & v_u32 (0x82000000);
37 v_f32_t s1 = v_as_f32_u32 (v_u32 (0x7f000000) + b);
38 v_f32_t s2 = v_as_f32_u32 (e - b);
39 v_u32_t cmp2 = v_cond_u32 (absn > v_f32 (192.0f));
40 v_u32_t r2 = v_as_u32_f32 (s1 * s1);
41 v_u32_t r1 = v_as_u32_f32 (v_fma_f32 (poly, s2, s2) * s1);
42 /* Similar to r1 but avoids double rounding in the subnormal range. */
43 v_u32_t r0 = v_as_u32_f32 (v_fma_f32 (poly, scale, scale));
44 return v_as_f32_u32 ((cmp2 & r2) | (~cmp2 & cmp1 & r1) | (~cmp1 & r0));
49 V_NAME(expf) (v_f32_t x)
51 v_f32_t n, r, r2, scale, p, q, poly, absn, z;
54 /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
55 x = ln2*n + r, with r in [-ln2/2, ln2/2]. */
57 z = v_fma_f32 (x, InvLn2, Shift);
59 r = v_fma_f32 (n, -Ln2hi, x);
60 r = v_fma_f32 (n, -Ln2lo, r);
61 e = v_as_u32_f32 (z) << 23;
65 r = v_fma_f32 (n, -Ln2hi, x);
66 r = v_fma_f32 (n, -Ln2lo, r);
67 e = v_as_u32_s32 (v_round_s32 (z)) << 23;
69 scale = v_as_f32_u32 (e + v_u32 (0x3f800000));
71 cmp = v_cond_u32 (absn > v_f32 (126.0f));
73 p = v_fma_f32 (C0, r, C1);
74 q = v_fma_f32 (C2, r, C3);
75 q = v_fma_f32 (p, r2, q);
77 poly = v_fma_f32 (q, r2, p);
78 if (unlikely (v_any_u32 (cmp)))
79 return specialcase (poly, n, e, absn, cmp, scale);
80 return v_fma_f32 (poly, scale, scale);