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 
13 static const float Poly[] = {
14   /*  maxerr: 0.36565 +0.5 ulp.  */
15   0x1.6a6000p-10f,
16   0x1.12718ep-7f,
17   0x1.555af0p-5f,
18   0x1.555430p-3f,
19   0x1.fffff4p-2f,
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 
32 VPCS_ATTR
33 static v_f32_t
specialcase(v_f32_t poly,v_f32_t n,v_u32_t e,v_f32_t absn)34 specialcase (v_f32_t poly, v_f32_t n, v_u32_t e, v_f32_t absn)
35 {
36   /* 2^n may overflow, break it up into s1*s2.  */
37   v_u32_t b = v_cond_u32 (n <= v_f32 (0.0f)) & v_u32 (0x83000000);
38   v_f32_t s1 = v_as_f32_u32 (v_u32 (0x7f000000) + b);
39   v_f32_t s2 = v_as_f32_u32 (e - b);
40   v_u32_t cmp = v_cond_u32 (absn > v_f32 (192.0f));
41   v_f32_t r1 = s1 * s1;
42   v_f32_t r0 = poly * s1 * s2;
43   return v_as_f32_u32 ((cmp & v_as_u32_f32 (r1)) | (~cmp & v_as_u32_f32 (r0)));
44 }
45 
46 VPCS_ATTR
47 v_f32_t
V_NAME(expf_1u)48 V_NAME(expf_1u) (v_f32_t x)
49 {
50   v_f32_t n, r, scale, poly, absn, z;
51   v_u32_t cmp, e;
52 
53   /* exp(x) = 2^n * poly(r), with poly(r) in [1/sqrt(2),sqrt(2)]
54      x = ln2*n + r, with r in [-ln2/2, ln2/2].  */
55 #if 1
56   z = v_fma_f32 (x, InvLn2, Shift);
57   n = z - Shift;
58   r = v_fma_f32 (n, -Ln2hi, x);
59   r = v_fma_f32 (n, -Ln2lo, r);
60   e = v_as_u32_f32 (z) << 23;
61 #else
62   z = x * InvLn2;
63   n = v_round_f32 (z);
64   r = v_fma_f32 (n, -Ln2hi, x);
65   r = v_fma_f32 (n, -Ln2lo, r);
66   e = v_as_u32_s32 (v_round_s32 (z)) << 23;
67 #endif
68   scale = v_as_f32_u32 (e + v_u32 (0x3f800000));
69   absn = v_abs_f32 (n);
70   cmp = v_cond_u32 (absn > v_f32 (126.0f));
71   poly = v_fma_f32 (C0, r, C1);
72   poly = v_fma_f32 (poly, r, C2);
73   poly = v_fma_f32 (poly, r, C3);
74   poly = v_fma_f32 (poly, r, C4);
75   poly = v_fma_f32 (poly, r, v_f32 (1.0f));
76   poly = v_fma_f32 (poly, r, v_f32 (1.0f));
77   if (unlikely (v_any_u32 (cmp)))
78     return specialcase (poly, n, e, absn);
79   return scale * poly;
80 }
81 #endif
82