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,113 +53,159 @@ public:
static float SwapFloat(float f) static float SwapFloat(float f)
{ {
std::uint32_t temp; if constexpr (std::endian::native == std::endian::little)
std::memcpy(&temp, &f, sizeof(std::uint32_t)); {
temp = ntohl(temp); // Variable is initialized by memcpy
std::memcpy(&f, &temp, sizeof(float)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
std::uint32_t temp;
std::memcpy(&temp, &f, sizeof(std::uint32_t));
temp = ntohl(temp);
std::memcpy(&f, &temp, sizeof(float));
}
return f; return f;
} }
static double SwapDouble(double d) static double SwapDouble(double d)
{ {
std::uint64_t temp; if constexpr (std::endian::native == std::endian::little)
std::memcpy(&temp, &d, sizeof(std::uint64_t)); {
temp = ntohll(temp); // Variable is initialized by memcpy
std::memcpy(&d, &temp, sizeof(float)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
std::uint64_t temp;
std::memcpy(&temp, &d, sizeof(std::uint64_t));
temp = Swap64(temp);
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)
{ {
std::transform(std::execution::par_unseq, if constexpr (std::endian::native == std::endian::little)
arr.begin(), {
arr.begin() + size, // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers)
arr.begin(), std::uint32_t high = ntohl(static_cast<std::uint32_t>(value >> 32));
[](float f) { return SwapFloat(f); }); 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 _Size> template<std::size_t kSize>
static void SwapArray(std::array<std::int16_t, _Size>& arr, static void SwapArray(std::array<float, kSize>& arr,
std::size_t size = _Size) std::size_t size = kSize)
{ {
std::transform(std::execution::par_unseq, if constexpr (std::endian::native == std::endian::little)
arr.begin(), {
arr.begin() + size, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
[](std::int16_t u) { return ntohs(u); }); arr.begin() + size,
arr.begin(),
[](float f) { return SwapFloat(f); });
}
} }
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::int16_t, kSize>& arr,
std::size_t size = _Size) std::size_t size = kSize)
{ {
std::transform(std::execution::par_unseq, if constexpr (std::endian::native == std::endian::little)
arr.begin(), {
arr.begin() + size, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
[](std::uint16_t u) { return ntohs(u); }); arr.begin() + size,
arr.begin(),
[](std::int16_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::uint16_t, kSize>& arr,
std::size_t size = _Size) std::size_t size = kSize)
{ {
std::transform(std::execution::par_unseq, if constexpr (std::endian::native == std::endian::little)
arr.begin(), {
arr.begin() + size, std::transform(std::execution::par_unseq,
arr.begin(), arr.begin(),
[](std::uint32_t u) { return ntohl(u); }); arr.begin() + size,
arr.begin(),
[](std::uint16_t u) { return ntohs(u); });
}
}
template<std::size_t kSize>
static void SwapArray(std::array<std::uint32_t, kSize>& arr,
std::size_t size = kSize)
{
if constexpr (std::endian::native == std::endian::little)
{
std::transform(std::execution::par_unseq,
arr.begin(),
arr.begin() + size,
arr.begin(),
[](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)
{ {
std::for_each(std::execution::par_unseq, if constexpr (std::endian::native == std::endian::little)
m.begin(), {
m.end(), std::for_each(std::execution::par_unseq,
[](auto& p) { p.second = SwapFloat(p.second); }); m.begin(),
m.end(),
[](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)
v.begin(), {
v.end(), std::transform(
v.begin(), std::execution::par_unseq,
[](T u) v.begin(),
{ v.end(),
if constexpr (std::is_same_v<T, std::uint16_t> || v.begin(),
std::is_same_v<T, std::int16_t>) [](T u)
{ {
return static_cast<T>(ntohs(u)); if constexpr (std::is_same_v<T, std::uint16_t> ||
} std::is_same_v<T, std::int16_t>)
else if constexpr (std::is_same_v<T, std::uint32_t> || {
std::is_same_v<T, std::int32_t>) return static_cast<T>(ntohs(u));
{ }
return static_cast<T>(ntohl(u)); else if constexpr (std::is_same_v<T, std::uint32_t> ||
} std::is_same_v<T, std::int32_t>)
else if constexpr (std::is_same_v<T, std::uint64_t> || {
std::is_same_v<T, std::int64_t>) return static_cast<T>(ntohl(u));
{ }
return static_cast<T>(ntohll(u)); else if constexpr (std::is_same_v<T, std::uint64_t> ||
} std::is_same_v<T, std::int64_t>)
else if constexpr (std::is_same_v<T, float>) {
{ return static_cast<T>(Swap64(u));
return SwapFloat(u); }
} else if constexpr (std::is_same_v<T, float>)
else if constexpr (std::is_same_v<T, double>) {
{ return SwapFloat(u);
return SwapDouble(u); }
} else if constexpr (std::is_same_v<T, double>)
else {
{ return SwapDouble(u);
static_assert(std::is_same_v<T, void>, }
"Unsupported type for SwapVector"); else
} {
}); static_assert(std::is_same_v<T, void>,
"Unsupported type for SwapVector");
}
});
}
} }
private: private: