Coverage Report

Created: 2025-10-10 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/logs/log2td.rs
Line
Count
Source
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
 */
29
use crate::logs::log2td_coeffs::LOG2_NEG_TD;
30
use crate::pow_tables::POW_INVERSE;
31
use crate::triple_double::TripleDouble;
32
33
#[inline(always)]
34
0
fn log2_poly(z: f64) -> TripleDouble {
35
    /*
36
      See ./notes/dd_log.sollya
37
    */
38
    const P: [(u64, u64, u64); 11] = [
39
        (0x38cb46674646bfff, 0x3c7777d0ffda0d23, 0x3ff71547652b82fe),
40
        (0xb90ea6e2f55cd900, 0xbc6777d0ffe87198, 0xbfe71547652b82fe),
41
        (0x391e004d54467330, 0x3c7d27f0556d8546, 0x3fdec709dc3a03fd),
42
        (0xb8b2d21aeeb27bff, 0xbc5775b3aa82c433, 0xbfd71547652b82fe),
43
        (0xb9164a4a186a0c00, 0x3c7e49c9bdb8b680, 0x3fd2776c50ef9bfe),
44
        (0xb8efe23702b5a940, 0x3c6195ba6326b1bf, 0xbfcec709dc3a0414),
45
        (0x38d7695a46fb4b00, 0x3c6f82e7add9bb4d, 0x3fca61762a7adf00),
46
        (0xb8c00e9d3285e000, 0x3c2caf76a9ee1e78, 0xbfc7154764fba5e4),
47
        (0xb8d1ff2b356eee80, 0xbc3f6102bc5ddc49, 0x3fc484b13d3bbed8),
48
        (0xb8edd22b4add09c0, 0x3c4c1da4a1a32f3b, 0xbfc2779952952c26),
49
        (0x388875bd65660001, 0x3c58e09839d588dd, 0x3fc0c9d962b39a7d),
50
    ];
51
0
    let mut t = TripleDouble::from_bit_pair(P[10]);
52
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[9]));
53
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[8]));
54
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[7]));
55
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[6]));
56
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[5]));
57
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[4]));
58
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[3]));
59
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[2]));
60
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[1]));
61
0
    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[0]));
62
0
    TripleDouble::quick_mult_f64(t, z)
63
0
}
64
65
#[inline]
66
0
pub(crate) fn log2_td(x: f64) -> TripleDouble {
67
0
    let x_u = x.to_bits();
68
0
    let mut m = x_u & 0xfffffffffffff;
69
0
    let mut e: i64 = ((x_u >> 52) & 0x7ff) as i64;
70
71
    let t;
72
0
    if e != 0 {
73
0
        t = m | (0x3ffu64 << 52);
74
0
        m = m.wrapping_add(1u64 << 52);
75
0
        e -= 0x3ff;
76
0
    } else {
77
0
        /* x is a subnormal double  */
78
0
        let k = m.leading_zeros() - 11;
79
0
80
0
        e = -0x3fei64 - k as i64;
81
0
        m = m.wrapping_shl(k);
82
0
        t = m | (0x3ffu64 << 52);
83
0
    }
84
85
    /* now |x| = 2^_e*_t = 2^(_e-52)*m with 1 <= _t < 2,
86
    and 2^52 <= _m < 2^53 */
87
88
    //   log2(x) = log2(t) + E ยท log(2)
89
0
    let mut t = f64::from_bits(t);
90
91
    // If m > sqrt(2) we divide it by 2 so ensure 1/sqrt(2) < t < sqrt(2)
92
0
    let c: usize = (m >= 0x16a09e667f3bcd) as usize;
93
    static CY: [f64; 2] = [1.0, 0.5];
94
    static CM: [u64; 2] = [44, 45];
95
96
0
    e = e.wrapping_add(c as i64);
97
0
    let be = e;
98
0
    let i = m >> CM[c];
99
0
    t *= CY[c];
100
101
0
    let r = f64::from_bits(POW_INVERSE[(i - 181) as usize]);
102
0
    let log_r = TripleDouble::from_bit_pair(LOG2_NEG_TD[(i - 181) as usize]);
103
104
0
    let z = f64::mul_add(r, t, -1.0);
105
106
0
    let v = TripleDouble::add_f64(be as f64, log_r);
107
0
    let p = log2_poly(z);
108
0
    TripleDouble::add_f64(v.hi, TripleDouble::new(v.lo + p.lo, v.mid + p.mid, p.hi))
109
0
}
110
111
#[cfg(test)]
112
mod tests {
113
    use crate::logs::log2td::log2_td;
114
115
    #[test]
116
    fn log2td_test() {
117
        assert_eq!(log2_td(0.0040283203125 / 2.).to_f64(), -8.955605880641546);
118
    }
119
}