Coverage Report

Created: 2026-09-04 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.16/src/math/expf.rs
Line
Count
Source
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(assert_no_panic, no_panic::no_panic)]
34
291M
pub fn expf(mut x: f32) -> f32 {
35
    select_implementation! {
36
        name: x87_expf,
37
        use_arch_required: x86_no_sse,
38
        args: x,
39
    }
40
41
291M
    let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
42
291M
    let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126  /*original 0x1p-149f    ??????????? */
43
291M
    let mut hx = x.to_bits();
44
291M
    let sign = (hx >> 31) as i32; /* sign bit of x */
45
291M
    let signb: bool = sign != 0;
46
291M
    hx &= 0x7fffffff; /* high word of |x| */
47
48
    /* special cases */
49
291M
    if hx >= 0x42aeac50 {
50
        /* if |x| >= -87.33655f or NaN */
51
4.82M
        if hx > 0x7f800000 {
52
            /* NaN */
53
0
            return x;
54
4.82M
        }
55
4.82M
        if (hx >= 0x42b17218) && (!signb) {
56
            /* x >= 88.722839f */
57
            /* overflow */
58
2.13M
            x *= x1p127;
59
2.13M
            return x;
60
2.68M
        }
61
2.68M
        if signb {
62
            /* underflow */
63
2.63M
            force_eval!(-x1p_126 / x);
64
2.63M
            if hx >= 0x42cff1b5 {
65
                /* x <= -103.972084f */
66
1.90M
                return 0.;
67
727k
            }
68
50.2k
        }
69
286M
    }
70
71
    /* argument reduction */
72
    let k: i32;
73
    let hi: f32;
74
    let lo: f32;
75
286M
    if hx > 0x3eb17218 {
76
        /* if |x| > 0.5 ln2 */
77
271M
        if hx > 0x3f851592 {
78
241M
            /* if |x| > 1.5 ln2 */
79
241M
            k = (INV_LN2 * x + i!(HALF, sign as usize)) as i32;
80
241M
        } else {
81
29.6M
            k = 1 - sign - sign;
82
29.6M
        }
83
271M
        let kf = k as f32;
84
271M
        hi = x - kf * LN2_HI; /* k*ln2hi is exact here */
85
271M
        lo = kf * LN2_LO;
86
271M
        x = hi - lo;
87
15.9M
    } else if hx > 0x39000000 {
88
15.9M
        /* |x| > 2**-14 */
89
15.9M
        k = 0;
90
15.9M
        hi = x;
91
15.9M
        lo = 0.;
92
15.9M
    } else {
93
        /* raise inexact */
94
2.86k
        force_eval!(x1p127 + x);
95
2.86k
        return 1. + x;
96
    }
97
98
    /* x is now in primary range */
99
286M
    let xx = x * x;
100
286M
    let c = x - xx * (P1 + xx * P2);
101
286M
    let y = 1. + (x * c / (2. - c) - lo + hi);
102
286M
    if k == 0 { y } else { scalbnf(y, k) }
103
291M
}