1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Double-precision e^x function.
3570af302Sopenharmony_ci *
4570af302Sopenharmony_ci * Copyright (c) 2018, Arm Limited.
5570af302Sopenharmony_ci * SPDX-License-Identifier: MIT
6570af302Sopenharmony_ci */
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci#include <math.h>
9570af302Sopenharmony_ci#include <stdint.h>
10570af302Sopenharmony_ci#include "libm.h"
11570af302Sopenharmony_ci#include "exp_data.h"
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci#define N (1 << EXP_TABLE_BITS)
14570af302Sopenharmony_ci#define InvLn2N __exp_data.invln2N
15570af302Sopenharmony_ci#define NegLn2hiN __exp_data.negln2hiN
16570af302Sopenharmony_ci#define NegLn2loN __exp_data.negln2loN
17570af302Sopenharmony_ci#define Shift __exp_data.shift
18570af302Sopenharmony_ci#define T __exp_data.tab
19570af302Sopenharmony_ci#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
20570af302Sopenharmony_ci#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
21570af302Sopenharmony_ci#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
22570af302Sopenharmony_ci#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci/* Handle cases that may overflow or underflow when computing the result that
25570af302Sopenharmony_ci   is scale*(1+TMP) without intermediate rounding.  The bit representation of
26570af302Sopenharmony_ci   scale is in SBITS, however it has a computed exponent that may have
27570af302Sopenharmony_ci   overflown into the sign bit so that needs to be adjusted before using it as
28570af302Sopenharmony_ci   a double.  (int32_t)KI is the k used in the argument reduction and exponent
29570af302Sopenharmony_ci   adjustment of scale, positive k here means the result may overflow and
30570af302Sopenharmony_ci   negative k means the result may underflow.  */
31570af302Sopenharmony_cistatic inline double specialcase(double_t tmp, uint64_t sbits, uint64_t ki)
32570af302Sopenharmony_ci{
33570af302Sopenharmony_ci	double_t scale, y;
34570af302Sopenharmony_ci
35570af302Sopenharmony_ci	if ((ki & 0x80000000) == 0) {
36570af302Sopenharmony_ci		/* k > 0, the exponent of scale might have overflowed by <= 460.  */
37570af302Sopenharmony_ci		sbits -= 1009ull << 52;
38570af302Sopenharmony_ci		scale = asdouble(sbits);
39570af302Sopenharmony_ci		y = 0x1p1009 * (scale + scale * tmp);
40570af302Sopenharmony_ci		return eval_as_double(y);
41570af302Sopenharmony_ci	}
42570af302Sopenharmony_ci	/* k < 0, need special care in the subnormal range.  */
43570af302Sopenharmony_ci	sbits += 1022ull << 52;
44570af302Sopenharmony_ci	scale = asdouble(sbits);
45570af302Sopenharmony_ci	y = scale + scale * tmp;
46570af302Sopenharmony_ci	if (y < 1.0) {
47570af302Sopenharmony_ci		/* Round y to the right precision before scaling it into the subnormal
48570af302Sopenharmony_ci		 range to avoid double rounding that can cause 0.5+E/2 ulp error where
49570af302Sopenharmony_ci		 E is the worst-case ulp error outside the subnormal range.  So this
50570af302Sopenharmony_ci		 is only useful if the goal is better than 1 ulp worst-case error.  */
51570af302Sopenharmony_ci		double_t hi, lo;
52570af302Sopenharmony_ci		lo = scale - y + scale * tmp;
53570af302Sopenharmony_ci		hi = 1.0 + y;
54570af302Sopenharmony_ci		lo = 1.0 - hi + y + lo;
55570af302Sopenharmony_ci		y = eval_as_double(hi + lo) - 1.0;
56570af302Sopenharmony_ci		/* Avoid -0.0 with downward rounding.  */
57570af302Sopenharmony_ci		if (WANT_ROUNDING && y == 0.0)
58570af302Sopenharmony_ci			y = 0.0;
59570af302Sopenharmony_ci		/* The underflow exception needs to be signaled explicitly.  */
60570af302Sopenharmony_ci		fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022);
61570af302Sopenharmony_ci	}
62570af302Sopenharmony_ci	y = 0x1p-1022 * y;
63570af302Sopenharmony_ci	return eval_as_double(y);
64570af302Sopenharmony_ci}
65570af302Sopenharmony_ci
66570af302Sopenharmony_ci/* Top 12 bits of a double (sign and exponent bits).  */
67570af302Sopenharmony_cistatic inline uint32_t top12(double x)
68570af302Sopenharmony_ci{
69570af302Sopenharmony_ci	return asuint64(x) >> 52;
70570af302Sopenharmony_ci}
71570af302Sopenharmony_ci
72570af302Sopenharmony_cidouble exp(double x)
73570af302Sopenharmony_ci{
74570af302Sopenharmony_ci	uint32_t abstop;
75570af302Sopenharmony_ci	uint64_t ki, idx, top, sbits;
76570af302Sopenharmony_ci	double_t kd, z, r, r2, scale, tail, tmp;
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci	abstop = top12(x) & 0x7ff;
79570af302Sopenharmony_ci	if (predict_false(abstop - top12(0x1p-54) >= top12(512.0) - top12(0x1p-54))) {
80570af302Sopenharmony_ci		if (abstop - top12(0x1p-54) >= 0x80000000)
81570af302Sopenharmony_ci			/* Avoid spurious underflow for tiny x.  */
82570af302Sopenharmony_ci			/* Note: 0 is common input.  */
83570af302Sopenharmony_ci			return WANT_ROUNDING ? 1.0 + x : 1.0;
84570af302Sopenharmony_ci		if (abstop >= top12(1024.0)) {
85570af302Sopenharmony_ci			if (asuint64(x) == asuint64(-INFINITY))
86570af302Sopenharmony_ci				return 0.0;
87570af302Sopenharmony_ci			if (abstop >= top12(INFINITY))
88570af302Sopenharmony_ci				return 1.0 + x;
89570af302Sopenharmony_ci			if (asuint64(x) >> 63)
90570af302Sopenharmony_ci				return __math_uflow(0);
91570af302Sopenharmony_ci			else
92570af302Sopenharmony_ci				return __math_oflow(0);
93570af302Sopenharmony_ci		}
94570af302Sopenharmony_ci		/* Large x is special cased below.  */
95570af302Sopenharmony_ci		abstop = 0;
96570af302Sopenharmony_ci	}
97570af302Sopenharmony_ci
98570af302Sopenharmony_ci	/* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)].  */
99570af302Sopenharmony_ci	/* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N].  */
100570af302Sopenharmony_ci	z = InvLn2N * x;
101570af302Sopenharmony_ci#if TOINT_INTRINSICS
102570af302Sopenharmony_ci	kd = roundtoint(z);
103570af302Sopenharmony_ci	ki = converttoint(z);
104570af302Sopenharmony_ci#elif EXP_USE_TOINT_NARROW
105570af302Sopenharmony_ci	/* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes.  */
106570af302Sopenharmony_ci	kd = eval_as_double(z + Shift);
107570af302Sopenharmony_ci	ki = asuint64(kd) >> 16;
108570af302Sopenharmony_ci	kd = (double_t)(int32_t)ki;
109570af302Sopenharmony_ci#else
110570af302Sopenharmony_ci	/* z - kd is in [-1, 1] in non-nearest rounding modes.  */
111570af302Sopenharmony_ci	kd = eval_as_double(z + Shift);
112570af302Sopenharmony_ci	ki = asuint64(kd);
113570af302Sopenharmony_ci	kd -= Shift;
114570af302Sopenharmony_ci#endif
115570af302Sopenharmony_ci	r = x + kd * NegLn2hiN + kd * NegLn2loN;
116570af302Sopenharmony_ci	/* 2^(k/N) ~= scale * (1 + tail).  */
117570af302Sopenharmony_ci	idx = 2 * (ki % N);
118570af302Sopenharmony_ci	top = ki << (52 - EXP_TABLE_BITS);
119570af302Sopenharmony_ci	tail = asdouble(T[idx]);
120570af302Sopenharmony_ci	/* This is only a valid scale when -1023*N < k < 1024*N.  */
121570af302Sopenharmony_ci	sbits = T[idx + 1] + top;
122570af302Sopenharmony_ci	/* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1).  */
123570af302Sopenharmony_ci	/* Evaluation is optimized assuming superscalar pipelined execution.  */
124570af302Sopenharmony_ci	r2 = r * r;
125570af302Sopenharmony_ci	/* Without fma the worst case error is 0.25/N ulp larger.  */
126570af302Sopenharmony_ci	/* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp.  */
127570af302Sopenharmony_ci	tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
128570af302Sopenharmony_ci	if (predict_false(abstop == 0))
129570af302Sopenharmony_ci		return specialcase(tmp, sbits, ki);
130570af302Sopenharmony_ci	scale = asdouble(sbits);
131570af302Sopenharmony_ci	/* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
132570af302Sopenharmony_ci	   is no spurious underflow here even without fma.  */
133570af302Sopenharmony_ci	return eval_as_double(scale + scale * tmp);
134570af302Sopenharmony_ci}
135