• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fenv.h>
73 #include <math.h>
74 #include <errno.h>
75 #define FE_ROUNDING_MASK \
76   (FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
77 
78 static __FLT_TYPE
internal_modf(__FLT_TYPE value,__FLT_TYPE * iptr)79 internal_modf (__FLT_TYPE value, __FLT_TYPE *iptr)
80 {
81   __FLT_TYPE int_part = (__FLT_TYPE) 0.0;
82   /* truncate */
83   /* truncate */
84 #ifdef __x86_64__
85   asm volatile ("pushq %%rax\n\tsubq $8, %%rsp\n"
86     "fnstcw 4(%%rsp)\n"
87     "movzwl 4(%%rsp), %%eax\n"
88     "orb $12, %%ah\n"
89     "movw %%ax, (%%rsp)\n"
90     "fldcw (%%rsp)\n"
91     "frndint\n"
92     "fldcw 4(%%rsp)\n"
93     "addq $8, %%rsp\npopq %%rax" : "=t" (int_part) : "0" (value)); /* round */
94 #else
95   asm volatile ("push %%eax\n\tsubl $8, %%esp\n"
96     "fnstcw 4(%%esp)\n"
97     "movzwl 4(%%esp), %%eax\n"
98     "orb $12, %%ah\n"
99     "movw %%ax, (%%esp)\n"
100     "fldcw (%%esp)\n"
101     "frndint\n"
102     "fldcw 4(%%esp)\n"
103     "addl $8, %%esp\n\tpop %%eax\n" : "=t" (int_part) : "0" (value)); /* round */
104 #endif
105   if (iptr)
106     *iptr = int_part;
107   return (isinf (value) ?  (__FLT_TYPE) 0.0 : value - int_part);
108 }
109 
110 __FLT_TYPE __cdecl __FLT_ABI(__powi) (__FLT_TYPE x, int n);
111 
112 __FLT_TYPE __cdecl
__FLT_ABI(pow)113 __FLT_ABI(pow) (__FLT_TYPE x, __FLT_TYPE y)
114 {
115   int x_class = fpclassify (x);
116   int y_class = fpclassify (y);
117   long odd_y = 0;
118   __FLT_TYPE d, rslt;
119 
120   if (y_class == FP_ZERO || x == __FLT_CST(1.0))
121     return __FLT_CST(1.0);
122   else if (x_class == FP_NAN || y_class == FP_NAN)
123     {
124       if (x_class == FP_NAN) {
125         __FLT_RPT_DOMAIN ("pow", x, y, x);
126         return x;
127       } else {
128         __FLT_RPT_DOMAIN ("pow", x, y, y);
129         return y;
130       }
131     }
132   else if (x_class == FP_ZERO)
133     {
134       if (y_class == FP_INFINITE)
135 	return (signbit(y) ? __FLT_HUGE_VAL : __FLT_CST(0.0));
136 
137       if (signbit(x) && internal_modf (y, &d) != 0.0)
138 	{
139 	  return signbit (y) ? (1.0 / -x) : __FLT_CST (0.0);
140 	  /*__FLT_RPT_DOMAIN ("pow", x, y, -__FLT_NAN);
141 	  return -__FLT_NAN; */
142 	}
143       odd_y = (internal_modf (__FLT_ABI (ldexp) (y, -1), &d) != 0.0) ? 1 : 0;
144       if (!signbit(y))
145 	{
146 	  if (!odd_y || !signbit (x))
147 	    return __FLT_CST (0.0);
148 	  return -__FLT_CST(0.0);
149 	}
150 
151       if (!odd_y || !signbit (x))
152 	return __FLT_HUGE_VAL;
153       return (signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL);
154     }
155   else if (y_class == FP_INFINITE)
156     {
157       __FLT_TYPE a_x;
158 
159       if (x_class == FP_INFINITE)
160 	return (signbit (y) ? __FLT_CST (0.0) : __FLT_HUGE_VAL);
161       a_x = (signbit (x) ? -x : x);
162       if (a_x == 1.0)
163 	return __FLT_CST (1.0);
164       if (a_x > 1.0)
165 	return (signbit (y) == 0 ? __FLT_HUGE_VAL : __FLT_CST (0.0));
166       return (!signbit (y) ? __FLT_CST (0.0) : __FLT_HUGE_VAL);
167     }
168   else if (x_class == FP_INFINITE)
169     {
170       /* pow (x, y) signals the invalid operation exception for finite x < 0 and finite non-integer y.  */
171       if (signbit(x) && internal_modf (y, &d) != 0.0)
172 	{
173 	  return signbit(y) ? 1.0 / -x : -x;
174 	  /*__FLT_RPT_DOMAIN ("pow", x, y, -__FLT_NAN);
175 	  return -__FLT_NAN;*/
176 	}
177       odd_y = (internal_modf (__FLT_ABI (ldexp) (y, -1), &d) != 0.0) ? 1 : 0;
178       /* pow( -inf, y) = +0 for y<0 and not an odd integer,  */
179       if (signbit(x) && signbit(y) && !odd_y)
180 	return __FLT_CST(0.0);
181       /* pow( -inf, y) = -inf for y an odd integer > 0.  */
182       if (signbit(x) && !signbit(y) && odd_y)
183 	return -__FLT_HUGE_VAL;
184       /* pow( -inf, y) = +inf for y>0 and not an odd integer.  */
185       if (signbit(x) && !signbit(y) && !odd_y)
186 	return __FLT_HUGE_VAL;
187       /* pow (+/-inf, y) is +/-0 with no exception for y an odd integer < 0. */
188       if (signbit(y))
189       {
190         /* pow (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer.  */
191 	return (odd_y && signbit(x) ? -__FLT_CST(0.0) : __FLT_CST(0.0));
192       }
193       /* pow (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer.  */
194       /* pow (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer.  */
195       return (odd_y && signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL);
196     }
197 
198   if (internal_modf (y, &d) != 0.0)
199     {
200       if (signbit (x))
201 	{
202 	  __FLT_RPT_DOMAIN ("pow", x, y, -__FLT_NAN);
203 	  return -__FLT_NAN;
204 	}
205       if (y == __FLT_CST(0.5))
206 	{
207 	  asm volatile ("fsqrt" : "=t" (rslt) : "0" (x));
208 	  return rslt;
209 	}
210     }
211   else if ((d <= (__FLT_TYPE) INT_MAX && d >= (__FLT_TYPE) INT_MIN))
212      return __FLT_ABI (__powi) (x, (int) y);
213   /* As exp already checks for minlog and maxlog no further checks are necessary.  */
214   rslt = (__FLT_TYPE) exp2l ((long double) y * log2l ((long double) __FLT_ABI(fabs) (x)));
215 
216   if (signbit (x) && internal_modf (__FLT_ABI (ldexp) (y, -1), &d) != 0.0)
217     rslt = -rslt;
218   return rslt;
219 }
220