Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uint: make max_value const, test, simplify #652

Merged
merged 5 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions uint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog].

## [Unreleased]
- Make `one` const. [#650](https://github.com/paritytech/parity-common/pull/650)
- Make `max_value` const and deprecate it. [#652](https://github.com/paritytech/parity-common/pull/652)

## [0.9.3] - 2022-02-04
- Simplified and faster `div_mod`. [#478](https://github.com/paritytech/parity-common/pull/478)
Expand Down
8 changes: 4 additions & 4 deletions uint/benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn u256_sub(c: &mut Criterion) {
black_box(x.overflowing_sub(y).0)
})
},
vec![(U256::max_value(), 1u64), (U256::from(3), 2)],
vec![(U256::MAX, 1u64), (U256::from(3), 2)],
),
);
}
Expand All @@ -159,7 +159,7 @@ fn u256_mul(c: &mut Criterion) {
})
},
vec![
(U256::max_value(), 1u64),
(U256::MAX, 1u64),
(U256::from(3), u64::max_value()),
(U256::from_dec_str("21674844646682989462120101885968193938394323990565507610662749").unwrap(), 173),
],
Expand All @@ -179,7 +179,7 @@ fn u512_div_mod(c: &mut Criterion) {
})
},
vec![
(U512::max_value(), U512::from(1u64)),
(U512::MAX, U512::from(1u64)),
(U512::from(u64::max_value()), U512::from(u32::max_value())),
(U512::from(u64::max_value()), U512::from(u64::max_value() - 1)),
(U512::from(u64::max_value()), U512::from(u64::max_value() - 1)),
Expand Down Expand Up @@ -240,7 +240,7 @@ fn u256_rem(c: &mut Criterion) {
"",
|b, (x, y)| b.iter(|| black_box(x % y)),
vec![
(U256::max_value(), U256::from(1u64)),
(U256::MAX, U256::from(1u64)),
(U256::from(u64::max_value()), U256::from(u64::from(u32::max_value()) + 1)),
(
U256([12767554894655550452, 16333049135534778834, 140317443000293558, 598963]),
Expand Down
15 changes: 6 additions & 9 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,10 @@ macro_rules! construct_uint {
}

/// The maximum value which can be inhabited by this type.
#[deprecated(note = "replaced by the `MAX` associated constant on this type")]
#[inline]
pub fn max_value() -> Self {
let mut result = [0; $n_words];
for i in 0..$n_words {
result[i] = u64::max_value();
}
$name(result)
pub const fn max_value() -> Self {
Self::MAX
}

fn full_shl(self, shift: u32) -> [u64; $n_words + 1] {
Expand Down Expand Up @@ -1071,10 +1068,10 @@ macro_rules! construct_uint {
)
}

/// Addition which saturates at the maximum value (Self::max_value()).
/// Addition which saturates at the maximum value (Self::MAX).
pub fn saturating_add(self, other: $name) -> $name {
match self.overflowing_add(other) {
(_, true) => $name::max_value(),
(_, true) => $name::MAX,
(val, false) => val,
}
}
Expand Down Expand Up @@ -1124,7 +1121,7 @@ macro_rules! construct_uint {
/// Multiplication which saturates at the maximum value..
pub fn saturating_mul(self, other: $name) -> $name {
match self.overflowing_mul(other) {
(_, true) => $name::max_value(),
(_, true) => $name::MAX,
(val, false) => val,
}
}
Expand Down
45 changes: 44 additions & 1 deletion uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ fn const_matching_works() {
}
}

#[test]
fn max() {
let max = U256::MAX;
assert_eq!(max.0, [0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF]);

let max = U512::MAX;
assert_eq!(
max.0,
[
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF
]
);
}

#[test]
fn one() {
let one = U256::one();
Expand All @@ -63,6 +84,28 @@ fn one() {
assert_eq!(any * U512::one(), any);
}

#[test]
#[allow(deprecated)]
fn max_value() {
let max = U256::max_value();
assert_eq!(max.0, [0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF]);

let max = U512::max_value();
assert_eq!(
max.0,
[
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF
]
);
}

#[test]
fn u128_conversions() {
let mut a = U256::from(u128::max_value());
Expand All @@ -85,7 +128,7 @@ fn uint256_checked_ops() {
assert_eq!(U256::from(10).checked_pow(U256::from(3)), Some(U256::from(1000)));
assert_eq!(U256::from(10).checked_pow(U256::from(20)), Some(U256::exp10(20)));
assert_eq!(U256::from(2).checked_pow(U256::from(0x100)), None);
assert_eq!(U256::max_value().checked_pow(U256::from(2)), None);
assert_eq!(U256::MAX.checked_pow(U256::from(2)), None);

assert_eq!(a.checked_add(b), None);
assert_eq!(a.checked_add(a), Some(20.into()));
Expand Down