From c7aba9523370005aadc5b5b649e654afbcd74362 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Mon, 17 Apr 2023 18:36:06 -0500 Subject: [PATCH] Type punning fixes --- wxdata/include/scwx/awips/message.hpp | 43 ++++++++++++++++----------- wxdata/source/scwx/util/float.cpp | 36 ++++++++++++---------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/wxdata/include/scwx/awips/message.hpp b/wxdata/include/scwx/awips/message.hpp index 049f2f7f..e3b75d0c 100644 --- a/wxdata/include/scwx/awips/message.hpp +++ b/wxdata/include/scwx/awips/message.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -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(&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 - static void SwapArray(std::array& arr, size_t size = _Size) + template + static void SwapArray(std::array& 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 - static void SwapArray(std::array& arr, size_t size = _Size) + template + static void SwapArray(std::array& 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 - static void SwapArray(std::array& arr, size_t size = _Size) + template + static void SwapArray(std::array& 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 - static void SwapArray(std::array& arr, size_t size = _Size) + template + static void SwapArray(std::array& 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 @@ -107,13 +116,13 @@ public: [](auto& p) { p.second = SwapFloat(p.second); }); } - static void SwapVector(std::vector& v) + static void SwapVector(std::vector& 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: diff --git a/wxdata/source/scwx/util/float.cpp b/wxdata/source/scwx/util/float.cpp index c837aeba..680188f3 100644 --- a/wxdata/source/scwx/util/float.cpp +++ b/wxdata/source/scwx/util/float.cpp @@ -1,6 +1,7 @@ #include #include +#include #ifdef WIN32 # include @@ -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(value); + std::memcpy(&floatValue, &value, sizeof(float)); + + return floatValue; } } // namespace util