| 1 |
/* |
| 2 |
This Software is provided under the Zope Public License (ZPL) Version 2.1. |
| 3 |
|
| 4 |
Copyright (c) 2009, 2010 by the mingw-w64 project |
| 5 |
|
| 6 |
See the AUTHORS file for the list of contributors to the mingw-w64 project. |
| 7 |
|
| 8 |
This license has been certified as open source. It has also been designated |
| 9 |
as GPL compatible by the Free Software Foundation (FSF). |
| 10 |
|
| 11 |
Redistribution and use in source and binary forms, with or without |
| 12 |
modification, are permitted provided that the following conditions are met: |
| 13 |
|
| 14 |
1. Redistributions in source code must retain the accompanying copyright |
| 15 |
notice, this list of conditions, and the following disclaimer. |
| 16 |
2. Redistributions in binary form must reproduce the accompanying |
| 17 |
copyright notice, this list of conditions, and the following disclaimer |
| 18 |
in the documentation and/or other materials provided with the |
| 19 |
distribution. |
| 20 |
3. Names of the copyright holders must not be used to endorse or promote |
| 21 |
products derived from this software without prior written permission |
| 22 |
from the copyright holders. |
| 23 |
4. The right to distribute this software or to use it for any purpose does |
| 24 |
not give you the right to use Servicemarks (sm) or Trademarks (tm) of |
| 25 |
the copyright holders. Use of them is covered by separate agreement |
| 26 |
with the copyright holders. |
| 27 |
5. If any files are modified, you must cause the modified files to carry |
| 28 |
prominent notices stating that you changed the files and the date of |
| 29 |
any change. |
| 30 |
|
| 31 |
Disclaimer |
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED |
| 34 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 35 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 36 |
EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 37 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 38 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 39 |
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 40 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 41 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 42 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 |
*/ |
| 44 |
|
| 45 |
/* IEEE 754 - Elementary Functions - Special Cases |
| 46 |
* pow(+/-0, oo) is +0 |
| 47 |
* pow(+/-0, -oo) is +oo |
| 48 |
* pow (x, +/-0) is 1 for any x (even a zero, quiet NaN, or infinity) |
| 49 |
* pow (+1, y) is 1 for any y (even a quiet NaN) |
| 50 |
* pow (+/-0, y) is +/-oo and signals the divideByZero exception for y an odd integer < 0 |
| 51 |
* pow (+/-0, y) is +oo and signals the divideByZero exception for finite y < 0 and not an odd integer |
| 52 |
* pow (+/-0, y) is +/-0 for finite y > 0 an odd integer |
| 53 |
* pow (+/-0, y) is +0 for finite y > 0 and not an odd integer |
| 54 |
* pow (-1, +/-oo) is 1 with no exception |
| 55 |
pow( -inf, y) = +0 for y<0 and not an odd integer |
| 56 |
pow( -inf, y) = -inf for y an odd integer > 0 |
| 57 |
pow( -inf, y) = +inf for y>0 and not an odd integer |
| 58 |
pow (+/-inf, y) is +/-0 with no exception for y an odd integer < 0 |
| 59 |
pow (+/-inf, -inf) is +0 with no exception |
| 60 |
pow (+/-inf, +inf) is +inf with no exception |
| 61 |
pow (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer |
| 62 |
pow (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer |
| 63 |
pow (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer |
| 64 |
pow (x, y) signals the invalid operation exception for finite x < 0 and finite non-integer y. |
| 65 |
|
| 66 |
For x /= 0: lim y->oo (1/x)^y results as: for |x| < 1 that sgn(x)*0 and for |x| > 0 that sgn(x)*Infinity |
| 67 |
|
| 68 |
*/ |
| 69 |
#include "../complex/complex_internal.h" |
| 70 |
#include <errno.h> |
| 71 |
#include <limits.h> |
| 72 |
#include <math.h> |
| 73 |
#include <errno.h> |
| 74 |
|
| 75 |
static __FLT_TYPE |
| 76 |
internal_modf (__FLT_TYPE value, __FLT_TYPE *iptr) |
| 77 |
{ |
| 78 |
__FLT_TYPE int_part = (__FLT_TYPE) 0.0; |
| 79 |
/* truncate */ |
| 80 |
/* truncate */ |
| 81 |
#ifdef __x86_64__ |
| 82 |
asm volatile ("pushq %%rax\n\tsubq $8, %%rsp\n" |
| 83 |
"fnstcw 4(%%rsp)\n" |
| 84 |
"movzwl 4(%%rsp), %%eax\n" |
| 85 |
"orb $12, %%ah\n" |
| 86 |
"movw %%ax, (%%rsp)\n" |
| 87 |
"fldcw (%%rsp)\n" |
| 88 |
"frndint\n" |
| 89 |
"fldcw 4(%%rsp)\n" |
| 90 |
"addq $8, %%rsp\npopq %%rax" : "=t" (int_part) : "0" (value)); /* round */ |
| 91 |
#else |
| 92 |
asm volatile ("push %%eax\n\tsubl $8, %%esp\n" |
| 93 |
"fnstcw 4(%%esp)\n" |
| 94 |
"movzwl 4(%%esp), %%eax\n" |
| 95 |
"orb $12, %%ah\n" |
| 96 |
"movw %%ax, (%%esp)\n" |
| 97 |
"fldcw (%%esp)\n" |
| 98 |
"frndint\n" |
| 99 |
"fldcw 4(%%esp)\n" |
| 100 |
"addl $8, %%esp\n\tpop %%eax\n" : "=t" (int_part) : "0" (value)); /* round */ |
| 101 |
#endif |
| 102 |
if (iptr) |
| 103 |
*iptr = int_part; |
| 104 |
return (isinf (value) ? (__FLT_TYPE) 0.0 : value - int_part); |
| 105 |
} |
| 106 |
|
| 107 |
__FLT_TYPE __cdecl __FLT_ABI(__powi) (__FLT_TYPE x, int n); |
| 108 |
|
| 109 |
__FLT_TYPE __cdecl |
| 110 |
__FLT_ABI(pow) (__FLT_TYPE x, __FLT_TYPE y) |
| 111 |
{ |
| 112 |
int x_class = fpclassify (x); |
| 113 |
int y_class = fpclassify (y); |
| 114 |
long odd_y = 0; |
| 115 |
__FLT_TYPE d, rslt; |
| 116 |
|
| 117 |
if (y_class == FP_ZERO || x == __FLT_CST(1.0)) |
| 118 |
return __FLT_CST(1.0); |
| 119 |
else if (x_class == FP_NAN || y_class == FP_NAN) |
| 120 |
{ |
| 121 |
if (x_class == FP_NAN) { |
| 122 |
__FLT_RPT_DOMAIN ("pow", x, y, x); |
| 123 |
return x; |
| 124 |
} else { |
| 125 |
__FLT_RPT_DOMAIN ("pow", x, y, y); |
| 126 |
return y; |
| 127 |
} |
| 128 |
} |
| 129 |
else if (x_class == FP_ZERO) |
| 130 |
{ |
| 131 |
if (y_class == FP_INFINITE) |
| 132 |
return (signbit(y) ? __FLT_HUGE_VAL : __FLT_CST(0.0)); |
| 133 |
|
| 134 |
if (signbit(x) && internal_modf (y, &d) != 0.0) |
| 135 |
{ |
| 136 |
return signbit (y) ? (1.0 / -x) : __FLT_CST (0.0); |
| 137 |
/*__FLT_RPT_DOMAIN ("pow", x, y, -__FLT_NAN); |
| 138 |
return -__FLT_NAN; */ |
| 139 |
} |
| 140 |
odd_y = (internal_modf (__FLT_ABI (ldexp) (y, -1), &d) != 0.0) ? 1 : 0; |
| 141 |
if (!signbit(y)) |
| 142 |
{ |
| 143 |
if (!odd_y || !signbit (x)) |
| 144 |
return __FLT_CST (0.0); |
| 145 |
return -__FLT_CST(0.0); |
| 146 |
} |
| 147 |
|
| 148 |
if (!odd_y || !signbit (x)) |
| 149 |
return __FLT_HUGE_VAL; |
| 150 |
return (signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL); |
| 151 |
} |
| 152 |
else if (y_class == FP_INFINITE) |
| 153 |
{ |
| 154 |
__FLT_TYPE a_x; |
| 155 |
|
| 156 |
if (x_class == FP_INFINITE) |
| 157 |
return (signbit (y) ? __FLT_CST (0.0) : __FLT_HUGE_VAL); |
| 158 |
a_x = (signbit (x) ? -x : x); |
| 159 |
if (a_x == 1.0) |
| 160 |
return __FLT_CST (1.0); |
| 161 |
if (a_x > 1.0) |
| 162 |
return (signbit (y) == 0 ? __FLT_HUGE_VAL : __FLT_CST (0.0)); |
| 163 |
return (!signbit (y) ? __FLT_CST (0.0) : __FLT_HUGE_VAL); |
| 164 |
} |
| 165 |
else if (x_class == FP_INFINITE) |
| 166 |
{ |
| 167 |
/* pow (x, y) signals the invalid operation exception for finite x < 0 and finite non-integer y. */ |
| 168 |
if (signbit(x) && internal_modf (y, &d) != 0.0) |
| 169 |
{ |
| 170 |
return signbit(y) ? 1.0 / -x : -x; |
| 171 |
/*__FLT_RPT_DOMAIN ("pow", x, y, -__FLT_NAN); |
| 172 |
return -__FLT_NAN;*/ |
| 173 |
} |
| 174 |
odd_y = (internal_modf (__FLT_ABI (ldexp) (y, -1), &d) != 0.0) ? 1 : 0; |
| 175 |
/* pow( -inf, y) = +0 for y<0 and not an odd integer, */ |
| 176 |
if (signbit(x) && signbit(y) && !odd_y) |
| 177 |
return __FLT_CST(0.0); |
| 178 |
/* pow( -inf, y) = -inf for y an odd integer > 0. */ |
| 179 |
if (signbit(x) && !signbit(y) && odd_y) |
| 180 |
return -__FLT_HUGE_VAL; |
| 181 |
/* pow( -inf, y) = +inf for y>0 and not an odd integer. */ |
| 182 |
if (signbit(x) && !signbit(y) && !odd_y) |
| 183 |
return __FLT_HUGE_VAL; |
| 184 |
/* pow (+/-inf, y) is +/-0 with no exception for y an odd integer < 0. */ |
| 185 |
if (signbit(y)) |
| 186 |
{ |
| 187 |
/* pow (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer. */ |
| 188 |
return (odd_y && signbit(x) ? -__FLT_CST(0.0) : __FLT_CST(0.0)); |
| 189 |
} |
| 190 |
/* pow (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer. */ |
| 191 |
/* pow (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer. */ |
| 192 |
return (odd_y && signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL); |
| 193 |
} |
| 194 |
|
| 195 |
if (internal_modf (y, &d) != 0.0) |
| 196 |
{ |
| 197 |
if (signbit (x)) |
| 198 |
{ |
| 199 |
__FLT_RPT_DOMAIN ("pow", x, y, -__FLT_NAN); |
| 200 |
return -__FLT_NAN; |
| 201 |
} |
| 202 |
if (y == __FLT_CST(0.5)) |
| 203 |
{ |
| 204 |
asm volatile ("fsqrt" : "=t" (rslt) : "0" (x)); |
| 205 |
return rslt; |
| 206 |
} |
| 207 |
} |
| 208 |
else if ((d <= (__FLT_TYPE) INT_MAX && d >= (__FLT_TYPE) INT_MIN)) |
| 209 |
return __FLT_ABI (__powi) (x, (int) y); |
| 210 |
/* As exp already checks for minlog and maxlog no further checks are necessary. */ |
| 211 |
rslt = (__FLT_TYPE) exp2l ((long double) y * log2l ((long double) __FLT_ABI(fabs) (x))); |
| 212 |
|
| 213 |
if (signbit (x) && internal_modf (__FLT_ABI (ldexp) (y, -1), &d) != 0.0) |
| 214 |
rslt = -rslt; |
| 215 |
return rslt; |
| 216 |
} |