#pragma once #include template inline T ByteSwap(T value) { if constexpr (sizeof(T) == 1) return value; else if constexpr (sizeof(T) == 2) return static_cast(__builtin_bswap16(static_cast(value))); else if constexpr (sizeof(T) == 4) return static_cast(__builtin_bswap32(static_cast(value))); else if constexpr (sizeof(T) == 8) return static_cast(__builtin_bswap64(static_cast(value))); assert(false && "Unexpected byte size."); return value; } template inline void ByteSwapInplace(T& value) { value = ByteSwap(value); }