-
Notifications
You must be signed in to change notification settings - Fork 25
/
e_powl.c
270 lines (248 loc) · 7.36 KB
/
e_powl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
/* Expansions and modifications for 128-bit long double are
Copyright (C) 2001 Stephen L. Moshier <[email protected]>
and are incorporated herein by permission of the author. The author
reserves the right to distribute this material elsewhere under different
copying permissions. These modifications are distributed here under
the following terms:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see
<http://www.gnu.org/licenses/>. */
/* __ieee754_powl(x,y) return x**y
*
* n
* Method: Let x = 2 * (1+f)
* 1. Compute and return log2(x) in two pieces:
* log2(x) = w1 + w2,
* where w1 has 113-53 = 60 bit trailing zeros.
* 2. Perform y*log2(x) = n+y' by simulating multi-precision
* arithmetic, where |y'|<=0.5.
* 3. Return x**y = 2**n*exp(y'*log2)
*
* Special cases:
* 1. (anything) ** 0 is 1
* 2. (anything) ** 1 is itself
* 3a. (anything) ** NAN is NAN except
* 3b. +1 ** NAN is 1
* 4. NAN ** (anything except 0) is NAN
* 5. +-(|x| > 1) ** +INF is +INF
* 6. +-(|x| > 1) ** -INF is +0
* 7. +-(|x| < 1) ** +INF is +0
* 8. +-(|x| < 1) ** -INF is +INF
* 9. +-1 ** +-INF is 1
* 10. +0 ** (+anything except 0, NAN) is +0
* 11. -0 ** (+anything except 0, NAN, odd integer) is +0
* 12. +0 ** (-anything except 0, NAN) is +INF
* 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
* 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
* 15. +INF ** (+anything except 0,NAN) is +INF
* 16. +INF ** (-anything except 0,NAN) is +0
* 17. -INF ** (anything) = -0 ** (-anything)
* 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
* 19. (-anything except 0 and inf) ** (non-integer) is NAN
*
*/
#ifndef __FDLIBM_H__
#include "fdlibm.h"
#endif
#ifndef __NO_LONG_DOUBLE_MATH
#ifndef __have_fpu_pow
long double __ieee754_powl(long double x, long double y)
{
int x_class = fpclassify(x);
int y_class = fpclassify(y);
int odd_y;
long double d, rslt;
int32_t dexp, yexp;
if (y_class == FP_ZERO || x == 1.0L)
{
/* unless x is signaling NaN */
if (issignalingl(x))
return __builtin_nanl("");
return 1.0L;
}
if (x_class == FP_NAN || y_class == FP_NAN)
{
rslt = signbit(x) ? -__builtin_nanl("") : __builtin_nanl("");
return rslt;
}
if (x_class == FP_ZERO)
{
if (y_class == FP_INFINITE)
return signbit(y) ? HUGE_VALL : 0.0L;
if (signbit(x) && __ieee754_truncl(y) != y)
{
return signbit(y) ? (1.0 / -x) : 0.0L;
}
d = __ieee754_scalbnl(y, -1);
odd_y = __ieee754_truncl(d) != d;
if (!signbit(y))
{
if (!odd_y || !signbit(x))
return 0.0L;
return -0.0L;
}
feraiseexcept(FE_DIVBYZERO);
if (!odd_y || !signbit(x))
return HUGE_VALL;
return signbit(x) ? -HUGE_VALL : HUGE_VALL;
}
if (y_class == FP_INFINITE)
{
long double a_x;
if (x_class == FP_INFINITE)
return (signbit(y) ? 0.0L : HUGE_VALL);
a_x = (signbit(x) ? -x : x);
if (a_x == 1.0L)
return 1.0L;
if (a_x > 1.0)
return (signbit(y) == 0 ? HUGE_VALL : 0.0L);
return (!signbit(y) ? 0.0L : HUGE_VALL);
}
if (x_class == FP_INFINITE)
{
/* pow (x, y) signals the invalid operation exception for finite x < 0 and finite non-integer y. */
if (signbit(x) && __ieee754_truncl(y) != y)
{
return signbit(y) ? 1.0 / -x : -x;
}
d = __ieee754_scalbnl(y, -1);
odd_y = __ieee754_truncl(d) != d;
/* pow( -inf, y) = +0 for y<0 and not an odd integer, */
if (signbit(x) && signbit(y) && !odd_y)
return 0.0L;
/* pow( -inf, y) = -inf for y an odd integer > 0. */
if (signbit(x) && !signbit(y) && odd_y)
return -HUGE_VALL;
/* pow( -inf, y) = +inf for y>0 and not an odd integer. */
if (signbit(x) && !signbit(y) && !odd_y)
return HUGE_VALL;
/* pow (+/-inf, y) is +/-0 with no exception for y an odd integer < 0. */
if (signbit(y))
{
/* pow (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer. */
return (odd_y && signbit(x)) ? -0.0L : 0.0L;
}
/* pow (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer. */
/* pow (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer. */
return (odd_y && signbit(x) ? -HUGE_VALL : HUGE_VALL);
}
d = __ieee754_truncl(y);
if (d != y)
{
if (signbit(x))
{
return -__builtin_nanl("");
}
if (y == 0.5L)
{
return __ieee754_sqrtl(x);
}
} else if ((d <= (long double) INT_MAX && d >= (long double) INT_MIN))
{
return __ieee754_powil(x, (int) y);
}
/* As exp already checks for minlog and maxlog no further checks are necessary. */
d = __ieee754_log2l(__ieee754_fabsl(x));
dexp = __ieee754_ilogbl(d);
yexp = __ieee754_ilogbl(y);
if (dexp > 0 && (yexp + dexp) >= LDBL_MAX_EXP)
{
dexp = __ieee754_ilogbl(x);
if ((dexp < 0 && y < 0.0L) || (dexp > 0 && y > 0.0L))
{
feraiseexcept(FE_OVERFLOW);
rslt = HUGE_VALL;
} else
{
feraiseexcept(FE_UNDERFLOW);
rslt = 0;
}
} else if (dexp < 0 && y > 0.0L && (-yexp + dexp) < LDBL_MIN_EXP)
{
feraiseexcept(FE_UNDERFLOW);
if ((-yexp + dexp) >= (LDBL_MIN_EXP - LDBL_MANT_DIG))
rslt = 1.0L;
else
rslt = 0;
} else
{
rslt = __ieee754_exp2l(y * d);
}
if (signbit(x))
{
y = __ieee754_scalbnl(y, -1);
if (y != __ieee754_truncl(y))
rslt = -rslt;
}
return rslt;
}
#endif
/* wrapper powl */
long double __powl(long double x, long double y)
{
long double z = __ieee754_powl(x, y);
if (!isfinite(z))
{
if (_LIB_VERSION != _IEEE_)
{
if (isnan(x))
{
if (y == 0.0L)
/* pow(NaN,0.0) */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_NAN);
} else if (isfinite(x) && isfinite(y))
{
if (isnan(z))
{
/* pow neg**non-int */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_NONINT);
} else if (x == 0.0L && y < 0.0L)
{
if (signbit(x) && signbit(z))
/* pow(-0.0,negative) */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_MINUS);
else
/* pow(+0.0,negative) */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_ZEROMINUS);
} else
{
/* pow overflow */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_OVERFLOW);
}
}
}
} else if (z == 0.0L && isfinite(x) && isfinite(y) && _LIB_VERSION != _IEEE_)
{
if (x == 0.0L)
{
if (y == 0.0L)
/* pow(0.0,0.0) */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_ZERO);
} else
{
/* pow underflow */
return __kernel_standard_l(x, y, z, KMATHERRL_POW_UNDERFLOW);
}
}
return z;
}
__typeof(__powl) powl __attribute__((weak, alias("__powl")));
#endif