From 590400071b0549d37f6b760870fb854da6bf2294 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Tue, 18 Jul 2023 00:00:59 +0200 Subject: [PATCH] [Wasm] Define canonical representation for NaNs explicitly instead of depending on Kotlin's one Surprisingly with `Float.NaN.toRawBits()` you can observe different values, which can lead to different wat files for the same input. --- .../kotlin/wasm/ir/convertors/WasmIrToText.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/convertors/WasmIrToText.kt b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/convertors/WasmIrToText.kt index a239cd8ad08..4ad6e8e8978 100644 --- a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/convertors/WasmIrToText.kt +++ b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/convertors/WasmIrToText.kt @@ -146,7 +146,6 @@ class WasmIrToText : SExpressionBuilder() { } } - private fun f32Str(x: WasmImmediate.ConstF32): String { val bits = x.rawBits.toInt() val v = Float.fromBits(bits) @@ -156,7 +155,8 @@ class WasmIrToText : SExpressionBuilder() { } else { "-" } - if (bits != Float.NaN.toRawBits()) { + + if (bits != F32_CANON_NAN) { val customPayload = bits and 0x7fffff "${sign}nan:0x${customPayload.toString(16)}" } else { @@ -182,7 +182,7 @@ class WasmIrToText : SExpressionBuilder() { } else { "-" } - if (bits != Double.NaN.toRawBits()) { + if (bits != F64_CANON_NAN) { val customPayload = bits and 0xfffffffffffff "${sign}nan:0x${customPayload.toString(16)}" } else { @@ -585,3 +585,7 @@ fun isValidWatIdentifierChar(c: Char): Boolean = // permitted identifiers: '?', '<' || c in "!#$%&′*+-./:<=>?@\\^_`|~" || c in "$.@_" + +// https://webassembly.github.io/spec/core/syntax/values.html#floating-point +private const val F32_CANON_NAN = 0x7FC0_0000 +private const val F64_CANON_NAN = 0x7FF8_0000_0000_0000L