[clang] Avoid linking libdl unless needed
[llvm-project.git] / libc / AOR_v20.02 / math / v_exp.c
blob9074cecdd1543b3e5f5ab6266ccfd6163619e3ef
1 /*
2 * Double-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
7 */
9 #include "mathlib.h"
10 #include "v_math.h"
11 #if V_SUPPORTED
12 #include "v_exp.h"
14 #if V_EXP_TABLE_BITS == 7
15 /* maxerr: 1.88 +0.5 ulp
16 rel error: 1.4337*2^-53
17 abs error: 1.4299*2^-53 in [ -ln2/256, ln2/256 ]. */
18 #define C1 v_f64 (0x1.ffffffffffd43p-2)
19 #define C2 v_f64 (0x1.55555c75adbb2p-3)
20 #define C3 v_f64 (0x1.55555da646206p-5)
21 #define InvLn2 v_f64 (0x1.71547652b82fep7) /* N/ln2. */
22 #define Ln2hi v_f64 (0x1.62e42fefa39efp-8) /* ln2/N. */
23 #define Ln2lo v_f64 (0x1.abc9e3b39803f3p-63)
24 #elif V_EXP_TABLE_BITS == 8
25 /* maxerr: 0.54 +0.5 ulp
26 rel error: 1.4318*2^-58
27 abs error: 1.4299*2^-58 in [ -ln2/512, ln2/512 ]. */
28 #define C1 v_f64 (0x1.fffffffffffd4p-2)
29 #define C2 v_f64 (0x1.5555571d6b68cp-3)
30 #define C3 v_f64 (0x1.5555576a59599p-5)
31 #define InvLn2 v_f64 (0x1.71547652b82fep8)
32 #define Ln2hi v_f64 (0x1.62e42fefa39efp-9)
33 #define Ln2lo v_f64 (0x1.abc9e3b39803f3p-64)
34 #endif
36 #define N (1 << V_EXP_TABLE_BITS)
37 #define Tab __v_exp_data
38 #define IndexMask v_u64 (N - 1)
39 #define Shift v_f64 (0x1.8p+52)
40 #define Thres v_f64 (704.0)
42 VPCS_ATTR
43 static v_f64_t
44 specialcase (v_f64_t s, v_f64_t y, v_f64_t n)
46 v_f64_t absn = v_abs_f64 (n);
48 /* 2^(n/N) may overflow, break it up into s1*s2. */
49 v_u64_t b = v_cond_u64 (n <= v_f64 (0.0)) & v_u64 (0x6000000000000000);
50 v_f64_t s1 = v_as_f64_u64 (v_u64 (0x7000000000000000) - b);
51 v_f64_t s2 = v_as_f64_u64 (v_as_u64_f64 (s) - v_u64 (0x3010000000000000) + b);
52 v_u64_t cmp = v_cond_u64 (absn > v_f64 (1280.0 * N));
53 v_f64_t r1 = s1 * s1;
54 v_f64_t r0 = v_fma_f64 (y, s2, s2) * s1;
55 return v_as_f64_u64 ((cmp & v_as_u64_f64 (r1)) | (~cmp & v_as_u64_f64 (r0)));
58 VPCS_ATTR
59 v_f64_t
60 V_NAME(exp) (v_f64_t x)
62 v_f64_t n, r, r2, s, y, z;
63 v_u64_t cmp, u, e, i;
65 cmp = v_cond_u64 (v_abs_f64 (x) > Thres);
67 /* n = round(x/(ln2/N)). */
68 z = v_fma_f64 (x, InvLn2, Shift);
69 u = v_as_u64_f64 (z);
70 n = z - Shift;
72 /* r = x - n*ln2/N. */
73 r = x;
74 r = v_fma_f64 (-Ln2hi, n, r);
75 r = v_fma_f64 (-Ln2lo, n, r);
77 e = u << (52 - V_EXP_TABLE_BITS);
78 i = u & IndexMask;
80 /* y = exp(r) - 1 ~= r + C1 r^2 + C2 r^3 + C3 r^4. */
81 r2 = r * r;
82 y = v_fma_f64 (C2, r, C1);
83 y = v_fma_f64 (C3, r2, y);
84 y = v_fma_f64 (y, r2, r);
86 /* s = 2^(n/N). */
87 u = v_lookup_u64 (Tab, i);
88 s = v_as_f64_u64 (u + e);
90 if (unlikely (v_any_u64 (cmp)))
91 return specialcase (s, y, n);
92 return v_fma_f64 (y, s, s);
94 VPCS_ALIAS
95 #endif