Skip to main content

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(assert_no_panic, no_panic::no_panic)]
34pub 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    let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
42    let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126  /*original 0x1p-149f    ??????????? */
43    let mut hx = x.to_bits();
44    let sign = (hx >> 31) as i32; /* sign bit of x */
45    let signb: bool = sign != 0;
46    hx &= 0x7fffffff; /* high word of |x| */
47
48    /* special cases */
49    if hx >= 0x42aeac50 {
50        /* if |x| >= -87.33655f or NaN */
51        if hx > 0x7f800000 {
52            /* NaN */
53            return x;
54        }
55        if (hx >= 0x42b17218) && (!signb) {
56            /* x >= 88.722839f */
57            /* overflow */
58            x *= x1p127;
59            return x;
60        }
61        if signb {
62            /* underflow */
63            unsafe { ::core::ptr::read_volatile(&(-x1p_126 / x)) };force_eval!(-x1p_126 / x);
64            if hx >= 0x42cff1b5 {
65                /* x <= -103.972084f */
66                return 0.;
67            }
68        }
69    }
70
71    /* argument reduction */
72    let k: i32;
73    let hi: f32;
74    let lo: f32;
75    if hx > 0x3eb17218 {
76        /* if |x| > 0.5 ln2 */
77        if hx > 0x3f851592 {
78            /* if |x| > 1.5 ln2 */
79            k = (INV_LN2 * x + *HALF.get(sign as usize).unwrap()i!(HALF, sign as usize)) as i32;
80        } else {
81            k = 1 - sign - sign;
82        }
83        let kf = k as f32;
84        hi = x - kf * LN2_HI; /* k*ln2hi is exact here */
85        lo = kf * LN2_LO;
86        x = hi - lo;
87    } else if hx > 0x39000000 {
88        /* |x| > 2**-14 */
89        k = 0;
90        hi = x;
91        lo = 0.;
92    } else {
93        /* raise inexact */
94        unsafe { ::core::ptr::read_volatile(&(x1p127 + x)) };force_eval!(x1p127 + x);
95        return 1. + x;
96    }
97
98    /* x is now in primary range */
99    let xx = x * x;
100    let c = x - xx * (P1 + xx * P2);
101    let y = 1. + (x * c / (2. - c) - lo + hi);
102    if k == 0 { y } else { scalbnf(y, k) }
103}