Skip to content

Commit

Permalink
Merge branch 'main' into refactor/opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
damiramirez committed Dec 3, 2024
2 parents 56cc3ee + 1c32950 commit 156133e
Showing 1 changed file with 14 additions and 32 deletions.
46 changes: 14 additions & 32 deletions crates/vm/levm/src/opcode_handlers/bitwise_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::LT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = if lho < rho { U256::one() } else { U256::zero() };
let result = u256_from_bool(lho < rho);
current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand All @@ -27,7 +27,7 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::GT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = if lho > rho { U256::one() } else { U256::zero() };
let result = u256_from_bool(lho > rho);
current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand All @@ -42,18 +42,10 @@ impl VM {
let rho_is_negative = rho.bit(255);
let result = if lho_is_negative == rho_is_negative {
// Compare magnitudes if signs are the same
if lho < rho {
U256::one()
} else {
U256::zero()
}
u256_from_bool(lho < rho)
} else {
// Negative is smaller if signs differ
if lho_is_negative {
U256::one()
} else {
U256::zero()
}
u256_from_bool(lho_is_negative)
};
current_call_frame.stack.push(result)?;

Expand All @@ -69,18 +61,10 @@ impl VM {
let rho_is_negative = rho.bit(255);
let result = if lho_is_negative == rho_is_negative {
// Compare magnitudes if signs are the same
if lho > rho {
U256::one()
} else {
U256::zero()
}
u256_from_bool(lho > rho)
} else {
// Positive is bigger if signs differ
if rho_is_negative {
U256::one()
} else {
U256::zero()
}
u256_from_bool(rho_is_negative)
};
current_call_frame.stack.push(result)?;

Expand All @@ -92,11 +76,8 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::EQ)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = if lho == rho {
U256::one()
} else {
U256::zero()
};
let result = u256_from_bool(lho == rho);

current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand All @@ -110,11 +91,8 @@ impl VM {
self.increase_consumed_gas(current_call_frame, gas_cost::ISZERO)?;

let operand = current_call_frame.stack.pop()?;
let result = if operand == U256::zero() {
U256::one()
} else {
U256::zero()
};
let result = u256_from_bool(operand.is_zero());

current_call_frame.stack.push(result)?;

Ok(OpcodeSuccess::Continue)
Expand Down Expand Up @@ -321,3 +299,7 @@ fn checked_shift_right(value: U256, shift: U256) -> Result<U256, VMError> {

Ok(result)
}

fn u256_from_bool(value: bool) -> U256 {
U256::from(u8::from(value))
}

0 comments on commit 156133e

Please sign in to comment.