-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e39b8f5
commit 7ffeace
Showing
5 changed files
with
42 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
use u256::U256; | ||
use u256::{U256, U512}; | ||
|
||
use crate::address_operands::{address_operands_read, address_operands_store}; | ||
use crate::{opcode::Opcode, state::VMState}; | ||
|
||
pub fn _mul(vm: &mut VMState, opcode: &Opcode) { | ||
let (src0, src1) = address_operands_read(vm, &opcode); | ||
let src0 = U512::from(src0); | ||
let src1 = U512::from(src1); | ||
let res = src0 * src1; | ||
|
||
let max = U256::max_value(); | ||
let low_mask = U256::from(max.low_u128()); | ||
let high_mask = !low_mask & max; | ||
let u256_mask = U512::from(U256::MAX); | ||
let low_bits = res & u256_mask; | ||
let high_bits = res >> 256 & u256_mask; | ||
|
||
let (res, overflow) = src0.overflowing_mul(src1); | ||
if opcode.alters_vm_flags { | ||
// If overflow, set the flag. | ||
// otherwise keep the current value. | ||
// Lt overflow, is set if | ||
// src0 * src1 >= 2^256 | ||
let overflow = res >= U512::from(U256::MAX); | ||
vm.flag_lt_of |= overflow; | ||
// Set eq if res == 0 | ||
vm.flag_eq |= res.is_zero(); | ||
// Eq is set if res_low == 0 | ||
vm.flag_eq |= low_bits.is_zero(); | ||
// Gt is set if both of lt_of and eq are cleared. | ||
vm.flag_gt |= !vm.flag_lt_of && !vm.flag_eq; | ||
} | ||
|
||
let low_bits = res & low_mask; | ||
let high_bits = res & high_mask; | ||
address_operands_store(vm, &opcode, (low_bits, Some(high_bits))); | ||
address_operands_store( | ||
vm, | ||
&opcode, | ||
( | ||
low_bits.try_into().unwrap(), | ||
Some(high_bits.try_into().unwrap()), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters