pxfm/logs/
log_td.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::log_td_table::LOG_NEG_TD;
31use crate::pow_tables::POW_INVERSE;
32use crate::triple_double::TripleDouble;
33
34#[inline(always)]
35fn log_td_poly(z: f64) -> TripleDouble {
36    /*
37      See ./notes/td_log.sollya
38    */
39    const P: [(u64, u64, u64); 10] = [
40        (0x38fc6038ddcc5680, 0x3c755555556795ff, 0x3fd5555555555555),
41        (0x372720effda9e638, 0xba86f68986749d55, 0xbfd0000000000000),
42        (0x38deb738e67c5280, 0xbc699b2852652c20, 0x3fc999999999999a),
43        (0x3900b719d69f9950, 0xbc65526cf5c3f935, 0xbfc5555555555555),
44        (0xb8ad0f51dccb7c02, 0xbc34fc5a6b8b9f2d, 0x3fc24924924924aa),
45        (0xb8faeee1e6f2b960, 0xbc52e65780d7da8f, 0xbfc0000000000023),
46        (0xb8f306e8001c6280, 0x3c5d0f68afb70c37, 0x3fbc71c71c2042d4),
47        (0x38d6940b092bf340, 0xbc3280fbebf81ea0, 0xbfb999999934f78b),
48        (0xb8daa1cce08da780, 0x3c48da171d2f9c73, 0x3fb74612a55c4784),
49        (0x38e9232517db1c80, 0x3c553b70480fc993, 0xbfb5559a592ab15c),
50    ];
51    let x2 = DoubleDouble::from_exact_mult(z, z);
52    let mut t = TripleDouble::f64_mul_add(
53        z,
54        TripleDouble::from_bit_pair(P[9]),
55        TripleDouble::from_bit_pair(P[8]),
56    );
57    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[7]));
58    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[6]));
59    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[5]));
60    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[4]));
61    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[3]));
62    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[2]));
63    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[1]));
64    t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[0]));
65    t = TripleDouble::quick_mult_dd(t, x2);
66    t = TripleDouble::quick_mult_f64(t, z);
67    TripleDouble::f64_mul_dd_add(-0.5, x2, t)
68}
69
70#[inline]
71pub(crate) fn log_td(x: f64) -> TripleDouble {
72    let x_u = x.to_bits();
73    let mut m = x_u & 0xfffffffffffff;
74    let mut e: i64 = ((x_u >> 52) & 0x7ff) as i64;
75
76    let t;
77    if e != 0 {
78        t = m | (0x3ffu64 << 52);
79        m = m.wrapping_add(1u64 << 52);
80        e -= 0x3ff;
81    } else {
82        /* x is a subnormal double */
83        let k = m.leading_zeros() - 11;
84
85        e = -0x3fei64 - k as i64;
86        m = m.wrapping_shl(k);
87        t = m | (0x3ffu64 << 52);
88    }
89
90    /* now |x| = 2^_e*_t = 2^(_e-52)*m with 1 <= _t < 2,
91    and 2^52 <= _m < 2^53 */
92
93    //   log(x) = log(t) + E ยท log(2)
94    let mut t = f64::from_bits(t);
95
96    // If m > sqrt(2) we divide it by 2 so ensure 1/sqrt(2) < t < sqrt(2)
97    let c: usize = (m >= 0x16a09e667f3bcd) as usize;
98    static CY: [f64; 2] = [1.0, 0.5];
99    static CM: [u64; 2] = [44, 45];
100
101    e = e.wrapping_add(c as i64);
102    let be = e;
103    let i = m >> CM[c];
104    t *= CY[c];
105
106    let r = f64::from_bits(POW_INVERSE[(i - 181) as usize]);
107
108    let log_r = TripleDouble::from_bit_pair(LOG_NEG_TD[(i - 181) as usize]);
109
110    let z = f64::mul_add(r, t, -1.0);
111
112    const LOG2_DD: TripleDouble =
113        TripleDouble::from_bit_pair((0x3907b57a079a1934, 0x3c7abc9e3b39803f, 0x3fe62e42fefa39ef));
114
115    let tt = TripleDouble::f64_mul_add(be as f64, LOG2_DD, log_r);
116
117    let v = TripleDouble::add_f64(z, tt);
118    let p = log_td_poly(z);
119    TripleDouble::add_f64(v.hi, TripleDouble::new(v.lo + p.lo, v.mid + p.mid, p.hi))
120}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125
126    #[test]
127    fn test_log_td() {
128        assert_eq!(log_td(184467440737095500000.).to_f64(), 46.66400464883055);
129    }
130}