2 * Single-precision vector e^x function.
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
13 static const float Poly
[] = {
14 /* maxerr: 0.36565 +0.5 ulp. */
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])
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)
34 specialcase (v_f32_t poly
, v_f32_t n
, v_u32_t e
, v_f32_t absn
)
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
));
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
)));
48 V_NAME(expf_1u
) (v_f32_t x
)
50 v_f32_t n
, r
, scale
, poly
, absn
, z
;
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]. */
56 z
= v_fma_f32 (x
, InvLn2
, 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;
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;
68 scale
= v_as_f32_u32 (e
+ v_u32 (0x3f800000));
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
);