Type punning fixes

This commit is contained in:
Dan Paulat 2023-04-17 18:36:06 -05:00
parent 78d00fc67b
commit c7aba95233
2 changed files with 46 additions and 33 deletions

View file

@ -1,6 +1,7 @@
#include <scwx/util/float.hpp>
#include <cmath>
#include <cstring>
#ifdef WIN32
# include <WinSock2.h>
@ -13,21 +14,21 @@ namespace scwx
namespace util
{
float DecodeFloat16(uint16_t hex)
float DecodeFloat16(std::uint16_t hex)
{
static constexpr uint16_t S_MASK = 0x8000;
static constexpr uint16_t S_LSB = 0;
static constexpr uint16_t S_SHIFT = 15 - S_LSB;
static constexpr uint16_t E_MASK = 0x7a00;
static constexpr uint16_t E_LSB = 5;
static constexpr uint16_t E_SHIFT = 15 - E_LSB;
static constexpr uint16_t F_MASK = 0x03ff;
static constexpr uint16_t F_LSB = 15;
static constexpr uint16_t F_SHIFT = 15 - F_LSB;
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_LSB = 5;
static constexpr std::uint16_t E_SHIFT = 15 - E_LSB;
static constexpr std::uint16_t F_MASK = 0x03ff;
static constexpr std::uint16_t F_LSB = 15;
static constexpr std::uint16_t F_SHIFT = 15 - F_LSB;
uint16_t sHex = (hex & S_MASK) >> S_SHIFT;
uint16_t eHex = (hex & E_MASK) >> E_SHIFT;
uint16_t fHex = (hex & F_MASK) >> F_SHIFT;
std::uint16_t sHex = (hex & S_MASK) >> S_SHIFT;
std::uint16_t eHex = (hex & E_MASK) >> E_SHIFT;
std::uint16_t fHex = (hex & F_MASK) >> F_SHIFT;
float value;
@ -51,11 +52,14 @@ float DecodeFloat16(uint16_t hex)
return value;
}
float DecodeFloat32(uint16_t msw, uint16_t lsw)
float DecodeFloat32(std::uint16_t msw, std::uint16_t lsw)
{
uint32_t value = msw << 16 | lsw;
std::uint32_t value = msw << 16 | lsw;
float floatValue;
return reinterpret_cast<float&>(value);
std::memcpy(&floatValue, &value, sizeof(float));
return floatValue;
}
} // namespace util