1 /*
2  * Single-precision vector 2^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.878 ulp.  */
15   0x1.416b5ep-13f, 0x1.5f082ep-10f, 0x1.3b2dep-7f, 0x1.c6af7cp-5f, 0x1.ebfbdcp-3f, 0x1.62e43p-1f
16 };
17 #define C0 v_f32 (Poly[0])
18 #define C1 v_f32 (Poly[1])
19 #define C2 v_f32 (Poly[2])
20 #define C3 v_f32 (Poly[3])
21 #define C4 v_f32 (Poly[4])
22 #define C5 v_f32 (Poly[5])
23 
TEST(Reductions,Int4Ops)24 #define Shift v_f32 (0x1.8p23f)
25 #define InvLn2 v_f32 (0x1.715476p+0f)
26 #define Ln2hi v_f32 (0x1.62e4p-1f)
27 #define Ln2lo v_f32 (0x1.7f7d1cp-20f)
28 
29 VPCS_ATTR
30 static v_f32_t
31 specialcase (v_f32_t poly, v_f32_t n, v_u32_t e, v_f32_t absn)
32 {
33   /* 2^n may overflow, break it up into s1*s2.  */
34   v_u32_t b = v_cond_u32 (n <= v_f32 (0.0f)) & v_u32 (0x83000000);
35   v_f32_t s1 = v_as_f32_u32 (v_u32 (0x7f000000) + b);
36   v_f32_t s2 = v_as_f32_u32 (e - b);
37   v_u32_t cmp = v_cond_u32 (absn > v_f32 (192.0f));
38   v_f32_t r1 = s1 * s1;
39   v_f32_t r0 = poly * s1 * s2;
40   return v_as_f32_u32 ((cmp & v_as_u32_f32 (r1)) | (~cmp & v_as_u32_f32 (r0)));
41 }
42 
43 VPCS_ATTR
44 v_f32_t
45 V_NAME(exp2f_1u) (v_f32_t x)
46 {
47   v_f32_t n, r, scale, poly, absn;
48   v_u32_t cmp, e;
49 
50   /* exp2(x) = 2^n * poly(r), with poly(r) in [1/sqrt(2),sqrt(2)]
51      x = n + r, with r in [-1/2, 1/2].  */
52 #if 0
53   v_f32_t z;
54   z = x + Shift;
55   n = z - Shift;
TEST(Reductions,DoubleMaxMinNorm2)56   r = x - n;
57   e = v_as_u32_f32 (z) << 23;
58 #else
59   n = v_round_f32 (x);
60   r = x - n;
61   e = v_as_u32_s32 (v_round_s32 (x)) << 23;
62 #endif
63   scale = v_as_f32_u32 (e + v_u32 (0x3f800000));
64   absn = v_abs_f32 (n);
65   cmp = v_cond_u32 (absn > v_f32 (126.0f));
66   poly = v_fma_f32 (C0, r, C1);
67   poly = v_fma_f32 (poly, r, C2);
68   poly = v_fma_f32 (poly, r, C3);
69   poly = v_fma_f32 (poly, r, C4);
70   poly = v_fma_f32 (poly, r, C5);
71   poly = v_fma_f32 (poly, r, v_f32 (1.0f));
72   if (unlikely (v_any_u32 (cmp)))
73     return specialcase (poly, n, e, absn);
74   return scale * poly;
75 }
76 #endif
77