/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bnum-0.12.1/src/bint/saturating.rs
Line | Count | Source |
1 | | use crate::{doc, ExpType}; |
2 | | |
3 | | macro_rules! saturating { |
4 | | ($BUint: ident, $BInt: ident, $Digit: ident) => { |
5 | | #[doc = doc::saturating::impl_desc!()] |
6 | | impl<const N: usize> $BInt<N> { |
7 | | #[doc = doc::saturating::saturating_add!(I)] |
8 | | #[must_use = doc::must_use_op!()] |
9 | | #[inline] |
10 | 0 | pub const fn saturating_add(self, rhs: Self) -> Self { |
11 | 0 | match self.checked_add(rhs) { |
12 | 0 | Some(add) => add, |
13 | | None => { |
14 | 0 | if self.is_negative() { |
15 | 0 | Self::MIN |
16 | | } else { |
17 | 0 | Self::MAX |
18 | | } |
19 | | } |
20 | | } |
21 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_add Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_add Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_add Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_add |
22 | | |
23 | | #[doc = doc::saturating::saturating_add_unsigned!(I)] |
24 | | #[must_use = doc::must_use_op!()] |
25 | | #[inline] |
26 | 0 | pub const fn saturating_add_unsigned(self, rhs: $BUint<N>) -> Self { |
27 | 0 | match self.checked_add_unsigned(rhs) { |
28 | 0 | Some(i) => i, |
29 | 0 | None => Self::MAX, |
30 | | } |
31 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_add_unsigned Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_add_unsigned Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_add_unsigned Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_add_unsigned |
32 | | |
33 | | #[doc = doc::saturating::saturating_sub!(I)] |
34 | | #[must_use = doc::must_use_op!()] |
35 | | #[inline] |
36 | 0 | pub const fn saturating_sub(self, rhs: Self) -> Self { |
37 | 0 | match self.checked_sub(rhs) { |
38 | 0 | Some(add) => add, |
39 | | None => { |
40 | 0 | if self.is_negative() { |
41 | 0 | Self::MIN |
42 | | } else { |
43 | 0 | Self::MAX |
44 | | } |
45 | | } |
46 | | } |
47 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_sub Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_sub Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_sub Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_sub |
48 | | |
49 | | #[doc = doc::saturating::saturating_sub_unsigned!(I)] |
50 | | #[must_use = doc::must_use_op!()] |
51 | | #[inline] |
52 | 0 | pub const fn saturating_sub_unsigned(self, rhs: $BUint<N>) -> Self { |
53 | 0 | match self.checked_sub_unsigned(rhs) { |
54 | 0 | Some(i) => i, |
55 | 0 | None => Self::MIN, |
56 | | } |
57 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_sub_unsigned Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_sub_unsigned Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_sub_unsigned Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_sub_unsigned |
58 | | |
59 | | #[doc = doc::saturating::saturating_mul!(I)] |
60 | | #[must_use = doc::must_use_op!()] |
61 | | #[inline] |
62 | 0 | pub const fn saturating_mul(self, rhs: Self) -> Self { |
63 | 0 | match self.checked_mul(rhs) { |
64 | 0 | Some(mul) => mul, |
65 | | None => { |
66 | 0 | if self.is_negative() == rhs.is_negative() { |
67 | 0 | Self::MAX |
68 | | } else { |
69 | 0 | Self::MIN |
70 | | } |
71 | | } |
72 | | } |
73 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_mul Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_mul Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_mul Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_mul |
74 | | |
75 | | #[doc = doc::saturating::saturating_div!(I)] |
76 | | #[must_use = doc::must_use_op!()] |
77 | | #[inline] |
78 | 0 | pub const fn saturating_div(self, rhs: Self) -> Self { |
79 | 0 | let (div, overflow) = self.overflowing_div(rhs); |
80 | 0 | if overflow { |
81 | 0 | Self::MAX |
82 | | } else { |
83 | 0 | div |
84 | | } |
85 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_div Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_div Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_div Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_div |
86 | | |
87 | | #[doc = doc::saturating::saturating_neg!(I)] |
88 | | #[must_use = doc::must_use_op!()] |
89 | | #[inline] |
90 | 0 | pub const fn saturating_neg(self) -> Self { |
91 | 0 | match self.checked_neg() { |
92 | 0 | Some(abs) => abs, |
93 | 0 | None => Self::MAX, |
94 | | } |
95 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_neg Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_neg Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_neg Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_neg |
96 | | |
97 | | #[doc = doc::saturating::saturating_abs!(I)] |
98 | | #[must_use = doc::must_use_op!()] |
99 | | #[inline] |
100 | 0 | pub const fn saturating_abs(self) -> Self { |
101 | 0 | match self.checked_abs() { |
102 | 0 | Some(abs) => abs, |
103 | 0 | None => Self::MAX, |
104 | | } |
105 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_abs Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_abs Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_abs Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_abs |
106 | | |
107 | | #[doc = doc::saturating::saturating_pow!(I)] |
108 | | #[must_use = doc::must_use_op!()] |
109 | | #[inline] |
110 | 0 | pub const fn saturating_pow(self, exp: ExpType) -> Self { |
111 | 0 | match self.checked_pow(exp) { |
112 | 0 | Some(pow) => pow, |
113 | | None => { |
114 | 0 | if self.is_negative() && exp & 1 != 0 { |
115 | 0 | Self::MIN |
116 | | } else { |
117 | 0 | Self::MAX |
118 | | } |
119 | | } |
120 | | } |
121 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_>>::saturating_pow Unexecuted instantiation: <bnum::bint::BIntD16<_>>::saturating_pow Unexecuted instantiation: <bnum::bint::BIntD8<_>>::saturating_pow Unexecuted instantiation: <bnum::bint::BIntD32<_>>::saturating_pow |
122 | | } |
123 | | }; |
124 | | } |
125 | | |
126 | | #[cfg(test)] |
127 | | crate::test::all_digit_tests! { |
128 | | use crate::test::{test_bignum, types::*}; |
129 | | |
130 | | test_bignum! { |
131 | | function: <itest>::saturating_add(a: itest, b: itest) |
132 | | } |
133 | | test_bignum! { |
134 | | function: <itest>::saturating_add_unsigned(a: itest, b: utest) |
135 | | } |
136 | | test_bignum! { |
137 | | function: <itest>::saturating_sub(a: itest, b: itest) |
138 | | } |
139 | | test_bignum! { |
140 | | function: <itest>::saturating_sub_unsigned(a: itest, b: utest) |
141 | | } |
142 | | test_bignum! { |
143 | | function: <itest>::saturating_div(a: itest, b: itest), |
144 | | skip: b == 0, |
145 | | cases: [ |
146 | | (itest::MIN, -1i8) |
147 | | ] |
148 | | } |
149 | | test_bignum! { |
150 | | function: <itest>::saturating_neg(a: itest), |
151 | | cases: [ |
152 | | (itest::MIN) |
153 | | ] |
154 | | } |
155 | | test_bignum! { |
156 | | function: <itest>::saturating_abs(a: itest), |
157 | | cases: [ |
158 | | (itest::MIN) |
159 | | ] |
160 | | } |
161 | | test_bignum! { |
162 | | function: <itest>::saturating_mul(a: itest, b: itest) |
163 | | } |
164 | | test_bignum! { |
165 | | function: <itest>::saturating_pow(a: itest, b: u16) |
166 | | } |
167 | | } |
168 | | |
169 | | crate::macro_impl!(saturating); |