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
13use core::f32;
14
15const LN2_HI: f32 = 6.9313812256e-01; /* 0x3f317180 */
16const 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]). */
18const LG1: f32 = 0.66666662693; /* 0xaaaaaa.0p-24 */
19const LG2: f32 = 0.40000972152; /* 0xccce13.0p-25 */
20const LG3: f32 = 0.28498786688; /* 0x91e9ee.0p-25 */
21const LG4: f32 = 0.24279078841; /* 0xf89e26.0p-26 */
22
23#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
24pub fn log1pf(x: f32) -> f32 {
25 let mut ui: u32 = x.to_bits();
26 let hfsq: f32;
27 let mut f: f32 = 0.;
28 let mut c: f32 = 0.;
29 let s: f32;
30 let z: f32;
31 let r: f32;
32 let w: f32;
33 let t1: f32;
34 let t2: f32;
35 let dk: f32;
36 let ix: u32;
37 let mut iu: u32;
38 let mut k: i32;
39
40 ix = ui;
41 k = 1;
42 if ix < 0x3ed413d0 || (ix >> 31) > 0 {
43 /* 1+x < sqrt(2)+ */
44 if ix >= 0xbf800000 {
45 /* x <= -1.0 */
46 if x == -1. {
47 return x / 0.0; /* log1p(-1)=+inf */
48 }
49 return (x - x) / 0.0; /* log1p(x<-1)=NaN */
50 }
51 if ix << 1 < 0x33800000 << 1 {
52 /* |x| < 2**-24 */
53 /* underflow if subnormal */
54 if (ix & 0x7f800000) == 0 {
55 force_eval!(x * x);
56 }
57 return x;
58 }
59 if ix <= 0xbe95f619 {
60 /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
61 k = 0;
62 c = 0.;
63 f = x;
64 }
65 } else if ix >= 0x7f800000 {
66 return x;
67 }
68 if k > 0 {
69 ui = (1. + x).to_bits();
70 iu = ui;
71 iu += 0x3f800000 - 0x3f3504f3;
72 k = (iu >> 23) as i32 - 0x7f;
73 /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
74 if k < 25 {
75 c = if k >= 2 {
76 1. - (f32::from_bits(ui) - x)
77 } else {
78 x - (f32::from_bits(ui) - 1.)
79 };
80 c /= f32::from_bits(ui);
81 } else {
82 c = 0.;
83 }
84 /* reduce u into [sqrt(2)/2, sqrt(2)] */
85 iu = (iu & 0x007fffff) + 0x3f3504f3;
86 ui = iu;
87 f = f32::from_bits(ui) - 1.;
88 }
89 s = f / (2.0 + f);
90 z = s * s;
91 w = z * z;
92 t1 = w * (LG2 + w * LG4);
93 t2 = z * (LG1 + w * LG3);
94 r = t2 + t1;
95 hfsq = 0.5 * f * f;
96 dk = k as f32;
97 s * (hfsq + r) + (dk * LN2_LO + c) - hfsq + f + dk * LN2_HI
98}
99