1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::num::arithmetic::traits::{MulAddMul, MulAddMulAssign, UnsignedAbs};
use crate::num::basic::signeds::PrimitiveSigned;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::num::conversion::traits::WrappingFrom;
// Where the exact value of $xy \pm zw$ sits relative to the type's range. The products are formed
// at double width, so no intermediate overflow can hide a result that would have fit.
pub(crate) enum Wide<T> {
Fits(T),
Above,
Below,
}
// The exact value of $xy + zw$, or of $xy - zw$ when `sub` is set, for an unsigned type.
pub(crate) fn mul_add_mul_wide_unsigned<T: PrimitiveUnsigned>(
x: T,
y: T,
z: T,
w: T,
sub: bool,
) -> Wide<T> {
let (p_1, p_0) = T::x_mul_y_to_zz(x, y);
let (q_1, q_0) = T::x_mul_y_to_zz(z, w);
let (r_1, r_0) = if sub {
if (p_1, p_0) < (q_1, q_0) {
return Wide::Below;
}
T::xx_sub_yy_to_zz(p_1, p_0, q_1, q_0)
} else {
let (r_1, r_0) = T::xx_add_yy_to_zz(p_1, p_0, q_1, q_0);
// Two products can carry out of double width, since $(2^W-1)^2 + (2^W-1)^2 \geq 2^{2W}$,
// and `xx_add_yy_to_zz` wraps without saying so. The sum wrapped iff it is now smaller than
// one of the addends.
if (r_1, r_0) < (p_1, p_0) {
return Wide::Above;
}
(r_1, r_0)
};
if r_1 == T::ZERO {
Wide::Fits(r_0)
} else {
Wide::Above
}
}
// The exact value of $xy + zw$, or of $xy - zw$ when `sub` is set, for a signed type. The two
// products are combined as a sign and a double-width magnitude, so the sign of an out-of-range
// result is known and the saturating variants can pick the right bound.
pub(crate) fn mul_add_mul_wide_signed<
U: PrimitiveUnsigned,
T: PrimitiveSigned + UnsignedAbs<Output = U> + WrappingFrom<U>,
>(
x: T,
y: T,
z: T,
w: T,
sub: bool,
) -> Wide<T> {
let p_neg = (x < T::ZERO) != (y < T::ZERO);
let q_neg = ((z < T::ZERO) != (w < T::ZERO)) != sub;
let (p_1, p_0) = U::x_mul_y_to_zz(x.unsigned_abs(), y.unsigned_abs());
let (q_1, q_0) = U::x_mul_y_to_zz(z.unsigned_abs(), w.unsigned_abs());
let (neg, r_1, r_0) = if p_neg == q_neg {
let (r_1, r_0) = U::xx_add_yy_to_zz(p_1, p_0, q_1, q_0);
(p_neg, r_1, r_0)
} else if (p_1, p_0) >= (q_1, q_0) {
let (r_1, r_0) = U::xx_sub_yy_to_zz(p_1, p_0, q_1, q_0);
(p_neg, r_1, r_0)
} else {
let (r_1, r_0) = U::xx_sub_yy_to_zz(q_1, q_0, p_1, p_0);
(q_neg, r_1, r_0)
};
if r_1 != U::ZERO {
return if neg { Wide::Below } else { Wide::Above };
}
if neg {
// The negative bound has one more magnitude than the positive one.
if r_0 <= T::MIN.unsigned_abs() {
Wide::Fits(T::wrapping_from(r_0).wrapping_neg())
} else {
Wide::Below
}
} else if r_0 <= T::MAX.unsigned_abs() {
Wide::Fits(T::wrapping_from(r_0))
} else {
Wide::Above
}
}
macro_rules! impl_mul_add_mul_primitive_int {
($t:ident) => {
impl MulAddMul for $t {
type Output = $t;
/// Adds the products of two pairs of numbers.
///
/// $f(x, y, z, w) = xy + zw$.
///
/// Both products and their sum wrap on overflow, as they do for
/// [`add_mul`](super::traits::AddMul::add_mul).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::mul_add_mul#mul_add_mul).
#[inline]
fn mul_add_mul(self, y: $t, z: $t, w: $t) -> $t {
self.wrapping_mul(y).wrapping_add(z.wrapping_mul(w))
}
}
impl MulAddMulAssign for $t {
/// Adds the products of two pairs of numbers, in place.
///
/// $x \gets xy + zw$.
///
/// Both products and their sum wrap on overflow, as they do for
/// [`add_mul`](super::traits::AddMul::add_mul).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::mul_add_mul#mul_add_mul_assign).
#[inline]
fn mul_add_mul_assign(&mut self, y: $t, z: $t, w: $t) {
*self = self.wrapping_mul(y).wrapping_add(z.wrapping_mul(w));
}
}
};
}
apply_to_primitive_ints!(impl_mul_add_mul_primitive_int);