mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 21:10:04 +00:00
Type punning fixes
This commit is contained in:
parent
78d00fc67b
commit
c7aba95233
2 changed files with 46 additions and 33 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <execution>
|
||||
#include <istream>
|
||||
#include <map>
|
||||
|
|
@ -24,18 +25,18 @@ class Message
|
|||
protected:
|
||||
explicit Message();
|
||||
|
||||
Message(const Message&) = delete;
|
||||
Message(const Message&) = delete;
|
||||
Message& operator=(const Message&) = delete;
|
||||
|
||||
Message(Message&&) noexcept;
|
||||
Message& operator=(Message&&) noexcept;
|
||||
|
||||
virtual bool ValidateMessage(std::istream& is, size_t bytesRead) const;
|
||||
virtual bool ValidateMessage(std::istream& is, std::size_t bytesRead) const;
|
||||
|
||||
public:
|
||||
virtual ~Message();
|
||||
|
||||
virtual size_t data_size() const = 0;
|
||||
virtual std::size_t data_size() const = 0;
|
||||
|
||||
virtual bool Parse(std::istream& is) = 0;
|
||||
|
||||
|
|
@ -55,11 +56,16 @@ public:
|
|||
|
||||
static float SwapFloat(float f)
|
||||
{
|
||||
return ntohf(*reinterpret_cast<uint32_t*>(&f));
|
||||
std::uint32_t temp;
|
||||
std::memcpy(&temp, &f, sizeof(std::uint32_t));
|
||||
temp = ntohl(temp);
|
||||
std::memcpy(&f, &temp, sizeof(float));
|
||||
return f;
|
||||
}
|
||||
|
||||
template<size_t _Size>
|
||||
static void SwapArray(std::array<float, _Size>& arr, size_t size = _Size)
|
||||
template<std::size_t _Size>
|
||||
static void SwapArray(std::array<float, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
{
|
||||
std::transform(std::execution::par_unseq,
|
||||
arr.begin(),
|
||||
|
|
@ -68,34 +74,37 @@ public:
|
|||
[](float f) { return SwapFloat(f); });
|
||||
}
|
||||
|
||||
template<size_t _Size>
|
||||
static void SwapArray(std::array<int16_t, _Size>& arr, size_t size = _Size)
|
||||
template<std::size_t _Size>
|
||||
static void SwapArray(std::array<std::int16_t, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
{
|
||||
std::transform(std::execution::par_unseq,
|
||||
arr.begin(),
|
||||
arr.begin() + size,
|
||||
arr.begin(),
|
||||
[](int16_t u) { return ntohs(u); });
|
||||
[](std::int16_t u) { return ntohs(u); });
|
||||
}
|
||||
|
||||
template<size_t _Size>
|
||||
static void SwapArray(std::array<uint16_t, _Size>& arr, size_t size = _Size)
|
||||
template<std::size_t _Size>
|
||||
static void SwapArray(std::array<std::uint16_t, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
{
|
||||
std::transform(std::execution::par_unseq,
|
||||
arr.begin(),
|
||||
arr.begin() + size,
|
||||
arr.begin(),
|
||||
[](uint16_t u) { return ntohs(u); });
|
||||
[](std::uint16_t u) { return ntohs(u); });
|
||||
}
|
||||
|
||||
template<size_t _Size>
|
||||
static void SwapArray(std::array<uint32_t, _Size>& arr, size_t size = _Size)
|
||||
template<std::size_t _Size>
|
||||
static void SwapArray(std::array<std::uint32_t, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
{
|
||||
std::transform(std::execution::par_unseq,
|
||||
arr.begin(),
|
||||
arr.begin() + size,
|
||||
arr.begin(),
|
||||
[](uint32_t u) { return ntohl(u); });
|
||||
[](std::uint32_t u) { return ntohl(u); });
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -107,13 +116,13 @@ public:
|
|||
[](auto& p) { p.second = SwapFloat(p.second); });
|
||||
}
|
||||
|
||||
static void SwapVector(std::vector<uint16_t>& v)
|
||||
static void SwapVector(std::vector<std::uint16_t>& v)
|
||||
{
|
||||
std::transform(std::execution::par_unseq,
|
||||
v.begin(),
|
||||
v.end(),
|
||||
v.begin(),
|
||||
[](uint16_t u) { return ntohs(u); });
|
||||
[](std::uint16_t u) { return ntohs(u); });
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue