Skip to content

Commit 5449705

Browse files
authored
Merge pull request #227 from JuliaMath/vs/powf
Fix #211
2 parents 6a85b33 + 98f8713 commit 5449705

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/e_powf.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ bp[] = {1.0, 1.5,},
2525
dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */
2626
dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */
2727
zero = 0.0,
28+
half = 0.5,
29+
qrtr = 0.25,
30+
thrd = 3.33333343e-01, /* 0x3eaaaaab */
2831
one = 1.0,
2932
two = 2.0,
3033
two24 = 16777216.0, /* 0x4b800000 */
@@ -74,7 +77,7 @@ __ieee754_powf(float x, float y)
7477
/* y!=zero: result is NaN if either arg is NaN */
7578
if(ix > 0x7f800000 ||
7679
iy > 0x7f800000)
77-
return (x+0.0F)+(y+0.0F);
80+
return nan_mix(x, y);
7881

7982
/* determine if y is an odd int when x < 0
8083
* yisint = 0 ... y is not an integer
@@ -103,15 +106,10 @@ __ieee754_powf(float x, float y)
103106
if(iy==0x3f800000) { /* y is +-1 */
104107
if(hy<0) return one/x; else return x;
105108
}
106-
if(hy==0x40000000) return x*x; /* y is 2 */
107-
if(hy==0x40400000) return x*x*x; /* y is 3 */
108-
if(hy==0x40800000) { /* y is 4 */
109-
u = x*x;
110-
return u*u;
111-
}
112-
if(hy==0x3f000000) { /* y is 0.5 */
109+
if(hy==0x40000000) return x*x; /* y is 2 */
110+
if(hy==0x3f000000) { /* y is 0.5 */
113111
if(hx>=0) /* x >= +0 */
114-
return __ieee754_sqrtf(x);
112+
return __ieee754_sqrtf(x);
115113
}
116114

117115
ax = fabsf(x);
@@ -144,7 +142,7 @@ __ieee754_powf(float x, float y)
144142
/* now |1-x| is tiny <= 2**-20, suffice to compute
145143
log(x) by x-x^2/2+x^3/3-x^4/4 */
146144
t = ax-1; /* t has 20 trailing zeros */
147-
w = (t*t)*((float)0.5-t*((float)0.333333333333-t*(float)0.25));
145+
w = (t*t)*(half-t*(thrd-t*qrtr));
148146
u = ivln2_h*t; /* ivln2_h has 16 sig. bits */
149147
v = t*ivln2_l-w*ivln2;
150148
t1 = u+v;
@@ -183,10 +181,10 @@ __ieee754_powf(float x, float y)
183181
r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
184182
r += s_l*(s_h+s);
185183
s2 = s_h*s_h;
186-
t_h = (float)3.0+s2+r;
184+
t_h = 3+s2+r;
187185
GET_FLOAT_WORD(is,t_h);
188186
SET_FLOAT_WORD(t_h,is&0xfffff000);
189-
t_l = r-((t_h-(float)3.0)-s2);
187+
t_l = r-((t_h-3)-s2);
190188
/* u+v = s*(1+...) */
191189
u = s_h*t_h;
192190
v = s_l*t_h+t_l*s;
@@ -198,7 +196,7 @@ __ieee754_powf(float x, float y)
198196
z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */
199197
z_l = cp_l*p_h+p_l*cp+dp_l[k];
200198
/* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
201-
t = (float)n;
199+
t = n;
202200
t1 = (((z_h+z_l)+dp_h[k])+t);
203201
GET_FLOAT_WORD(is,t1);
204202
SET_FLOAT_WORD(t1,is&0xfffff000);

src/math_private.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ do { \
230230
*/
231231
void __scan_nan(u_int32_t *__words, int __num_words, const char *__s);
232232

233+
/*
234+
* Mix 1 or 2 NaNs. First add 0 to each arg. This normally just turns
235+
* signaling NaNs into quiet NaNs by setting a quiet bit. We do this
236+
* because we want to never return a signaling NaN, and also because we
237+
* don't want the quiet bit to affect the result. Then mix the converted
238+
* args using addition. The result is typically the arg whose mantissa
239+
* bits (considered as in integer) are largest.
240+
*
241+
* Technical complications: the result in bits might depend on the precision
242+
* and/or on compiler optimizations, especially when different register sets
243+
* are used for different precisions. Try to make the result not depend on
244+
* at least the precision by always doing the main mixing step in long double
245+
* precision. Try to reduce dependencies on optimizations by adding the
246+
* the 0's in different precisions (unless everything is in long double
247+
* precision).
248+
*/
249+
#define nan_mix(x, y) (((x) + 0.0L) + ((y) + 0))
250+
233251
#ifdef __GNUCLIKE_ASM
234252

235253
/* Asm versions of some functions. */

test/test-211.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
#include <assert.h>
4+
5+
int
6+
main()
7+
{
8+
float x = 0xd.65874p-4f;
9+
float y = 4.0f;
10+
float z = powf (x, y);
11+
assert(z==0x1.f74424p-2);
12+
}

0 commit comments

Comments
 (0)