xref: /reactos/sdk/lib/crt/math/libm_sse2/exp2.c (revision 09dde2cf)
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 /* exp2 is only provided for use by powf under Windows, so give
48    it a leading underscore. */
49 double FN_PROTOTYPE(_exp2)(double x)
50 {
51   static const double
52     max_exp2_arg = 1024.0,  /* 0x4090000000000000 */
53     min_exp2_arg = -1074.0, /* 0xc090c80000000000 */
54     log2 = 6.931471805599453094178e-01, /* 0x3fe62e42fefa39ef */
55     log2_lead = 6.93147167563438415527E-01, /* 0x3fe62e42f8000000 */
56     log2_tail = 1.29965068938898869640E-08, /* 0x3e4be8e7bcd5e4f1 */
57     one_by_32_lead = 0.03125;
58 
59   double y, z1, z2, z, hx, tx, y1, y2;
60   int m;
61   unsigned long long ux, ax;
62 
63   /*
64     Computation of exp2(x).
65 
66     We compute the values m, z1, and z2 such that
67     exp2(x) = 2**m * (z1 + z2),  where exp2(x) is 2**x.
68 
69     Computations needed in order to obtain m, z1, and z2
70     involve three steps.
71 
72     First, we reduce the argument x to the form
73     x = n/32 + remainder,
74     where n has the value of an integer and |remainder| <= 1/64.
75     The value of n = x * 32 rounded to the nearest integer and
76     the remainder = x - n/32.
77 
78     Second, we approximate exp2(r1 + r2) - 1 where r1 is the leading
79     part of the remainder and r2 is the trailing part of the remainder.
80 
81     Third, we reconstruct exp2(x) so that
82     exp2(x) = 2**m * (z1 + z2).
83   */
84 
85 
86   GET_BITS_DP64(x, ux);
87   ax = ux & (~SIGNBIT_DP64);
88 
89   if (ax >= 0x4090000000000000) /* abs(x) >= 1024.0 */
90     {
91       if(ax >= 0x7ff0000000000000)
92         {
93           /* x is either NaN or infinity */
94           if (ux & MANTBITS_DP64)
95             /* x is NaN */
96             return _handle_error("exp2", OP_EXP, ux|0x0008000000000000, _DOMAIN,
97                                 0, EDOM, x, 0.0, 1);
98           else if (ux & SIGNBIT_DP64)
99             /* x is negative infinity; return 0.0 with no flags. */
100             return 0.0;
101           else
102             /* x is positive infinity */
103             return x;
104         }
105       if (x > max_exp2_arg)
106         /* Return +infinity with overflow flag */
107         return _handle_error("exp2", OP_EXP, PINFBITPATT_DP64, _OVERFLOW,
108                             AMD_F_OVERFLOW | AMD_F_INEXACT, ERANGE, x, 0.0, 1);
109       else if (x < min_exp2_arg)
110         /* x is negative. Return +zero with underflow and inexact flags */
111         return _handle_error("exp2", OP_EXP, 0, _UNDERFLOW,
112                             AMD_F_UNDERFLOW | AMD_F_INEXACT, ERANGE, x, 0.0, 1);
113     }
114 
115 
116   /* Handle small arguments separately */
117   if (ax < 0x3fb7154764ee6c2f)   /* abs(x) < 1/(16*log2) */
118     {
119       if (ax < 0x3c00000000000000)   /* abs(x) < 2^(-63) */
120         return 1.0 + x; /* Raises inexact if x is non-zero */
121       else
122         {
123           /* Split x into hx (head) and tx (tail). */
124           unsigned long long u;
125           hx = x;
126           GET_BITS_DP64(hx, u);
127           u &= 0xfffffffff8000000;
128           PUT_BITS_DP64(u, hx);
129           tx = x - hx;
130           /* Carefully multiply x by log2. y1 is the most significant
131              part of the result, and y2 the least significant part */
132           y1 = x * log2_lead;
133           y2 = (((hx * log2_lead - y1) + hx * log2_tail) +
134                   tx * log2_lead) + tx * log2_tail;
135 
136           y = y1 + y2;
137 		z = (9.99564649780173690e-1 +
138 		     (1.61251249355268050e-5 +
139 		      (2.37986978239838493e-2 +
140 		        2.68724774856111190e-7*y)*y)*y)/
141 		    (9.99564649780173692e-1 +
142 		     (-4.99766199765151309e-1 +
143 		      (1.070876894098586184e-1 +
144 		       (-1.189773642681502232e-2 +
145 			 5.9480622371960190616e-4*y)*y)*y)*y);
146           z = ((z * y1) + (z * y2)) + 1.0;
147         }
148     }
149   else
150     {
151       /* Find m, z1 and z2 such that exp2(x) = 2**m * (z1 + z2) */
152 
153       splitexp(x, log2, 32.0, one_by_32_lead, 0.0, &m, &z1, &z2);
154 
155       /* Scale (z1 + z2) by 2.0**m */
156       if (m > EMIN_DP64 && m < EMAX_DP64)
157 	z = scaleDouble_1((z1+z2),m);
158       else
159 	z = scaleDouble_2((z1+z2),m);
160     }
161   return z;
162 }
163