Coverage Report

Created: 2025-08-29 06:18

/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/powf.rs
Line
Count
Source (jump to first uncovered line)
1
/* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
2
/*
3
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4
 */
5
/*
6
 * ====================================================
7
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8
 *
9
 * Developed at SunPro, a Sun Microsystems, Inc. business.
10
 * Permission to use, copy, modify, and distribute this
11
 * software is freely granted, provided that this notice
12
 * is preserved.
13
 * ====================================================
14
 */
15
16
use super::{fabsf, scalbnf, sqrtf};
17
18
const BP: [f32; 2] = [1.0, 1.5];
19
const DP_H: [f32; 2] = [0.0, 5.84960938e-01]; /* 0x3f15c000 */
20
const DP_L: [f32; 2] = [0.0, 1.56322085e-06]; /* 0x35d1cfdc */
21
const TWO24: f32 = 16777216.0; /* 0x4b800000 */
22
const HUGE: f32 = 1.0e30;
23
const TINY: f32 = 1.0e-30;
24
const L1: f32 = 6.0000002384e-01; /* 0x3f19999a */
25
const L2: f32 = 4.2857143283e-01; /* 0x3edb6db7 */
26
const L3: f32 = 3.3333334327e-01; /* 0x3eaaaaab */
27
const L4: f32 = 2.7272811532e-01; /* 0x3e8ba305 */
28
const L5: f32 = 2.3066075146e-01; /* 0x3e6c3255 */
29
const L6: f32 = 2.0697501302e-01; /* 0x3e53f142 */
30
const P1: f32 = 1.6666667163e-01; /* 0x3e2aaaab */
31
const P2: f32 = -2.7777778450e-03; /* 0xbb360b61 */
32
const P3: f32 = 6.6137559770e-05; /* 0x388ab355 */
33
const P4: f32 = -1.6533901999e-06; /* 0xb5ddea0e */
34
const P5: f32 = 4.1381369442e-08; /* 0x3331bb4c */
35
const LG2: f32 = 6.9314718246e-01; /* 0x3f317218 */
36
const LG2_H: f32 = 6.93145752e-01; /* 0x3f317200 */
37
const LG2_L: f32 = 1.42860654e-06; /* 0x35bfbe8c */
38
const OVT: f32 = 4.2995665694e-08; /* -(128-log2(ovfl+.5ulp)) */
39
const CP: f32 = 9.6179670095e-01; /* 0x3f76384f =2/(3ln2) */
40
const CP_H: f32 = 9.6191406250e-01; /* 0x3f764000 =12b cp */
41
const CP_L: f32 = -1.1736857402e-04; /* 0xb8f623c6 =tail of cp_h */
42
const IVLN2: f32 = 1.4426950216e+00;
43
const IVLN2_H: f32 = 1.4426879883e+00;
44
const IVLN2_L: f32 = 7.0526075433e-06;
45
46
/// Returns `x` to the power of `y` (f32).
47
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
48
0
pub fn powf(x: f32, y: f32) -> f32 {
49
0
    let mut z: f32;
50
0
    let mut ax: f32;
51
0
    let z_h: f32;
52
0
    let z_l: f32;
53
0
    let mut p_h: f32;
54
0
    let mut p_l: f32;
55
0
    let y1: f32;
56
0
    let mut t1: f32;
57
0
    let t2: f32;
58
0
    let mut r: f32;
59
0
    let s: f32;
60
0
    let mut sn: f32;
61
0
    let mut t: f32;
62
0
    let mut u: f32;
63
0
    let mut v: f32;
64
0
    let mut w: f32;
65
0
    let i: i32;
66
0
    let mut j: i32;
67
0
    let mut k: i32;
68
0
    let mut yisint: i32;
69
0
    let mut n: i32;
70
0
    let hx: i32;
71
0
    let hy: i32;
72
0
    let mut ix: i32;
73
0
    let iy: i32;
74
0
    let mut is: i32;
75
0
76
0
    hx = x.to_bits() as i32;
77
0
    hy = y.to_bits() as i32;
78
0
79
0
    ix = hx & 0x7fffffff;
80
0
    iy = hy & 0x7fffffff;
81
0
82
0
    /* x**0 = 1, even if x is NaN */
83
0
    if iy == 0 {
84
0
        return 1.0;
85
0
    }
86
0
87
0
    /* 1**y = 1, even if y is NaN */
88
0
    if hx == 0x3f800000 {
89
0
        return 1.0;
90
0
    }
91
0
92
0
    /* NaN if either arg is NaN */
93
0
    if ix > 0x7f800000 || iy > 0x7f800000 {
94
0
        return x + y;
95
0
    }
96
0
97
0
    /* determine if y is an odd int when x < 0
98
0
     * yisint = 0       ... y is not an integer
99
0
     * yisint = 1       ... y is an odd int
100
0
     * yisint = 2       ... y is an even int
101
0
     */
102
0
    yisint = 0;
103
0
    if hx < 0 {
104
0
        if iy >= 0x4b800000 {
105
0
            yisint = 2; /* even integer y */
106
0
        } else if iy >= 0x3f800000 {
107
0
            k = (iy >> 23) - 0x7f; /* exponent */
108
0
            j = iy >> (23 - k);
109
0
            if (j << (23 - k)) == iy {
110
0
                yisint = 2 - (j & 1);
111
0
            }
112
0
        }
113
0
    }
114
115
    /* special value of y */
116
0
    if iy == 0x7f800000 {
117
        /* y is +-inf */
118
0
        if ix == 0x3f800000 {
119
            /* (-1)**+-inf is 1 */
120
0
            return 1.0;
121
0
        } else if ix > 0x3f800000 {
122
            /* (|x|>1)**+-inf = inf,0 */
123
0
            return if hy >= 0 { y } else { 0.0 };
124
        } else {
125
            /* (|x|<1)**+-inf = 0,inf */
126
0
            return if hy >= 0 { 0.0 } else { -y };
127
        }
128
0
    }
129
0
    if iy == 0x3f800000 {
130
        /* y is +-1 */
131
0
        return if hy >= 0 { x } else { 1.0 / x };
132
0
    }
133
0
134
0
    if hy == 0x40000000 {
135
        /* y is 2 */
136
0
        return x * x;
137
0
    }
138
0
139
0
    if hy == 0x3f000000
140
       /* y is  0.5 */
141
0
       && hx >= 0
142
    {
143
        /* x >= +0 */
144
0
        return sqrtf(x);
145
0
    }
146
0
147
0
    ax = fabsf(x);
148
0
    /* special value of x */
149
0
    if ix == 0x7f800000 || ix == 0 || ix == 0x3f800000 {
150
        /* x is +-0,+-inf,+-1 */
151
0
        z = ax;
152
0
        if hy < 0 {
153
0
            /* z = (1/|x|) */
154
0
            z = 1.0 / z;
155
0
        }
156
157
0
        if hx < 0 {
158
0
            if ((ix - 0x3f800000) | yisint) == 0 {
159
0
                z = (z - z) / (z - z); /* (-1)**non-int is NaN */
160
0
            } else if yisint == 1 {
161
0
                z = -z; /* (x<0)**odd = -(|x|**odd) */
162
0
            }
163
0
        }
164
0
        return z;
165
0
    }
166
0
167
0
    sn = 1.0; /* sign of result */
168
0
    if hx < 0 {
169
0
        if yisint == 0 {
170
            /* (x<0)**(non-int) is NaN */
171
0
            return (x - x) / (x - x);
172
0
        }
173
0
174
0
        if yisint == 1 {
175
0
            /* (x<0)**(odd int) */
176
0
            sn = -1.0;
177
0
        }
178
0
    }
179
180
    /* |y| is HUGE */
181
0
    if iy > 0x4d000000 {
182
        /* if |y| > 2**27 */
183
        /* over/underflow if x is not close to one */
184
0
        if ix < 0x3f7ffff8 {
185
0
            return if hy < 0 { sn * HUGE * HUGE } else { sn * TINY * TINY };
186
0
        }
187
0
188
0
        if ix > 0x3f800007 {
189
0
            return if hy > 0 { sn * HUGE * HUGE } else { sn * TINY * TINY };
190
0
        }
191
0
192
0
        /* now |1-x| is TINY <= 2**-20, suffice to compute
193
0
        log(x) by x-x^2/2+x^3/3-x^4/4 */
194
0
        t = ax - 1.; /* t has 20 trailing zeros */
195
0
        w = (t * t) * (0.5 - t * (0.333333333333 - t * 0.25));
196
0
        u = IVLN2_H * t; /* IVLN2_H has 16 sig. bits */
197
0
        v = t * IVLN2_L - w * IVLN2;
198
0
        t1 = u + v;
199
0
        is = t1.to_bits() as i32;
200
0
        t1 = f32::from_bits(is as u32 & 0xfffff000);
201
0
        t2 = v - (t1 - u);
202
    } else {
203
        let mut s2: f32;
204
        let mut s_h: f32;
205
        let s_l: f32;
206
        let mut t_h: f32;
207
        let mut t_l: f32;
208
209
0
        n = 0;
210
0
        /* take care subnormal number */
211
0
        if ix < 0x00800000 {
212
0
            ax *= TWO24;
213
0
            n -= 24;
214
0
            ix = ax.to_bits() as i32;
215
0
        }
216
0
        n += ((ix) >> 23) - 0x7f;
217
0
        j = ix & 0x007fffff;
218
0
        /* determine interval */
219
0
        ix = j | 0x3f800000; /* normalize ix */
220
0
        if j <= 0x1cc471 {
221
0
            /* |x|<sqrt(3/2) */
222
0
            k = 0;
223
0
        } else if j < 0x5db3d7 {
224
0
            /* |x|<sqrt(3)   */
225
0
            k = 1;
226
0
        } else {
227
0
            k = 0;
228
0
            n += 1;
229
0
            ix -= 0x00800000;
230
0
        }
231
0
        ax = f32::from_bits(ix as u32);
232
0
233
0
        /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
234
0
        u = ax - i!(BP, k as usize); /* bp[0]=1.0, bp[1]=1.5 */
235
0
        v = 1.0 / (ax + i!(BP, k as usize));
236
0
        s = u * v;
237
0
        s_h = s;
238
0
        is = s_h.to_bits() as i32;
239
0
        s_h = f32::from_bits(is as u32 & 0xfffff000);
240
0
        /* t_h=ax+bp[k] High */
241
0
        is = (((ix as u32 >> 1) & 0xfffff000) | 0x20000000) as i32;
242
0
        t_h = f32::from_bits(is as u32 + 0x00400000 + ((k as u32) << 21));
243
0
        t_l = ax - (t_h - i!(BP, k as usize));
244
0
        s_l = v * ((u - s_h * t_h) - s_h * t_l);
245
0
        /* compute log(ax) */
246
0
        s2 = s * s;
247
0
        r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
248
0
        r += s_l * (s_h + s);
249
0
        s2 = s_h * s_h;
250
0
        t_h = 3.0 + s2 + r;
251
0
        is = t_h.to_bits() as i32;
252
0
        t_h = f32::from_bits(is as u32 & 0xfffff000);
253
0
        t_l = r - ((t_h - 3.0) - s2);
254
0
        /* u+v = s*(1+...) */
255
0
        u = s_h * t_h;
256
0
        v = s_l * t_h + t_l * s;
257
0
        /* 2/(3log2)*(s+...) */
258
0
        p_h = u + v;
259
0
        is = p_h.to_bits() as i32;
260
0
        p_h = f32::from_bits(is as u32 & 0xfffff000);
261
0
        p_l = v - (p_h - u);
262
0
        z_h = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
263
0
        z_l = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
264
0
        /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
265
0
        t = n as f32;
266
0
        t1 = ((z_h + z_l) + i!(DP_H, k as usize)) + t;
267
0
        is = t1.to_bits() as i32;
268
0
        t1 = f32::from_bits(is as u32 & 0xfffff000);
269
0
        t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
270
    };
271
272
    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
273
0
    is = y.to_bits() as i32;
274
0
    y1 = f32::from_bits(is as u32 & 0xfffff000);
275
0
    p_l = (y - y1) * t1 + y * t2;
276
0
    p_h = y1 * t1;
277
0
    z = p_l + p_h;
278
0
    j = z.to_bits() as i32;
279
0
    if j > 0x43000000 {
280
        /* if z > 128 */
281
0
        return sn * HUGE * HUGE; /* overflow */
282
0
    } else if j == 0x43000000 {
283
        /* if z == 128 */
284
0
        if p_l + OVT > z - p_h {
285
0
            return sn * HUGE * HUGE; /* overflow */
286
0
        }
287
0
    } else if (j & 0x7fffffff) > 0x43160000 {
288
        /* z < -150 */
289
        // FIXME: check should be  (uint32_t)j > 0xc3160000
290
0
        return sn * TINY * TINY; /* underflow */
291
0
    } else if j as u32 == 0xc3160000
292
              /* z == -150 */
293
0
              && p_l <= z - p_h
294
    {
295
0
        return sn * TINY * TINY; /* underflow */
296
0
    }
297
298
    /*
299
     * compute 2**(p_h+p_l)
300
     */
301
0
    i = j & 0x7fffffff;
302
0
    k = (i >> 23) - 0x7f;
303
0
    n = 0;
304
0
    if i > 0x3f000000 {
305
        /* if |z| > 0.5, set n = [z+0.5] */
306
0
        n = j + (0x00800000 >> (k + 1));
307
0
        k = ((n & 0x7fffffff) >> 23) - 0x7f; /* new k for n */
308
0
        t = f32::from_bits(n as u32 & !(0x007fffff >> k));
309
0
        n = ((n & 0x007fffff) | 0x00800000) >> (23 - k);
310
0
        if j < 0 {
311
0
            n = -n;
312
0
        }
313
0
        p_h -= t;
314
0
    }
315
0
    t = p_l + p_h;
316
0
    is = t.to_bits() as i32;
317
0
    t = f32::from_bits(is as u32 & 0xffff8000);
318
0
    u = t * LG2_H;
319
0
    v = (p_l - (t - p_h)) * LG2 + t * LG2_L;
320
0
    z = u + v;
321
0
    w = v - (z - u);
322
0
    t = z * z;
323
0
    t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
324
0
    r = (z * t1) / (t1 - 2.0) - (w + z * w);
325
0
    z = 1.0 - (r - z);
326
0
    j = z.to_bits() as i32;
327
0
    j += n << 23;
328
0
    if (j >> 23) <= 0 {
329
0
        /* subnormal output */
330
0
        z = scalbnf(z, n);
331
0
    } else {
332
0
        z = f32::from_bits(j as u32);
333
0
    }
334
0
    sn * z
335
0
}