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

chore(interpreter): use U256::arithmetic_shr in SAR #1525

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Changes from all 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
23 changes: 5 additions & 18 deletions crates/interpreter/src/instructions/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,12 @@ pub fn sar<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, _host: &
pop_top!(interpreter, op1, op2);

let shift = as_usize_saturated!(op1);
*op2 = if shift >= 256 {
// If the shift is 256 or more, the result depends on the sign of the last bit.
if op2.bit(255) {
U256::MAX // Negative number, all bits set to one.
} else {
U256::ZERO // Non-negative number, all bits set to zero.
}
*op2 = if shift < 256 {
op2.arithmetic_shr(shift)
} else if op2.bit(255) {
U256::MAX
} else {
// Normal shift
if op2.bit(255) {
// Check the most significant bit.
// Arithmetic right shift for negative numbers.
let shifted_value = *op2 >> shift;
let mask = U256::MAX << (256 - shift); // Mask for the sign bits.
shifted_value | mask // Apply the mask to simulate the filling of sign bits.
} else {
// Logical right shift for non-negative numbers.
*op2 >> shift
}
U256::ZERO
};
}

Expand Down
Loading