annotate include/core/endian.h @ 355:83e6ba09338f

gui/pages/seasons: fix function disambiguation on win32
author Paper <paper@paper.us.eu.org>
date Sun, 14 Jul 2024 23:46:18 -0400
parents 9aaf1e788896
children 99c961c91809
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #ifndef MINORI_CORE_ENDIAN_H_
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #define MINORI_CORE_ENDIAN_H_
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 /* definition of endian-related stuff. primarily used for*/
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <cstdint>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <type_traits>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 class Endian {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 private:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 static constexpr uint32_t uint32_ = 0x01020304;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 static constexpr uint8_t magic_ = static_cast<const uint8_t&>(uint32_);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 /* check for compiler builtins for byteswapping */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 #ifdef __has_builtin
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 # if __has_builtin(__builtin_bswap16)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 # define COMPILER_BUILTIN_BSWAP16(x) __builtin_bswap16(x)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 # endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 # if __has_builtin(__builtin_bswap32)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 # define COMPILER_BUILTIN_BSWAP32(x) __builtin_bswap32(x)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 # endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 # if __has_builtin(__builtin_bswap64)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 # define COMPILER_BUILTIN_BSWAP64(x) __builtin_bswap64(x)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 # endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 static constexpr uint16_t byteswap_16(uint16_t x) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 #ifdef COMPILER_BUILTIN_BSWAP16
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 return COMPILER_BUILTIN_BSWAP16(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 #else
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 return (
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 ((x & UINT16_C(0x00FF)) << 8)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 | ((x & UINT16_C(0xFF00)) >> 8)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 );
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 static constexpr uint32_t byteswap_32(uint32_t x) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 #ifdef COMPILER_BUILTIN_BSWAP32
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 return COMPILER_BUILTIN_BSWAP32(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 #else
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 return (
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 ((x & UINT32_C(0x000000FF)) << 24)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 | ((x & UINT32_C(0x0000FF00)) << 8)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 | ((x & UINT32_C(0x00FF0000)) >> 8)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 | ((x & UINT32_C(0xFF000000)) >> 24)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 );
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 static constexpr uint64_t byteswap_64(uint64_t x) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 #ifdef COMPILER_BUILTIN_BSWAP64
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 return COMPILER_BUILTIN_BSWAP64(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 #else
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 return (
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 ((x & UINT64_C(0x00000000000000FF)) << 56)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 | ((x & UINT64_C(0x000000000000FF00)) << 40)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 | ((x & UINT64_C(0x0000000000FF0000)) << 24)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 | ((x & UINT64_C(0x00000000FF000000)) << 8)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 | ((x & UINT64_C(0x000000FF00000000)) >> 8)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 | ((x & UINT64_C(0x0000FF0000000000)) >> 24)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 | ((x & UINT64_C(0x00FF000000000000)) >> 40)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 | ((x & UINT64_C(0xFF00000000000000)) >> 56)
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 );
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 #ifdef COMPILER_BUILTIN_BSWAP16
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 # undef COMPILER_BUILTIN_BSWAP16
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 #ifdef COMPILER_BUILTIN_BSWAP32
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 # undef COMPILER_BUILTIN_BSWAP32
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 #ifdef COMPILER_BUILTIN_BSWAP64
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 # undef COMPILER_BUILTIN_BSWAP64
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 #endif
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 public:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 static constexpr bool little = magic_ == 0x04;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 static constexpr bool big = magic_ == 0x01;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 static_assert(little || big, "unsupported endianness");
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 static constexpr T byteswap(T x) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 static_assert(std::is_integral<T>::value);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 static_assert(std::is_unsigned<T>::value);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 if constexpr (std::is_same<T, uint8_t>::value) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 return x;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 } else if constexpr (std::is_same<T, uint16_t>::value) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 return byteswap_16(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 } else if constexpr (std::is_same<T, uint32_t>::value) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 return byteswap_32(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 } else if constexpr (std::is_same<T, uint64_t>::value) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 return byteswap_64(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 } else {
354
9aaf1e788896 core/endian: fix compile error under clang
Paper <paper@paper.us.eu.org>
parents: 351
diff changeset
96 static_assert(!sizeof(T), "invalid integer type given to byteswap");
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 static constexpr T byteswap_little_to_host(T x) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 if constexpr (little) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 return x;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 } else if constexpr (big) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 return byteswap(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109 template<typename T>
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 static constexpr T byteswap_big_to_host(T x) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 if constexpr (big) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 return x;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 } else if constexpr (little) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 return byteswap(x);
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 private:
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118 Endian() = delete;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121 #endif /* MINORI_CORE_ENDIAN_H_ */