endian: add signed byteswap
This commit is contained in:
parent
f3fb385686
commit
0997bbbaac
27
endian.hpp
27
endian.hpp
@ -35,12 +35,29 @@ namespace util {
|
|||||||
constexpr T
|
constexpr T
|
||||||
bswap (T);
|
bswap (T);
|
||||||
|
|
||||||
//template <> constexpr int8_t bswap ( int8_t v) { return v; }
|
// CXX: Update using "if constexpr" when available. It doesn't seem to be
|
||||||
//template <> constexpr int16_t bswap (int16_t v) { return __builtin_bswap16 (v); }
|
// worth the dicking about to use enable_if_t to differentiate the
|
||||||
//template <> constexpr int32_t bswap (int32_t v) { return __builtin_bswap32 (v); }
|
// signed/unsigned cases.
|
||||||
//template <> constexpr int64_t bswap (int64_t v) { return __builtin_bswap64 (v); }
|
#define SIGNED_BSWAP(S) \
|
||||||
|
template <> \
|
||||||
|
constexpr \
|
||||||
|
int##S##_t \
|
||||||
|
bswap (int##S##_t v) { \
|
||||||
|
const union { \
|
||||||
|
int##S##_t s; \
|
||||||
|
uint##S##_t u; \
|
||||||
|
} vu = { v }; \
|
||||||
|
\
|
||||||
|
return __builtin_bswap##S (vu.u); \
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNED_BSWAP(16)
|
||||||
|
SIGNED_BSWAP(32)
|
||||||
|
SIGNED_BSWAP(64)
|
||||||
|
|
||||||
|
template <> constexpr int8_t bswap ( int8_t v) { return v; }
|
||||||
|
template <> constexpr uint8_t bswap (uint8_t v) { return v; }
|
||||||
|
|
||||||
template <> constexpr uint8_t bswap ( uint8_t v) { return v; }
|
|
||||||
template <> constexpr uint16_t bswap (uint16_t v) { return __builtin_bswap16 (v); }
|
template <> constexpr uint16_t bswap (uint16_t v) { return __builtin_bswap16 (v); }
|
||||||
template <> constexpr uint32_t bswap (uint32_t v) { return __builtin_bswap32 (v); }
|
template <> constexpr uint32_t bswap (uint32_t v) { return __builtin_bswap32 (v); }
|
||||||
template <> constexpr uint64_t bswap (uint64_t v) { return __builtin_bswap64 (v); }
|
template <> constexpr uint64_t bswap (uint64_t v) { return __builtin_bswap64 (v); }
|
||||||
|
Loading…
Reference in New Issue
Block a user