Mercurial > minori
diff include/core/endian.h @ 369:47c9f8502269
*: clang-format all the things
I've edited the formatting a bit. Now pointer asterisks (and reference
ampersands) are on the variable instead of the type, as well as having
newlines for function braces (but nothing else)
author | Paper <paper@tflc.us> |
---|---|
date | Fri, 25 Jul 2025 10:16:02 -0400 |
parents | 99c961c91809 |
children |
line wrap: on
line diff
--- a/include/core/endian.h Fri Jul 25 10:05:23 2025 -0400 +++ b/include/core/endian.h Fri Jul 25 10:16:02 2025 -0400 @@ -12,66 +12,56 @@ private: /* check for compiler builtins for byteswapping */ #ifdef __has_builtin -# if __has_builtin(__builtin_bswap16) -# define COMPILER_BUILTIN_BSWAP16(x) __builtin_bswap16(x) -# endif -# if __has_builtin(__builtin_bswap32) -# define COMPILER_BUILTIN_BSWAP32(x) __builtin_bswap32(x) -# endif -# if __has_builtin(__builtin_bswap64) -# define COMPILER_BUILTIN_BSWAP64(x) __builtin_bswap64(x) -# endif +# if __has_builtin(__builtin_bswap16) +# define COMPILER_BUILTIN_BSWAP16(x) __builtin_bswap16(x) +# endif +# if __has_builtin(__builtin_bswap32) +# define COMPILER_BUILTIN_BSWAP32(x) __builtin_bswap32(x) +# endif +# if __has_builtin(__builtin_bswap64) +# define COMPILER_BUILTIN_BSWAP64(x) __builtin_bswap64(x) +# endif #endif - static constexpr uint16_t byteswap_16(uint16_t x) { + static constexpr uint16_t byteswap_16(uint16_t x) + { #ifdef COMPILER_BUILTIN_BSWAP16 return COMPILER_BUILTIN_BSWAP16(x); #else - return ( - ((x & UINT16_C(0x00FF)) << 8) - | ((x & UINT16_C(0xFF00)) >> 8) - ); + return (((x & UINT16_C(0x00FF)) << 8) | ((x & UINT16_C(0xFF00)) >> 8)); #endif } - static constexpr uint32_t byteswap_32(uint32_t x) { + static constexpr uint32_t byteswap_32(uint32_t x) + { #ifdef COMPILER_BUILTIN_BSWAP32 return COMPILER_BUILTIN_BSWAP32(x); #else - return ( - ((x & UINT32_C(0x000000FF)) << 24) - | ((x & UINT32_C(0x0000FF00)) << 8) - | ((x & UINT32_C(0x00FF0000)) >> 8) - | ((x & UINT32_C(0xFF000000)) >> 24) - ); + return (((x & UINT32_C(0x000000FF)) << 24) | ((x & UINT32_C(0x0000FF00)) << 8) | + ((x & UINT32_C(0x00FF0000)) >> 8) | ((x & UINT32_C(0xFF000000)) >> 24)); #endif } - static constexpr uint64_t byteswap_64(uint64_t x) { + static constexpr uint64_t byteswap_64(uint64_t x) + { #ifdef COMPILER_BUILTIN_BSWAP64 return COMPILER_BUILTIN_BSWAP64(x); #else - return ( - ((x & UINT64_C(0x00000000000000FF)) << 56) - | ((x & UINT64_C(0x000000000000FF00)) << 40) - | ((x & UINT64_C(0x0000000000FF0000)) << 24) - | ((x & UINT64_C(0x00000000FF000000)) << 8) - | ((x & UINT64_C(0x000000FF00000000)) >> 8) - | ((x & UINT64_C(0x0000FF0000000000)) >> 24) - | ((x & UINT64_C(0x00FF000000000000)) >> 40) - | ((x & UINT64_C(0xFF00000000000000)) >> 56) - ); + return (((x & UINT64_C(0x00000000000000FF)) << 56) | ((x & UINT64_C(0x000000000000FF00)) << 40) | + ((x & UINT64_C(0x0000000000FF0000)) << 24) | ((x & UINT64_C(0x00000000FF000000)) << 8) | + ((x & UINT64_C(0x000000FF00000000)) >> 8) | ((x & UINT64_C(0x0000FF0000000000)) >> 24) | + ((x & UINT64_C(0x00FF000000000000)) >> 40) | ((x & UINT64_C(0xFF00000000000000)) >> 56)); #endif } #ifdef COMPILER_BUILTIN_BSWAP16 -# undef COMPILER_BUILTIN_BSWAP16 +# undef COMPILER_BUILTIN_BSWAP16 #endif #ifdef COMPILER_BUILTIN_BSWAP32 -# undef COMPILER_BUILTIN_BSWAP32 +# undef COMPILER_BUILTIN_BSWAP32 #endif #ifdef COMPILER_BUILTIN_BSWAP64 -# undef COMPILER_BUILTIN_BSWAP64 +# undef COMPILER_BUILTIN_BSWAP64 #endif public: #if defined(BYTE_ORDER_BIG) @@ -81,11 +71,12 @@ static constexpr bool big = false; static constexpr bool little = true; #else -#error "unsupported endianness" +# error "unsupported endianness" #endif template<typename T> - static constexpr T byteswap(T x) { + static constexpr T byteswap(T x) + { static_assert(std::is_integral<T>::value); static_assert(std::is_unsigned<T>::value, "use signed_byteswap"); @@ -104,7 +95,8 @@ /* this can't be constexpr */ template<typename T> - static T signed_byteswap(T x) { + static T signed_byteswap(T x) + { static_assert(std::is_integral<T>::value); static_assert(std::is_signed<T>::value, "use regular byteswap"); @@ -113,7 +105,8 @@ } template<typename T> - static constexpr T byteswap_little_to_host(T x) { + static constexpr T byteswap_little_to_host(T x) + { if constexpr (little) { return x; } else if constexpr (big) { @@ -122,7 +115,8 @@ } template<typename T> - static constexpr T byteswap_big_to_host(T x) { + static constexpr T byteswap_big_to_host(T x) + { if constexpr (big) { return x; } else if constexpr (little) { @@ -131,7 +125,8 @@ } template<typename T> - static T signed_byteswap_little_to_host(T x) { + static T signed_byteswap_little_to_host(T x) + { if constexpr (little) { return x; } else if constexpr (big) { @@ -140,13 +135,15 @@ } template<typename T> - static T signed_byteswap_big_to_host(T x) { + static T signed_byteswap_big_to_host(T x) + { if constexpr (big) { return x; } else if constexpr (little) { return signed_byteswap(x); } } + private: Endian() = delete; };