wasm_interp: implement float neg and abs

This commit is contained in:
Brian Carroll 2022-11-28 21:17:31 +00:00
parent 284eeb5537
commit 1e114d6eef
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
4 changed files with 56 additions and 12 deletions

View file

@ -1043,8 +1043,14 @@ impl<'a> Instance<'a> {
self.value_stack.push(Value::from(arg1.rotate_right(k))); self.value_stack.push(Value::from(arg1.rotate_right(k)));
} }
F32ABS => todo!("{:?} @ {:#x}", op_code, file_offset), F32ABS => {
F32NEG => todo!("{:?} @ {:#x}", op_code, file_offset), let arg = self.value_stack.pop_f32();
self.value_stack.push(Value::F32(arg.abs()));
}
F32NEG => {
let arg = self.value_stack.pop_f32();
self.value_stack.push(Value::F32(-arg));
}
F32CEIL => todo!("{:?} @ {:#x}", op_code, file_offset), F32CEIL => todo!("{:?} @ {:#x}", op_code, file_offset),
F32FLOOR => todo!("{:?} @ {:#x}", op_code, file_offset), F32FLOOR => todo!("{:?} @ {:#x}", op_code, file_offset),
F32TRUNC => todo!("{:?} @ {:#x}", op_code, file_offset), F32TRUNC => todo!("{:?} @ {:#x}", op_code, file_offset),
@ -1074,8 +1080,14 @@ impl<'a> Instance<'a> {
F32MAX => todo!("{:?} @ {:#x}", op_code, file_offset), F32MAX => todo!("{:?} @ {:#x}", op_code, file_offset),
F32COPYSIGN => todo!("{:?} @ {:#x}", op_code, file_offset), F32COPYSIGN => todo!("{:?} @ {:#x}", op_code, file_offset),
F64ABS => todo!("{:?} @ {:#x}", op_code, file_offset), F64ABS => {
F64NEG => todo!("{:?} @ {:#x}", op_code, file_offset), let arg = self.value_stack.pop_f64();
self.value_stack.push(Value::F64(arg.abs()));
}
F64NEG => {
let arg = self.value_stack.pop_f64();
self.value_stack.push(Value::F64(-arg));
}
F64CEIL => todo!("{:?} @ {:#x}", op_code, file_offset), F64CEIL => todo!("{:?} @ {:#x}", op_code, file_offset),
F64FLOOR => todo!("{:?} @ {:#x}", op_code, file_offset), F64FLOOR => todo!("{:?} @ {:#x}", op_code, file_offset),
F64TRUNC => todo!("{:?} @ {:#x}", op_code, file_offset), F64TRUNC => todo!("{:?} @ {:#x}", op_code, file_offset),

View file

@ -68,11 +68,11 @@ where
// Dump the generated module to a file (this is mainly for debugging the test itself) // Dump the generated module to a file (this is mainly for debugging the test itself)
if std::env::var("DEBUG_WASM_INTERP_TEST").is_ok() { if std::env::var("DEBUG_WASM_INTERP_TEST").is_ok() {
let filename = format!("/tmp/{:?}.wasm", op);
println!("\nDumping test module to {}\n", &filename);
let mut outfile_buf = Vec::new_in(&arena); let mut outfile_buf = Vec::new_in(&arena);
module.serialize(&mut outfile_buf); module.serialize(&mut outfile_buf);
let filename = format!("/tmp/roc/{:?}.wasm", op);
std::fs::write(&filename, outfile_buf).unwrap(); std::fs::write(&filename, outfile_buf).unwrap();
println!("\nWrote to {}\n", &filename);
} }
let mut state = Instance::for_module(&arena, &module, "test", true, []).unwrap(); let mut state = Instance::for_module(&arena, &module, "test", true, []).unwrap();

View file

@ -19,9 +19,9 @@ fn test_f32_binop(op: OpCode, arg1: f32, arg2: f32, expected: f32) {
) )
} }
// fn test_f32_unop(op: OpCode, arg: f32, expected: f32) { fn test_f32_unop(op: OpCode, arg: f32, expected: f32) {
// test_op_example(op, [Value::F32(arg)], Value::F32(expected)) test_op_example(op, [Value::F32(arg)], Value::F32(expected))
// } }
#[test] #[test]
fn test_f32eq() { fn test_f32eq() {
@ -69,6 +69,22 @@ fn test_f32ge() {
test_f32_comparison(op, -1.1, 1.1, false); test_f32_comparison(op, -1.1, 1.1, false);
} }
#[test]
fn test_f32abs() {
let op = F32ABS;
test_f32_unop(op, 0.0, 0.0);
test_f32_unop(op, 1.1, 1.1);
test_f32_unop(op, -1.1, 1.1);
}
#[test]
fn test_f32neg() {
let op = F32NEG;
test_f32_unop(op, 0.0, 0.0);
test_f32_unop(op, 1.1, -1.1);
test_f32_unop(op, -1.1, 1.1);
}
#[test] #[test]
fn test_f32add() { fn test_f32add() {
let op = F32ADD; let op = F32ADD;

View file

@ -19,9 +19,9 @@ fn test_f64_binop(op: OpCode, arg1: f64, arg2: f64, expected: f64) {
) )
} }
// fn test_f64_unop(op: OpCode, arg: f64, expected: f64) { fn test_f64_unop(op: OpCode, arg: f64, expected: f64) {
// test_op_example(op, [Value::F64(arg)], Value::F64(expected)) test_op_example(op, [Value::F64(arg)], Value::F64(expected))
// } }
#[test] #[test]
fn test_f64eq() { fn test_f64eq() {
@ -69,6 +69,22 @@ fn test_f64ge() {
test_f64_comparison(op, -1.1, 1.1, false); test_f64_comparison(op, -1.1, 1.1, false);
} }
#[test]
fn test_f64abs() {
let op = F64ABS;
test_f64_unop(op, 0.0, 0.0);
test_f64_unop(op, 1.1, 1.1);
test_f64_unop(op, -1.1, 1.1);
}
#[test]
fn test_f64neg() {
let op = F64NEG;
test_f64_unop(op, 0.0, 0.0);
test_f64_unop(op, 1.1, -1.1);
test_f64_unop(op, -1.1, 1.1);
}
#[test] #[test]
fn test_f64add() { fn test_f64add() {
let op = F64ADD; let op = F64ADD;