mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 13: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 | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <array> | #include <array> | ||||||
|  | #include <cstring> | ||||||
| #include <execution> | #include <execution> | ||||||
| #include <istream> | #include <istream> | ||||||
| #include <map> | #include <map> | ||||||
|  | @ -30,12 +31,12 @@ protected: | ||||||
|    Message(Message&&) noexcept; |    Message(Message&&) noexcept; | ||||||
|    Message& operator=(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: | public: | ||||||
|    virtual ~Message(); |    virtual ~Message(); | ||||||
| 
 | 
 | ||||||
|    virtual size_t data_size() const = 0; |    virtual std::size_t data_size() const = 0; | ||||||
| 
 | 
 | ||||||
|    virtual bool Parse(std::istream& is) = 0; |    virtual bool Parse(std::istream& is) = 0; | ||||||
| 
 | 
 | ||||||
|  | @ -55,11 +56,16 @@ public: | ||||||
| 
 | 
 | ||||||
|    static float SwapFloat(float f) |    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> |    template<std::size_t _Size> | ||||||
|    static void SwapArray(std::array<float, _Size>& arr, size_t size = _Size) |    static void SwapArray(std::array<float, _Size>& arr, | ||||||
|  |                          std::size_t               size = _Size) | ||||||
|    { |    { | ||||||
|       std::transform(std::execution::par_unseq, |       std::transform(std::execution::par_unseq, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|  | @ -68,34 +74,37 @@ public: | ||||||
|                      [](float f) { return SwapFloat(f); }); |                      [](float f) { return SwapFloat(f); }); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    template<size_t _Size> |    template<std::size_t _Size> | ||||||
|    static void SwapArray(std::array<int16_t, _Size>& arr, size_t size = _Size) |    static void SwapArray(std::array<std::int16_t, _Size>& arr, | ||||||
|  |                          std::size_t                      size = _Size) | ||||||
|    { |    { | ||||||
|       std::transform(std::execution::par_unseq, |       std::transform(std::execution::par_unseq, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|                      arr.begin() + size, |                      arr.begin() + size, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|                      [](int16_t u) { return ntohs(u); }); |                      [](std::int16_t u) { return ntohs(u); }); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    template<size_t _Size> |    template<std::size_t _Size> | ||||||
|    static void SwapArray(std::array<uint16_t, _Size>& arr, size_t size = _Size) |    static void SwapArray(std::array<std::uint16_t, _Size>& arr, | ||||||
|  |                          std::size_t                       size = _Size) | ||||||
|    { |    { | ||||||
|       std::transform(std::execution::par_unseq, |       std::transform(std::execution::par_unseq, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|                      arr.begin() + size, |                      arr.begin() + size, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|                      [](uint16_t u) { return ntohs(u); }); |                      [](std::uint16_t u) { return ntohs(u); }); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    template<size_t _Size> |    template<std::size_t _Size> | ||||||
|    static void SwapArray(std::array<uint32_t, _Size>& arr, size_t size = _Size) |    static void SwapArray(std::array<std::uint32_t, _Size>& arr, | ||||||
|  |                          std::size_t                       size = _Size) | ||||||
|    { |    { | ||||||
|       std::transform(std::execution::par_unseq, |       std::transform(std::execution::par_unseq, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|                      arr.begin() + size, |                      arr.begin() + size, | ||||||
|                      arr.begin(), |                      arr.begin(), | ||||||
|                      [](uint32_t u) { return ntohl(u); }); |                      [](std::uint32_t u) { return ntohl(u); }); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    template<typename T> |    template<typename T> | ||||||
|  | @ -107,13 +116,13 @@ public: | ||||||
|                     [](auto& p) { p.second = SwapFloat(p.second); }); |                     [](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, |       std::transform(std::execution::par_unseq, | ||||||
|                      v.begin(), |                      v.begin(), | ||||||
|                      v.end(), |                      v.end(), | ||||||
|                      v.begin(), |                      v.begin(), | ||||||
|                      [](uint16_t u) { return ntohs(u); }); |                      [](std::uint16_t u) { return ntohs(u); }); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| #include <scwx/util/float.hpp> | #include <scwx/util/float.hpp> | ||||||
| 
 | 
 | ||||||
| #include <cmath> | #include <cmath> | ||||||
|  | #include <cstring> | ||||||
| 
 | 
 | ||||||
| #ifdef WIN32 | #ifdef WIN32 | ||||||
| #   include <WinSock2.h> | #   include <WinSock2.h> | ||||||
|  | @ -13,21 +14,21 @@ namespace scwx | ||||||
| namespace util | namespace util | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| float DecodeFloat16(uint16_t hex) | float DecodeFloat16(std::uint16_t hex) | ||||||
| { | { | ||||||
|    static constexpr uint16_t S_MASK  = 0x8000; |    static constexpr std::uint16_t S_MASK  = 0x8000; | ||||||
|    static constexpr uint16_t S_LSB   = 0; |    static constexpr std::uint16_t S_LSB   = 0; | ||||||
|    static constexpr uint16_t S_SHIFT = 15 - S_LSB; |    static constexpr std::uint16_t S_SHIFT = 15 - S_LSB; | ||||||
|    static constexpr uint16_t E_MASK  = 0x7a00; |    static constexpr std::uint16_t E_MASK  = 0x7a00; | ||||||
|    static constexpr uint16_t E_LSB   = 5; |    static constexpr std::uint16_t E_LSB   = 5; | ||||||
|    static constexpr uint16_t E_SHIFT = 15 - E_LSB; |    static constexpr std::uint16_t E_SHIFT = 15 - E_LSB; | ||||||
|    static constexpr uint16_t F_MASK  = 0x03ff; |    static constexpr std::uint16_t F_MASK  = 0x03ff; | ||||||
|    static constexpr uint16_t F_LSB   = 15; |    static constexpr std::uint16_t F_LSB   = 15; | ||||||
|    static constexpr uint16_t F_SHIFT = 15 - F_LSB; |    static constexpr std::uint16_t F_SHIFT = 15 - F_LSB; | ||||||
| 
 | 
 | ||||||
|    uint16_t sHex = (hex & S_MASK) >> S_SHIFT; |    std::uint16_t sHex = (hex & S_MASK) >> S_SHIFT; | ||||||
|    uint16_t eHex = (hex & E_MASK) >> E_SHIFT; |    std::uint16_t eHex = (hex & E_MASK) >> E_SHIFT; | ||||||
|    uint16_t fHex = (hex & F_MASK) >> F_SHIFT; |    std::uint16_t fHex = (hex & F_MASK) >> F_SHIFT; | ||||||
| 
 | 
 | ||||||
|    float value; |    float value; | ||||||
| 
 | 
 | ||||||
|  | @ -51,11 +52,14 @@ float DecodeFloat16(uint16_t hex) | ||||||
|    return value; |    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
 | } // namespace util
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat