pxfm/logs/log1pmxf.rs
1/*
2 * // Copyright (c) Radzivon Bartoshyk 8/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1. Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2. Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3. Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::polyeval::f_estrin_polyeval7;
30
31/// Computes log(1+x) - x
32///
33/// ulp 0.5
34pub fn f_log1pmxf(x: f32) -> f32 {
35 let ux = x.to_bits().wrapping_shl(1);
36 if ux >= 0xffu32 << 24 || ux == 0 {
37 // |x| == 0, |x| == inf, x == NaN
38 if ux == 0 {
39 return x;
40 }
41 if x.is_infinite() {
42 return f32::NAN;
43 }
44 return x + f32::NAN;
45 }
46
47 let ax = x.to_bits() & 0x7fff_ffffu32;
48 let xd = x as f64;
49
50 // Use log1p(x) = log(1 + x) for |x| > 2^-6;
51 if ax > 0x3c80_0000u32 {
52 if x == -1. {
53 return f32::NEG_INFINITY;
54 }
55 let x1p = xd + 1.;
56 if x1p <= 0. {
57 if x1p == 0. {
58 return f32::NEG_INFINITY;
59 }
60 return f32::NAN;
61 }
62 return (crate::logs::log1pf::core_logf(x1p) - xd) as f32;
63 }
64
65 // log(1+x) is expected to be used near zero
66 // Polynomial generated by Sollya in form log(1+x) - x = x^2 * R(x):
67 // d = [-2^-6; 2^-6];
68 // f_log1pf = (log(1+x) - x)/x^2;
69 // Q = fpminimax(f_log1pf, 6, [|0, D...|], d);
70 // See ./notes/log1pmxf.sollya
71
72 let xd_sqr = xd * xd;
73 let p = f_estrin_polyeval7(
74 xd,
75 f64::from_bits(0xbfe0000000000000),
76 f64::from_bits(0x3fd55555555561c8),
77 f64::from_bits(0xbfd0000000000f5a),
78 f64::from_bits(0x3fc999998d26deab),
79 f64::from_bits(0xbfc555554878739c),
80 f64::from_bits(0x3fc24ab2e3c10eca),
81 f64::from_bits(0xbfc0017973fafec4),
82 ) * xd_sqr;
83 p as f32
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_log1pmxfm() {
92 assert_eq!(f_log1pmxf(-0.0000014305108), -1.0231815e-12);
93 assert_eq!(f_log1pmxf(0.0), 0.0);
94 assert_eq!(f_log1pmxf(2.0), -0.9013877);
95 assert_eq!(f_log1pmxf(-0.7), -0.50397277);
96 assert_eq!(
97 f_log1pmxf(-0.0000000000043243),
98 -0.000000000000000000000009349785
99 );
100 assert!(f_log1pmxf(f32::INFINITY).is_nan());
101 assert!(f_log1pmxf(f32::NAN).is_nan());
102 assert!(f_log1pmxf(f32::NEG_INFINITY).is_nan());
103 assert!(f_log1pmxf(-2.0).is_nan());
104 }
105}