Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 500e8db

Browse files
committedOct 2, 2024
Add comments. Fix spacing
1 parent af6942e commit 500e8db

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed
 

Diff for: ‎library/core/src/num/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,9 @@ from_str_radix_size_impl! { i64 isize, u64 usize }
15911591
mod verify {
15921592
use super::*;
15931593

1594+
// Verify `unchecked_{add, sub, mul}`
1595+
// However, `unchecked_mul` harnesses have bad performance, so
1596+
// recommend to use generate_unchecked_mul_harness! to set input limits
15941597
macro_rules! generate_unchecked_math_harness {
15951598
($type:ty, $method:ident, $harness_name:ident) => {
15961599
#[kani::proof_for_contract($type::$method)]
@@ -1605,14 +1608,15 @@ mod verify {
16051608
}
16061609
}
16071610

1611+
// Improve unchecked_mul performance for {32, 64, 128}-bit integer types
1612+
// by adding upper and lower limits for inputs
16081613
macro_rules! generate_unchecked_mul_harness {
16091614
($type:ty, $method:ident, $harness_name:ident, $min:expr, $max:expr) => {
16101615
#[kani::proof_for_contract($type::$method)]
16111616
pub fn $harness_name() {
1612-
let num1: $type = kani::any();
1613-
let num2: $type = kani::any();
1614-
1615-
// Limit the values of num1 and num2 to the specified range for multiplication
1617+
let num1: $type = kani::any::<$type>();
1618+
let num2: $type = kani::any::<$type>();
1619+
16161620
kani::assume(num1 >= $min && num1 <= $max);
16171621
kani::assume(num2 >= $min && num2 <= $max);
16181622

@@ -1622,8 +1626,8 @@ mod verify {
16221626
}
16231627
}
16241628
}
1625-
16261629

1630+
// Verify `unchecked_{shl, shr}`
16271631
macro_rules! generate_unchecked_shift_harness {
16281632
($type:ty, $method:ident, $harness_name:ident) => {
16291633
#[kani::proof_for_contract($type::$method)]
@@ -1637,7 +1641,6 @@ mod verify {
16371641
}
16381642
}
16391643
}
1640-
16411644

16421645
macro_rules! generate_unchecked_neg_harness {
16431646
($type:ty, $method:ident, $harness_name:ident) => {
@@ -1699,7 +1702,6 @@ mod verify {
16991702
generate_unchecked_mul_harness!(u128, unchecked_mul, checked_unchecked_mul_u128, 0u128, 1_000_000_000_000_000u128);
17001703
generate_unchecked_mul_harness!(usize, unchecked_mul, checked_unchecked_mul_usize, 0usize, 100_000usize);
17011704

1702-
17031705
// unchecked_shr proofs
17041706
//
17051707
// Target types:
@@ -1722,6 +1724,4 @@ mod verify {
17221724
generate_unchecked_shift_harness!(u64, unchecked_shr, checked_unchecked_shr_u64);
17231725
generate_unchecked_shift_harness!(u128, unchecked_shr, checked_unchecked_shr_u128);
17241726
generate_unchecked_shift_harness!(usize, unchecked_shr, checked_unchecked_shr_usize);
1725-
}
1726-
}
17271727
}

0 commit comments

Comments
 (0)
Please sign in to comment.