File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33#include < cassert>
44
5+ #ifdef __clang__
6+ #define _byte_swap16 (value ) __builtin_bswap16(static_cast <uint16_t >(value))
7+ #define _byte_swap32 (value ) __builtin_bswap32(static_cast <uint32_t >(value))
8+ #define _byte_swap64 (value ) __builtin_bswap64(static_cast <uint64_t >(value))
9+ #elif defined(MSC_VER)
10+ #define _byte_swap16 (value ) _byteswap_ushort(static_cast <uint16_t >(value))
11+ #define _byte_swap32 (value ) _byteswap_ulong(static_cast <uint32_t >(value))
12+ #define _byte_swap64 (value ) _byteswap_uint64(static_cast <uint64_t >(value))
13+ #endif
14+
515template <typename T>
616inline T ByteSwap (T value)
717{
818 if constexpr (sizeof (T) == 1 )
919 return value;
10- else if constexpr (sizeof (T) == 2 )
11- return static_cast <T>(__builtin_bswap16 ( static_cast < uint16_t >( value) ));
12- else if constexpr (sizeof (T) == 4 )
13- return static_cast <T>(__builtin_bswap32 ( static_cast < uint32_t >( value) ));
14- else if constexpr (sizeof (T) == 8 )
15- return static_cast <T>(__builtin_bswap64 ( static_cast < uint64_t >( value) ));
20+ if constexpr (sizeof (T) == 2 )
21+ return static_cast <T>(_byte_swap16 ( value));
22+ if constexpr (sizeof (T) == 4 )
23+ return static_cast <T>(_byte_swap32 ( value));
24+ if constexpr (sizeof (T) == 8 )
25+ return static_cast <T>(_byte_swap64 ( value));
1626
1727 assert (false && " Unexpected byte size." );
1828 return value;
You can’t perform that action at this time.
0 commit comments