#ifndef KYTY_COMMON_PLATFORM_SYSSWAPBYTEORDER_H_ #define KYTY_COMMON_PLATFORM_SYSSWAPBYTEORDER_H_ #include "common/common.h" #include inline uint16_t SwapByteOrder16(uint16_t value) { uint16_t hi = value << 8u; uint16_t lo = value >> 8u; return hi | lo; } inline uint32_t SwapByteOrder32(uint32_t value) { return __builtin_bswap32(value); } inline uint64_t SwapByteOrder64(uint64_t value) { return __builtin_bswap64(value); } template inline void SwapByteOrder(T& x) { if (sizeof(x) == 2) { if (std::is_signed_v) { x = static_cast>( SwapByteOrder16(static_cast>(x))); } else { x = SwapByteOrder16(x); } } if (sizeof(x) == 4) { if (std::is_signed_v) { x = static_cast>( SwapByteOrder32(static_cast>(x))); } else { x = SwapByteOrder32(x); } } if (sizeof(x) == 8) { if (std::is_signed_v) { x = static_cast>( SwapByteOrder64(static_cast>(x))); } else { x = SwapByteOrder64(x); } } } template inline void NoSwapByteOrder(T& x) {} #endif /* KYTY_COMMON_PLATFORM_SYSSWAPBYTEORDER_H_ */