Coverage Report

Created: 2025-10-10 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.11/src/math/log1pf.rs
Line
Count
Source
1
/* origin: FreeBSD /usr/src/lib/msun/src/s_log1pf.c */
2
/*
3
 * ====================================================
4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * Developed at SunPro, a Sun Microsystems, Inc. business.
7
 * Permission to use, copy, modify, and distribute this
8
 * software is freely granted, provided that this notice
9
 * is preserved.
10
 * ====================================================
11
 */
12
13
use core::f32;
14
15
const LN2_HI: f32 = 6.9313812256e-01; /* 0x3f317180 */
16
const LN2_LO: f32 = 9.0580006145e-06; /* 0x3717f7d1 */
17
/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
18
const LG1: f32 = 0.66666662693; /* 0xaaaaaa.0p-24 */
19
const LG2: f32 = 0.40000972152; /* 0xccce13.0p-25 */
20
const LG3: f32 = 0.28498786688; /* 0x91e9ee.0p-25 */
21
const LG4: f32 = 0.24279078841; /* 0xf89e26.0p-26 */
22
23
/// The natural logarithm of 1+`x` (f32).
24
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
25
0
pub fn log1pf(x: f32) -> f32 {
26
0
    let mut ui: u32 = x.to_bits();
27
    let hfsq: f32;
28
0
    let mut f: f32 = 0.;
29
0
    let mut c: f32 = 0.;
30
    let s: f32;
31
    let z: f32;
32
    let r: f32;
33
    let w: f32;
34
    let t1: f32;
35
    let t2: f32;
36
    let dk: f32;
37
    let ix: u32;
38
    let mut iu: u32;
39
    let mut k: i32;
40
41
0
    ix = ui;
42
0
    k = 1;
43
0
    if ix < 0x3ed413d0 || (ix >> 31) > 0 {
44
        /* 1+x < sqrt(2)+  */
45
0
        if ix >= 0xbf800000 {
46
            /* x <= -1.0 */
47
0
            if x == -1. {
48
0
                return x / 0.0; /* log1p(-1)=+inf */
49
0
            }
50
0
            return (x - x) / 0.0; /* log1p(x<-1)=NaN */
51
0
        }
52
0
        if ix << 1 < 0x33800000 << 1 {
53
            /* |x| < 2**-24 */
54
            /* underflow if subnormal */
55
0
            if (ix & 0x7f800000) == 0 {
56
0
                force_eval!(x * x);
57
0
            }
58
0
            return x;
59
0
        }
60
0
        if ix <= 0xbe95f619 {
61
0
            /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
62
0
            k = 0;
63
0
            c = 0.;
64
0
            f = x;
65
0
        }
66
0
    } else if ix >= 0x7f800000 {
67
0
        return x;
68
0
    }
69
0
    if k > 0 {
70
0
        ui = (1. + x).to_bits();
71
0
        iu = ui;
72
0
        iu += 0x3f800000 - 0x3f3504f3;
73
0
        k = (iu >> 23) as i32 - 0x7f;
74
        /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
75
0
        if k < 25 {
76
0
            c = if k >= 2 { 1. - (f32::from_bits(ui) - x) } else { x - (f32::from_bits(ui) - 1.) };
77
0
            c /= f32::from_bits(ui);
78
0
        } else {
79
0
            c = 0.;
80
0
        }
81
        /* reduce u into [sqrt(2)/2, sqrt(2)] */
82
0
        iu = (iu & 0x007fffff) + 0x3f3504f3;
83
0
        ui = iu;
84
0
        f = f32::from_bits(ui) - 1.;
85
0
    }
86
0
    s = f / (2.0 + f);
87
0
    z = s * s;
88
0
    w = z * z;
89
0
    t1 = w * (LG2 + w * LG4);
90
0
    t2 = z * (LG1 + w * LG3);
91
0
    r = t2 + t1;
92
0
    hfsq = 0.5 * f * f;
93
0
    dk = k as f32;
94
0
    s * (hfsq + r) + (dk * LN2_LO + c) - hfsq + f + dk * LN2_HI
95
0
}