AWIPS message byte swap cleanup

This commit is contained in:
Dan Paulat 2025-05-17 15:38:26 -05:00
parent 2a5068c4bb
commit ade40806b6

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <array> #include <array>
#include <bit>
#include <cstring> #include <cstring>
#include <execution> #include <execution>
#include <istream> #include <istream>
@ -52,25 +53,54 @@ public:
static float SwapFloat(float f) static float SwapFloat(float f)
{ {
if constexpr (std::endian::native == std::endian::little)
{
// Variable is initialized by memcpy
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
std::uint32_t temp; std::uint32_t temp;
std::memcpy(&temp, &f, sizeof(std::uint32_t)); std::memcpy(&temp, &f, sizeof(std::uint32_t));
temp = ntohl(temp); temp = ntohl(temp);
std::memcpy(&f, &temp, sizeof(float)); std::memcpy(&f, &temp, sizeof(float));
}
return f; return f;
} }
static double SwapDouble(double d) static double SwapDouble(double d)
{ {
if constexpr (std::endian::native == std::endian::little)
{
// Variable is initialized by memcpy
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
std::uint64_t temp; std::uint64_t temp;
std::memcpy(&temp, &d, sizeof(std::uint64_t)); std::memcpy(&temp, &d, sizeof(std::uint64_t));
temp = ntohll(temp); temp = Swap64(temp);
std::memcpy(&d, &temp, sizeof(float)); std::memcpy(&d, &temp, sizeof(float));
}
return d; return d;
} }
template<std::size_t _Size> static std::uint64_t Swap64(std::uint64_t value)
static void SwapArray(std::array<float, _Size>& arr, {
std::size_t size = _Size) if constexpr (std::endian::native == std::endian::little)
{
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers)
std::uint32_t high = ntohl(static_cast<std::uint32_t>(value >> 32));
std::uint32_t low =
ntohl(static_cast<std::uint32_t>(value & 0xFFFFFFFFULL));
return (static_cast<std::uint64_t>(low) << 32) | high;
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers)
}
else
{
return value;
}
}
template<std::size_t kSize>
static void SwapArray(std::array<float, kSize>& arr,
std::size_t size = kSize)
{
if constexpr (std::endian::native == std::endian::little)
{ {
std::transform(std::execution::par_unseq, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
@ -78,10 +108,13 @@ public:
arr.begin(), arr.begin(),
[](float f) { return SwapFloat(f); }); [](float f) { return SwapFloat(f); });
} }
}
template<std::size_t _Size> template<std::size_t kSize>
static void SwapArray(std::array<std::int16_t, _Size>& arr, static void SwapArray(std::array<std::int16_t, kSize>& arr,
std::size_t size = _Size) std::size_t size = kSize)
{
if constexpr (std::endian::native == std::endian::little)
{ {
std::transform(std::execution::par_unseq, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
@ -89,10 +122,13 @@ public:
arr.begin(), arr.begin(),
[](std::int16_t u) { return ntohs(u); }); [](std::int16_t u) { return ntohs(u); });
} }
}
template<std::size_t _Size> template<std::size_t kSize>
static void SwapArray(std::array<std::uint16_t, _Size>& arr, static void SwapArray(std::array<std::uint16_t, kSize>& arr,
std::size_t size = _Size) std::size_t size = kSize)
{
if constexpr (std::endian::native == std::endian::little)
{ {
std::transform(std::execution::par_unseq, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
@ -100,10 +136,13 @@ public:
arr.begin(), arr.begin(),
[](std::uint16_t u) { return ntohs(u); }); [](std::uint16_t u) { return ntohs(u); });
} }
}
template<std::size_t _Size> template<std::size_t kSize>
static void SwapArray(std::array<std::uint32_t, _Size>& arr, static void SwapArray(std::array<std::uint32_t, kSize>& arr,
std::size_t size = _Size) std::size_t size = kSize)
{
if constexpr (std::endian::native == std::endian::little)
{ {
std::transform(std::execution::par_unseq, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
@ -111,20 +150,27 @@ public:
arr.begin(), arr.begin(),
[](std::uint32_t u) { return ntohl(u); }); [](std::uint32_t u) { return ntohl(u); });
} }
}
template<typename T> template<typename T>
static void SwapMap(std::map<T, float>& m) static void SwapMap(std::map<T, float>& m)
{
if constexpr (std::endian::native == std::endian::little)
{ {
std::for_each(std::execution::par_unseq, std::for_each(std::execution::par_unseq,
m.begin(), m.begin(),
m.end(), m.end(),
[](auto& p) { p.second = SwapFloat(p.second); }); [](auto& p) { p.second = SwapFloat(p.second); });
} }
}
template<typename T> template<typename T>
static void SwapVector(std::vector<T>& v) static void SwapVector(std::vector<T>& v)
{ {
std::transform(std::execution::par_unseq, if constexpr (std::endian::native == std::endian::little)
{
std::transform(
std::execution::par_unseq,
v.begin(), v.begin(),
v.end(), v.end(),
v.begin(), v.begin(),
@ -143,7 +189,7 @@ public:
else if constexpr (std::is_same_v<T, std::uint64_t> || else if constexpr (std::is_same_v<T, std::uint64_t> ||
std::is_same_v<T, std::int64_t>) std::is_same_v<T, std::int64_t>)
{ {
return static_cast<T>(ntohll(u)); return static_cast<T>(Swap64(u));
} }
else if constexpr (std::is_same_v<T, float>) else if constexpr (std::is_same_v<T, float>)
{ {
@ -160,6 +206,7 @@ public:
} }
}); });
} }
}
private: private:
class Impl; class Impl;