Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix canonical_nan and arithmetic_nan tests #4

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spectests/inline_module.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(func) (memory 0) (func (export "f"))
112 changes: 108 additions & 4 deletions src/build_spectests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ impl WastTestGenerator {
dead_code
)]
use crate::webassembly::{{instantiate, compile, ImportObject, ResultObject, VmCtx, Export}};
use super::_common::spectest_importobject;
use std::{{f32, f64}};
use super::_common::{{
spectest_importobject,
NaNCheck,
}};
use wabt::wat2wasm;\n\n",
self.filename
));
Expand All @@ -179,6 +181,7 @@ use wabt::wat2wasm;\n\n",
self.flush_module_calls(n);
}
}

fn command_name(&self) -> String {
format!("c{}_l{}", self.command_no, self.last_line)
}
Expand Down Expand Up @@ -271,6 +274,107 @@ fn {}_assert_invalid() {{
.as_str(),
);
}

fn visit_assert_return_arithmetic_nan(&mut self, action: &Action) {
match action {
Action::Invoke { module, field, args, } => {
let mut return_type = wabt2rust_type(&args[0]);
let mut func_return = format!(" -> {}", return_type);
let assertion = String::from("assert!(result.is_quiet_nan())");

// We map the arguments provided into the raw Arguments provided
// to libffi
let mut args_types: Vec<String> = args.iter().map(wabt2rust_type).collect();
args_types.push("&VmCtx".to_string());
let mut args_values: Vec<String> = args.iter().map(wabt2rust_value).collect();
args_values.push("&vm_context".to_string());
let func_name = format!("{}_assert_return_arithmetic_nan", self.command_name());
self.buffer.push_str(
format!(
"fn {}(result_object: &ResultObject, vm_context: &VmCtx) {{
println!(\"Executing function {{}}\", \"{}\");
let func_index = match result_object.module.info.exports.get({:?}) {{
Some(&Export::Function(index)) => index,
_ => panic!(\"Function not found\"),
}};
let invoke_fn: fn({}){} = get_instance_function!(result_object.instance, func_index);
let result = invoke_fn({});
{}
}}\n",
func_name,
func_name,
field,
args_types.join(", "),
func_return,
args_values.join(", "),
assertion,
)
.as_str(),
);
self.module_calls
.entry(self.last_module)
.or_insert(Vec::new())
.push(func_name);
// let mut module_calls = self.module_calls.get(&self.last_module).unwrap();
// module_calls.push(func_name);
}
_ => {}
};
}

// PROBLEM: Im assuming the return type from the first argument type
// and wabt does gives us the `expected` result
fn visit_assert_return_canonical_nan(&mut self, action: &Action) {
match action {
Action::Invoke { module, field, args, } => {
let mut return_type = match &field.as_str() {
&"f64.promote_f32" => String::from("f64"),
&"f32.promote_f64" => String::from("f32"),
_ => wabt2rust_type(&args[0]),
};
let mut func_return = format!(" -> {}", return_type);
let assertion = String::from("assert!(result.is_quiet_nan())");

// We map the arguments provided into the raw Arguments provided
// to libffi
let mut args_types: Vec<String> = args.iter().map(wabt2rust_type).collect();
args_types.push("&VmCtx".to_string());
let mut args_values: Vec<String> = args.iter().map(wabt2rust_value).collect();
args_values.push("&vm_context".to_string());
let func_name = format!("{}_assert_return_canonical_nan", self.command_name());
self.buffer.push_str(
format!(
"fn {}(result_object: &ResultObject, vm_context: &VmCtx) {{
println!(\"Executing function {{}}\", \"{}\");
let func_index = match result_object.module.info.exports.get({:?}) {{
Some(&Export::Function(index)) => index,
_ => panic!(\"Function not found\"),
}};
let invoke_fn: fn({}){} = get_instance_function!(result_object.instance, func_index);
let result = invoke_fn({});
{}
}}\n",
func_name,
func_name,
field,
args_types.join(", "),
func_return,
args_values.join(", "),
assertion,
)
.as_str(),
);
self.module_calls
.entry(self.last_module)
.or_insert(Vec::new())
.push(func_name);
// let mut module_calls = self.module_calls.get(&self.last_module).unwrap();
// module_calls.push(func_name);
}
_ => {}
};
}

