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