Debian Codegraph @

src/binary.rs

raw symbols SCIP
1 use crate::common::AdjustedMantissa;
2 use crate::float::Float;
3 use crate::table::{LARGEST_POWER_OF_FIVE, POWER_OF_FIVE_128, SMALLEST_POWER_OF_FIVE};
4
5 #[inline]
6 pub fn compute_float<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
7 let am_zero = AdjustedMantissa::zero_pow2(0);
8 let am_inf = AdjustedMantissa::zero_pow2(F::INFINITE_POWER);
9 let am_error = AdjustedMantissa::zero_pow2(-1);
10
11 if w == 0 || q < F::SMALLEST_POWER_OF_TEN as i64 {
12 return am_zero;
13 } else if q > F::LARGEST_POWER_OF_TEN as i64 {
14 return am_inf;
15 }
16 let lz = w.leading_zeros();
17 w <<= lz;
18 let (lo, hi) = compute_product_approx(q, w, F::MANTISSA_EXPLICIT_BITS + 3);
19 if lo == 0xFFFF_FFFF_FFFF_FFFF {
20 let inside_safe_exponent = (-27..=55).contains(&q);
21 if !inside_safe_exponent {
22 return am_error;
23 }
24 }
25 let upperbit = (hi >> 63) as i32;
26 let mut mantissa = hi >> (upperbit + 64 - F::MANTISSA_EXPLICIT_BITS as i32 - 3);
27 let mut power2 = power(q as i32) + upperbit - lz as i32 - F::MINIMUM_EXPONENT;
28 if power2 <= 0 {
29 if -power2 + 1 >= 64 {
30 return am_zero;
31 }
32 mantissa >>= -power2 + 1;
33 mantissa += mantissa & 1;
34 mantissa >>= 1;
35 power2 = (mantissa >= (1_u64 << F::MANTISSA_EXPLICIT_BITS)) as i32;
36 return AdjustedMantissa {
37 mantissa,
38 power2,
39 };
40 }
41 if lo <= 1
42 && q >= F::MIN_EXPONENT_ROUND_TO_EVEN as i64
43 && q <= F::MAX_EXPONENT_ROUND_TO_EVEN as i64
44 && mantissa & 3 == 1
45 && (mantissa << (upperbit + 64 - F::MANTISSA_EXPLICIT_BITS as i32 - 3)) == hi
46 {
47 mantissa &= !1_u64;
48 }
49 mantissa += mantissa & 1;
50 mantissa >>= 1;
51 if mantissa >= (2_u64 << F::MANTISSA_EXPLICIT_BITS) {
52 mantissa = 1_u64 << F::MANTISSA_EXPLICIT_BITS;
53 power2 += 1;
54 }
55 mantissa &= !(1_u64 << F::MANTISSA_EXPLICIT_BITS);
56 if power2 >= F::INFINITE_POWER {
57 return am_inf;
58 }
59 AdjustedMantissa {
60 mantissa,
61 power2,
62 }
63 }
64
65 #[inline]
66 fn power(q: i32) -> i32 {
67 (q.wrapping_mul(152_170 + 65536) >> 16) + 63
68 }
69
70 #[inline]
71 fn full_multiplication(a: u64, b: u64) -> (u64, u64) {
72 let r = (a as u128) * (b as u128);
73 (r as u64, (r >> 64) as u64)
74 }
75
76 // This will compute or rather approximate w * 5**q and return a pair of 64-bit
77 // words approximating the result, with the "high" part corresponding to the
78 // most significant bits and the low part corresponding to the least significant
79 // bits.
80 #[inline]
81 fn compute_product_approx(q: i64, w: u64, precision: usize) -> (u64, u64) {
82 debug_assert!(q >= SMALLEST_POWER_OF_FIVE as i64);
83 debug_assert!(q <= LARGEST_POWER_OF_FIVE as i64);
84 debug_assert!(precision <= 64);
85
86 let mask = if precision < 64 {
87 0xFFFF_FFFF_FFFF_FFFF_u64 >> precision
88 } else {
89 0xFFFF_FFFF_FFFF_FFFF_u64
90 };
91 let index = (q - SMALLEST_POWER_OF_FIVE as i64) as usize;
92 // NOTE: this cannot be ellided by the compiler, but the proof the index
93 // must be within the bounds is non-trivial, especially because this
94 // comes from a parsed result. Since this is unlikely to have any major
95 // performance implications, as is determined empirically, we keep the
96 // bounds check despite the performance hit.
97 let (lo5, hi5) = POWER_OF_FIVE_128[index];
98 let (mut first_lo, mut first_hi) = full_multiplication(w, lo5);
99 if first_hi & mask == mask {
100 let (_, second_hi) = full_multiplication(w, hi5);
101 first_lo = first_lo.wrapping_add(second_hi);
102 if second_hi > first_lo {
103 first_hi += 1;
104 }
105 }
106 (first_lo, first_hi)
107 }
108
109 #[cfg(test)]
110 mod tests {
111 use super::*;
112
113 #[test]
114 fn test_full_multiplication() {
115 fn check(a: u64, b: u64, lo: u64, hi: u64) {
116 assert_eq!(full_multiplication(a, b), (lo, hi));
117 assert_eq!(full_multiplication(b, a), (lo, hi));
118 }
119 check(1 << 0, 1 << 0, 1, 0);
120 check(1 << 0, 1 << 63, 1 << 63, 0);
121 check(1 << 1, 1 << 63, 0, 1);
122 check(1 << 63, 1 << 0, 1 << 63, 0);
123 check(1 << 63, 1 << 1, 0, 1);
124 check(1 << 63, 1 << 2, 0, 2);
125 check(1 << 63, 1 << 63, 0, 1 << 62);
126 }
127 }