xref: /freebsd/contrib/arm-optimized-routines/math/exp2.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  * Double-precision 2^x function.
3  *
4  * Copyright (c) 2018-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include <float.h>
9 #include <math.h>
10 #include <stdint.h>
11 #include "math_config.h"
12 #include "test_defs.h"
13 #include "test_sig.h"
14 
15 #define N (1 << EXP_TABLE_BITS)
16 #define Shift __exp_data.exp2_shift
17 #define T __exp_data.tab
18 #define C1 __exp_data.exp2_poly[0]
19 #define C2 __exp_data.exp2_poly[1]
20 #define C3 __exp_data.exp2_poly[2]
21 #define C4 __exp_data.exp2_poly[3]
22 #define C5 __exp_data.exp2_poly[4]
23 #define C6 __exp_data.exp2_poly[5]
24 
25 /* Handle cases that may overflow or underflow when computing the result that
26    is scale*(1+TMP) without intermediate rounding.  The bit representation of
27    scale is in SBITS, however it has a computed exponent that may have
28    overflown into the sign bit so that needs to be adjusted before using it as
29    a double.  (int32_t)KI is the k used in the argument reduction and exponent
30    adjustment of scale, positive k here means the result may overflow and
31    negative k means the result may underflow.  */
32 static inline double
33 specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
34 {
35   double_t scale, y;
36 
37   if ((ki & 0x80000000) == 0)
38     {
39       /* k > 0, the exponent of scale might have overflowed by 1.  */
40       sbits -= 1ull << 52;
41       scale = asdouble (sbits);
42       y = 2 * (scale + scale * tmp);
43       return check_oflow (eval_as_double (y));
44     }
45   /* k < 0, need special care in the subnormal range.  */
46   sbits += 1022ull << 52;
47   scale = asdouble (sbits);
48   y = scale + scale * tmp;
49   if (y < 1.0)
50     {
51       /* Round y to the right precision before scaling it into the subnormal
52 	 range to avoid double rounding that can cause 0.5+E/2 ulp error where
53 	 E is the worst-case ulp error outside the subnormal range.  So this
54 	 is only useful if the goal is better than 1 ulp worst-case error.  */
55       double_t hi, lo;
56       lo = scale - y + scale * tmp;
57       hi = 1.0 + y;
58       lo = 1.0 - hi + y + lo;
59       y = eval_as_double (hi + lo) - 1.0;
60       /* Avoid -0.0 with downward rounding.  */
61       if (WANT_ROUNDING && y == 0.0)
62 	y = 0.0;
63       /* The underflow exception needs to be signaled explicitly.  */
64       force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
65     }
66   y = 0x1p-1022 * y;
67   return check_uflow (eval_as_double (y));
68 }
69 
70 /* Top 12 bits of a double (sign and exponent bits).  */
71 static inline uint32_t
72 top12 (double x)
73 {
74   return asuint64 (x) >> 52;
75 }
76 
77 double
78 exp2 (double x)
79 {
80   uint32_t abstop;
81   uint64_t ki, idx, top, sbits;
82   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
83   double_t kd, r, r2, scale, tail, tmp;
84 
85   abstop = top12 (x) & 0x7ff;
86   if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
87     {
88       if (abstop - top12 (0x1p-54) >= 0x80000000)
89 	/* Avoid spurious underflow for tiny x.  */
90 	/* Note: 0 is common input.  */
91 	return WANT_ROUNDING ? 1.0 + x : 1.0;
92       if (abstop >= top12 (1024.0))
93 	{
94 	  if (asuint64 (x) == asuint64 (-INFINITY))
95 	    return 0.0;
96 	  if (abstop >= top12 (INFINITY))
97 	    return 1.0 + x;
98 	  if (!(asuint64 (x) >> 63))
99 	    return __math_oflow (0);
100 	  else if (asuint64 (x) >= asuint64 (-1075.0))
101 	    return __math_uflow (0);
102 	}
103       if (2 * asuint64 (x) > 2 * asuint64 (928.0))
104 	/* Large x is special cased below.  */
105 	abstop = 0;
106     }
107 
108   /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)].  */
109   /* x = k/N + r, with int k and r in [-1/2N, 1/2N].  */
110   kd = eval_as_double (x + Shift);
111   ki = asuint64 (kd); /* k.  */
112   kd -= Shift; /* k/N for int k.  */
113   r = x - kd;
114   /* 2^(k/N) ~= scale * (1 + tail).  */
115   idx = 2 * (ki % N);
116   top = ki << (52 - EXP_TABLE_BITS);
117   tail = asdouble (T[idx]);
118   /* This is only a valid scale when -1023*N < k < 1024*N.  */
119   sbits = T[idx + 1] + top;
120   /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1).  */
121   /* Evaluation is optimized assuming superscalar pipelined execution.  */
122   r2 = r * r;
123   /* Without fma the worst case error is 0.5/N ulp larger.  */
124   /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp.  */
125 #if EXP2_POLY_ORDER == 4
126   tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4);
127 #elif EXP2_POLY_ORDER == 5
128   tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
129 #elif EXP2_POLY_ORDER == 6
130   tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
131 #endif
132   if (unlikely (abstop == 0))
133     return specialcase (tmp, sbits, ki);
134   scale = asdouble (sbits);
135   /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
136      is no spurious underflow here even without fma.  */
137   return eval_as_double (scale + scale * tmp);
138 }
139 #if USE_GLIBC_ABI
140 strong_alias (exp2, __exp2_finite)
141 hidden_alias (exp2, __ieee754_exp2)
142 # if LDBL_MANT_DIG == 53
143 long double exp2l (long double x) { return exp2 (x); }
144 # endif
145 #endif
146 
147 TEST_SIG (S, D, 1, exp2, -9.9, 9.9)
148 TEST_ULP (exp2, 0.01)
149 TEST_ULP_NONNEAREST (exp2, 0.5)
150 TEST_INTERVAL (exp2, 0, 0xffff000000000000, 10000)
151 TEST_SYM_INTERVAL (exp2, 0x1p-6, 0x1p6, 40000)
152 TEST_SYM_INTERVAL (exp2, 633.3, 733.3, 10000)
153