fn visit_assert_malformed(&mut self, module: &ModuleBinary) {
let wasm_binary: Vec<u8> = module.clone().into_vec();
// let wast_string = wasm2wat(wasm_binary).expect("Can't convert back to wasm");
Expand Down Expand Up @@ -393,10 +497,10 @@ fn {}_assert_malformed() {{
self.visit_assert_return(action, expected)
}
CommandKind::AssertReturnCanonicalNan { action } => {
// Do nothing for now
self.visit_assert_return_canonical_nan(action);
}
CommandKind::AssertReturnArithmeticNan { action } => {
// Do nothing for now
self.visit_assert_return_arithmetic_nan(action);
}
CommandKind::AssertTrap { action, message: _ } => {
// Do nothing for now
Expand Down
51 changes: 51 additions & 0 deletions src/spectests/_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,54 @@ pub fn spectest_importobject<'a, 'b>() -> ImportObject<&'a str, &'b str> {
import_object.set("spectest", "global_i32", GLOBAL_I32 as *const u8);
return import_object;
}

/// Bit pattern of an f32 value:
/// 1-bit sign + 8-bit mantissa + 23-bit exponent = 32 bits
///
/// Bit pattern of an f64 value:
/// 1-bit sign + 11-bit mantissa + 52-bit exponent = 64 bits
///
/// NOTE: On some old platforms (PA-RISC, some MIPS) quiet NaNs (qNaN) have
/// their mantissa MSB unset and set for signaling NaNs (sNaN).
///
/// Links:
/// * https://en.wikipedia.org/wiki/Floating-point_arithmetic
/// * https://github.com/WebAssembly/spec/issues/286
/// * https://en.wikipedia.org/wiki/NaN
///
pub trait NaNCheck {
fn is_quiet_nan(&self) -> bool;
fn is_canonical_nan(&self) -> bool;
}

impl NaNCheck for f32 {
/// The MSB of the mantissa must be set for a NaN to be a quiet NaN.
fn is_quiet_nan(&self) -> bool {
let bit_mask = (0b1 << 22); // Used to check if 23rd bit is set, which is MSB of the mantissa
self.is_nan() && (self.to_bits() & bit_mask) == bit_mask
}

/// For a NaN to be canonical, its mantissa bits must all be set,
/// only the MSB is disregarded. (i.e we don't care if the MSB of the mantissa is set or not)
fn is_canonical_nan(&self) -> bool {
let bit_mask = 0b0____1111_1111____011_1111_1111_1111_1111_1111;
(self.to_bits() & bit_mask) == bit_mask
}
}

impl NaNCheck for f64 {
/// The MSB of the mantissa must be set for a NaN to be a quiet NaN.
fn is_quiet_nan(&self) -> bool {
let bit_mask = (0b1 << 51); // Used to check if 51st bit is set, which is MSB of the mantissa
self.is_nan() && (self.to_bits() & bit_mask) == bit_mask
}

/// For a NaN to be canonical, its mantissa bits must all be set,
/// only the MSB is disregarded. (i.e we don't care if the MSB of the mantissa is set or not)
fn is_canonical_nan(&self) -> bool {
// 0b0____111_1111_1111____0111_1111_1111_1111 ... 1111
let bit_mask = 0x7FF7FFFFFFFFFFFF;
(self.to_bits() & bit_mask) == bit_mask
}
}

6 changes: 4 additions & 2 deletions src/spectests/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/br_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/br_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/break_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/call_indirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
6 changes: 4 additions & 2 deletions src/spectests/const_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
dead_code
)]
use crate::webassembly::{instantiate, compile, ImportObject, ResultObject, VmCtx, Export};
use super::_common::spectest_importobject;
use std::{f32, f64};
use super::_common::{
spectest_importobject,
NaNCheck,
};
use wabt::wat2wasm;


Expand Down
Loading