| 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::double_double::DoubleDouble; |
| 30 |
use crate::logs::log10dd_coeffs::LOG10_NEG_DD; |
| 31 |
use crate::pow_tables::POW_INVERSE; |
| 32 |
|
| 33 |
#[inline(always)] |
| 34 |
fn log10_poly(z: f64) -> DoubleDouble { |
| 35 |
/* |
| 36 |
See ./notes/dd_log10.sollya |
| 37 |
*/ |
| 38 |
const P: [(u64, u64); 10] = [ |
| 39 |
(0x3c695355baabd6d2, 0x3fdbcb7b1526e50e), |
| 40 |
(0xbc595355bababc0c, 0xbfcbcb7b1526e50e), |
| 41 |
(0xbc59c8c251be5fee, 0x3fc287a7636f435f), |
| 42 |
(0xbc49515e0a562cbe, 0xbfbbcb7b1526e50e), |
| 43 |
(0xbc5603802582ebad, 0x3fb63c62775250e2), |
| 44 |
(0x3c37a0e7de39c80c, 0xbfb287a7636f4371), |
| 45 |
(0x3c2c4afd722dc9ec, 0x3fafc3fa611f0291), |
| 46 |
(0xbc4a52f6541b67ee, 0xbfabcb7b14e178e5), |
| 47 |
(0x3c3ef541147ede8e, 0x3fa8b514ee02cfad), |
| 48 |
(0xbc28b9ea54e8f3b4, 0xbfa63c9d960501f1), |
| 49 |
]; |
| 50 |
let mut t = DoubleDouble::quick_mul_f64_add( |
| 51 |
DoubleDouble::from_bit_pair(P[9]), |
| 52 |
z, |
| 53 |
DoubleDouble::from_bit_pair(P[8]), |
| 54 |
); |
| 55 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[7])); |
| 56 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[6])); |
| 57 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[5])); |
| 58 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[4])); |
| 59 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[3])); |
| 60 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[2])); |
| 61 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[1])); |
| 62 |
t = DoubleDouble::quick_mul_f64_add(t, z, DoubleDouble::from_bit_pair(P[0])); |
| 63 |
DoubleDouble::quick_mult_f64(t, z) |
| 64 |
} |
| 65 |
|
| 66 |
#[inline] |
| 67 |
pub(crate) fn log10_dd(x: f64) -> DoubleDouble { |
| 68 |
let x_u = x.to_bits(); |
| 69 |
let mut m = x_u & 0xfffffffffffff; |
| 70 |
let mut e: i64 = ((x_u >> 52) & 0x7ff) as i64; |
| 71 |
|
| 72 |
let t; |
| 73 |
if e != 0 { |
| 74 |
t = m | (0x3ffu64 << 52); |
| 75 |
m = m.wrapping_add(1u64 << 52); |
| 76 |
e -= 0x3ff; |
| 77 |
} else { |
| 78 |
/* x is a subnormal double */ |
| 79 |
let k = m.leading_zeros() - 11; |
| 80 |
|
| 81 |
e = -0x3fei64 - k as i64; |
| 82 |
m = m.wrapping_shl(k); |
| 83 |
t = m | (0x3ffu64 << 52); |
| 84 |
} |
| 85 |
|
| 86 |
/* now |x| = 2^_e*_t = 2^(_e-52)*m with 1 <= _t < 2, |
| 87 |
and 2^52 <= _m < 2^53 */ |
| 88 |
|
| 89 |
// log10(x) = log10(t) + E ยท log10(e) |
| 90 |
let mut t = f64::from_bits(t); |
| 91 |
|
| 92 |
// If m > sqrt(2) we divide it by 2 so ensure 1/sqrt(2) < t < sqrt(2) |
| 93 |
let c: usize = (m >= 0x16a09e667f3bcd) as usize; |
| 94 |
static CY: [f64; 2] = [1.0, 0.5]; |
| 95 |
static CM: [u64; 2] = [44, 45]; |
| 96 |
|
| 97 |
e = e.wrapping_add(c as i64); |
| 98 |
let be = e; |
| 99 |
let i = m >> CM[c]; |
| 100 |
t *= CY[c]; |
| 101 |
|
| 102 |
let r = f64::from_bits(POW_INVERSE[(i - 181) as usize]); |
| 103 |
let log10_r = DoubleDouble::from_bit_pair(LOG10_NEG_DD[(i - 181) as usize]); |
| 104 |
|
| 105 |
let z = f64::mul_add(r, t, -1.0); |
| 106 |
|
| 107 |
const LOG10_2_DD: DoubleDouble = |
| 108 |
DoubleDouble::from_bit_pair((0xbc49dc1da994fd21, 0x3fd34413509f79ff)); |
| 109 |
|
| 110 |
let v = DoubleDouble::mul_f64_add(LOG10_2_DD, be as f64, log10_r); |
| 111 |
|
| 112 |
let p = log10_poly(z); |
| 113 |
DoubleDouble::f64_add(v.hi, DoubleDouble::new(v.lo + p.lo, p.hi)) |
| 114 |
} |
| 115 |
|
| 116 |
#[cfg(test)] |
| 117 |
mod tests { |
| 118 |
use super::*; |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn test_log10_dd() { |
| 122 |
assert_eq!(log10_dd(10.).to_f64(), 1.); |
| 123 |
} |
| 124 |
} |