My Project
ByteSwap.h
1 /*
2 Code is mostly from Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 Copyright (c) 2006-2012, assimp team
5 All rights reserved.
6 ----------------------------------------------------------------------
7 */
8 
11 #pragma once
12 #include "ParaEngine.h"
13 
14 #if _MSC_VER >= 1400
15 #include <stdlib.h>
16 #endif
17 
18 namespace ParaEngine {
19 // --------------------------------------------------------------------------------------
24 // --------------------------------------------------------------------------------------
25 class ByteSwap
26 {
27  ByteSwap() {}
28 
29 public:
30 
31  // ----------------------------------------------------------------------
34  static inline void Swap2(void* _szOut)
35  {
36  PE_ASSERT(_szOut);
37 
38 #if _MSC_VER >= 1400
39  uint16_t* const szOut = reinterpret_cast<uint16_t*>(_szOut);
40  *szOut = _byteswap_ushort(*szOut);
41 #else
42  uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
43  std::swap(szOut[0],szOut[1]);
44 #endif
45  }
46 
47  // ----------------------------------------------------------------------
50  static inline void Swap4(void* _szOut)
51  {
52  PE_ASSERT(_szOut);
53 
54 #if _MSC_VER >= 1400
55  uint32_t* const szOut = reinterpret_cast<uint32_t*>(_szOut);
56  *szOut = _byteswap_ulong(*szOut);
57 #else
58  uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
59  std::swap(szOut[0],szOut[3]);
60  std::swap(szOut[1],szOut[2]);
61 #endif
62  }
63 
64  // ----------------------------------------------------------------------
67  static inline void Swap8(void* _szOut)
68  {
69  PE_ASSERT(_szOut);
70 
71 #if _MSC_VER >= 1400
72  uint64_t* const szOut = reinterpret_cast<uint64_t*>(_szOut);
73  *szOut = _byteswap_uint64(*szOut);
74 #else
75  uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
76  std::swap(szOut[0],szOut[7]);
77  std::swap(szOut[1],szOut[6]);
78  std::swap(szOut[2],szOut[5]);
79  std::swap(szOut[3],szOut[4]);
80 #endif
81  }
82 
83  // ----------------------------------------------------------------------
86  static inline void Swap(float* fOut) {
87  Swap4(fOut);
88  }
89 
90  // ----------------------------------------------------------------------
93  static inline void Swap(double* fOut) {
94  Swap8(fOut);
95  }
96 
97 
98  // ----------------------------------------------------------------------
101  static inline void Swap(int16_t* fOut) {
102  Swap2(fOut);
103  }
104 
105  static inline void Swap(uint16_t* fOut) {
106  Swap2(fOut);
107  }
108 
109  // ----------------------------------------------------------------------
112  static inline void Swap(int32_t* fOut){
113  Swap4(fOut);
114  }
115 
116  static inline void Swap(uint32_t* fOut){
117  Swap4(fOut);
118  }
119 
120  // ----------------------------------------------------------------------
123  static inline void Swap(int64_t* fOut) {
124  Swap8(fOut);
125  }
126 
127  static inline void Swap(uint64_t* fOut) {
128  Swap8(fOut);
129  }
130 
131  // ----------------------------------------------------------------------
134  template<typename Type>
135  static inline Type Swapped(Type tOut)
136  {
137  return _swapper<Type,sizeof(Type)>()(tOut);
138  }
139 
140 private:
141 
142  template <typename T, size_t size> struct _swapper;
143 };
144 
145 template <typename T> struct ByteSwap::_swapper<T,2> {
146  T operator() (T tOut) {
147  Swap2(&tOut);
148  return tOut;
149  }
150 };
151 
152 template <typename T> struct ByteSwap::_swapper<T,4> {
153  T operator() (T tOut) {
154  Swap4(&tOut);
155  return tOut;
156  }
157 };
158 
159 template <typename T> struct ByteSwap::_swapper<T,8> {
160  T operator() (T tOut) {
161  Swap8(&tOut);
162  return tOut;
163  }
164 };
165 
166 
167 // --------------------------------------------------------------------------------------
168 // ByteSwap macros for BigEndian/LittleEndian support
169 // --------------------------------------------------------------------------------------
170 #if ! PLATFORM_LITTLE_ENDIAN
171 # define PE_LE(t) (t)
172 # define PE_BE(t) ByteSwap::Swapped(t)
173 # define PE_LSWAP2(p)
174 # define PE_LSWAP4(p)
175 # define PE_LSWAP8(p)
176 # define PE_LSWAP2P(p)
177 # define PE_LSWAP4P(p)
178 # define PE_LSWAP8P(p)
179 # define LE_NCONST const
180 # define PE_SWAP2(p) ByteSwap::Swap2(&(p))
181 # define PE_SWAP4(p) ByteSwap::Swap4(&(p))
182 # define PE_SWAP8(p) ByteSwap::Swap8(&(p))
183 # define PE_SWAP2P(p) ByteSwap::Swap2((p))
184 # define PE_SWAP4P(p) ByteSwap::Swap4((p))
185 # define PE_SWAP8P(p) ByteSwap::Swap8((p))
186 # define BE_NCONST
187 #else
188 # define PE_BE(t) (t)
189 # define PE_LE(t) ByteSwap::Swapped(t)
190 # define PE_SWAP2(p)
191 # define PE_SWAP4(p)
192 # define PE_SWAP8(p)
193 # define PE_SWAP2P(p)
194 # define PE_SWAP4P(p)
195 # define PE_SWAP8P(p)
196 # define BE_NCONST const
197 # define PE_LSWAP2(p) ByteSwap::Swap2(&(p))
198 # define PE_LSWAP4(p) ByteSwap::Swap4(&(p))
199 # define PE_LSWAP8(p) ByteSwap::Swap8(&(p))
200 # define PE_LSWAP2P(p) ByteSwap::Swap2((p))
201 # define PE_LSWAP4P(p) ByteSwap::Swap4((p))
202 # define PE_LSWAP8P(p) ByteSwap::Swap8((p))
203 # define LE_NCONST
204 #endif
205 
206 
207 namespace Intern {
208 
209 // --------------------------------------------------------------------------------------------
210 template <typename T, bool doit>
211 struct ByteSwapper {
212  void operator() (T* inout) {
213  ByteSwap::Swap(inout);
214  }
215 };
216 
217 template <typename T>
218 struct ByteSwapper<T,false> {
219  void operator() (T*) {
220  }
221 };
222 
223 // --------------------------------------------------------------------------------------------
224 template <bool SwapEndianess, typename T, bool RuntimeSwitch>
225 struct Getter {
226  void operator() (T* inout, bool le) {
227 #ifdef AI_BUILD_BIG_ENDIAN
228  le = le;
229 #else
230  le = !le;
231 #endif
232  if (le) {
233  ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout);
234  }
235  else ByteSwapper<T,false> () (inout);
236  }
237 };
238 
239 template <bool SwapEndianess, typename T>
240 struct Getter<SwapEndianess,T,false> {
241 
242  void operator() (T* inout, bool /*le*/) {
243  // static branch
245  }
246 };
247 } // end Intern
248 }
249 
static void Swap4(void *_szOut)
Swap four bytes of data.
Definition: ByteSwap.h:50
static void Swap8(void *_szOut)
Swap eight bytes of data.
Definition: ByteSwap.h:67
different physics engine has different winding order.
Definition: EventBinding.h:32
static Type Swapped(Type tOut)
Templatized ByteSwap.
Definition: ByteSwap.h:135
static void Swap(int16_t *fOut)
ByteSwap an int16t.
Definition: ByteSwap.h:101
static void Swap(int64_t *fOut)
ByteSwap an int64t.
Definition: ByteSwap.h:123
Definition: ByteSwap.h:211
Defines some useful byte order swap routines.
Definition: ByteSwap.h:25
static void Swap(int32_t *fOut)
ByteSwap an int32t.
Definition: ByteSwap.h:112
static void Swap(double *fOut)
ByteSwap a double.
Definition: ByteSwap.h:93
static void Swap(float *fOut)
ByteSwap a float.
Definition: ByteSwap.h:86
static void Swap2(void *_szOut)
Swap two bytes of data.
Definition: ByteSwap.h:34
Definition: ByteSwap.h:225