libm/math/
expf.rs

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
16use super::scalbnf;
17
18const HALF: [f32; 2] = [0.5, -0.5];
19const LN2_HI: f32 = 6.9314575195e-01; /* 0x3f317200 */
20const LN2_LO: f32 = 1.4286067653e-06; /* 0x35bfbe8e */
21const 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 */
26const P1: f32 = 1.6666625440e-1; /*  0xaaaa8f.0p-26 */
27const 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)]
34pub fn expf(mut x: f32) -> f32 {
35    let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
36    let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126  /*original 0x1p-149f    ??????????? */
37    let mut hx = x.to_bits();
38    let sign = (hx >> 31) as i32; /* sign bit of x */
39    let signb: bool = sign != 0;
40    hx &= 0x7fffffff; /* high word of |x| */
41
42    /* special cases */
43    if hx >= 0x42aeac50 {
44        /* if |x| >= -87.33655f or NaN */
45        if hx > 0x7f800000 {
46            /* NaN */
47            return x;
48        }
49        if (hx >= 0x42b17218) && (!signb) {
50            /* x >= 88.722839f */
51            /* overflow */
52            x *= x1p127;
53            return x;
54        }
55        if signb {
56            /* underflow */
57            force_eval!(-x1p_126 / x);
58            if hx >= 0x42cff1b5 {
59                /* x <= -103.972084f */
60                return 0.;
61            }
62        }
63    }
64
65    /* argument reduction */
66    let k: i32;
67    let hi: f32;
68    let lo: f32;
69    if hx > 0x3eb17218 {
70        /* if |x| > 0.5 ln2 */
71        if hx > 0x3f851592 {
72            /* if |x| > 1.5 ln2 */
73            k = (INV_LN2 * x + i!(HALF, sign as usize)) as i32;
74        } else {
75            k = 1 - sign - sign;
76        }
77        let kf = k as f32;
78        hi = x - kf * LN2_HI; /* k*ln2hi is exact here */
79        lo = kf * LN2_LO;
80        x = hi - lo;
81    } else if hx > 0x39000000 {
82        /* |x| > 2**-14 */
83        k = 0;
84        hi = x;
85        lo = 0.;
86    } else {
87        /* raise inexact */
88        force_eval!(x1p127 + x);
89        return 1. + x;
90    }
91
92    /* x is now in primary range */
93    let xx = x * x;
94    let c = x - xx * (P1 + xx * P2);
95    let y = 1. + (x * c / (2. - c) - lo + hi);
96    if k == 0 { y } else { scalbnf(y, k) }
97}