diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-08 12:38:55 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-08 12:38:55 -0800 |
commit | 57481a28ac4935928bca3deae8d202d88c780789 (patch) | |
tree | 4007cd62fab563fd6261dba34b3d9013046df097 /src/wasm.h | |
parent | 8bbc4d10232a73bb8c61a315b224d0b93c4713c6 (diff) | |
download | binaryen-57481a28ac4935928bca3deae8d202d88c780789.tar.gz binaryen-57481a28ac4935928bca3deae8d202d88c780789.tar.bz2 binaryen-57481a28ac4935928bca3deae8d202d88c780789.zip |
print and compare nans properly
Diffstat (limited to 'src/wasm.h')
-rw-r--r-- | src/wasm.h | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/wasm.h b/src/wasm.h index 2794f868c..a8a6b8149 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -136,9 +136,28 @@ struct Literal { bool operator==(Literal& other) { if (type != other.type) return false; - if (type == none) return true; - if (type == WasmType::i32 || type == WasmType::f32) return i32 == other.i32; - return i64 == other.i64; + switch (type) { + case WasmType::none: return true; + case WasmType::i32: return i32 == other.i32; + case WasmType::i64: return i64 == other.i64; + // reinterpret floating-point, to avoid nan != nan + case WasmType::f32: return reinterpreti32() == other.reinterpreti32(); + case WasmType::f64: return reinterpreti64() == other.reinterpreti64(); + default: abort(); + } + } + + void printFloat(std::ostream &o, float f) { + if (isnan(f)) { + union { + float ff; + uint32_t ll; + } u; + u.ff = f; + o << "nan:" << std::hex << u.ll; + return; + } + printDouble(o, f); } void printDouble(std::ostream &o, double d) { @@ -146,6 +165,15 @@ struct Literal { o << "-0"; return; } + if (isnan(d)) { + union { + double dd; + uint64_t ll; + } u; + u.dd = d; + o << "nan:" << std::hex << u.ll; + return; + } const char *text = cashew::JSPrinter::numToString(d); // spec interpreter hates floats starting with '.' if (text[0] == '.') { @@ -164,7 +192,7 @@ struct Literal { case none: o << "?"; break; case WasmType::i32: o << literal.i32; break; case WasmType::i64: o << literal.i64; break; - case WasmType::f32: literal.printDouble(o, literal.f32); break; + case WasmType::f32: literal.printFloat(o, literal.f32); break; case WasmType::f64: literal.printDouble(o, literal.f64); break; } restoreNormalColor(o); |