Skip to content

Commit

Permalink
chore(interpreter): use U256::arithmetic_shr in SAR (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jun 15, 2024
1 parent b988a0f commit af6797e
Showing 1 changed file with 5 additions and 18 deletions.
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

0 comments on commit af6797e

Please sign in to comment.