1*0928368fSKristof Beyls /*
2*0928368fSKristof Beyls * Double-precision vector e^x function.
3*0928368fSKristof Beyls *
4*0928368fSKristof Beyls * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*0928368fSKristof Beyls * See https://llvm.org/LICENSE.txt for license information.
6*0928368fSKristof Beyls * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*0928368fSKristof Beyls */
8*0928368fSKristof Beyls
9*0928368fSKristof Beyls #include "mathlib.h"
10*0928368fSKristof Beyls #include "v_math.h"
11*0928368fSKristof Beyls #if V_SUPPORTED
12*0928368fSKristof Beyls #include "v_exp.h"
13*0928368fSKristof Beyls
14*0928368fSKristof Beyls #if V_EXP_TABLE_BITS == 7
15*0928368fSKristof Beyls /* maxerr: 1.88 +0.5 ulp
16*0928368fSKristof Beyls rel error: 1.4337*2^-53
17*0928368fSKristof Beyls abs error: 1.4299*2^-53 in [ -ln2/256, ln2/256 ]. */
18*0928368fSKristof Beyls #define C1 v_f64 (0x1.ffffffffffd43p-2)
19*0928368fSKristof Beyls #define C2 v_f64 (0x1.55555c75adbb2p-3)
20*0928368fSKristof Beyls #define C3 v_f64 (0x1.55555da646206p-5)
21*0928368fSKristof Beyls #define InvLn2 v_f64 (0x1.71547652b82fep7) /* N/ln2. */
22*0928368fSKristof Beyls #define Ln2hi v_f64 (0x1.62e42fefa39efp-8) /* ln2/N. */
23*0928368fSKristof Beyls #define Ln2lo v_f64 (0x1.abc9e3b39803f3p-63)
24*0928368fSKristof Beyls #elif V_EXP_TABLE_BITS == 8
25*0928368fSKristof Beyls /* maxerr: 0.54 +0.5 ulp
26*0928368fSKristof Beyls rel error: 1.4318*2^-58
27*0928368fSKristof Beyls abs error: 1.4299*2^-58 in [ -ln2/512, ln2/512 ]. */
28*0928368fSKristof Beyls #define C1 v_f64 (0x1.fffffffffffd4p-2)
29*0928368fSKristof Beyls #define C2 v_f64 (0x1.5555571d6b68cp-3)
30*0928368fSKristof Beyls #define C3 v_f64 (0x1.5555576a59599p-5)
31*0928368fSKristof Beyls #define InvLn2 v_f64 (0x1.71547652b82fep8)
32*0928368fSKristof Beyls #define Ln2hi v_f64 (0x1.62e42fefa39efp-9)
33*0928368fSKristof Beyls #define Ln2lo v_f64 (0x1.abc9e3b39803f3p-64)
34*0928368fSKristof Beyls #endif
35*0928368fSKristof Beyls
36*0928368fSKristof Beyls #define N (1 << V_EXP_TABLE_BITS)
37*0928368fSKristof Beyls #define Tab __v_exp_data
38*0928368fSKristof Beyls #define IndexMask v_u64 (N - 1)
39*0928368fSKristof Beyls #define Shift v_f64 (0x1.8p+52)
40*0928368fSKristof Beyls #define Thres v_f64 (704.0)
41*0928368fSKristof Beyls
42*0928368fSKristof Beyls VPCS_ATTR
43*0928368fSKristof Beyls static v_f64_t
specialcase(v_f64_t s,v_f64_t y,v_f64_t n)44*0928368fSKristof Beyls specialcase (v_f64_t s, v_f64_t y, v_f64_t n)
45*0928368fSKristof Beyls {
46*0928368fSKristof Beyls v_f64_t absn = v_abs_f64 (n);
47*0928368fSKristof Beyls
48*0928368fSKristof Beyls /* 2^(n/N) may overflow, break it up into s1*s2. */
49*0928368fSKristof Beyls v_u64_t b = v_cond_u64 (n <= v_f64 (0.0)) & v_u64 (0x6000000000000000);
50*0928368fSKristof Beyls v_f64_t s1 = v_as_f64_u64 (v_u64 (0x7000000000000000) - b);
51*0928368fSKristof Beyls v_f64_t s2 = v_as_f64_u64 (v_as_u64_f64 (s) - v_u64 (0x3010000000000000) + b);
52*0928368fSKristof Beyls v_u64_t cmp = v_cond_u64 (absn > v_f64 (1280.0 * N));
53*0928368fSKristof Beyls v_f64_t r1 = s1 * s1;
54*0928368fSKristof Beyls v_f64_t r0 = v_fma_f64 (y, s2, s2) * s1;
55*0928368fSKristof Beyls return v_as_f64_u64 ((cmp & v_as_u64_f64 (r1)) | (~cmp & v_as_u64_f64 (r0)));
56*0928368fSKristof Beyls }
57*0928368fSKristof Beyls
58*0928368fSKristof Beyls VPCS_ATTR
59*0928368fSKristof Beyls v_f64_t
V_NAME(exp)60*0928368fSKristof Beyls V_NAME(exp) (v_f64_t x)
61*0928368fSKristof Beyls {
62*0928368fSKristof Beyls v_f64_t n, r, r2, s, y, z;
63*0928368fSKristof Beyls v_u64_t cmp, u, e, i;
64*0928368fSKristof Beyls
65*0928368fSKristof Beyls cmp = v_cond_u64 (v_abs_f64 (x) > Thres);
66*0928368fSKristof Beyls
67*0928368fSKristof Beyls /* n = round(x/(ln2/N)). */
68*0928368fSKristof Beyls z = v_fma_f64 (x, InvLn2, Shift);
69*0928368fSKristof Beyls u = v_as_u64_f64 (z);
70*0928368fSKristof Beyls n = z - Shift;
71*0928368fSKristof Beyls
72*0928368fSKristof Beyls /* r = x - n*ln2/N. */
73*0928368fSKristof Beyls r = x;
74*0928368fSKristof Beyls r = v_fma_f64 (-Ln2hi, n, r);
75*0928368fSKristof Beyls r = v_fma_f64 (-Ln2lo, n, r);
76*0928368fSKristof Beyls
77*0928368fSKristof Beyls e = u << (52 - V_EXP_TABLE_BITS);
78*0928368fSKristof Beyls i = u & IndexMask;
79*0928368fSKristof Beyls
80*0928368fSKristof Beyls /* y = exp(r) - 1 ~= r + C1 r^2 + C2 r^3 + C3 r^4. */
81*0928368fSKristof Beyls r2 = r * r;
82*0928368fSKristof Beyls y = v_fma_f64 (C2, r, C1);
83*0928368fSKristof Beyls y = v_fma_f64 (C3, r2, y);
84*0928368fSKristof Beyls y = v_fma_f64 (y, r2, r);
85*0928368fSKristof Beyls
86*0928368fSKristof Beyls /* s = 2^(n/N). */
87*0928368fSKristof Beyls u = v_lookup_u64 (Tab, i);
88*0928368fSKristof Beyls s = v_as_f64_u64 (u + e);
89*0928368fSKristof Beyls
90*0928368fSKristof Beyls if (unlikely (v_any_u64 (cmp)))
91*0928368fSKristof Beyls return specialcase (s, y, n);
92*0928368fSKristof Beyls return v_fma_f64 (y, s, s);
93*0928368fSKristof Beyls }
94*0928368fSKristof Beyls VPCS_ALIAS
95*0928368fSKristof Beyls #endif
96