JBus
JoyBus communication server
Common.hpp
Go to the documentation of this file.
1 #ifndef JBUS_COMMON_HPP
2 #define JBUS_COMMON_HPP
3 
4 #include <functional>
5 #include <stdint.h>
6 #include <stdlib.h>
7 
8 namespace jbus
9 {
10 
11 using s8 = int8_t;
12 using u8 = uint8_t;
13 using s16 = int16_t;
14 using u16 = uint16_t;
15 using s32 = int32_t;
16 using u32 = uint32_t;
17 using s64 = int64_t;
18 using u64 = uint64_t;
19 
20 #ifndef DOXYGEN_SHOULD_SKIP_THIS
21 
22 #undef bswap16
23 #undef bswap32
24 #undef bswap64
25 
26 /* Type-sensitive byte swappers */
27 template <typename T>
28 static inline T bswap16(T val)
29 {
30 #if __GNUC__
31  return __builtin_bswap16(val);
32 #elif _WIN32
33  return _byteswap_ushort(val);
34 #else
35  return (val = (val << 8) | ((val >> 8) & 0xFF));
36 #endif
37 }
38 
39 template <typename T>
40 static inline T bswap32(T val)
41 {
42 #if __GNUC__
43  return __builtin_bswap32(val);
44 #elif _WIN32
45  return _byteswap_ulong(val);
46 #else
47  val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
48  val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
49  return val;
50 #endif
51 }
52 
53 template <typename T>
54 static inline T bswap64(T val)
55 {
56 #if __GNUC__
57  return __builtin_bswap64(val);
58 #elif _WIN32
59  return _byteswap_uint64(val);
60 #else
61  return ((val & 0xFF00000000000000ULL) >> 56) |
62  ((val & 0x00FF000000000000ULL) >> 40) |
63  ((val & 0x0000FF0000000000ULL) >> 24) |
64  ((val & 0x000000FF00000000ULL) >> 8) |
65  ((val & 0x00000000FF000000ULL) << 8) |
66  ((val & 0x0000000000FF0000ULL) << 24) |
67  ((val & 0x000000000000FF00ULL) << 40) |
68  ((val & 0x00000000000000FFULL) << 56);
69 #endif
70 }
71 
72 
73 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
74 static inline int16_t SBig(int16_t val) {return bswap16(val);}
75 static inline uint16_t SBig(uint16_t val) {return bswap16(val);}
76 static inline int32_t SBig(int32_t val) {return bswap32(val);}
77 static inline uint32_t SBig(uint32_t val) {return bswap32(val);}
78 static inline int64_t SBig(int64_t val) {return bswap64(val);}
79 static inline uint64_t SBig(uint64_t val) {return bswap64(val);}
80 static inline float SBig(float val)
81 {
82  int32_t ival = bswap32(*((int32_t*)(&val)));
83  return *((float*)(&ival));
84 }
85 static inline double SBig(double val)
86 {
87  int64_t ival = bswap64(*((int64_t*)(&val)));
88  return *((double*)(&ival));
89 }
90 #ifndef SBIG
91 #define SBIG(q) ( ( (q) & 0x000000FF ) << 24 | ( (q) & 0x0000FF00 ) << 8 \
92  | ( (q) & 0x00FF0000 ) >> 8 | ( (q) & 0xFF000000 ) >> 24 )
93 #endif
94 
95 static inline int16_t SLittle(int16_t val) {return val;}
96 static inline uint16_t SLittle(uint16_t val) {return val;}
97 static inline int32_t SLittle(int32_t val) {return val;}
98 static inline uint32_t SLittle(uint32_t val) {return val;}
99 static inline int64_t SLittle(int64_t val) {return val;}
100 static inline uint64_t SLittle(uint64_t val) {return val;}
101 static inline float SLittle(float val) {return val;}
102 static inline double SLittle(double val) {return val;}
103 #ifndef SLITTLE
104 #define SLITTLE(q) (q)
105 #endif
106 #else
107 static inline int16_t SLittle(int16_t val) {return bswap16(val);}
108 static inline uint16_t SLittle(uint16_t val) {return bswap16(val);}
109 static inline int32_t SLittle(int32_t val) {return bswap32(val);}
110 static inline uint32_t SLittle(uint32_t val) {return bswap32(val);}
111 static inline int64_t SLittle(int64_t val) {return bswap64(val);}
112 static inline uint64_t SLittle(uint64_t val) {return bswap64(val);}
113 static inline float SLittle(float val)
114 {
115  int32_t ival = bswap32(*((int32_t*)(&val)));
116  return *((float*)(&ival));
117 }
118 static inline double SLittle(double val)
119 {
120  int64_t ival = bswap64(*((int64_t*)(&val)));
121  return *((double*)(&ival));
122 }
123 #ifndef SLITTLE
124 #define SLITTLE(q) ( ( (q) & 0x000000FF ) << 24 | ( (q) & 0x0000FF00 ) << 8 \
125  | ( (q) & 0x00FF0000 ) >> 8 | ( (q) & 0xFF000000 ) >> 24 )
126 #endif
127 
128 static inline int16_t SBig(int16_t val) {return val;}
129 static inline uint16_t SBig(uint16_t val) {return val;}
130 static inline int32_t SBig(int32_t val) {return val;}
131 static inline uint32_t SBig(uint32_t val) {return val;}
132 static inline int64_t SBig(int64_t val) {return val;}
133 static inline uint64_t SBig(uint64_t val) {return val;}
134 static inline float SBig(float val) {return val;}
135 static inline double SBig(double val) {return val;}
136 #ifndef SBIG
137 #define SBIG(q) (q)
138 #endif
139 #endif
140 
141 class Endpoint;
142 class ThreadLocalEndpoint;
143 
144 #endif
145 
147 {
155 };
156 
158 {
161  GBA_BUSY = 2,
164 };
165 
169 using FGBACallback = std::function<void(ThreadLocalEndpoint& endpoint, EJoyReturn status)>;
170 
173 u64 GetGCTicks();
174 
177 void WaitGCTicks(u64 ticks);
178 
181 static constexpr u64 GetGCTicksPerSec() { return 486000000ull; }
182 
184 void Initialize();
185 
186 }
187 
188 #endif // JBUS_COMMON_HPP
uint16_t u16
Definition: Common.hpp:14
int8_t s8
Definition: Common.hpp:11
Definition: Common.hpp:149
int64_t s64
Definition: Common.hpp:17
std::function< void(ThreadLocalEndpoint &endpoint, EJoyReturn status)> FGBACallback
Standard callback for asynchronous jbus::Endpoint APIs.
Definition: Common.hpp:169
Definition: Common.hpp:148
Definition: Common.hpp:160
void WaitGCTicks(u64 ticks)
Wait an approximate Dolphin tick duration (avoid using, it&#39;s rather inaccurate).
Definition: Common.hpp:154
Definition: Common.hpp:159
Definition: Common.hpp:153
uint8_t u8
Definition: Common.hpp:12
Definition: Common.hpp:8
Definition: Common.hpp:152
EJStatFlags
Definition: Common.hpp:146
uint64_t u64
Definition: Common.hpp:18
int16_t s16
Definition: Common.hpp:13
int32_t s32
Definition: Common.hpp:15
u64 GetGCTicks()
Get host system&#39;s timebase scaled into Dolphin ticks.
EJoyReturn
Definition: Common.hpp:157
Definition: Common.hpp:163
Definition: Endpoint.hpp:16
uint32_t u32
Definition: Common.hpp:16
Definition: Common.hpp:161
void Initialize()
Initialize platform specifics of JBus library.
Definition: Common.hpp:151
Definition: Common.hpp:162
Definition: Common.hpp:150
Definition: Endpoint.hpp:272