1
0
mirror of synced 2025-01-31 03:53:44 +01:00

ui: Added half floats to data inspector

This commit is contained in:
WerWolv 2021-09-01 00:23:45 +02:00
parent 9b87bb8850
commit 82cb7917e4
6 changed files with 44 additions and 0 deletions

View File

@ -82,6 +82,11 @@ namespace hex::plugin::builtin {
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
ContentRegistry::DataInspector::add("hex.builtin.inspector.float16", sizeof(u16), [](auto buffer, auto endian, auto style) {
auto value = hex::format("{0:G}", hex::changeEndianess(float16ToFloat32(*reinterpret_cast<u16*>(buffer.data())), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
ContentRegistry::DataInspector::add("hex.builtin.inspector.float", sizeof(float), [](auto buffer, auto endian, auto style) {
auto value = hex::format("{0:G}", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };

View File

@ -313,6 +313,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.inspector.s32", "int32_t" },
{ "hex.builtin.inspector.u64", "uint64_t" },
{ "hex.builtin.inspector.s64", "int64_t" },
{ "hex.builtin.inspector.float16", "half float (16 bit)" },
{ "hex.builtin.inspector.float", "float (32 bit)" },
{ "hex.builtin.inspector.double", "double (64 bit)" },
{ "hex.builtin.inspector.ascii", "ASCII Zeichen" },

View File

@ -313,6 +313,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.inspector.s32", "int32_t" },
{ "hex.builtin.inspector.u64", "uint64_t" },
{ "hex.builtin.inspector.s64", "int64_t" },
{ "hex.builtin.inspector.float16", "half float (16 bit)" },
{ "hex.builtin.inspector.float", "float (32 bit)" },
{ "hex.builtin.inspector.double", "double (64 bit)" },
{ "hex.builtin.inspector.ascii", "ASCII Character" },

View File

@ -312,6 +312,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.inspector.s32", "int32_t" },
{ "hex.builtin.inspector.u64", "uint64_t" },
{ "hex.builtin.inspector.s64", "int64_t" },
{ "hex.builtin.inspector.float16", "half float (16 bit)" },
{ "hex.builtin.inspector.float", "float (32 bit)" },
{ "hex.builtin.inspector.double", "double (64 bit)" },
{ "hex.builtin.inspector.ascii", "ASCII Character" },

View File

@ -179,6 +179,8 @@ namespace hex {
void openFileBrowser(std::string_view title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback);
float float16ToFloat32(u16 float16);
namespace scope_guard {
#define SCOPE_GUARD ::hex::scope_guard::ScopeGuardOnExit() + [&]()

View File

@ -325,4 +325,38 @@ namespace hex {
NFD::Quit();
}
float float16ToFloat32(u16 float16) {
u32 sign = float16 >> 15;
u32 exponent = (float16 >> 10) & 0x1F;
u32 mantissa = float16 & 0x3FF;
u32 result;
if (exponent == 0) {
if (mantissa == 0) {
// +- Zero
result = sign << 31;
} else {
// Subnormal value
exponent = 0x7F - 14;
while ((mantissa & (1 << 10)) == 0) {
exponent--;
mantissa <<= 1;
}
mantissa &= 0x3FF;
result = (sign << 31) | (exponent << 23) | (mantissa << 13);
}
} else if (exponent == 0x1F) {
// +-Inf or +-NaN
result = (sign << 31) | (0xFF << 23) | (mantissa << 13);
} else {
// Normal value
result = (sign << 31) | ((exponent + (0x7F - 15)) << 23) | (mantissa << 13);
}
return reinterpret_cast<float&>(result);
}
}