xref: /freebsd-src/contrib/arm-optimized-routines/math/exp2.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * Double-precision 2^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 Shift __exp_data.exp2_shift
1731914882SAlex Richardson #define T __exp_data.tab
1831914882SAlex Richardson #define C1 __exp_data.exp2_poly[0]
1931914882SAlex Richardson #define C2 __exp_data.exp2_poly[1]
2031914882SAlex Richardson #define C3 __exp_data.exp2_poly[2]
2131914882SAlex Richardson #define C4 __exp_data.exp2_poly[3]
2231914882SAlex Richardson #define C5 __exp_data.exp2_poly[4]
2331914882SAlex Richardson #define C6 __exp_data.exp2_poly[5]
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 1.  */
4031914882SAlex Richardson       sbits -= 1ull << 52;
4131914882SAlex Richardson       scale = asdouble (sbits);
4231914882SAlex Richardson       y = 2 * (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 double
7831914882SAlex Richardson exp2 (double x)
7931914882SAlex Richardson {
8031914882SAlex Richardson   uint32_t abstop;
8131914882SAlex Richardson   uint64_t ki, idx, top, sbits;
8231914882SAlex Richardson   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
8331914882SAlex Richardson   double_t kd, r, r2, scale, tail, tmp;
8431914882SAlex Richardson 
8531914882SAlex Richardson   abstop = top12 (x) & 0x7ff;
8631914882SAlex Richardson   if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
8731914882SAlex Richardson     {
8831914882SAlex Richardson       if (abstop - top12 (0x1p-54) >= 0x80000000)
8931914882SAlex Richardson 	/* Avoid spurious underflow for tiny x.  */
9031914882SAlex Richardson 	/* Note: 0 is common input.  */
9131914882SAlex Richardson 	return WANT_ROUNDING ? 1.0 + x : 1.0;
9231914882SAlex Richardson       if (abstop >= top12 (1024.0))
9331914882SAlex Richardson 	{
9431914882SAlex Richardson 	  if (asuint64 (x) == asuint64 (-INFINITY))
9531914882SAlex Richardson 	    return 0.0;
9631914882SAlex Richardson 	  if (abstop >= top12 (INFINITY))
9731914882SAlex Richardson 	    return 1.0 + x;
9831914882SAlex Richardson 	  if (!(asuint64 (x) >> 63))
9931914882SAlex Richardson 	    return __math_oflow (0);
10031914882SAlex Richardson 	  else if (asuint64 (x) >= asuint64 (-1075.0))
10131914882SAlex Richardson 	    return __math_uflow (0);
10231914882SAlex Richardson 	}
10331914882SAlex Richardson       if (2 * asuint64 (x) > 2 * asuint64 (928.0))
10431914882SAlex Richardson 	/* Large x is special cased below.  */
10531914882SAlex Richardson 	abstop = 0;
10631914882SAlex Richardson     }
10731914882SAlex Richardson 
10831914882SAlex Richardson   /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)].  */
10931914882SAlex Richardson   /* x = k/N + r, with int k and r in [-1/2N, 1/2N].  */
11031914882SAlex Richardson   kd = eval_as_double (x + Shift);
11131914882SAlex Richardson   ki = asuint64 (kd); /* k.  */
11231914882SAlex Richardson   kd -= Shift; /* k/N for int k.  */
11331914882SAlex Richardson   r = x - kd;
11431914882SAlex Richardson   /* 2^(k/N) ~= scale * (1 + tail).  */
11531914882SAlex Richardson   idx = 2 * (ki % N);
11631914882SAlex Richardson   top = ki << (52 - EXP_TABLE_BITS);
11731914882SAlex Richardson   tail = asdouble (T[idx]);
11831914882SAlex Richardson   /* This is only a valid scale when -1023*N < k < 1024*N.  */
11931914882SAlex Richardson   sbits = T[idx + 1] + top;
12031914882SAlex Richardson   /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1).  */
12131914882SAlex Richardson   /* Evaluation is optimized assuming superscalar pipelined execution.  */
12231914882SAlex Richardson   r2 = r * r;
12331914882SAlex Richardson   /* Without fma the worst case error is 0.5/N ulp larger.  */
12431914882SAlex Richardson   /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp.  */
12531914882SAlex Richardson #if EXP2_POLY_ORDER == 4
12631914882SAlex Richardson   tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4);
12731914882SAlex Richardson #elif EXP2_POLY_ORDER == 5
12831914882SAlex Richardson   tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
12931914882SAlex Richardson #elif EXP2_POLY_ORDER == 6
13031914882SAlex Richardson   tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
13131914882SAlex Richardson #endif
13231914882SAlex Richardson   if (unlikely (abstop == 0))
13331914882SAlex Richardson     return specialcase (tmp, sbits, ki);
13431914882SAlex Richardson   scale = asdouble (sbits);
13531914882SAlex Richardson   /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
13631914882SAlex Richardson      is no spurious underflow here even without fma.  */
13731914882SAlex Richardson   return eval_as_double (scale + scale * tmp);
13831914882SAlex Richardson }
13931914882SAlex Richardson #if USE_GLIBC_ABI
14031914882SAlex Richardson strong_alias (exp2, __exp2_finite)
14131914882SAlex Richardson hidden_alias (exp2, __ieee754_exp2)
14231914882SAlex Richardson # if LDBL_MANT_DIG == 53
14331914882SAlex Richardson long double exp2l (long double x) { return exp2 (x); }
14431914882SAlex Richardson # endif
14531914882SAlex Richardson #endif
146*f3087befSAndrew Turner 
147*f3087befSAndrew Turner TEST_SIG (S, D, 1, exp2, -9.9, 9.9)
148*f3087befSAndrew Turner TEST_ULP (exp2, 0.01)
149*f3087befSAndrew Turner TEST_ULP_NONNEAREST (exp2, 0.5)
150*f3087befSAndrew Turner TEST_INTERVAL (exp2, 0, 0xffff000000000000, 10000)
151*f3087befSAndrew Turner TEST_SYM_INTERVAL (exp2, 0x1p-6, 0x1p6, 40000)
152*f3087befSAndrew Turner TEST_SYM_INTERVAL (exp2, 633.3, 733.3, 10000)
153