Coverage Report

Created: 2025-06-16 06:50

/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/expf.rs
Line
Count
Source (jump to first uncovered line)
1
/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.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::scalbnf;
17
18
const HALF: [f32; 2] = [0.5, -0.5];
19
const LN2_HI: f32 = 6.9314575195e-01; /* 0x3f317200 */
20
const LN2_LO: f32 = 1.4286067653e-06; /* 0x35bfbe8e */
21
const INV_LN2: f32 = 1.4426950216e+00; /* 0x3fb8aa3b */
22
/*
23
 * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
24
 * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
25
 */
26
const P1: f32 = 1.6666625440e-1; /*  0xaaaa8f.0p-26 */
27
const P2: f32 = -2.7667332906e-3; /* -0xb55215.0p-32 */
28
29
/// Exponential, base *e* (f32)
30
///
31
/// Calculate the exponential of `x`, that is, *e* raised to the power `x`
32
/// (where *e* is the base of the natural system of logarithms, approximately 2.71828).
33
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
34
0
pub fn expf(mut x: f32) -> f32 {
35
0
    let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
36
0
    let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126  /*original 0x1p-149f    ??????????? */
37
0
    let mut hx = x.to_bits();
38
0
    let sign = (hx >> 31) as i32; /* sign bit of x */
39
0
    let signb: bool = sign != 0;
40
0
    hx &= 0x7fffffff; /* high word of |x| */
41
0
42
0
    /* special cases */
43
0
    if hx >= 0x42aeac50 {
44
        /* if |x| >= -87.33655f or NaN */
45
0
        if hx > 0x7f800000 {
46
            /* NaN */
47
0
            return x;
48
0
        }
49
0
        if (hx >= 0x42b17218) && (!signb) {
50
            /* x >= 88.722839f */
51
            /* overflow */
52
0
            x *= x1p127;
53
0
            return x;
54
0
        }
55
0
        if signb {
56
            /* underflow */
57
0
            force_eval!(-x1p_126 / x);
58
0
            if hx >= 0x42cff1b5 {
59
                /* x <= -103.972084f */
60
0
                return 0.;
61
0
            }
62
0
        }
63
0
    }
64
65
    /* argument reduction */
66
    let k: i32;
67
    let hi: f32;
68
    let lo: f32;
69
0
    if hx > 0x3eb17218 {
70
        /* if |x| > 0.5 ln2 */
71
0
        if hx > 0x3f851592 {
72
0
            /* if |x| > 1.5 ln2 */
73
0
            k = (INV_LN2 * x + i!(HALF, sign as usize)) as i32;
74
0
        } else {
75
0
            k = 1 - sign - sign;
76
0
        }
77
0
        let kf = k as f32;
78
0
        hi = x - kf * LN2_HI; /* k*ln2hi is exact here */
79
0
        lo = kf * LN2_LO;
80
0
        x = hi - lo;
81
0
    } else if hx > 0x39000000 {
82
0
        /* |x| > 2**-14 */
83
0
        k = 0;
84
0
        hi = x;
85
0
        lo = 0.;
86
0
    } else {
87
        /* raise inexact */
88
0
        force_eval!(x1p127 + x);
89
0
        return 1. + x;
90
    }
91
92
    /* x is now in primary range */
93
0
    let xx = x * x;
94
0
    let c = x - xx * (P1 + xx * P2);
95
0
    let y = 1. + (x * c / (2. - c) - lo + hi);
96
0
    if k == 0 { y } else { scalbnf(y, k) }
97
0
}