From 54862c8a6b496fee208b2b7f48f9e446de52a3af Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Sat, 10 Feb 2024 14:43:31 -0800 Subject: [PATCH] c-writer.cc: represent float consts as integer literals --- src/c-writer.cc | 48 ++++---------------------------------- test/wasm2c/tail-calls.txt | 4 ++-- 2 files changed, 7 insertions(+), 45 deletions(-) diff --git a/src/c-writer.cc b/src/c-writer.cc index d7a61f421..9dec2148c 100644 --- a/src/c-writer.cc +++ b/src/c-writer.cc @@ -1250,52 +1250,14 @@ void CWriter::Write(const Const& const_) { Writef("%" PRIu64 "ull", static_cast(const_.u64())); break; - case Type::F32: { - uint32_t f32_bits = const_.f32_bits(); - // TODO(binji): Share with similar float info in interp.cc and literal.cc - if ((f32_bits & 0x7f800000u) == 0x7f800000u) { - const char* sign = (f32_bits & 0x80000000) ? "-" : ""; - uint32_t significand = f32_bits & 0x7fffffu; - if (significand == 0) { - // Infinity. - Writef("%sINFINITY", sign); - } else { - // Nan. - Writef("f32_reinterpret_i32(0x%08x) /* %snan:0x%06x */", f32_bits, - sign, significand); - } - } else if (f32_bits == 0x80000000) { - // Negative zero. Special-cased so it isn't written as -0 below. - Writef("-0.f"); - } else { - Writef("%.9g", Bitcast(f32_bits)); - } + case Type::F32: + Writef("f32_reinterpret_i32(0x%" PRIx32 ")", const_.f32_bits()); break; - } - case Type::F64: { - uint64_t f64_bits = const_.f64_bits(); - // TODO(binji): Share with similar float info in interp.cc and literal.cc - if ((f64_bits & 0x7ff0000000000000ull) == 0x7ff0000000000000ull) { - const char* sign = (f64_bits & 0x8000000000000000ull) ? "-" : ""; - uint64_t significand = f64_bits & 0xfffffffffffffull; - if (significand == 0) { - // Infinity. - Writef("%sINFINITY", sign); - } else { - // Nan. - Writef("f64_reinterpret_i64(0x%016" PRIx64 ") /* %snan:0x%013" PRIx64 - " */", - f64_bits, sign, significand); - } - } else if (f64_bits == 0x8000000000000000ull) { - // Negative zero. Special-cased so it isn't written as -0 below. - Writef("-0.0"); - } else { - Writef("%.17g", Bitcast(f64_bits)); - } + case Type::F64: + Writef("f64_reinterpret_i64(0x%" PRIx64 ")", const_.f64_bits()); break; - } + case Type::V128: { Writef("v128_const(0x%02x", const_.vec128().u8(0)); for (int i = 1; i < 16; i++) { diff --git a/test/wasm2c/tail-calls.txt b/test/wasm2c/tail-calls.txt index fe9815e35..e3b51bbe2 100644 --- a/test/wasm2c/tail-calls.txt +++ b/test/wasm2c/tail-calls.txt @@ -823,7 +823,7 @@ void w2c_test_tailcaller_0(w2c_test* instance) { u32 var_i0, var_i2; f32 var_f1; var_i0 = 1u; - var_f1 = 2; + var_f1 = f32_reinterpret_i32(0x40000000); var_i2 = 0u; static_assert(sizeof(struct wasm_multi_if) <= 1024); CHECK_CALL_INDIRECT(instance->w2c_tab, w2c_test_i32_f32, var_i2); @@ -855,7 +855,7 @@ void wasm_tailcall_w2c_test_tailcaller_0(void **instance_ptr, void *tail_call_st u32 var_i0, var_i2; f32 var_f1; var_i0 = 1u; - var_f1 = 2; + var_f1 = f32_reinterpret_i32(0x40000000); var_i2 = 0u; static_assert(sizeof(struct wasm_multi_if) <= 1024); CHECK_CALL_INDIRECT(instance->w2c_tab, w2c_test_i32_f32, var_i2);