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

c-writer.cc: represent float consts as integer literals #2389

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 7 additions & 43 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1250,52 +1250,16 @@ void CWriter::Write(const Const& const_) {
Writef("%" PRIu64 "ull", static_cast<int64_t>(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<float>(f32_bits));
}
case Type::F32:
Writef("f32_reinterpret_i32(0x%" PRIx32 " /* %.9g */)", const_.f32_bits(),
Bitcast<float>(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<double>(f64_bits));
}
case Type::F64:
Writef("f64_reinterpret_i64(0x%" PRIx64 " /* %.17g */)",
const_.f64_bits(), Bitcast<double>(const_.f64_bits()));
break;
}

case Type::V128: {
Writef("v128_const(0x%02x", const_.vec128().u8(0));
for (int i = 1; i < 16; i++) {
Expand Down
4 changes: 2 additions & 2 deletions test/wasm2c/tail-calls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 /* 2 */);
var_i2 = 0u;
static_assert(sizeof(struct wasm_multi_if) <= 1024);
CHECK_CALL_INDIRECT(instance->w2c_tab, w2c_test_i32_f32, var_i2);
Expand Down Expand Up @@ -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 /* 2 */);
var_i2 = 0u;
static_assert(sizeof(struct wasm_multi_if) <= 1024);
CHECK_CALL_INDIRECT(instance->w2c_tab, w2c_test_i32_f32, var_i2);
Expand Down
Loading