pxfm/logs/
log2dd.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::double_double::DoubleDouble;
30use crate::logs::log2dd_coeffs::LOG2_NEG_DD;
31use crate::pow_tables::POW_INVERSE;
32
33#[inline(always)]
34fn log2_poly(z: f64) -> DoubleDouble {
35    /*
36      See ./notes/dd_log2.sollya
37    */
38    const P: [(u64, u64); 10] = [
39        (0x3c7777d0ffdb7a88, 0x3ff71547652b82fe),
40        (0xbc6777d0fff4382c, 0xbfe71547652b82fe),
41        (0x3c7d27ad39ed5b3e, 0x3fdec709dc3a03fd),
42        (0xbc57748c64010a70, 0xbfd71547652b82fe),
43        (0x3c771aed4208f9c1, 0x3fd2776c50ef9c06),
44        (0x3c6dd0178c438bee, 0xbfcec709dc3a041c),
45        (0x3c6054a5812d9fc4, 0x3fca61762a515608),
46        (0x3c5662d846baea2e, 0xbfc7154764f1db89),
47        (0x3c66b62df1fd3c4b, 0x3fc484dddfe20481),
48        (0xbc6b16c93b93c1e3, 0xbfc2779d6a07c6b3),
49    ];
50    let mut t = DoubleDouble::from_bit_pair(P[9]);
51    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[8]));
52    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[7]));
53    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[6]));
54    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[5]));
55    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[4]));
56    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[3]));
57    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[2]));
58    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[1]));
59    t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[0]));
60    DoubleDouble::quick_mult_f64(t, z)
61}
62
63#[inline]
64pub(crate) fn log2_dd(x: f64) -> DoubleDouble {
65    let x_u = x.to_bits();
66    let mut m = x_u & 0xfffffffffffff;
67    let mut e: i64 = ((x_u >> 52) & 0x7ff) as i64;
68
69    let t;
70    if e != 0 {
71        t = m | (0x3ffu64 << 52);
72        m = m.wrapping_add(1u64 << 52);
73        e -= 0x3ff;
74    } else {
75        /* x is a subnormal double  */
76        let k = m.leading_zeros() - 11;
77
78        e = -0x3fei64 - k as i64;
79        m = m.wrapping_shl(k);
80        t = m | (0x3ffu64 << 52);
81    }
82
83    /* now |x| = 2^_e*_t = 2^(_e-52)*m with 1 <= _t < 2,
84    and 2^52 <= _m < 2^53 */
85
86    //   log2(x) = log2(t) + E ยท log(2)
87    let mut t = f64::from_bits(t);
88
89    // If m > sqrt(2) we divide it by 2 so ensure 1/sqrt(2) < t < sqrt(2)
90    let c: usize = (m >= 0x16a09e667f3bcd) as usize;
91    static CY: [f64; 2] = [1.0, 0.5];
92    static CM: [u64; 2] = [44, 45];
93
94    e = e.wrapping_add(c as i64);
95    let be = e;
96    let i = m >> CM[c];
97    t *= CY[c];
98
99    let idx = (i - 181) as usize;
100
101    let r = f64::from_bits(POW_INVERSE[idx]);
102    let log_r = DoubleDouble::from_bit_pair(LOG2_NEG_DD[idx]);
103
104    let z = f64::mul_add(r, t, -1.0);
105
106    let v = DoubleDouble::full_add_f64(log_r, be as f64);
107    let p = log2_poly(z);
108    let z0 = DoubleDouble::new(v.lo + p.lo, p.hi);
109    DoubleDouble::full_add_f64(z0, v.hi)
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn log2dd_test() {
118        assert_eq!(log2_dd(0.0040283203125 / 2.).to_f64(), -8.955605880641546);
119    }
120}