use super::i256::{i256_div, i256_mod};
use crate::{
interpreter_types::{InterpreterTypes, StackTr},
InstructionContext,
};
use context_interface::Host;
use primitives::U256;
pub fn add<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
*op2 = op1.wrapping_add(*op2);
}
pub fn mul<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
*op2 = op1.wrapping_mul(*op2);
}
pub fn sub<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
*op2 = op1.wrapping_sub(*op2);
}
pub fn div<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
if !op2.is_zero() {
*op2 = op1.wrapping_div(*op2);
}
}
pub fn sdiv<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
*op2 = i256_div(op1, *op2);
}
pub fn rem<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
if !op2.is_zero() {
*op2 = op1.wrapping_rem(*op2);
}
}
pub fn smod<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
*op2 = i256_mod(op1, *op2)
}
pub fn addmod<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1, op2], op3, context.interpreter);
*op3 = op1.add_mod(op2, *op3)
}
pub fn mulmod<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1, op2], op3, context.interpreter);
*op3 = op1.mul_mod(op2, *op3)
}
pub fn exp<WIRE: InterpreterTypes, H: Host + ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([op1], op2, context.interpreter);
gas!(
context.interpreter,
context.host.gas_params().exp_cost(*op2)
);
*op2 = op1.pow(*op2);
}
pub fn signextend<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([ext], x, context.interpreter);
if ext < U256::from(31) {
let ext = ext.as_limbs()[0];
let bit_index = (8 * ext + 7) as usize;
let bit = x.bit(bit_index);
let mask = (U256::from(1) << bit_index) - U256::from(1);
*x = if bit { *x | !mask } else { *x & mask };
}
}