Skip to content

Commit

Permalink
implement binary float instruction execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Aug 22, 2023
1 parent 83a8f48 commit 8c67ec3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
33 changes: 17 additions & 16 deletions crates/wasmi/src/engine/regmach/executor/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
use core::cmp;
use wasmi_core::UntypedValue;

mod binary;
mod call;
mod comparison;
mod conversion;
Expand Down Expand Up @@ -751,22 +752,22 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
Instr::F64Trunc(instr) => self.execute_f64_trunc(instr),
Instr::F64Nearest(instr) => self.execute_f64_nearest(instr),
Instr::F64Sqrt(instr) => self.execute_f64_sqrt(instr),
Instr::F32Add(_) => todo!(),
Instr::F32Sub(_) => todo!(),
Instr::F32Mul(_) => todo!(),
Instr::F32Div(_) => todo!(),
Instr::F32Min(_) => todo!(),
Instr::F32Max(_) => todo!(),
Instr::F32Copysign(_) => todo!(),
Instr::F32CopysignImm(_) => todo!(),
Instr::F64Add(_) => todo!(),
Instr::F64Sub(_) => todo!(),
Instr::F64Mul(_) => todo!(),
Instr::F64Div(_) => todo!(),
Instr::F64Min(_) => todo!(),
Instr::F64Max(_) => todo!(),
Instr::F64Copysign(_) => todo!(),
Instr::F64CopysignImm(_) => todo!(),
Instr::F32Add(instr) => self.execute_f32_add(instr),
Instr::F32Sub(instr) => self.execute_f32_sub(instr),
Instr::F32Mul(instr) => self.execute_f32_mul(instr),
Instr::F32Div(instr) => self.execute_f32_div(instr),
Instr::F32Min(instr) => self.execute_f32_min(instr),
Instr::F32Max(instr) => self.execute_f32_max(instr),
Instr::F32Copysign(instr) => self.execute_f32_copysign(instr),
Instr::F32CopysignImm(instr) => self.execute_f32_copysign_imm(instr),
Instr::F64Add(instr) => self.execute_f64_add(instr),
Instr::F64Sub(instr) => self.execute_f64_sub(instr),
Instr::F64Mul(instr) => self.execute_f64_mul(instr),
Instr::F64Div(instr) => self.execute_f64_div(instr),
Instr::F64Min(instr) => self.execute_f64_min(instr),
Instr::F64Max(instr) => self.execute_f64_max(instr),
Instr::F64Copysign(instr) => self.execute_f64_copysign(instr),
Instr::F64CopysignImm(instr) => self.execute_f64_copysign_imm(instr),
Instr::I32WrapI64(instr) => self.execute_i32_wrap_i64(instr),
Instr::I64ExtendI32S(instr) => self.execute_i64_extend_i32_s(instr),
Instr::I64ExtendI32U(instr) => self.execute_i64_extend_i32_u(instr),
Expand Down
64 changes: 64 additions & 0 deletions crates/wasmi/src/engine/regmach/executor/instrs/binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::Executor;
use crate::{
core::UntypedValue,
engine::bytecode2::{BinInstr, CopysignImmInstr, Sign},
};

#[cfg(doc)]
use crate::engine::bytecode2::Instruction;

macro_rules! impl_binary {
( $( (Instruction::$var_name:ident, $fn_name:ident, $op:expr) ),* $(,)? ) => {
$(
#[doc = concat!("Executes an [`Instruction::", stringify!($var_name), "`].")]
#[inline(always)]
pub fn $fn_name(&mut self, instr: BinInstr) {
self.execute_binary(instr, $op)
}
)*
};
}

impl<'ctx, 'engine> Executor<'ctx, 'engine> {
impl_binary! {
(Instruction::F32Add, execute_f32_add, UntypedValue::f32_add),
(Instruction::F32Sub, execute_f32_sub, UntypedValue::f32_sub),
(Instruction::F32Mul, execute_f32_mul, UntypedValue::f32_mul),
(Instruction::F32Div, execute_f32_div, UntypedValue::f32_div),
(Instruction::F32Min, execute_f32_min, UntypedValue::f32_min),
(Instruction::F32Max, execute_f32_max, UntypedValue::f32_max),
(Instruction::F32Copysign, execute_f32_copysign, UntypedValue::f32_copysign),

(Instruction::F64Add, execute_f64_add, UntypedValue::f64_add),
(Instruction::F64Sub, execute_f64_sub, UntypedValue::f64_sub),
(Instruction::F64Mul, execute_f64_mul, UntypedValue::f64_mul),
(Instruction::F64Div, execute_f64_div, UntypedValue::f64_div),
(Instruction::F64Min, execute_f64_min, UntypedValue::f64_min),
(Instruction::F64Max, execute_f64_max, UntypedValue::f64_max),
(Instruction::F64Copysign, execute_f64_copysign, UntypedValue::f64_copysign),
}

/// Executes an [`Instruction::F32CopysignImm`].
#[inline(always)]
pub fn execute_f32_copysign_imm(&mut self, instr: CopysignImmInstr) {
let lhs = self.get_register(instr.lhs);
let rhs = match instr.rhs {
Sign::Pos => 1.0_f32,
Sign::Neg => -1.0_f32,
};
self.set_register(instr.result, UntypedValue::f32_copysign(lhs, rhs.into()));
self.next_instr()
}

/// Executes an [`Instruction::F64CopysignImm`].
#[inline(always)]
pub fn execute_f64_copysign_imm(&mut self, instr: CopysignImmInstr) {
let lhs = self.get_register(instr.lhs);
let rhs = match instr.rhs {
Sign::Pos => 1.0_f64,
Sign::Neg => -1.0_f64,
};
self.set_register(instr.result, UntypedValue::f64_copysign(lhs, rhs.into()));
self.next_instr()
}
}

0 comments on commit 8c67ec3

Please sign in to comment.