Skip to content

interpret: move saturating_add/sub into (pub) helper method #94685

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

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
use singed_int_max/min helper methods
  • Loading branch information
RalfJung committed Mar 7, 2022
commit ac844986d8f5646b686e2a5619e5a90372953b38
11 changes: 4 additions & 7 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,23 +493,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
// Negative overflow not possible since the positive first term
// can only increase an (in range) negative term for addition
// or corresponding negated positive term for subtraction
Scalar::from_uint(
(1u128 << (num_bits - 1)) - 1, // max positive
Size::from_bits(num_bits),
)
Scalar::from_int(size.signed_int_max(), size)
} else {
// Positive overflow not possible for similar reason
// max negative
Scalar::from_uint(1u128 << (num_bits - 1), Size::from_bits(num_bits))
Scalar::from_int(size.signed_int_min(), size)
}
} else {
// unsigned
if matches!(mir_op, BinOp::Add) {
// max unsigned
Scalar::from_uint(size.unsigned_int_max(), Size::from_bits(num_bits))
Scalar::from_uint(size.unsigned_int_max(), size)
} else {
// underflow to 0
Scalar::from_uint(0u128, Size::from_bits(num_bits))
Scalar::from_uint(0u128, size)
}
}
} else {
Expand Down