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  * powi (x, +/-0) is 1 for any x (even a zero, quiet NaN, or infinity)
47  * powi (+1, y) is 1 for any y (even a quiet NaN)
48  * powi (+/-0, y) is +/-oo and signals the divideByZero exception for y an odd integer < 0
49  * powi (+/-0, y) is +oo and signals the divideByZero exception for finite y < 0 and not an odd integer
50  * powi (+/-0, y) is +/-0 for finite y > 0 an odd integer
51  * powi (+/-0, y) is +0 for finite y > 0 and not an odd integer
52  powi (-inf, y) = +0 for y<0 and not an odd integer
53  powi (-inf, y) = -inf for y an odd integer > 0
54  powi (-inf, y) = +inf for y>0 and not an odd integer
55  powi (+/-inf, y) is +/-0 with no exception for y an odd integer < 0
56  powi (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer
57  powi (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer
58  powi (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer
59  powi (x, y) signals the invalid operation exception for finite x < 0 and finite non-integer y.
60 
61  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
62 
63 */
64 #include "../complex/complex_internal.h"
65 #include <errno.h>
66 #include <limits.h>
67 #include <fenv.h>
68 #include <math.h>
69 #include <errno.h>
70 
71 __FLT_TYPE __cdecl
72 __FLT_ABI(__powi) (__FLT_TYPE x, int y);
73 
74 __FLT_TYPE __cdecl
__FLT_ABI(__powi)75 __FLT_ABI(__powi) (__FLT_TYPE x, int y)
76 {
77   int x_class = fpclassify (x);
78   int odd_y = y & 1;
79   __FLT_TYPE d, rslt;
80 
81   if (y == 0 || x == __FLT_CST(1.0))
82     return __FLT_CST(1.0);
83   else if (x_class == FP_NAN)
84     {
85       rslt = (signbit(x) ? -__FLT_NAN : __FLT_NAN);
86       __FLT_RPT_DOMAIN ("__powi", x, (__FLT_TYPE) y, rslt);
87       return rslt;
88     }
89   else if (x_class == FP_ZERO)
90     {
91       if (y >= 0)
92 	{
93 	  if (!odd_y || !signbit (x))
94 	    return __FLT_CST (0.0);
95 	  return -__FLT_CST(0.0);
96 	}
97 
98       if (!odd_y || !signbit (x))
99 	return __FLT_HUGE_VAL;
100       return (signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL);
101     }
102   else if (x_class == FP_INFINITE)
103     {
104       /* pow( -inf, y) = +0 for y<0 and not an odd integer,  */
105       if (signbit(x) && y < 0 && !odd_y)
106 	return __FLT_CST(0.0);
107       /* pow( -inf, y) = -inf for y an odd integer > 0.  */
108       if (signbit(x) && y >= 0 && odd_y)
109 	return -__FLT_HUGE_VAL;
110       /* pow( -inf, y) = +inf for y>0 and not an odd integer.  */
111       if (signbit(x) && y >= 0 && !odd_y)
112 	return __FLT_HUGE_VAL;
113       /* pow (+/-inf, y) is +/-0 with no exception for y an odd integer < 0. */
114       if (y < 0)
115       {
116         /* pow (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer.  */
117 	return (odd_y && signbit(x) ? -__FLT_CST(0.0) : __FLT_CST(0.0));
118       }
119       /* pow (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer.  */
120       /* pow (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer.  */
121       return (odd_y && signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL);
122     }
123 
124   d = __FLT_ABI(fabs) (x);
125 
126   if (y < 0)
127     {
128       d = __FLT_CST(1.0) / d;
129       y = -y;
130     }
131 
132   if (!y)
133     rslt = __FLT_CST(1.0);
134   else if (y == 1)
135     rslt = d;
136   else
137     {
138       unsigned int u = (unsigned int) y;
139       rslt = ((u & 1) != 0) ? d : __FLT_CST(1.0);
140       u >>= 1;
141       do
142 	{
143 	  d *= d;
144 	  if ((u & 1) != 0)
145 	    rslt *= d;
146 	  u >>= 1;
147 	}
148       while (u > 0);
149     }
150   if (signbit (x) && odd_y)
151     rslt = -rslt;
152   return rslt;
153 }
154