1bbbf1280Sopenharmony_ci/*
2bbbf1280Sopenharmony_ci * Double-precision e^x function.
3bbbf1280Sopenharmony_ci *
4bbbf1280Sopenharmony_ci * Copyright (c) 2018-2019, Arm Limited.
5bbbf1280Sopenharmony_ci * SPDX-License-Identifier: MIT
6bbbf1280Sopenharmony_ci */
7bbbf1280Sopenharmony_ci
8bbbf1280Sopenharmony_ci#include <float.h>
9bbbf1280Sopenharmony_ci#include <math.h>
10bbbf1280Sopenharmony_ci#include <stdint.h>
11bbbf1280Sopenharmony_ci#include "math_config.h"
12bbbf1280Sopenharmony_ci
13bbbf1280Sopenharmony_ci#define N (1 << EXP_TABLE_BITS)
14bbbf1280Sopenharmony_ci#define InvLn2N __exp_data.invln2N
15bbbf1280Sopenharmony_ci#define NegLn2hiN __exp_data.negln2hiN
16bbbf1280Sopenharmony_ci#define NegLn2loN __exp_data.negln2loN
17bbbf1280Sopenharmony_ci#define Shift __exp_data.shift
18bbbf1280Sopenharmony_ci#define T __exp_data.tab
19bbbf1280Sopenharmony_ci#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
20bbbf1280Sopenharmony_ci#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
21bbbf1280Sopenharmony_ci#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
22bbbf1280Sopenharmony_ci#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
23bbbf1280Sopenharmony_ci#define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
24bbbf1280Sopenharmony_ci
25bbbf1280Sopenharmony_ci/* Handle cases that may overflow or underflow when computing the result that
26bbbf1280Sopenharmony_ci   is scale*(1+TMP) without intermediate rounding.  The bit representation of
27bbbf1280Sopenharmony_ci   scale is in SBITS, however it has a computed exponent that may have
28bbbf1280Sopenharmony_ci   overflown into the sign bit so that needs to be adjusted before using it as
29bbbf1280Sopenharmony_ci   a double.  (int32_t)KI is the k used in the argument reduction and exponent
30bbbf1280Sopenharmony_ci   adjustment of scale, positive k here means the result may overflow and
31bbbf1280Sopenharmony_ci   negative k means the result may underflow.  */
32bbbf1280Sopenharmony_cistatic inline double
33bbbf1280Sopenharmony_cispecialcase (double_t tmp, uint64_t sbits, uint64_t ki)
34bbbf1280Sopenharmony_ci{
35bbbf1280Sopenharmony_ci  double_t scale, y;
36bbbf1280Sopenharmony_ci
37bbbf1280Sopenharmony_ci  if ((ki & 0x80000000) == 0)
38bbbf1280Sopenharmony_ci    {
39bbbf1280Sopenharmony_ci      /* k > 0, the exponent of scale might have overflowed by <= 460.  */
40bbbf1280Sopenharmony_ci      sbits -= 1009ull << 52;
41bbbf1280Sopenharmony_ci      scale = asdouble (sbits);
42bbbf1280Sopenharmony_ci      y = 0x1p1009 * (scale + scale * tmp);
43bbbf1280Sopenharmony_ci      return check_oflow (eval_as_double (y));
44bbbf1280Sopenharmony_ci    }
45bbbf1280Sopenharmony_ci  /* k < 0, need special care in the subnormal range.  */
46bbbf1280Sopenharmony_ci  sbits += 1022ull << 52;
47bbbf1280Sopenharmony_ci  scale = asdouble (sbits);
48bbbf1280Sopenharmony_ci  y = scale + scale * tmp;
49bbbf1280Sopenharmony_ci  if (y < 1.0)
50bbbf1280Sopenharmony_ci    {
51bbbf1280Sopenharmony_ci      /* Round y to the right precision before scaling it into the subnormal
52bbbf1280Sopenharmony_ci	 range to avoid double rounding that can cause 0.5+E/2 ulp error where
53bbbf1280Sopenharmony_ci	 E is the worst-case ulp error outside the subnormal range.  So this
54bbbf1280Sopenharmony_ci	 is only useful if the goal is better than 1 ulp worst-case error.  */
55bbbf1280Sopenharmony_ci      double_t hi, lo;
56bbbf1280Sopenharmony_ci      lo = scale - y + scale * tmp;
57bbbf1280Sopenharmony_ci      hi = 1.0 + y;
58bbbf1280Sopenharmony_ci      lo = 1.0 - hi + y + lo;
59bbbf1280Sopenharmony_ci      y = eval_as_double (hi + lo) - 1.0;
60bbbf1280Sopenharmony_ci      /* Avoid -0.0 with downward rounding.  */
61bbbf1280Sopenharmony_ci      if (WANT_ROUNDING && y == 0.0)
62bbbf1280Sopenharmony_ci	y = 0.0;
63bbbf1280Sopenharmony_ci      /* The underflow exception needs to be signaled explicitly.  */
64bbbf1280Sopenharmony_ci      force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
65bbbf1280Sopenharmony_ci    }
66bbbf1280Sopenharmony_ci  y = 0x1p-1022 * y;
67bbbf1280Sopenharmony_ci  return check_uflow (eval_as_double (y));
68bbbf1280Sopenharmony_ci}
69bbbf1280Sopenharmony_ci
70bbbf1280Sopenharmony_ci/* Top 12 bits of a double (sign and exponent bits).  */
71bbbf1280Sopenharmony_cistatic inline uint32_t
72bbbf1280Sopenharmony_citop12 (double x)
73bbbf1280Sopenharmony_ci{
74bbbf1280Sopenharmony_ci  return asuint64 (x) >> 52;
75bbbf1280Sopenharmony_ci}
76bbbf1280Sopenharmony_ci
77bbbf1280Sopenharmony_ci/* Computes exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
78bbbf1280Sopenharmony_ci   If hastail is 0 then xtail is assumed to be 0 too.  */
79bbbf1280Sopenharmony_cistatic inline double
80bbbf1280Sopenharmony_ciexp_inline (double x, double xtail, int hastail)
81bbbf1280Sopenharmony_ci{
82bbbf1280Sopenharmony_ci  uint32_t abstop;
83bbbf1280Sopenharmony_ci  uint64_t ki, idx, top, sbits;
84bbbf1280Sopenharmony_ci  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
85bbbf1280Sopenharmony_ci  double_t kd, z, r, r2, scale, tail, tmp;
86bbbf1280Sopenharmony_ci
87bbbf1280Sopenharmony_ci  abstop = top12 (x) & 0x7ff;
88bbbf1280Sopenharmony_ci  if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
89bbbf1280Sopenharmony_ci    {
90bbbf1280Sopenharmony_ci      if (abstop - top12 (0x1p-54) >= 0x80000000)
91bbbf1280Sopenharmony_ci	/* Avoid spurious underflow for tiny x.  */
92bbbf1280Sopenharmony_ci	/* Note: 0 is common input.  */
93bbbf1280Sopenharmony_ci	return WANT_ROUNDING ? 1.0 + x : 1.0;
94bbbf1280Sopenharmony_ci      if (abstop >= top12 (1024.0))
95bbbf1280Sopenharmony_ci	{
96bbbf1280Sopenharmony_ci	  if (asuint64 (x) == asuint64 (-INFINITY))
97bbbf1280Sopenharmony_ci	    return 0.0;
98bbbf1280Sopenharmony_ci	  if (abstop >= top12 (INFINITY))
99bbbf1280Sopenharmony_ci	    return 1.0 + x;
100bbbf1280Sopenharmony_ci	  if (asuint64 (x) >> 63)
101bbbf1280Sopenharmony_ci	    return __math_uflow (0);
102bbbf1280Sopenharmony_ci	  else
103bbbf1280Sopenharmony_ci	    return __math_oflow (0);
104bbbf1280Sopenharmony_ci	}
105bbbf1280Sopenharmony_ci      /* Large x is special cased below.  */
106bbbf1280Sopenharmony_ci      abstop = 0;
107bbbf1280Sopenharmony_ci    }
108bbbf1280Sopenharmony_ci
109bbbf1280Sopenharmony_ci  /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)].  */
110bbbf1280Sopenharmony_ci  /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N].  */
111bbbf1280Sopenharmony_ci  z = InvLn2N * x;
112bbbf1280Sopenharmony_ci#if TOINT_INTRINSICS
113bbbf1280Sopenharmony_ci  kd = roundtoint (z);
114bbbf1280Sopenharmony_ci  ki = converttoint (z);
115bbbf1280Sopenharmony_ci#elif EXP_USE_TOINT_NARROW
116bbbf1280Sopenharmony_ci  /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes.  */
117bbbf1280Sopenharmony_ci  kd = eval_as_double (z + Shift);
118bbbf1280Sopenharmony_ci  ki = asuint64 (kd) >> 16;
119bbbf1280Sopenharmony_ci  kd = (double_t) (int32_t) ki;
120bbbf1280Sopenharmony_ci#else
121bbbf1280Sopenharmony_ci  /* z - kd is in [-1, 1] in non-nearest rounding modes.  */
122bbbf1280Sopenharmony_ci  kd = eval_as_double (z + Shift);
123bbbf1280Sopenharmony_ci  ki = asuint64 (kd);
124bbbf1280Sopenharmony_ci  kd -= Shift;
125bbbf1280Sopenharmony_ci#endif
126bbbf1280Sopenharmony_ci  r = x + kd * NegLn2hiN + kd * NegLn2loN;
127bbbf1280Sopenharmony_ci  /* The code assumes 2^-200 < |xtail| < 2^-8/N.  */
128bbbf1280Sopenharmony_ci  if (hastail)
129bbbf1280Sopenharmony_ci    r += xtail;
130bbbf1280Sopenharmony_ci  /* 2^(k/N) ~= scale * (1 + tail).  */
131bbbf1280Sopenharmony_ci  idx = 2 * (ki % N);
132bbbf1280Sopenharmony_ci  top = ki << (52 - EXP_TABLE_BITS);
133bbbf1280Sopenharmony_ci  tail = asdouble (T[idx]);
134bbbf1280Sopenharmony_ci  /* This is only a valid scale when -1023*N < k < 1024*N.  */
135bbbf1280Sopenharmony_ci  sbits = T[idx + 1] + top;
136bbbf1280Sopenharmony_ci  /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1).  */
137bbbf1280Sopenharmony_ci  /* Evaluation is optimized assuming superscalar pipelined execution.  */
138bbbf1280Sopenharmony_ci  r2 = r * r;
139bbbf1280Sopenharmony_ci  /* Without fma the worst case error is 0.25/N ulp larger.  */
140bbbf1280Sopenharmony_ci  /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp.  */
141bbbf1280Sopenharmony_ci#if EXP_POLY_ORDER == 4
142bbbf1280Sopenharmony_ci  tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
143bbbf1280Sopenharmony_ci#elif EXP_POLY_ORDER == 5
144bbbf1280Sopenharmony_ci  tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
145bbbf1280Sopenharmony_ci#elif EXP_POLY_ORDER == 6
146bbbf1280Sopenharmony_ci  tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
147bbbf1280Sopenharmony_ci#endif
148bbbf1280Sopenharmony_ci  if (unlikely (abstop == 0))
149bbbf1280Sopenharmony_ci    return specialcase (tmp, sbits, ki);
150bbbf1280Sopenharmony_ci  scale = asdouble (sbits);
151bbbf1280Sopenharmony_ci  /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
152bbbf1280Sopenharmony_ci     is no spurious underflow here even without fma.  */
153bbbf1280Sopenharmony_ci  return eval_as_double (scale + scale * tmp);
154bbbf1280Sopenharmony_ci}
155bbbf1280Sopenharmony_ci
156bbbf1280Sopenharmony_cidouble
157bbbf1280Sopenharmony_ciexp (double x)
158bbbf1280Sopenharmony_ci{
159bbbf1280Sopenharmony_ci  return exp_inline (x, 0, 0);
160bbbf1280Sopenharmony_ci}
161bbbf1280Sopenharmony_ci
162bbbf1280Sopenharmony_ci/* May be useful for implementing pow where more than double
163bbbf1280Sopenharmony_ci   precision input is needed.  */
164bbbf1280Sopenharmony_cidouble
165bbbf1280Sopenharmony_ci__exp_dd (double x, double xtail)
166bbbf1280Sopenharmony_ci{
167bbbf1280Sopenharmony_ci  return exp_inline (x, xtail, 1);
168bbbf1280Sopenharmony_ci}
169bbbf1280Sopenharmony_ci#if USE_GLIBC_ABI
170bbbf1280Sopenharmony_cistrong_alias (exp, __exp_finite)
171bbbf1280Sopenharmony_cihidden_alias (exp, __ieee754_exp)
172bbbf1280Sopenharmony_cihidden_alias (__exp_dd, __exp1)
173bbbf1280Sopenharmony_ci# if LDBL_MANT_DIG == 53
174bbbf1280Sopenharmony_cilong double expl (long double x) { return exp (x); }
175bbbf1280Sopenharmony_ci# endif
176bbbf1280Sopenharmony_ci#endif
177