xref: /reactos/sdk/lib/crt/math/libm_sse2/exp2.c (revision 59c55e00)
1 
2 /*******************************************************************************
3 MIT License
4 -----------
5 
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26 
27 #include "libm.h"
28 #include "libm_util.h"
29 
30 #define USE_SPLITEXP
31 #define USE_SCALEDOUBLE_1
32 #define USE_SCALEDOUBLE_2
33 #define USE_ZERO_WITH_FLAGS
34 #define USE_INFINITY_WITH_FLAGS
35 #define USE_HANDLE_ERROR
36 
37 #include "libm_inlines.h"
38 #undef USE_ZERO_WITH_FLAGS
39 #undef USE_SPLITEXP
40 #undef USE_SCALEDOUBLE_1
41 #undef USE_SCALEDOUBLE_2
42 #undef USE_INFINITY_WITH_FLAGS
43 #undef USE_HANDLE_ERROR
44 
45 #include "libm_errno.h"
46 
47 double FN_PROTOTYPE(exp2)(double x)
48 {
49   static const double
50     max_exp2_arg = 1024.0,  /* 0x4090000000000000 */
51     min_exp2_arg = -1074.0, /* 0xc090c80000000000 */
52     log2 = 6.931471805599453094178e-01, /* 0x3fe62e42fefa39ef */
53     log2_lead = 6.93147167563438415527E-01, /* 0x3fe62e42f8000000 */
54     log2_tail = 1.29965068938898869640E-08, /* 0x3e4be8e7bcd5e4f1 */
55     one_by_32_lead = 0.03125;
56 
57   double y, z1, z2, z, hx, tx, y1, y2;
58   int m;
59   unsigned long long ux, ax;
60 
61   /*
62     Computation of exp2(x).
63 
64     We compute the values m, z1, and z2 such that
65     exp2(x) = 2**m * (z1 + z2),  where exp2(x) is 2**x.
66 
67     Computations needed in order to obtain m, z1, and z2
68     involve three steps.
69 
70     First, we reduce the argument x to the form
71     x = n/32 + remainder,
72     where n has the value of an integer and |remainder| <= 1/64.
73     The value of n = x * 32 rounded to the nearest integer and
74     the remainder = x - n/32.
75 
76     Second, we approximate exp2(r1 + r2) - 1 where r1 is the leading
77     part of the remainder and r2 is the trailing part of the remainder.
78 
79     Third, we reconstruct exp2(x) so that
80     exp2(x) = 2**m * (z1 + z2).
81   */
82 
83 
84   GET_BITS_DP64(x, ux);
85   ax = ux & (~SIGNBIT_DP64);
86 
87   if (ax >= 0x4090000000000000) /* abs(x) >= 1024.0 */
88     {
89       if(ax >= 0x7ff0000000000000)
90         {
91           /* x is either NaN or infinity */
92           if (ux & MANTBITS_DP64)
93             /* x is NaN */
94             return _handle_error("exp2", OP_EXP, ux|0x0008000000000000, _DOMAIN,
95                                 0, EDOM, x, 0.0, 1);
96           else if (ux & SIGNBIT_DP64)
97             /* x is negative infinity; return 0.0 with no flags. */
98             return 0.0;
99           else
100             /* x is positive infinity */
101             return x;
102         }
103       if (x > max_exp2_arg)
104         /* Return +infinity with overflow flag */
105         return _handle_error("exp2", OP_EXP, PINFBITPATT_DP64, _OVERFLOW,
106                             AMD_F_OVERFLOW | AMD_F_INEXACT, ERANGE, x, 0.0, 1);
107       else if (x < min_exp2_arg)
108         /* x is negative. Return +zero with underflow and inexact flags */
109         return _handle_error("exp2", OP_EXP, 0, _UNDERFLOW,
110                             AMD_F_UNDERFLOW | AMD_F_INEXACT, ERANGE, x, 0.0, 1);
111     }
112 
113 
114   /* Handle small arguments separately */
115   if (ax < 0x3fb7154764ee6c2f)   /* abs(x) < 1/(16*log2) */
116     {
117       if (ax < 0x3c00000000000000)   /* abs(x) < 2^(-63) */
118         return 1.0 + x; /* Raises inexact if x is non-zero */
119       else
120         {
121           /* Split x into hx (head) and tx (tail). */
122           unsigned long long u;
123           hx = x;
124           GET_BITS_DP64(hx, u);
125           u &= 0xfffffffff8000000;
126           PUT_BITS_DP64(u, hx);
127           tx = x - hx;
128           /* Carefully multiply x by log2. y1 is the most significant
129              part of the result, and y2 the least significant part */
130           y1 = x * log2_lead;
131           y2 = (((hx * log2_lead - y1) + hx * log2_tail) +
132                   tx * log2_lead) + tx * log2_tail;
133 
134           y = y1 + y2;
135 		z = (9.99564649780173690e-1 +
136 		     (1.61251249355268050e-5 +
137 		      (2.37986978239838493e-2 +
138 		        2.68724774856111190e-7*y)*y)*y)/
139 		    (9.99564649780173692e-1 +
140 		     (-4.99766199765151309e-1 +
141 		      (1.070876894098586184e-1 +
142 		       (-1.189773642681502232e-2 +
143 			 5.9480622371960190616e-4*y)*y)*y)*y);
144           z = ((z * y1) + (z * y2)) + 1.0;
145         }
146     }
147   else
148     {
149       /* Find m, z1 and z2 such that exp2(x) = 2**m * (z1 + z2) */
150 
151       splitexp(x, log2, 32.0, one_by_32_lead, 0.0, &m, &z1, &z2);
152 
153       /* Scale (z1 + z2) by 2.0**m */
154       if (m > EMIN_DP64 && m < EMAX_DP64)
155 	z = scaleDouble_1((z1+z2),m);
156       else
157 	z = scaleDouble_2((z1+z2),m);
158     }
159   return z;
160 }
161