xref: /freebsd-src/contrib/arm-optimized-routines/math/exp.c (revision 072a4ba82a01476eaee33781ccd241033eefcf0b)
131914882SAlex Richardson /*
231914882SAlex Richardson  * Double-precision e^x function.
331914882SAlex Richardson  *
431914882SAlex Richardson  * Copyright (c) 2018-2019, Arm Limited.
5*072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #include <float.h>
931914882SAlex Richardson #include <math.h>
1031914882SAlex Richardson #include <stdint.h>
1131914882SAlex Richardson #include "math_config.h"
1231914882SAlex Richardson 
1331914882SAlex Richardson #define N (1 << EXP_TABLE_BITS)
1431914882SAlex Richardson #define InvLn2N __exp_data.invln2N
1531914882SAlex Richardson #define NegLn2hiN __exp_data.negln2hiN
1631914882SAlex Richardson #define NegLn2loN __exp_data.negln2loN
1731914882SAlex Richardson #define Shift __exp_data.shift
1831914882SAlex Richardson #define T __exp_data.tab
1931914882SAlex Richardson #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
2031914882SAlex Richardson #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
2131914882SAlex Richardson #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
2231914882SAlex Richardson #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
2331914882SAlex Richardson #define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
2431914882SAlex Richardson 
2531914882SAlex Richardson /* Handle cases that may overflow or underflow when computing the result that
2631914882SAlex Richardson    is scale*(1+TMP) without intermediate rounding.  The bit representation of
2731914882SAlex Richardson    scale is in SBITS, however it has a computed exponent that may have
2831914882SAlex Richardson    overflown into the sign bit so that needs to be adjusted before using it as
2931914882SAlex Richardson    a double.  (int32_t)KI is the k used in the argument reduction and exponent
3031914882SAlex Richardson    adjustment of scale, positive k here means the result may overflow and
3131914882SAlex Richardson    negative k means the result may underflow.  */
3231914882SAlex Richardson static inline double
3331914882SAlex Richardson specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
3431914882SAlex Richardson {
3531914882SAlex Richardson   double_t scale, y;
3631914882SAlex Richardson 
3731914882SAlex Richardson   if ((ki & 0x80000000) == 0)
3831914882SAlex Richardson     {
3931914882SAlex Richardson       /* k > 0, the exponent of scale might have overflowed by <= 460.  */
4031914882SAlex Richardson       sbits -= 1009ull << 52;
4131914882SAlex Richardson       scale = asdouble (sbits);
4231914882SAlex Richardson       y = 0x1p1009 * (scale + scale * tmp);
4331914882SAlex Richardson       return check_oflow (eval_as_double (y));
4431914882SAlex Richardson     }
4531914882SAlex Richardson   /* k < 0, need special care in the subnormal range.  */
4631914882SAlex Richardson   sbits += 1022ull << 52;
4731914882SAlex Richardson   scale = asdouble (sbits);
4831914882SAlex Richardson   y = scale + scale * tmp;
4931914882SAlex Richardson   if (y < 1.0)
5031914882SAlex Richardson     {
5131914882SAlex Richardson       /* Round y to the right precision before scaling it into the subnormal
5231914882SAlex Richardson 	 range to avoid double rounding that can cause 0.5+E/2 ulp error where
5331914882SAlex Richardson 	 E is the worst-case ulp error outside the subnormal range.  So this
5431914882SAlex Richardson 	 is only useful if the goal is better than 1 ulp worst-case error.  */
5531914882SAlex Richardson       double_t hi, lo;
5631914882SAlex Richardson       lo = scale - y + scale * tmp;
5731914882SAlex Richardson       hi = 1.0 + y;
5831914882SAlex Richardson       lo = 1.0 - hi + y + lo;
5931914882SAlex Richardson       y = eval_as_double (hi + lo) - 1.0;
6031914882SAlex Richardson       /* Avoid -0.0 with downward rounding.  */
6131914882SAlex Richardson       if (WANT_ROUNDING && y == 0.0)
6231914882SAlex Richardson 	y = 0.0;
6331914882SAlex Richardson       /* The underflow exception needs to be signaled explicitly.  */
6431914882SAlex Richardson       force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
6531914882SAlex Richardson     }
6631914882SAlex Richardson   y = 0x1p-1022 * y;
6731914882SAlex Richardson   return check_uflow (eval_as_double (y));
6831914882SAlex Richardson }
6931914882SAlex Richardson 
7031914882SAlex Richardson /* Top 12 bits of a double (sign and exponent bits).  */
7131914882SAlex Richardson static inline uint32_t
7231914882SAlex Richardson top12 (double x)
7331914882SAlex Richardson {
7431914882SAlex Richardson   return asuint64 (x) >> 52;
7531914882SAlex Richardson }
7631914882SAlex Richardson 
7731914882SAlex Richardson /* Computes exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
7831914882SAlex Richardson    If hastail is 0 then xtail is assumed to be 0 too.  */
7931914882SAlex Richardson static inline double
8031914882SAlex Richardson exp_inline (double x, double xtail, int hastail)
8131914882SAlex Richardson {
8231914882SAlex Richardson   uint32_t abstop;
8331914882SAlex Richardson   uint64_t ki, idx, top, sbits;
8431914882SAlex Richardson   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
8531914882SAlex Richardson   double_t kd, z, r, r2, scale, tail, tmp;
8631914882SAlex Richardson 
8731914882SAlex Richardson   abstop = top12 (x) & 0x7ff;
8831914882SAlex Richardson   if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
8931914882SAlex Richardson     {
9031914882SAlex Richardson       if (abstop - top12 (0x1p-54) >= 0x80000000)
9131914882SAlex Richardson 	/* Avoid spurious underflow for tiny x.  */
9231914882SAlex Richardson 	/* Note: 0 is common input.  */
9331914882SAlex Richardson 	return WANT_ROUNDING ? 1.0 + x : 1.0;
9431914882SAlex Richardson       if (abstop >= top12 (1024.0))
9531914882SAlex Richardson 	{
9631914882SAlex Richardson 	  if (asuint64 (x) == asuint64 (-INFINITY))
9731914882SAlex Richardson 	    return 0.0;
9831914882SAlex Richardson 	  if (abstop >= top12 (INFINITY))
9931914882SAlex Richardson 	    return 1.0 + x;
10031914882SAlex Richardson 	  if (asuint64 (x) >> 63)
10131914882SAlex Richardson 	    return __math_uflow (0);
10231914882SAlex Richardson 	  else
10331914882SAlex Richardson 	    return __math_oflow (0);
10431914882SAlex Richardson 	}
10531914882SAlex Richardson       /* Large x is special cased below.  */
10631914882SAlex Richardson       abstop = 0;
10731914882SAlex Richardson     }
10831914882SAlex Richardson 
10931914882SAlex Richardson   /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)].  */
11031914882SAlex Richardson   /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N].  */
11131914882SAlex Richardson   z = InvLn2N * x;
11231914882SAlex Richardson #if TOINT_INTRINSICS
11331914882SAlex Richardson   kd = roundtoint (z);
11431914882SAlex Richardson   ki = converttoint (z);
11531914882SAlex Richardson #elif EXP_USE_TOINT_NARROW
11631914882SAlex Richardson   /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes.  */
11731914882SAlex Richardson   kd = eval_as_double (z + Shift);
11831914882SAlex Richardson   ki = asuint64 (kd) >> 16;
11931914882SAlex Richardson   kd = (double_t) (int32_t) ki;
12031914882SAlex Richardson #else
12131914882SAlex Richardson   /* z - kd is in [-1, 1] in non-nearest rounding modes.  */
12231914882SAlex Richardson   kd = eval_as_double (z + Shift);
12331914882SAlex Richardson   ki = asuint64 (kd);
12431914882SAlex Richardson   kd -= Shift;
12531914882SAlex Richardson #endif
12631914882SAlex Richardson   r = x + kd * NegLn2hiN + kd * NegLn2loN;
12731914882SAlex Richardson   /* The code assumes 2^-200 < |xtail| < 2^-8/N.  */
12831914882SAlex Richardson   if (hastail)
12931914882SAlex Richardson     r += xtail;
13031914882SAlex Richardson   /* 2^(k/N) ~= scale * (1 + tail).  */
13131914882SAlex Richardson   idx = 2 * (ki % N);
13231914882SAlex Richardson   top = ki << (52 - EXP_TABLE_BITS);
13331914882SAlex Richardson   tail = asdouble (T[idx]);
13431914882SAlex Richardson   /* This is only a valid scale when -1023*N < k < 1024*N.  */
13531914882SAlex Richardson   sbits = T[idx + 1] + top;
13631914882SAlex Richardson   /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1).  */
13731914882SAlex Richardson   /* Evaluation is optimized assuming superscalar pipelined execution.  */
13831914882SAlex Richardson   r2 = r * r;
13931914882SAlex Richardson   /* Without fma the worst case error is 0.25/N ulp larger.  */
14031914882SAlex Richardson   /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp.  */
14131914882SAlex Richardson #if EXP_POLY_ORDER == 4
14231914882SAlex Richardson   tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
14331914882SAlex Richardson #elif EXP_POLY_ORDER == 5
14431914882SAlex Richardson   tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
14531914882SAlex Richardson #elif EXP_POLY_ORDER == 6
14631914882SAlex Richardson   tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
14731914882SAlex Richardson #endif
14831914882SAlex Richardson   if (unlikely (abstop == 0))
14931914882SAlex Richardson     return specialcase (tmp, sbits, ki);
15031914882SAlex Richardson   scale = asdouble (sbits);
15131914882SAlex Richardson   /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
15231914882SAlex Richardson      is no spurious underflow here even without fma.  */
15331914882SAlex Richardson   return eval_as_double (scale + scale * tmp);
15431914882SAlex Richardson }
15531914882SAlex Richardson 
15631914882SAlex Richardson double
15731914882SAlex Richardson exp (double x)
15831914882SAlex Richardson {
15931914882SAlex Richardson   return exp_inline (x, 0, 0);
16031914882SAlex Richardson }
16131914882SAlex Richardson 
16231914882SAlex Richardson /* May be useful for implementing pow where more than double
16331914882SAlex Richardson    precision input is needed.  */
16431914882SAlex Richardson double
16531914882SAlex Richardson __exp_dd (double x, double xtail)
16631914882SAlex Richardson {
16731914882SAlex Richardson   return exp_inline (x, xtail, 1);
16831914882SAlex Richardson }
16931914882SAlex Richardson #if USE_GLIBC_ABI
17031914882SAlex Richardson strong_alias (exp, __exp_finite)
17131914882SAlex Richardson hidden_alias (exp, __ieee754_exp)
17231914882SAlex Richardson hidden_alias (__exp_dd, __exp1)
17331914882SAlex Richardson # if LDBL_MANT_DIG == 53
17431914882SAlex Richardson long double expl (long double x) { return exp (x); }
17531914882SAlex Richardson # endif
17631914882SAlex Richardson #endif
177