Skip to content
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
10 changes: 8 additions & 2 deletions ir/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "util/compiler.h"
#include <bit>
#include <cassert>
#include <charconv>
#include <cmath>
// TODO: remove cstring when migrated to std::bit_cast
#include <cstring>
Expand Down Expand Up @@ -66,8 +67,13 @@ static string bits_to_float(Type &type, const string &val) {
uint64_t num = strtoull(val.c_str(), nullptr, 10);
TO fp = mbit_cast<TI, TO>(num);
auto fpclass = fpclassify(fp);
return fpclass == FP_NAN || fpclass == FP_SUBNORMAL ? to_hex(type, val)
: to_string(fp);
if (fpclass == FP_NAN || fpclass == FP_SUBNORMAL)
return to_hex(type, val);
char buf[64];
auto [ptr, ec] = to_chars(buf, buf + sizeof(buf), fp,
chars_format::scientific);
*ptr = '\0';
return buf;
}

static string int_to_readable_float(Type &type, const string &val) {
Expand Down
23 changes: 22 additions & 1 deletion ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "util/compiler.h"
#include <array>
#include <cassert>
#include <charconv>
#include <cstring>
#include <numeric>
#include <sstream>

Expand Down Expand Up @@ -639,7 +641,26 @@ void FloatType::printVal(ostream &os, const State &s, const expr &e) const {
} else if (f.isInf().isTrue()) {
os << (f.isFPNegative().isTrue() ? "-oo" : "+oo");
} else {
os << f.float2Real().numeral_string();
uint64_t bits = strtoull(e.numeral_string().data(), nullptr, 10);
if (fpType == Float) {
float fp;
memcpy(&fp, &bits, sizeof(fp));
char buf[64];
auto [p, ec] = to_chars(buf, buf + sizeof(buf), fp,
chars_format::scientific);
*p = '\0';
os << buf;
} else if (fpType == Double) {
double fp;
memcpy(&fp, &bits, sizeof(fp));
char buf[64];
auto [p, ec] = to_chars(buf, buf + sizeof(buf), fp,
chars_format::scientific);
*p = '\0';
os << buf;
} else {
os << f.float2Real().numeral_string();
}
}
os << ')';
}
Expand Down
Loading