29 lines
649 B
C++
29 lines
649 B
C++
|
#pragma once
|
||
|
#include <bit>
|
||
|
#include <base/Types.hpp>
|
||
|
|
||
|
namespace lightningbolt {
|
||
|
namespace detail {
|
||
|
|
||
|
template <class T, class U>
|
||
|
constexpr T PunCast(U v) {
|
||
|
return *std::bit_cast<const T*>(&v);
|
||
|
}
|
||
|
|
||
|
template <class T>
|
||
|
constexpr T ByteSwap(T v) {
|
||
|
if constexpr(sizeof(T) == 2)
|
||
|
return static_cast<T>(__builtin_bswap16(PunCast<u16>(v)));
|
||
|
else if constexpr(sizeof(T) == 4)
|
||
|
return static_cast<T>(__builtin_bswap32(PunCast<u32>(v)));
|
||
|
else if constexpr(sizeof(T) == 8)
|
||
|
return static_cast<T>(__builtin_bswap64(PunCast<u64>(v)));
|
||
|
|
||
|
return v;
|
||
|
}
|
||
|
|
||
|
} // namespace detail
|
||
|
|
||
|
using detail::ByteSwap;
|
||
|
} // namespace lightningbolt
|