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
use num::arithmetic::traits::XXXSubYYYToZZZ;
use num::basic::integers::PrimitiveInt;
use num::basic::unsigneds::PrimitiveUnsigned;
use num::conversion::traits::WrappingFrom;
pub_test! {xxx_sub_yyy_to_zzz<T: PrimitiveUnsigned>(
x_2: T,
x_1: T,
x_0: T,
y_2: T,
y_1: T,
y_0: T,
) -> (T, T, T) {
let (z_0, borrow_1) = x_0.overflowing_sub(y_0);
let (mut z_1, mut borrow_2) = x_1.overflowing_sub(y_1);
if borrow_1 {
borrow_2 |= z_1.overflowing_sub_assign(T::ONE);
}
let mut z_2 = x_2.wrapping_sub(y_2);
if borrow_2 {
z_2.wrapping_sub_assign(T::ONE);
}
(z_2, z_1, z_0)
}}
macro_rules! impl_xxx_sub_yyy_to_zzz {
($t:ident) => {
impl XXXSubYYYToZZZ for $t {
/// Subtracts two numbers, each composed of three `Self` values, returning the
/// difference as a triple of `Self` values.
///
/// The more significant value always comes first. Subtraction is wrapping, and
/// overflow is not indicated.
///
/// $$
/// f(x_2, x_1, x_0, y_2, y_1, y_0) = (z_2, z_1, z_0),
/// $$
/// where $W$ is `Self::WIDTH`,
///
/// $x_2, x_1, x_0, y_2, y_1, y_0, z_2, z_1, z_0 < 2^W$, and
/// $$
/// (2^{2W}x_2 + 2^Wx_1 + x_0) - (2^{2W}y_2 + 2^Wy_1 + y_0)
/// \equiv 2^{2W}z_2 + 2^Wz_1 + z_0 \mod 2^{3W}.
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::xxx_sub_yyy_to_zzz#xxx_sub_yyy_to_zzz).
///
/// This is equivalent to `sub_dddmmmsss` from `longlong.h`, FLINT 2.7.1, where
/// `(dh, dm, dl)` is returned.
#[inline]
fn xxx_sub_yyy_to_zzz(
x_2: $t,
x_1: $t,
x_0: $t,
y_2: $t,
y_1: $t,
y_0: $t,
) -> ($t, $t, $t) {
xxx_sub_yyy_to_zzz::<$t>(x_2, x_1, x_0, y_2, y_1, y_0)
}
}
};
}
impl_xxx_sub_yyy_to_zzz!(u8);
impl_xxx_sub_yyy_to_zzz!(u16);
impl_xxx_sub_yyy_to_zzz!(u32);
impl_xxx_sub_yyy_to_zzz!(u64);
impl_xxx_sub_yyy_to_zzz!(u128);
impl XXXSubYYYToZZZ for usize {
/// Subtracts two numbers, each composed of three [`usize`] values, returning the difference as
/// a triple of [`usize`] values.
///
/// The more significant value always comes first. Subtraction is wrapping, and overflow is not
/// indicated.
///
/// $$
/// f(x_2, x_1, x_0, y_2, y_1, y_0) = (z_2, z_1, z_0),
/// $$
/// where $W$ is `Self::WIDTH`,
///
/// $x_2, x_1, x_0, y_2, y_1, y_0, z_2, z_1, z_0 < 2^W$, and
/// $$
/// (2^{2W}x_2 + 2^Wx_1 + x_0) - (2^{2W}y_2 + 2^Wy_1 + y_0)
/// \equiv 2^{2W}z_2 + 2^Wz_1 + z_0 \mod 2^{3W}.
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::xxx_sub_yyy_to_zzz#xxx_sub_yyy_to_zzz).
///
/// This is equivalent to `sub_dddmmmsss` from `longlong.h`, FLINT 2.7.1, where `(dh, dm, dl)`
/// is returned.
fn xxx_sub_yyy_to_zzz(
x_2: usize,
x_1: usize,
x_0: usize,
y_2: usize,
y_1: usize,
y_0: usize,
) -> (usize, usize, usize) {
if usize::WIDTH == u32::WIDTH {
let (z_2, z_1, z_0) = u32::xxx_sub_yyy_to_zzz(
u32::wrapping_from(x_2),
u32::wrapping_from(x_1),
u32::wrapping_from(x_0),
u32::wrapping_from(y_2),
u32::wrapping_from(y_1),
u32::wrapping_from(y_0),
);
(
usize::wrapping_from(z_2),
usize::wrapping_from(z_1),
usize::wrapping_from(z_0),
)
} else {
let (z_2, z_1, z_0) = u64::xxx_sub_yyy_to_zzz(
u64::wrapping_from(x_2),
u64::wrapping_from(x_1),
u64::wrapping_from(x_0),
u64::wrapping_from(y_2),
u64::wrapping_from(y_1),
u64::wrapping_from(y_0),
);
(
usize::wrapping_from(z_2),
usize::wrapping_from(z_1),
usize::wrapping_from(z_0),
)
}
}
}