/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/logs/log10td.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::double_double::DoubleDouble; |
30 | | use crate::logs::log10td_coeffs::LOG10_NEG_TD; |
31 | | use crate::pow_tables::POW_INVERSE; |
32 | | use crate::triple_double::TripleDouble; |
33 | | |
34 | | #[inline(always)] |
35 | 0 | fn log10_poly(z: f64) -> TripleDouble { |
36 | | /* |
37 | | Poly generated by Sollya: |
38 | | |
39 | | d = [-0.0040283203125,0.0040283203125]; |
40 | | f = log10(1+x)/x; |
41 | | pf = fpminimax(f, 10, [|157...|], d, absolute, floating); |
42 | | |
43 | | See ./notes/td_log10.sollya |
44 | | */ |
45 | | const P: [(u64, u64, u64); 11] = [ |
46 | | (0xb8f29c4663277dc0, 0x3c695355baaafad3, 0x3fdbcb7b1526e50e), |
47 | | (0x38ee76408ee1b680, 0xbc595355bab3a517, 0xbfcbcb7b1526e50e), |
48 | | (0x38f58fe4d121c480, 0xbc59c8718361a853, 0x3fc287a7636f435f), |
49 | | (0xb8e80f425616e240, 0xbc49520fd0630f7e, 0xbfbbcb7b1526e50e), |
50 | | (0x38eb058acb9ab2c0, 0x3c443f5b242c637b, 0x3fb63c62775250d8), |
51 | | (0x38c32c088e8da180, 0xbc4a6666386cc60e, 0xbfb287a7636f436c), |
52 | | (0x38bb2e95bc6a3a80, 0x3c45ac4260107e46, 0x3fafc3fa615105f6), |
53 | | (0xb8e14f1e5b926690, 0x3c421ee12f4a23de, 0xbfabcb7b14ed42da), |
54 | | (0x38d4b4d370b610e0, 0x3c47e2c6f24d3d30, 0x3fa8b4df2ef1a288), |
55 | | (0x38bd632a8b3a6500, 0xbc1a922cdefb3bb1, 0xbfa63c98a8c44069), |
56 | | (0xb8c077b6551291c0, 0x3c4337142dd97816, 0x3fa4372045637cca), |
57 | | ]; |
58 | 0 | let mut t = TripleDouble::f64_mul_add( |
59 | 0 | z, |
60 | 0 | TripleDouble::from_bit_pair(P[9]), |
61 | 0 | TripleDouble::from_bit_pair(P[8]), |
62 | | ); |
63 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[7])); |
64 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[6])); |
65 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[5])); |
66 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[4])); |
67 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[3])); |
68 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[2])); |
69 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[1])); |
70 | 0 | t = TripleDouble::f64_mul_add(z, t, TripleDouble::from_bit_pair(P[0])); |
71 | 0 | TripleDouble::quick_mult_f64(t, z) |
72 | 0 | } |
73 | | |
74 | | #[inline] |
75 | 0 | pub(crate) fn log10_td(x: f64) -> TripleDouble { |
76 | 0 | let x_u = x.to_bits(); |
77 | 0 | let mut m = x_u & 0xfffffffffffff; |
78 | 0 | let mut e: i64 = ((x_u >> 52) & 0x7ff) as i64; |
79 | | |
80 | | let t; |
81 | 0 | if e != 0 { |
82 | 0 | t = m | (0x3ffu64 << 52); |
83 | 0 | m = m.wrapping_add(1u64 << 52); |
84 | 0 | e -= 0x3ff; |
85 | 0 | } else { |
86 | 0 | /* x is a subnormal double */ |
87 | 0 | let k = m.leading_zeros() - 11; |
88 | 0 |
|
89 | 0 | e = -0x3fei64 - k as i64; |
90 | 0 | m = m.wrapping_shl(k); |
91 | 0 | t = m | (0x3ffu64 << 52); |
92 | 0 | } |
93 | | |
94 | | /* now |x| = 2^_e*_t = 2^(_e-52)*m with 1 <= _t < 2, |
95 | | and 2^52 <= _m < 2^53 */ |
96 | | |
97 | | // log10(x) = log10(t) + E ยท log10(e) |
98 | 0 | let mut t = f64::from_bits(t); |
99 | | |
100 | | // If m > sqrt(2) we divide it by 2 so ensure 1/sqrt(2) < t < sqrt(2) |
101 | 0 | let c: usize = (m >= 0x16a09e667f3bcd) as usize; |
102 | | static CY: [f64; 2] = [1.0, 0.5]; |
103 | | static CM: [u64; 2] = [44, 45]; |
104 | | |
105 | 0 | e = e.wrapping_add(c as i64); |
106 | 0 | let be = e; |
107 | 0 | let i = m >> CM[c]; |
108 | 0 | t *= CY[c]; |
109 | | |
110 | 0 | let r = f64::from_bits(POW_INVERSE[(i - 181) as usize]); |
111 | 0 | let log10_r = TripleDouble::from_bit_pair(LOG10_NEG_TD[(i - 181) as usize]); |
112 | | |
113 | 0 | let z = f64::mul_add(r, t, -1.0); |
114 | | |
115 | | const LOG10_2_DD: DoubleDouble = |
116 | | DoubleDouble::from_bit_pair((0xbc49dc1da994fd21, 0x3fd34413509f79ff)); |
117 | | |
118 | 0 | let v = TripleDouble::f64_mul_dd_add(be as f64, LOG10_2_DD, log10_r); |
119 | | |
120 | 0 | let p = log10_poly(z); |
121 | 0 | TripleDouble::add_f64(v.hi, TripleDouble::new(v.lo + p.lo, v.mid + p.mid, p.hi)) |
122 | 0 | } |
123 | | |
124 | | #[cfg(test)] |
125 | | mod tests { |
126 | | use super::*; |
127 | | |
128 | | #[test] |
129 | | fn test_log10_dd() { |
130 | | assert_eq!(log10_td(10.).to_f64(), 1.); |
131 | | } |
132 | | } |