fixed_bigint/
patch_num_traits.rs1pub trait OverflowingShl: Sized {
17 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
18}
19pub trait OverflowingShr: Sized {
20 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
21}
22
23macro_rules! overflowing_shift_impl {
24 ($trait_name:ident, $method:ident, $t:ty) => {
25 impl $trait_name for $t {
26 #[inline]
27 fn $method(self, rhs: u32) -> ($t, bool) {
28 <$t>::$method(self, rhs)
29 }
30 }
31 };
32}
33
34overflowing_shift_impl!(OverflowingShl, overflowing_shl, u8);
35overflowing_shift_impl!(OverflowingShl, overflowing_shl, u16);
36overflowing_shift_impl!(OverflowingShl, overflowing_shl, u32);
37overflowing_shift_impl!(OverflowingShl, overflowing_shl, u64);
38
39overflowing_shift_impl!(OverflowingShr, overflowing_shr, u8);
40overflowing_shift_impl!(OverflowingShr, overflowing_shr, u16);
41overflowing_shift_impl!(OverflowingShr, overflowing_shr, u32);
42overflowing_shift_impl!(OverflowingShr, overflowing_shr, u64);
43
44pub trait WideningMul: Sized {
51 fn widening_mul(self, rhs: Self) -> (Self, Self);
55}
56
57macro_rules! widening_mul_impl {
58 ($t:ty, $double:ty, $bits:expr) => {
59 impl WideningMul for $t {
60 #[inline]
61 fn widening_mul(self, rhs: Self) -> (Self, Self) {
62 let product = (self as $double) * (rhs as $double);
63 (product as $t, (product >> $bits) as $t)
64 }
65 }
66 };
67}
68
69widening_mul_impl!(u8, u16, 8);
70widening_mul_impl!(u16, u32, 16);
71widening_mul_impl!(u32, u64, 32);
72widening_mul_impl!(u64, u128, 64);
73
74pub trait CarryingMul: Sized {
81 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self);
86
87 fn carrying_mul_add(self, rhs: Self, addend: Self, carry: Self) -> (Self, Self);
92}
93
94macro_rules! carrying_mul_impl {
95 ($t:ty, $double:ty, $bits:expr) => {
96 impl CarryingMul for $t {
97 #[inline]
98 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
99 let product = (self as $double) * (rhs as $double) + (carry as $double);
100 (product as $t, (product >> $bits) as $t)
101 }
102
103 #[inline]
104 fn carrying_mul_add(self, rhs: Self, addend: Self, carry: Self) -> (Self, Self) {
105 let product =
106 (self as $double) * (rhs as $double) + (addend as $double) + (carry as $double);
107 (product as $t, (product >> $bits) as $t)
108 }
109 }
110 };
111}
112
113carrying_mul_impl!(u8, u16, 8);
114carrying_mul_impl!(u16, u32, 16);
115carrying_mul_impl!(u32, u64, 32);
116carrying_mul_impl!(u64, u128, 64);