1/*
2 * Single-precision vector e^x function.
3 *
4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 * See https://llvm.org/LICENSE.txt for license information.
6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 */
8
9#include "mathlib.h"
10#include "v_math.h"
11#if V_SUPPORTED
12
13static const float Poly[] = {
14 /* maxerr: 1.45358 +0.5 ulp. */
15 0x1.0e4020p-7f,
16 0x1.573e2ep-5f,
17 0x1.555e66p-3f,
18 0x1.fffdb6p-2f,
19 0x1.ffffecp-1f,
20};
21#define C0 v_f32 (Poly[0])
22#define C1 v_f32 (Poly[1])
23#define C2 v_f32 (Poly[2])
24#define C3 v_f32 (Poly[3])
25#define C4 v_f32 (Poly[4])
26
27#define Shift v_f32 (0x1.8p23f)
28#define InvLn2 v_f32 (0x1.715476p+0f)
29#define Ln2hi v_f32 (0x1.62e4p-1f)
30#define Ln2lo v_f32 (0x1.7f7d1cp-20f)
31
32VPCS_ATTR
33static v_f32_t
34specialcase (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{
36 /* 2^n may overflow, break it up into s1*s2. */
37 v_u32_t b = v_cond_u32 (x: n <= v_f32 (x: 0.0f)) & v_u32 (x: 0x82000000);
38 v_f32_t s1 = v_as_f32_u32 (x: v_u32 (x: 0x7f000000) + b);
39 v_f32_t s2 = v_as_f32_u32 (x: e - b);
40 v_u32_t cmp2 = v_cond_u32 (x: absn > v_f32 (x: 192.0f));
41 v_u32_t r2 = v_as_u32_f32 (x: s1 * s1);
42 v_u32_t r1 = v_as_u32_f32 (x: v_fma_f32 (x: poly, y: s2, z: s2) * s1);
43 /* Similar to r1 but avoids double rounding in the subnormal range. */
44 v_u32_t r0 = v_as_u32_f32 (x: v_fma_f32 (x: poly, y: scale, z: scale));
45 return v_as_f32_u32 (x: (cmp2 & r2) | (~cmp2 & cmp1 & r1) | (~cmp1 & r0));
46}
47
48VPCS_ATTR
49v_f32_t
50V_NAME(expf) (v_f32_t x)
51{
52 v_f32_t n, r, r2, scale, p, q, poly, absn, z;
53 v_u32_t cmp, e;
54
55 /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
56 x = ln2*n + r, with r in [-ln2/2, ln2/2]. */
57#if 1
58 z = v_fma_f32 (x, InvLn2, Shift);
59 n = z - Shift;
60 r = v_fma_f32 (x: n, y: -Ln2hi, z: x);
61 r = v_fma_f32 (x: n, y: -Ln2lo, z: r);
62 e = v_as_u32_f32 (x: z) << 23;
63#else
64 z = x * InvLn2;
65 n = v_round_f32 (z);
66 r = v_fma_f32 (n, -Ln2hi, x);
67 r = v_fma_f32 (n, -Ln2lo, r);
68 e = v_as_u32_s32 (v_round_s32 (z)) << 23;
69#endif
70 scale = v_as_f32_u32 (x: e + v_u32 (x: 0x3f800000));
71 absn = v_abs_f32 (x: n);
72 cmp = v_cond_u32 (x: absn > v_f32 (x: 126.0f));
73 r2 = r * r;
74 p = v_fma_f32 (C0, y: r, C1);
75 q = v_fma_f32 (C2, y: r, C3);
76 q = v_fma_f32 (x: p, y: r2, z: q);
77 p = C4 * r;
78 poly = v_fma_f32 (x: q, y: r2, z: p);
79 if (unlikely (v_any_u32 (cmp)))
80 return specialcase (poly, n, e, absn, cmp1: cmp, scale);
81 return v_fma_f32 (x: poly, y: scale, z: scale);
82}
83VPCS_ALIAS
84#endif
85

source code of libc/AOR_v20.02/math/v_expf.c