Debian Codegraph @

src/ops/mul.rs

raw symbols SCIP
1 use crate::constants::{BIG_POWERS_10, MAX_I64_SCALE, U32_MAX};
2 use crate::decimal::{CalculationResult, Decimal};
3 use crate::ops::common::Buf24;
4
5 #[inline(always)]
6 pub(crate) fn mul_impl(d1: &Decimal, d2: &Decimal) -> CalculationResult {
7 if d1.is_zero() || d2.is_zero() {
8 // We should think about this - does zero need to maintain precision? This treats it like
9 // an absolute which I think is ok, especially since we have is_zero() functions etc.
10 return CalculationResult::Ok(Decimal::ZERO);
11 }
12
13 let mut scale = d1.scale() + d2.scale();
14 let negative = d1.is_sign_negative() ^ d2.is_sign_negative();
15
16 // See if we can optimize this calculation depending on whether the hi bits are set
17 if d1.hi() | d1.mid() == 0 {
18 if d2.hi() | d2.mid() == 0 {
19 // We're multiplying two 32 bit integers, so we can take some liberties to optimize this.
20 let mut low64 = d1.lo() as u64 * d2.lo() as u64;
21 if scale > Decimal::MAX_SCALE {
22 // We've exceeded maximum scale so we need to start reducing the precision (aka
23 // rounding) until we have something that fits.
24 // If we're too big then we effectively round to zero.
25 if scale > Decimal::MAX_SCALE + MAX_I64_SCALE {
26 return CalculationResult::Ok(Decimal::ZERO);
27 }
28
29 scale -= Decimal::MAX_SCALE + 1;
30 let mut power = BIG_POWERS_10[scale as usize];
31
32 let tmp = low64 / power;
33 let remainder = low64 - tmp * power;
34 low64 = tmp;
35
36 // Round the result. Since the divisor was a power of 10, it's always even.
37 power >>= 1;
38 if remainder >= power && (remainder > power || (low64 as u32 & 1) > 0) {
39 low64 += 1;
40 }
41
42 scale = Decimal::MAX_SCALE;
43 }
44
45 // Early exit
46 return CalculationResult::Ok(Decimal::from_parts(
47 low64 as u32,
48 (low64 >> 32) as u32,
49 0,
50 negative,
51 scale,
52 ));
53 }
54
55 // We know that the left hand side is just 32 bits but the right hand side is either
56 // 64 or 96 bits.
57 let mut product = Buf24::zero();
58 mul_by_32bit_lhs(d1.lo() as u64, d2, &mut product);
59 finish_mul(product, negative, scale)
60 } else if d2.mid() | d2.hi() == 0 {
61 // We know that the right hand side is just 32 bits.
62 let mut product = Buf24::zero();
63 mul_by_32bit_lhs(d2.lo() as u64, d1, &mut product);
64 finish_mul(product, negative, scale)
65 } else {
66 let mut product = Buf24::zero();
67 // We know we're not dealing with simple 32 bit operands on either side.
68 // We compute and accumulate the 9 partial products using long multiplication
69
70 // 1: ll * rl
71 let mut tmp = d1.lo() as u64 * d2.lo() as u64;
72 product.data[0] = tmp as u32;
73
74 // 2: ll * rm
75 let mut tmp2 = (d1.lo() as u64 * d2.mid() as u64).wrapping_add(tmp >> 32);
76
77 // 3: lm * rl
78 tmp = d1.mid() as u64 * d2.lo() as u64;
79 tmp = tmp.wrapping_add(tmp2);
80 product.data[1] = tmp as u32;
81
82 // Detect if carry happened from the wrapping add
83 if tmp < tmp2 {
84 tmp2 = (tmp >> 32) | (1u64 << 32);
85 } else {
86 tmp2 = tmp >> 32;
87 }
88
89 // 4: lm * rm
90 tmp = (d1.mid() as u64 * d2.mid() as u64) + tmp2;
91
92 // If the high bit isn't set then we can stop here. Otherwise, we need to continue calculating
93 // using the high bits.
94 if (d1.hi() | d2.hi()) > 0 {
95 // 5. ll * rh
96 tmp2 = d1.lo() as u64 * d2.hi() as u64;
97 tmp = tmp.wrapping_add(tmp2);
98 // Detect if we carried
99 let mut tmp3 = if tmp < tmp2 { 1 } else { 0 };
100
101 // 6. lh * rl
102 tmp2 = d1.hi() as u64 * d2.lo() as u64;
103 tmp = tmp.wrapping_add(tmp2);
104 product.data[2] = tmp as u32;
105 // Detect if we carried
106 if tmp < tmp2 {
107 tmp3 += 1;
108 }
109 tmp2 = (tmp3 << 32) | (tmp >> 32);
110
111 // 7. lm * rh
112 tmp = d1.mid() as u64 * d2.hi() as u64;
113 tmp = tmp.wrapping_add(tmp2);
114 // Check for carry
115 tmp3 = if tmp < tmp2 { 1 } else { 0 };
116
117 // 8. lh * rm
118 tmp2 = d1.hi() as u64 * d2.mid() as u64;
119 tmp = tmp.wrapping_add(tmp2);
120 product.data[3] = tmp as u32;
121 // Check for carry
122 if tmp < tmp2 {
123 tmp3 += 1;
124 }
125 tmp = (tmp3 << 32) | (tmp >> 32);
126
127 // 9. lh * rh
128 product.set_high64(d1.hi() as u64 * d2.hi() as u64 + tmp);
129 } else {
130 product.set_mid64(tmp);
131 }
132 finish_mul(product, negative, scale)
133 }
134 }
135
136 #[inline(always)]
137 fn finish_mul(mut product: Buf24, negative: bool, mut scale: u32) -> CalculationResult {
138 // We may want to "rescale". This is the case if the mantissa is > 96 bits or if the scale
139 // exceeds the maximum precision.
140 let upper_word = product.upper_word();
141 if upper_word > 2 || scale > Decimal::MAX_SCALE {
142 scale = if let Some(new_scale) = product.rescale(upper_word, scale) {
143 new_scale
144 } else {
145 return CalculationResult::Overflow;
146 }
147 }
148
149 // Result is non-zero (both inputs are non-zero, checked at entry of mul_impl)
150 CalculationResult::Ok(Decimal::from_parts_raw_unchecked(
151 product.data[0],
152 product.data[1],
153 product.data[2],
154 crate::decimal::flags(negative, scale),
155 ))
156 }
157
158 #[inline(always)]
159 fn mul_by_32bit_lhs(d1: u64, d2: &Decimal, product: &mut Buf24) {
160 let mut tmp = d1 * d2.lo() as u64;
161 product.data[0] = tmp as u32;
162 tmp = (d1 * d2.mid() as u64).wrapping_add(tmp >> 32);
163 product.data[1] = tmp as u32;
164 tmp >>= 32;
165
166 // If we're multiplying by a 96 bit integer then continue the calculation
167 if d2.hi() > 0 {
168 tmp = tmp.wrapping_add(d1 * d2.hi() as u64);
169 if tmp > U32_MAX {
170 product.set_mid64(tmp);
171 } else {
172 product.data[2] = tmp as u32;
173 }
174 } else {
175 product.data[2] = tmp as u32;
176 }
177 }