Skip to content

Commit

Permalink
uint: make max_value const, test, simplify (#652)
Browse files Browse the repository at this point in the history
* Make max_value const, test and simplify

* Deprecate max_value

* Test ::MAX

* Update uint/src/uint.rs

Co-authored-by: Andronik <[email protected]>

* Apply suggestions from code review

Co-authored-by: Andronik <[email protected]>

Co-authored-by: Andronik <[email protected]>
  • Loading branch information
webmaster128 and ordian authored Aug 14, 2022
1 parent 55d5fe5 commit 6153693
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
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. [#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
14 changes: 5 additions & 9 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,8 @@ macro_rules! construct_uint {

/// The maximum value which can be inhabited by 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 @@ -1063,10 +1059,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 @@ -1116,7 +1112,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

0 comments on commit 6153693

Please sign in to comment.