comparison foosdk/sdk/pfc/byte_order.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 namespace pfc {
4 void byteswap_raw(void * p_buffer,t_size p_bytes);
5
6 template<typename T> T byteswap_t(T p_source);
7
8 template<> inline char byteswap_t<char>(char p_source) {return p_source;}
9 template<> inline unsigned char byteswap_t<unsigned char>(unsigned char p_source) {return p_source;}
10 template<> inline signed char byteswap_t<signed char>(signed char p_source) {return p_source;}
11
12 template<typename T> T byteswap_int_t(T p_source) {
13 enum { width = sizeof(T) };
14 typedef typename sized_int_t<width>::t_unsigned tU;
15 tU in = p_source, out = 0;
16 for(unsigned walk = 0; walk < width; ++walk) {
17 out |= ((in >> (walk * 8)) & 0xFF) << ((width - 1 - walk) * 8);
18 }
19 return out;
20 }
21
22 #ifdef _MSC_VER//does this even help with performance/size?
23 template<> inline wchar_t byteswap_t<wchar_t>(wchar_t p_source) {return _byteswap_ushort(p_source);}
24
25 template<> inline short byteswap_t<short>(short p_source) {return _byteswap_ushort(p_source);}
26 template<> inline unsigned short byteswap_t<unsigned short>(unsigned short p_source) {return _byteswap_ushort(p_source);}
27
28 template<> inline int byteswap_t<int>(int p_source) {return _byteswap_ulong(p_source);}
29 template<> inline unsigned int byteswap_t<unsigned int>(unsigned int p_source) {return _byteswap_ulong(p_source);}
30
31 template<> inline long byteswap_t<long>(long p_source) {return _byteswap_ulong(p_source);}
32 template<> inline unsigned long byteswap_t<unsigned long>(unsigned long p_source) {return _byteswap_ulong(p_source);}
33
34 template<> inline long long byteswap_t<long long>(long long p_source) {return _byteswap_uint64(p_source);}
35 template<> inline unsigned long long byteswap_t<unsigned long long>(unsigned long long p_source) {return _byteswap_uint64(p_source);}
36 #else
37 template<> inline wchar_t byteswap_t<wchar_t>(wchar_t p_source) {return byteswap_int_t(p_source);}
38
39 template<> inline short byteswap_t<short>(short p_source) {return byteswap_int_t(p_source);}
40 template<> inline unsigned short byteswap_t<unsigned short>(unsigned short p_source) {return byteswap_int_t(p_source);}
41
42 template<> inline int byteswap_t<int>(int p_source) {return byteswap_int_t(p_source);}
43 template<> inline unsigned int byteswap_t<unsigned int>(unsigned int p_source) {return byteswap_int_t(p_source);}
44
45 template<> inline long byteswap_t<long>(long p_source) {return byteswap_int_t(p_source);}
46 template<> inline unsigned long byteswap_t<unsigned long>(unsigned long p_source) {return byteswap_int_t(p_source);}
47
48 template<> inline long long byteswap_t<long long>(long long p_source) {return byteswap_int_t(p_source);}
49 template<> inline unsigned long long byteswap_t<unsigned long long>(unsigned long long p_source) {return byteswap_int_t(p_source);}
50 #endif
51
52 template<> inline float byteswap_t<float>(float p_source) {
53 float ret;
54 *(t_uint32*) &ret = byteswap_t(*(const t_uint32*)&p_source );
55 return ret;
56 }
57
58 template<> inline double byteswap_t<double>(double p_source) {
59 double ret;
60 *(t_uint64*) &ret = byteswap_t(*(const t_uint64*)&p_source );
61 return ret;
62 }
63
64 //blargh at GUID byteswap issue
65 template<> inline GUID byteswap_t<GUID>(GUID p_guid) {
66 GUID ret;
67 ret.Data1 = pfc::byteswap_t(p_guid.Data1);
68 ret.Data2 = pfc::byteswap_t(p_guid.Data2);
69 ret.Data3 = pfc::byteswap_t(p_guid.Data3);
70 ret.Data4[0] = p_guid.Data4[0];
71 ret.Data4[1] = p_guid.Data4[1];
72 ret.Data4[2] = p_guid.Data4[2];
73 ret.Data4[3] = p_guid.Data4[3];
74 ret.Data4[4] = p_guid.Data4[4];
75 ret.Data4[5] = p_guid.Data4[5];
76 ret.Data4[6] = p_guid.Data4[6];
77 ret.Data4[7] = p_guid.Data4[7];
78 return ret;
79 }
80
81 #if ! defined(_MSC_VER) || _MSC_VER >= 1900
82 template<> inline char16_t byteswap_t<char16_t>(char16_t v) { return (char16_t)byteswap_t((uint16_t)v); }
83 template<> inline char32_t byteswap_t<char32_t>(char32_t v) { return (char32_t)byteswap_t((uint32_t)v); }
84 #endif
85 };
86
87 #ifdef _MSC_VER
88
89 // No MSVC platform is ever big endian
90 #define PFC_BYTE_ORDER_IS_BIG_ENDIAN 0
91
92 #else//_MSC_VER
93
94 #if defined(__APPLE__)
95 #include <architecture/byte_order.h>
96 #else
97 #include <endian.h>
98 #endif
99
100 #if __BYTE_ORDER == __LITTLE_ENDIAN
101 #define PFC_BYTE_ORDER_IS_BIG_ENDIAN 0
102 #else
103 #define PFC_BYTE_ORDER_IS_BIG_ENDIAN 1
104 #endif
105
106 #endif//_MSC_VER
107
108 #ifdef PFC_BYTE_ORDER_IS_BIG_ENDIAN
109 #define PFC_BYTE_ORDER_IS_LITTLE_ENDIAN (!(PFC_BYTE_ORDER_IS_BIG_ENDIAN))
110 #else
111 #error please update byte order #defines
112 #endif
113
114
115 namespace pfc {
116 static constexpr bool byte_order_is_big_endian = !!PFC_BYTE_ORDER_IS_BIG_ENDIAN;
117 static constexpr bool byte_order_is_little_endian = !!PFC_BYTE_ORDER_IS_LITTLE_ENDIAN;
118
119 template<typename T> T byteswap_if_be_t(T p_param) {return byte_order_is_big_endian ? byteswap_t(p_param) : p_param;}
120 template<typename T> T byteswap_if_le_t(T p_param) {return byte_order_is_little_endian ? byteswap_t(p_param) : p_param;}
121 }
122
123 namespace byte_order {
124
125 #if PFC_BYTE_ORDER_IS_BIG_ENDIAN//big endian
126 template<typename T> inline void order_native_to_le_t(T& param) {param = pfc::byteswap_t(param);}
127 template<typename T> inline void order_native_to_be_t(T& param) { (void)param; }
128 template<typename T> inline void order_le_to_native_t(T& param) {param = pfc::byteswap_t(param);}
129 template<typename T> inline void order_be_to_native_t(T& param) { (void)param; }
130 #else//little endian
131 template<typename T> inline void order_native_to_le_t(T& param) { (void)param; }
132 template<typename T> inline void order_native_to_be_t(T& param) {param = pfc::byteswap_t(param);}
133 template<typename T> inline void order_le_to_native_t(T& param) { (void)param; }
134 template<typename T> inline void order_be_to_native_t(T& param) {param = pfc::byteswap_t(param);}
135 #endif
136 };
137
138
139
140 namespace pfc {
141 template<typename TInt,unsigned width,bool IsBigEndian>
142 class __EncodeIntHelper {
143 public:
144 inline static void Run(TInt p_value,t_uint8 * p_out) {
145 *p_out = (t_uint8)(p_value);
146 __EncodeIntHelper<TInt,width-1,IsBigEndian>::Run(p_value >> 8,p_out + (IsBigEndian ? -1 : 1));
147 }
148 };
149
150 template<typename TInt,bool IsBigEndian>
151 class __EncodeIntHelper<TInt,1,IsBigEndian> {
152 public:
153 inline static void Run(TInt p_value,t_uint8* p_out) {
154 *p_out = (t_uint8)(p_value);
155 }
156 };
157 template<typename TInt,bool IsBigEndian>
158 class __EncodeIntHelper<TInt,0,IsBigEndian> {
159 public:
160 inline static void Run(TInt,t_uint8*) {}
161 };
162
163 template<typename TInt>
164 inline void encode_little_endian(t_uint8 * p_buffer,TInt p_value) {
165 __EncodeIntHelper<TInt,sizeof(TInt),false>::Run(p_value,p_buffer);
166 }
167 template<typename TInt>
168 inline void encode_big_endian(t_uint8 * p_buffer,TInt p_value) {
169 __EncodeIntHelper<TInt,sizeof(TInt),true>::Run(p_value,p_buffer + (sizeof(TInt) - 1));
170 }
171
172
173 template<typename TInt,unsigned width,bool IsBigEndian>
174 class __DecodeIntHelper {
175 public:
176 inline static TInt Run(const t_uint8 * p_in) {
177 return (__DecodeIntHelper<TInt,width-1,IsBigEndian>::Run(p_in + (IsBigEndian ? -1 : 1)) << 8) + *p_in;
178 }
179 };
180
181 template<typename TInt,bool IsBigEndian>
182 class __DecodeIntHelper<TInt,1,IsBigEndian> {
183 public:
184 inline static TInt Run(const t_uint8* p_in) {return *p_in;}
185 };
186
187 template<typename TInt,bool IsBigEndian>
188 class __DecodeIntHelper<TInt,0,IsBigEndian> {
189 public:
190 inline static TInt Run(const t_uint8*) {return 0;}
191 };
192
193 template<typename TInt>
194 inline void decode_little_endian(TInt & p_out,const t_uint8 * p_buffer) {
195 p_out = __DecodeIntHelper<TInt,sizeof(TInt),false>::Run(p_buffer);
196 }
197
198 template<typename TInt>
199 inline void decode_big_endian(TInt & p_out,const t_uint8 * p_buffer) {
200 p_out = __DecodeIntHelper<TInt,sizeof(TInt),true>::Run(p_buffer + (sizeof(TInt) - 1));
201 }
202
203 template<typename TInt>
204 inline TInt decode_little_endian(const t_uint8 * p_buffer) {
205 TInt temp;
206 decode_little_endian(temp,p_buffer);
207 return temp;
208 }
209
210 template<typename TInt>
211 inline TInt decode_big_endian(const t_uint8 * p_buffer) {
212 TInt temp;
213 decode_big_endian(temp,p_buffer);
214 return temp;
215 }
216
217 template<bool IsBigEndian,typename TInt>
218 inline void decode_endian(TInt & p_out,const t_uint8 * p_buffer) {
219 if (IsBigEndian) decode_big_endian(p_out,p_buffer);
220 else decode_little_endian(p_out,p_buffer);
221 }
222 template<bool IsBigEndian,typename TInt>
223 inline void encode_endian(t_uint8 * p_buffer,TInt p_in) {
224 if (IsBigEndian) encode_big_endian(p_in,p_buffer);
225 else encode_little_endian(p_in,p_buffer);
226 }
227
228
229
230 template<unsigned width>
231 inline void reverse_bytes(t_uint8 * p_buffer) {
232 pfc::swap_t(p_buffer[0],p_buffer[width-1]);
233 reverse_bytes<width-2>(p_buffer+1);
234 }
235
236 template<> inline void reverse_bytes<1>(t_uint8 * ) { }
237 template<> inline void reverse_bytes<0>(t_uint8 * ) { }
238
239 inline int32_t readInt24(const void * mem) {
240 const uint8_t * p = (const uint8_t*) mem;
241 int32_t ret;
242 if (byte_order_is_little_endian) {
243 ret = p[0];
244 ret |= (uint32_t)p[1] << 8;
245 ret |= (int32_t)(int8_t)p[2] << 16;
246 return ret;
247 } else {
248 ret = p[2];
249 ret |= (uint32_t)p[1] << 8;
250 ret |= (int32_t)(int8_t)p[0] << 16;
251 return ret;
252 }
253 }
254 }
255