Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 0a09954

Browse files
committed
pow!
1 parent e979c7d commit 0a09954

File tree

1 file changed

+395
-0
lines changed

1 file changed

+395
-0
lines changed

src/math/pow.rs

Lines changed: 395 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,395 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_pow.c */
2+
/*
3+
* ====================================================
4+
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
5+
*
6+
* Permission to use, copy, modify, and distribute this
7+
* software is freely granted, provided that this notice
8+
* is preserved.
9+
* ====================================================
10+
*/
11+
/* pow(x,y) return x**y
12+
*
13+
* n
14+
* Method: Let x = 2 * (1+f)
15+
* 1. Compute and return log2(x) in two pieces:
16+
* log2(x) = w1 + w2,
17+
* where w1 has 53-24 = 29 bit trailing zeros.
18+
* 2. Perform y*log2(x) = n+y' by simulating muti-precision
19+
* arithmetic, where |y'|<=0.5.
20+
* 3. Return x**y = 2**n*exp(y'*log2)
21+
*
22+
* Special cases:
23+
* 1. (anything) ** 0 is 1
24+
* 2. 1 ** (anything) is 1
25+
* 3. (anything except 1) ** NAN is NAN
26+
* 4. NAN ** (anything except 0) is NAN
27+
* 5. +-(|x| > 1) ** +INF is +INF
28+
* 6. +-(|x| > 1) ** -INF is +0
29+
* 7. +-(|x| < 1) ** +INF is +0
30+
* 8. +-(|x| < 1) ** -INF is +INF
31+
* 9. -1 ** +-INF is 1
32+
* 10. +0 ** (+anything except 0, NAN) is +0
33+
* 11. -0 ** (+anything except 0, NAN, odd integer) is +0
34+
* 12. +0 ** (-anything except 0, NAN) is +INF, raise divbyzero
35+
* 13. -0 ** (-anything except 0, NAN, odd integer) is +INF, raise divbyzero
36+
* 14. -0 ** (+odd integer) is -0
37+
* 15. -0 ** (-odd integer) is -INF, raise divbyzero
38+
* 16. +INF ** (+anything except 0,NAN) is +INF
39+
* 17. +INF ** (-anything except 0,NAN) is +0
40+
* 18. -INF ** (+odd integer) is -INF
41+
* 19. -INF ** (anything) = -0 ** (-anything), (anything except odd integer)
42+
* 20. (anything) ** 1 is (anything)
43+
* 21. (anything) ** -1 is 1/(anything)
44+
* 22. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
45+
* 23. (-anything except 0 and inf) ** (non-integer) is NAN
46+
*
47+
* Accuracy:
48+
* pow(x,y) returns x**y nearly rounded. In particular
49+
* pow(integer,integer)
50+
* always returns the correct integer provided it is
51+
* representable.
52+
*
53+
* Constants :
54+
* The hexadecimal values are the intended ones for the following
55+
* constants. The decimal values may be used, provided that the
56+
* compiler will convert from decimal to binary accurately enough
57+
* to produce the hexadecimal values shown.
58+
*/
59+
60+
// #include "libm.h"
61+
62+
/* Concerns:
63+
* - Some constants are shared; DRY?
64+
* - FLT_EVAL_METHOD: the others sidestep this (epsilon or just always true in the case of hypot (#71))
65+
*/
66+
67+
use super::{fabs, scalbn, sqrt, with_set_low_word, with_set_high_word, get_high_word};
68+
69+
const BP: [f64; 2] = [1.0, 1.5];
70+
const DP_H: [f64; 2] = [0.0, 5.84962487220764160156e-01]; /* 0x3fe2b803_40000000 */
71+
const DP_L: [f64; 2] = [0.0, 1.35003920212974897128e-08]; /* 0x3E4CFDEB, 0x43CFD006 */
72+
const TWO53: f64 = 9007199254740992.0; /* 0x43400000_00000000 */
73+
const HUGE: f64 = 1.0e300;
74+
const TINY: f64 = 1.0e-300;
75+
76+
// poly coefs for (3/2)*(log(x)-2s-2/3*s**3:
77+
const L1: f64 = 5.99999999999994648725e-01; /* 0x3fe33333_33333303 */
78+
const L2: f64 = 4.28571428578550184252e-01; /* 0x3fdb6db6_db6fabff */
79+
const L3: f64 = 3.33333329818377432918e-01; /* 0x3fd55555_518f264d */
80+
const L4: f64 = 2.72728123808534006489e-01; /* 0x3fd17460_a91d4101 */
81+
const L5: f64 = 2.30660745775561754067e-01; /* 0x3fcd864a_93c9db65 */
82+
const L6: f64 = 2.06975017800338417784e-01; /* 0x3fca7e28_4a454eef */
83+
const P1: f64 = 1.66666666666666019037e-01; /* 0x3fc55555_5555553e */
84+
const P2: f64 = -2.77777777770155933842e-03; /* 0xbf66c16c_16bebd93 */
85+
const P3: f64 = 6.61375632143793436117e-05; /* 0x3f11566a_af25de2c */
86+
const P4: f64 = -1.65339022054652515390e-06; /* 0xbebbbd41_c5d26bf1 */
87+
const P5: f64 = 4.13813679705723846039e-08; /* 0x3e663769_72bea4d0 */
88+
const LG2: f64 = 6.93147180559945286227e-01; /* 0x3fe62e42_fefa39ef */
89+
const LG2_H: f64 = 6.93147182464599609375e-01; /* 0x3fe62e43_00000000 */
90+
const LG2_L: f64 = -1.90465429995776804525e-09; /* 0xbe205c61_0ca86c39 */
91+
const OVT: f64 = 8.0085662595372944372e-017; /* -(1024-log2(ovfl+.5ulp)) */
92+
const CP: f64 = 9.61796693925975554329e-01; /* 0x3feec709_dc3a03fd =2/(3ln2) */
93+
const CP_H: f64 = 9.61796700954437255859e-01; /* 0x3feec709_e0000000 =(float)cp */
94+
const CP_L: f64 = -7.02846165095275826516e-09; /* 0xbe3e2fe0_145b01f5 =tail of cp_h*/
95+
const IVLN2: f64 = 1.44269504088896338700e+00; /* 0x3ff71547_652b82fe =1/ln2 */
96+
const IVLN2_H: f64 = 1.44269502162933349609e+00; /* 0x3ff71547_60000000 =24b 1/ln2*/
97+
const IVLN2_L: f64 = 1.92596299112661746887e-08; /* 0x3e54ae0b_f85ddf44 =1/ln2 tail*/
98+
99+
#[inline]
100+
pub fn pow(x: f64, y: f64) -> f64 {
101+
let t1: f64;
102+
let t2: f64;
103+
104+
let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32);
105+
let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32);
106+
107+
let mut ix: i32 = (hx & 0x7fffffff) as i32;
108+
let iy: i32 = (hy & 0x7fffffff) as i32;
109+
110+
/* x**0 = 1, even if x is NaN */
111+
if ((iy as u32) | ly) == 0 {
112+
return 1.0;
113+
}
114+
115+
/* 1**y = 1, even if y is NaN */
116+
if hx == 0x3ff00000 && lx == 0 {
117+
return 1.0;
118+
}
119+
120+
/* NaN if either arg is NaN */
121+
if ix > 0x7ff00000 || (ix == 0x7ff00000 && lx != 0) ||
122+
iy > 0x7ff00000 || (iy == 0x7ff00000 && ly != 0) {
123+
return x + y;
124+
}
125+
126+
/* determine if y is an odd int when x < 0
127+
* yisint = 0 ... y is not an integer
128+
* yisint = 1 ... y is an odd int
129+
* yisint = 2 ... y is an even int
130+
*/
131+
let mut yisint: i32 = 0;
132+
let mut k: i32;
133+
let mut j: i32;
134+
if hx < 0 {
135+
if iy >= 0x43400000 {
136+
yisint = 2; /* even integer y */
137+
} else if iy >= 0x3ff00000 {
138+
k = (iy >> 20) - 0x3ff; /* exponent */
139+
140+
if k > 20 {
141+
j = (ly >> (52 - k)) as i32;
142+
143+
if (j << (52 - k)) == (ly as i32) {
144+
yisint = 2 - (j & 1);
145+
}
146+
} else if ly == 0 {
147+
j = iy >> (20 - k);
148+
149+
if (j << (20 - k)) == iy {
150+
yisint = 2 - (j & 1);
151+
}
152+
}
153+
}
154+
}
155+
156+
if ly == 0 {
157+
/* special value of y */
158+
if iy == 0x7ff00000 {
159+
/* y is +-inf */
160+
return if ((ix - 0x3ff00000) | (lx as i32)) == 0 {
161+
/* (-1)**+-inf is 1 */
162+
1.0
163+
} else if ix >= 0x3ff00000 {
164+
/* (|x|>1)**+-inf = inf,0 */
165+
if hy >= 0 { y } else { 0.0 }
166+
} else {
167+
/* (|x|<1)**+-inf = 0,inf */
168+
if hy >= 0 { 0.0 } else { -y }
169+
};
170+
}
171+
172+
if iy == 0x3ff00000 {
173+
/* y is +-1 */
174+
return if hy >= 0 { x } else { 1.0 / x };
175+
}
176+
177+
if hy == 0x40000000 {
178+
/* y is 2 */
179+
return x * x;
180+
}
181+
182+
if hy == 0x3fe00000 {
183+
/* y is 0.5 */
184+
if hx >= 0 {
185+
/* x >= +0 */
186+
return sqrt(x);
187+
}
188+
}
189+
}
190+
191+
let mut ax: f64 = fabs(x);
192+
if lx == 0 {
193+
/* special value of x */
194+
if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 {
195+
/* x is +-0,+-inf,+-1 */
196+
let mut z: f64 = ax;
197+
198+
if hy < 0 {
199+
/* z = (1/|x|) */
200+
z = 1.0 / z;
201+
}
202+
203+
if hx < 0 {
204+
if ((ix-0x3ff00000)|yisint) == 0 {
205+
z = (z - z) / (z - z); /* (-1)**non-int is NaN */
206+
} else if yisint == 1 {
207+
z = -z; /* (x<0)**odd = -(|x|**odd) */
208+
}
209+
}
210+
211+
return z;
212+
}
213+
}
214+
215+
let mut s: f64 = 1.0; /* sign of result */
216+
if hx < 0 {
217+
if yisint == 0 {
218+
/* (x<0)**(non-int) is NaN */
219+
return (x - x) / (x - x);
220+
}
221+
222+
if yisint == 1 {
223+
/* (x<0)**(odd int) */
224+
s = -1.0;
225+
}
226+
}
227+
228+
/* |y| is HUGE */
229+
if iy > 0x41e00000 {
230+
/* if |y| > 2**31 */
231+
if iy > 0x43f00000 {
232+
/* if |y| > 2**64, must o/uflow */
233+
if ix <= 0x3fefffff {
234+
return if hy < 0 { HUGE * HUGE } else { TINY * TINY };
235+
}
236+
237+
if ix >= 0x3ff00000 {
238+
return if hy > 0 { HUGE * HUGE } else { TINY * TINY };
239+
}
240+
}
241+
242+
/* over/underflow if x is not close to one */
243+
if ix < 0x3fefffff {
244+
return if hy < 0 { s * HUGE * HUGE } else { s * TINY * TINY };
245+
}
246+
if ix > 0x3ff00000 {
247+
return if hy > 0 { s * HUGE * HUGE } else { s * TINY * TINY };
248+
}
249+
250+
/* now |1-x| is TINY <= 2**-20, suffice to compute
251+
log(x) by x-x^2/2+x^3/3-x^4/4 */
252+
let t: f64 = ax - 1.0; /* t has 20 trailing zeros */
253+
let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
254+
let u: f64 = IVLN2_H * t; /* ivln2_h has 21 sig. bits */
255+
let v: f64 = t * IVLN2_L - w * IVLN2;
256+
t1 = with_set_low_word(u + v, 0);
257+
t2 = v - (t1 - u);
258+
} else {
259+
// double ss,s2,s_h,s_l,t_h,t_l;
260+
let mut n: i32 = 0;
261+
262+
if ix < 0x00100000 {
263+
/* take care subnormal number */
264+
ax *= TWO53;
265+
n -= 53;
266+
ix = get_high_word(ax) as i32;
267+
}
268+
269+
n += (ix >> 20) - 0x3ff;
270+
j = ix & 0x000fffff;
271+
272+
/* determine interval */
273+
let k: i32;
274+
ix = j | 0x3ff00000; /* normalize ix */
275+
if j <= 0x3988E {
276+
/* |x|<sqrt(3/2) */
277+
k = 0;
278+
}
279+
else if j < 0xBB67A {
280+
/* |x|<sqrt(3) */
281+
k = 1;
282+
} else {
283+
k = 0;
284+
n += 1;
285+
ix -= 0x00100000;
286+
}
287+
ax = with_set_high_word(ax, ix as u32);
288+
289+
/* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
290+
let u: f64 = ax - BP[k as usize]; /* bp[0]=1.0, bp[1]=1.5 */
291+
let v: f64 = 1.0 / (ax + BP[k as usize]);
292+
let ss: f64 = u * v;
293+
let s_h = with_set_low_word(ss, 0);
294+
295+
/* t_h=ax+bp[k] High */
296+
let t_h: f64 = with_set_high_word(0.0,
297+
((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18));
298+
let t_l: f64 = ax - (t_h - BP[k as usize]);
299+
let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l);
300+
301+
/* compute log(ax) */
302+
let s2: f64 = ss * ss;
303+
let mut r: f64 = s2 * s2 * (L1 + s2 * (L2 + s2 *(L3 + s2 *(L4 + s2 *(L5 + s2 * L6)))));
304+
r += s_l * (s_h + ss);
305+
let s2: f64 = s_h * s_h;
306+
let t_h: f64 = with_set_low_word(3.0 + s2 + r, 0);
307+
let t_l: f64 = r - ((t_h - 3.0) - s2);
308+
309+
/* u+v = ss*(1+...) */
310+
let u: f64 = s_h * t_h;
311+
let v: f64 = s_l * t_h + t_l * ss;
312+
313+
/* 2/(3log2)*(ss+...) */
314+
let p_h: f64 = with_set_low_word(u + v, 0);
315+
let p_l = v - (p_h-u);
316+
let z_h: f64 = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
317+
let z_l: f64 = CP_L * p_h + p_l * CP + DP_L[k as usize];
318+
319+
/* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
320+
let t: f64 = n as f64;
321+
t1 = with_set_low_word(((z_h + z_l) + DP_H[k as usize]) + t, 0);
322+
t2 = z_l - (((t1 - t) - DP_H[k as usize]) - z_h);
323+
}
324+
325+
/* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
326+
let y1: f64 = with_set_low_word(y, 0);
327+
let p_l: f64 = (y - y1) * t1 + y * t2;
328+
let mut p_h: f64 = y1 * t1;
329+
let z: f64 = p_l + p_h;
330+
let mut j: i32 = (z.to_bits() >> 32) as i32;
331+
let i: i32 = z.to_bits() as i32;
332+
// let (j, i): (i32, i32) = ((z.to_bits() >> 32) as i32, z.to_bits() as i32);
333+
334+
if j >= 0x40900000 {
335+
/* z >= 1024 */
336+
if (j - 0x40900000) | i != 0 {
337+
/* if z > 1024 */
338+
return s * HUGE * HUGE; /* overflow */
339+
}
340+
341+
if p_l + OVT > z - p_h {
342+
return s * HUGE * HUGE; /* overflow */
343+
}
344+
} else if (j & 0x7fffffff) >= 0x4090cc00 {
345+
/* z <= -1075 */
346+
// FIXME: instead of abs(j) use unsigned j
347+
348+
if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 {
349+
/* z < -1075 */
350+
return s * TINY * TINY; /* underflow */
351+
}
352+
353+
if p_l <= z - p_h {
354+
return s * TINY * TINY; /* underflow */
355+
}
356+
}
357+
358+
/* compute 2**(p_h+p_l) */
359+
let i: i32 = j & (0x7fffffff as i32);
360+
k = (i >> 20) - 0x3ff;
361+
let mut n: i32 = 0;
362+
363+
if i > 0x3fe00000 {
364+
/* if |z| > 0.5, set n = [z+0.5] */
365+
n = j + (0x00100000 >> (k + 1));
366+
k = ((n&0x7fffffff) >> 20) - 0x3ff; /* new k for n */
367+
let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32);
368+
n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
369+
if j < 0 {
370+
n = -n;
371+
}
372+
p_h -= t;
373+
}
374+
375+
let t: f64 = with_set_low_word(p_l + p_h, 0);
376+
let u: f64 = t * LG2_H;
377+
let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L;
378+
let mut z: f64 = u + v;
379+
let w: f64 = v - (z - u);
380+
let t: f64 = z * z;
381+
let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
382+
let r: f64 = (z * t1) / (t1 - 2.0) - (w + z*w);
383+
z = 1.0 - (r - z);
384+
j = get_high_word(z) as i32;
385+
j += n << 20;
386+
387+
if (j >> 20) <= 0 {
388+
/* subnormal output */
389+
z = scalbn(z,n);
390+
} else {
391+
z = with_set_high_word(z, j as u32);
392+
}
393+
394+
return s*z;
395+
}

0 commit comments

Comments
 (0)