mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
feat: Added bfloat and fp24 data inspector entries
This commit is contained in:
@@ -285,7 +285,54 @@ namespace hex {
|
||||
return result;
|
||||
}
|
||||
|
||||
[[nodiscard]] float float16ToFloat32(u16 float16);
|
||||
template<u8 ExponentBits, u8 MantissaBits>
|
||||
[[nodiscard]] constexpr float customFloatToFloat32(u32 value) {
|
||||
static_assert(ExponentBits <= 8, "ExponentBits must be less than 8");
|
||||
static_assert(ExponentBits + MantissaBits + 1 <= 32, "Format doesn't fit into a 32-bit float");
|
||||
|
||||
const u32 sign = value >> (ExponentBits + MantissaBits);
|
||||
const u32 exponent = (value >> MantissaBits) & ((1u << ExponentBits) - 1);
|
||||
u32 mantissa = value & ((1u << MantissaBits) - 1);
|
||||
|
||||
// Calculate the bias for the input format and IEEE-754 float32
|
||||
i32 inputBias = (1 << (ExponentBits - 1)) - 1;
|
||||
i32 float32Bias = 127;
|
||||
|
||||
u32 result = 0;
|
||||
|
||||
if (exponent == 0) {
|
||||
if (mantissa == 0) {
|
||||
// Zero
|
||||
result = sign << 31;
|
||||
} else {
|
||||
// Subnormal
|
||||
int shift = 0;
|
||||
while ((mantissa & (1u << MantissaBits)) == 0) {
|
||||
mantissa <<= 1;
|
||||
shift++;
|
||||
}
|
||||
mantissa &= ((1u << MantissaBits) - 1); // clear implicit bit
|
||||
int adjustedExp = float32Bias - inputBias - shift + 1;
|
||||
result = (sign << 31) | (adjustedExp << 23) | (mantissa << (23 - MantissaBits));
|
||||
}
|
||||
} else if (exponent == ((1u << ExponentBits) - 1)) {
|
||||
// Inf or NaN
|
||||
result = (sign << 31) | (0xFF << 23) | (mantissa << (23 - MantissaBits));
|
||||
} else {
|
||||
// Normalized number
|
||||
int adjustedExp = exponent - inputBias + float32Bias;
|
||||
result = (sign << 31) | (adjustedExp << 23) | (mantissa << (23 - MantissaBits));
|
||||
}
|
||||
|
||||
float floatResult;
|
||||
std::memcpy(&floatResult, &result, sizeof(float));
|
||||
|
||||
return floatResult;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float float16ToFloat32(u16 float16) {
|
||||
return customFloatToFloat32<5, 10>(float16);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool equalsIgnoreCase(std::string_view left, std::string_view right) {
|
||||
return std::equal(left.begin(), left.end(), right.begin(), right.end(), [](char a, char b) {
|
||||
|
||||
@@ -612,43 +612,6 @@ namespace hex {
|
||||
return utf8;
|
||||
}
|
||||
|
||||
float float16ToFloat32(u16 float16) {
|
||||
u32 sign = float16 >> 15;
|
||||
u32 exponent = (float16 >> 10) & 0x1F;
|
||||
u32 mantissa = float16 & 0x3FF;
|
||||
|
||||
u32 result = 0x00;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
float floatResult = 0;
|
||||
std::memcpy(&floatResult, &result, sizeof(float));
|
||||
|
||||
return floatResult;
|
||||
}
|
||||
|
||||
bool isProcessElevated() {
|
||||
#if defined(OS_WINDOWS)
|
||||
bool elevated = false;
|
||||
|
||||
Reference in New Issue
Block a user