Skip to content

Add support for SIMD NaN directives #686

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

Merged
merged 2 commits into from
Dec 7, 2019
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
2 changes: 1 addition & 1 deletion crates/wast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cranelift-wasm = { version = "0.50.0", features = ["enable-serde"] }
wasmtime-jit = { path = "../jit" }
wasmtime-runtime = { path = "../runtime" }
wasmtime-environ = { path = "../environ" }
wast = "3.0.0"
wast = "4.0.0"
anyhow = "1.0.19"
target-lexicon = "0.9.0"

Expand Down
159 changes: 155 additions & 4 deletions crates/wast/src/wast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::spectest::instantiate_spectest;
use anyhow::{bail, Context as _, Result};
use std::convert::TryInto;
use std::path::Path;
use std::str;
use wasmtime_jit::{
Expand Down Expand Up @@ -254,12 +255,12 @@ impl WastContext {
bail!("{}\nunexpected vector in NaN test", context(span))
}
RuntimeValue::F32(x) => {
if (x & 0x7fffffff) != 0x7fc00000 {
if !is_canonical_f32_nan(x) {
bail!("{}\nexpected canonical NaN", context(span))
}
}
RuntimeValue::F64(x) => {
if (x & 0x7fffffffffffffff) != 0x7ff8000000000000 {
if !is_canonical_f64_nan(x) {
bail!("{}\nexpected canonical NaN", context(span))
}
}
Expand All @@ -271,6 +272,68 @@ impl WastContext {
}
}
}
AssertReturnCanonicalNanF32x4 { span, invoke } => {
match self.perform_invoke(invoke).with_context(|| context(span))? {
ActionOutcome::Returned { values } => {
for v in values.iter() {
match v {
RuntimeValue::I32(_) | RuntimeValue::I64(_) => {
bail!("{}\nunexpected integer in NaN test", context(span))
}
RuntimeValue::F32(_) | RuntimeValue::F64(_) => bail!(
"{}\nunexpected scalar float in vector NaN test",
context(span)
),
RuntimeValue::V128(x) => {
for l in 0..4 {
if !is_canonical_f32_nan(&extract_lane_as_u32(x, l)?) {
bail!(
"{}\nexpected f32x4 canonical NaN in lane {}",
context(span),
l
)
}
}
}
};
}
}
ActionOutcome::Trapped { message } => {
bail!("{}\nunexpected trap: {}", context(span), message)
}
}
}
AssertReturnCanonicalNanF64x2 { span, invoke } => {
match self.perform_invoke(invoke).with_context(|| context(span))? {
ActionOutcome::Returned { values } => {
for v in values.iter() {
match v {
RuntimeValue::I32(_) | RuntimeValue::I64(_) => {
bail!("{}\nunexpected integer in NaN test", context(span))
}
RuntimeValue::F32(_) | RuntimeValue::F64(_) => bail!(
"{}\nunexpected scalar float in vector NaN test",
context(span)
),
RuntimeValue::V128(x) => {
for l in 0..2 {
if !is_canonical_f64_nan(&extract_lane_as_u64(x, l)?) {
bail!(
"{}\nexpected f64x2 canonical NaN in lane {}",
context(span),
l
)
}
}
}
};
}
}
ActionOutcome::Trapped { message } => {
bail!("{}\nunexpected trap: {}", context(span), message)
}
}
}
AssertReturnArithmeticNan { span, invoke } => {
match self.perform_invoke(invoke).with_context(|| context(span))? {
ActionOutcome::Returned { values } => {
Expand All @@ -283,12 +346,12 @@ impl WastContext {
bail!("{}\nunexpected vector in NaN test", context(span))
}
RuntimeValue::F32(x) => {
if (x & 0x00400000) != 0x00400000 {
if !is_arithmetic_f32_nan(x) {
bail!("{}\nexpected arithmetic NaN", context(span))
}
}
RuntimeValue::F64(x) => {
if (x & 0x0008000000000000) != 0x0008000000000000 {
if !is_arithmetic_f64_nan(x) {
bail!("{}\nexpected arithmetic NaN", context(span))
}
}
Expand All @@ -300,6 +363,68 @@ impl WastContext {
}
}
}
AssertReturnArithmeticNanF32x4 { span, invoke } => {
match self.perform_invoke(invoke).with_context(|| context(span))? {
ActionOutcome::Returned { values } => {
for v in values.iter() {
match v {
RuntimeValue::I32(_) | RuntimeValue::I64(_) => {
bail!("{}\nunexpected integer in NaN test", context(span))
}
RuntimeValue::F32(_) | RuntimeValue::F64(_) => bail!(
"{}\nunexpected scalar float in vector NaN test",
context(span)
),
RuntimeValue::V128(x) => {
for l in 0..4 {
if !is_arithmetic_f32_nan(&extract_lane_as_u32(x, l)?) {
bail!(
"{}\nexpected f32x4 arithmetic NaN in lane {}",
context(span),
l
)
}
}
}
};
}
}
ActionOutcome::Trapped { message } => {
bail!("{}\nunexpected trap: {}", context(span), message)
}
}
}
AssertReturnArithmeticNanF64x2 { span, invoke } => {
match self.perform_invoke(invoke).with_context(|| context(span))? {
ActionOutcome::Returned { values } => {
for v in values.iter() {
match v {
RuntimeValue::I32(_) | RuntimeValue::I64(_) => {
bail!("{}\nunexpected integer in NaN test", context(span))
}
RuntimeValue::F32(_) | RuntimeValue::F64(_) => bail!(
"{}\nunexpected scalar float in vector NaN test",
context(span)
),
RuntimeValue::V128(x) => {
for l in 0..2 {
if !is_arithmetic_f64_nan(&extract_lane_as_u64(x, l)?) {
bail!(
"{}\nexpected f64x2 arithmetic NaN in lane {}",
context(span),
l
)
}
}
}
};
}
}
ActionOutcome::Trapped { message } => {
bail!("{}\nunexpected trap: {}", context(span), message)
}
}
}
AssertInvalid {
span,
mut module,
Expand Down Expand Up @@ -384,3 +509,29 @@ impl WastContext {
self.run_buffer(path.to_str().unwrap(), &bytes)
}
}

fn extract_lane_as_u32(bytes: &[u8; 16], lane: usize) -> Result<u32> {
let i = lane * 4;
Ok(u32::from_le_bytes(bytes[i..i + 4].try_into()?))
}

fn extract_lane_as_u64(bytes: &[u8; 16], lane: usize) -> Result<u64> {
let i = lane * 8;
Ok(u64::from_le_bytes(bytes[i..i + 8].try_into()?))
}

fn is_canonical_f32_nan(bits: &u32) -> bool {
return (bits & 0x7fffffff) == 0x7fc00000;
}

fn is_canonical_f64_nan(bits: &u64) -> bool {
return (bits & 0x7fffffffffffffff) == 0x7ff8000000000000;
}

fn is_arithmetic_f32_nan(bits: &u32) -> bool {
return (bits & 0x00400000) == 0x00400000;
}

fn is_arithmetic_f64_nan(bits: &u64) -> bool {
return (bits & 0x0008000000000000) == 0x0008000000000000;
}