Fix 16-bit floating point conversions

This commit is contained in:
Dan Paulat 2024-02-25 18:18:50 -06:00
parent b0ebc8eab6
commit 6254b0a84e
2 changed files with 21 additions and 4 deletions

View file

@ -7,6 +7,11 @@ namespace scwx
namespace util
{
class Float16Test :
public testing ::TestWithParam<std ::pair<std::uint16_t, float>>
{
};
TEST(FloatTest, Decode32Positive1)
{
uint16_t msw = 0x3f80;
@ -37,14 +42,26 @@ TEST(FloatTest, Decode32Positive12345678)
EXPECT_FLOAT_EQ(x, 12345678.0f);
}
TEST(FloatTest, Decode16h0x5bb4)
TEST_P(Float16Test, DecodeFloat16)
{
uint16_t hex = 0x5bb4;
auto param = GetParam();
std::uint16_t hex = param.first;
float x = DecodeFloat16(hex);
EXPECT_FLOAT_EQ(x, 123.25f);
EXPECT_FLOAT_EQ(x, param.second);
}
INSTANTIATE_TEST_SUITE_P(
FloatTest,
Float16Test,
testing::Values(std::pair<std::uint16_t, float> {0x4400, 2.0f},
std::pair<std::uint16_t, float> {0x59ab, 90.6875f},
std::pair<std::uint16_t, float> {0x593e, 83.875f},
std::pair<std::uint16_t, float> {0x54dc, 38.875f},
std::pair<std::uint16_t, float> {0xc82a, -4.1640625f},
std::pair<std::uint16_t, float> {0x5bb4, 123.25f}));
} // namespace util
} // namespace scwx

View file

@ -19,7 +19,7 @@ float DecodeFloat16(std::uint16_t hex)
static constexpr std::uint16_t S_MASK = 0x8000;
static constexpr std::uint16_t S_LSB = 0;
static constexpr std::uint16_t S_SHIFT = 15 - S_LSB;
static constexpr std::uint16_t E_MASK = 0x7a00;
static constexpr std::uint16_t E_MASK = 0x7c00;
static constexpr std::uint16_t E_LSB = 5;
static constexpr std::uint16_t E_SHIFT = 15 - E_LSB;
static constexpr std::uint16_t F_MASK = 0x03ff;