master
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
71static __FLT_TYPE do_powi_iter(__FLT_TYPE d, int y)
72{
73 unsigned int u = (unsigned int) y;
74 __FLT_TYPE rslt = ((u & 1) != 0) ? d : __FLT_CST(1.0);
75 u >>= 1;
76 do
77 {
78 d *= d;
79 if ((u & 1) != 0)
80 rslt *= d;
81 u >>= 1;
82 }
83 while (u > 0);
84 return rslt;
85}
86
87__FLT_TYPE __cdecl
88__FLT_ABI(__powi) (__FLT_TYPE x, int y);
89
90__FLT_TYPE __cdecl
91__FLT_ABI(__powi) (__FLT_TYPE x, int y)
92{
93 int x_class = fpclassify (x);
94 int odd_y = y & 1;
95 int recip = 0;
96 __FLT_TYPE d, rslt;
97
98 if (y == 0 || x == __FLT_CST(1.0))
99 return __FLT_CST(1.0);
100 else if (x_class == FP_NAN)
101 {
102 rslt = (signbit(x) ? -__FLT_NAN : __FLT_NAN);
103 __FLT_RPT_DOMAIN ("__powi", x, (__FLT_TYPE) y, rslt);
104 return rslt;
105 }
106 else if (x_class == FP_ZERO)
107 {
108 if (y >= 0)
109 {
110 if (!odd_y || !signbit (x))
111 return __FLT_CST (0.0);
112 return -__FLT_CST(0.0);
113 }
114
115 if (!odd_y || !signbit (x))
116 return __FLT_HUGE_VAL;
117 return (signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL);
118 }
119 else if (x_class == FP_INFINITE)
120 {
121 /* pow( -inf, y) = +0 for y<0 and not an odd integer, */
122 if (signbit(x) && y < 0 && !odd_y)
123 return __FLT_CST(0.0);
124 /* pow( -inf, y) = -inf for y an odd integer > 0. */
125 if (signbit(x) && y >= 0 && odd_y)
126 return -__FLT_HUGE_VAL;
127 /* pow( -inf, y) = +inf for y>0 and not an odd integer. */
128 if (signbit(x) && y >= 0 && !odd_y)
129 return __FLT_HUGE_VAL;
130 /* pow (+/-inf, y) is +/-0 with no exception for y an odd integer < 0. */
131 if (y < 0)
132 {
133 /* pow (+/-inf, y) is +0 with no exception for finite y < 0 and not an odd integer. */
134 return (odd_y && signbit(x) ? -__FLT_CST(0.0) : __FLT_CST(0.0));
135 }
136 /* pow (+/-inf, y) is +/-inf with no exception for finite y > 0 an odd integer. */
137 /* pow (+/-inf, y) is +inf with no exception for finite y > 0 and not an odd integer. */
138 return (odd_y && signbit(x) ? -__FLT_HUGE_VAL : __FLT_HUGE_VAL);
139 }
140
141 d = __FLT_ABI(fabs) (x);
142
143 if (y < 0)
144 {
145 /* By default, do the reciprocal of the result. */
146 recip = 1;
147 y = -y;
148 }
149
150 if (!y)
151 rslt = __FLT_CST(1.0);
152 else if (y == 1)
153 rslt = d;
154 else
155 {
156 rslt = do_powi_iter(d, y);
157 if (recip && fpclassify(rslt) == FP_INFINITE && d > __FLT_CST(1.0))
158 {
159 /* Uncommon case - we had overflow, but we're going to calculate
160 the reciprocal. If this happened, redo the calculation by doing
161 the reciprocal upfront instead. Instead of trying to calculate
162 whether this will happen, we prefer keeping the default case
163 cheap. */
164 d = __FLT_CST(1.0) / d;
165 recip = 0;
166 rslt = do_powi_iter(d, y);
167 }
168 }
169 if (recip)
170 rslt = __FLT_CST(1.0) / rslt;
171 if (signbit (x) && odd_y)
172 rslt = -rslt;
173 return rslt;
174}