Path Tracer
ByteSwapper.h
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15  copyright notice, this list of conditions and the
16  following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the
20  following disclaimer in the documentation and/or other
21  materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24  contributors may be used to endorse or promote products
25  derived from this software without specific prior
26  written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
42 
45 #pragma once
46 #ifndef AI_BYTESWAPPER_H_INC
47 #define AI_BYTESWAPPER_H_INC
48 
49 #ifdef __GNUC__
50 # pragma GCC system_header
51 #endif
52 
53 #include <assimp/ai_assert.h>
54 #include <assimp/types.h>
55 #include <stdint.h>
56 
57 #if _MSC_VER >= 1400
58 #include <stdlib.h>
59 #endif
60 
61 namespace Assimp {
62 // --------------------------------------------------------------------------------------
67 // --------------------------------------------------------------------------------------
68 class ByteSwap {
69  ByteSwap() AI_NO_EXCEPT {}
70 
71 public:
72 
73  // ----------------------------------------------------------------------
76  static inline void Swap2(void* _szOut)
77  {
78  ai_assert(_szOut);
79 
80 #if _MSC_VER >= 1400
81  uint16_t* const szOut = reinterpret_cast<uint16_t*>(_szOut);
82  *szOut = _byteswap_ushort(*szOut);
83 #else
84  uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
85  std::swap(szOut[0],szOut[1]);
86 #endif
87  }
88 
89  // ----------------------------------------------------------------------
92  static inline void Swap4(void* _szOut)
93  {
94  ai_assert(_szOut);
95 
96 #if _MSC_VER >= 1400
97  uint32_t* const szOut = reinterpret_cast<uint32_t*>(_szOut);
98  *szOut = _byteswap_ulong(*szOut);
99 #else
100  uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
101  std::swap(szOut[0],szOut[3]);
102  std::swap(szOut[1],szOut[2]);
103 #endif
104  }
105 
106  // ----------------------------------------------------------------------
109  static inline void Swap8(void* _szOut)
110  {
111  ai_assert(_szOut);
112 
113 #if _MSC_VER >= 1400
114  uint64_t* const szOut = reinterpret_cast<uint64_t*>(_szOut);
115  *szOut = _byteswap_uint64(*szOut);
116 #else
117  uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
118  std::swap(szOut[0],szOut[7]);
119  std::swap(szOut[1],szOut[6]);
120  std::swap(szOut[2],szOut[5]);
121  std::swap(szOut[3],szOut[4]);
122 #endif
123  }
124 
125  // ----------------------------------------------------------------------
128  static inline void Swap(float* fOut) {
129  Swap4(fOut);
130  }
131 
132  // ----------------------------------------------------------------------
135  static inline void Swap(double* fOut) {
136  Swap8(fOut);
137  }
138 
139 
140  // ----------------------------------------------------------------------
143  static inline void Swap(int16_t* fOut) {
144  Swap2(fOut);
145  }
146 
147  static inline void Swap(uint16_t* fOut) {
148  Swap2(fOut);
149  }
150 
151  // ----------------------------------------------------------------------
154  static inline void Swap(int32_t* fOut){
155  Swap4(fOut);
156  }
157 
158  static inline void Swap(uint32_t* fOut){
159  Swap4(fOut);
160  }
161 
162  // ----------------------------------------------------------------------
165  static inline void Swap(int64_t* fOut) {
166  Swap8(fOut);
167  }
168 
169  static inline void Swap(uint64_t* fOut) {
170  Swap8(fOut);
171  }
172 
173  // ----------------------------------------------------------------------
176  template<typename Type>
177  static inline Type Swapped(Type tOut)
178  {
179  return _swapper<Type,sizeof(Type)>()(tOut);
180  }
181 
182 private:
183 
184  template <typename T, size_t size> struct _swapper;
185 };
186 
187 template <typename T> struct ByteSwap::_swapper<T,2> {
188  T operator() (T tOut) {
189  Swap2(&tOut);
190  return tOut;
191  }
192 };
193 
194 template <typename T> struct ByteSwap::_swapper<T,4> {
195  T operator() (T tOut) {
196  Swap4(&tOut);
197  return tOut;
198  }
199 };
200 
201 template <typename T> struct ByteSwap::_swapper<T,8> {
202  T operator() (T tOut) {
203  Swap8(&tOut);
204  return tOut;
205  }
206 };
207 
208 
209 // --------------------------------------------------------------------------------------
210 // ByteSwap macros for BigEndian/LittleEndian support
211 // --------------------------------------------------------------------------------------
212 #if (defined AI_BUILD_BIG_ENDIAN)
213 # define AI_LE(t) (t)
214 # define AI_BE(t) ByteSwap::Swapped(t)
215 # define AI_LSWAP2(p)
216 # define AI_LSWAP4(p)
217 # define AI_LSWAP8(p)
218 # define AI_LSWAP2P(p)
219 # define AI_LSWAP4P(p)
220 # define AI_LSWAP8P(p)
221 # define LE_NCONST const
222 # define AI_SWAP2(p) ByteSwap::Swap2(&(p))
223 # define AI_SWAP4(p) ByteSwap::Swap4(&(p))
224 # define AI_SWAP8(p) ByteSwap::Swap8(&(p))
225 # define AI_SWAP2P(p) ByteSwap::Swap2((p))
226 # define AI_SWAP4P(p) ByteSwap::Swap4((p))
227 # define AI_SWAP8P(p) ByteSwap::Swap8((p))
228 # define BE_NCONST
229 #else
230 # define AI_BE(t) (t)
231 # define AI_LE(t) ByteSwap::Swapped(t)
232 # define AI_SWAP2(p)
233 # define AI_SWAP4(p)
234 # define AI_SWAP8(p)
235 # define AI_SWAP2P(p)
236 # define AI_SWAP4P(p)
237 # define AI_SWAP8P(p)
238 # define BE_NCONST const
239 # define AI_LSWAP2(p) ByteSwap::Swap2(&(p))
240 # define AI_LSWAP4(p) ByteSwap::Swap4(&(p))
241 # define AI_LSWAP8(p) ByteSwap::Swap8(&(p))
242 # define AI_LSWAP2P(p) ByteSwap::Swap2((p))
243 # define AI_LSWAP4P(p) ByteSwap::Swap4((p))
244 # define AI_LSWAP8P(p) ByteSwap::Swap8((p))
245 # define LE_NCONST
246 #endif
247 
248 
249 namespace Intern {
250 
251 // --------------------------------------------------------------------------------------------
252 template <typename T, bool doit>
253 struct ByteSwapper {
254  void operator() (T* inout) {
255  ByteSwap::Swap(inout);
256  }
257 };
258 
259 template <typename T>
260 struct ByteSwapper<T,false> {
261  void operator() (T*) {
262  }
263 };
264 
265 // --------------------------------------------------------------------------------------------
266 template <bool SwapEndianess, typename T, bool RuntimeSwitch>
267 struct Getter {
268  void operator() (T* inout, bool le) {
269 #ifdef AI_BUILD_BIG_ENDIAN
270  le = le;
271 #else
272  le = !le;
273 #endif
274  if (le) {
275  ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout);
276  }
277  else ByteSwapper<T,false> () (inout);
278  }
279 };
280 
281 template <bool SwapEndianess, typename T>
282 struct Getter<SwapEndianess,T,false> {
283 
284  void operator() (T* inout, bool /*le*/) {
285  // static branch
286  ByteSwapper<T,(SwapEndianess && sizeof(T)>1)> () (inout);
287  }
288 };
289 } // end Intern
290 } // end Assimp
291 
292 #endif
Assimp::ByteSwap::Swap
static void Swap(int64_t *fOut)
Definition: ByteSwapper.h:165
types.h
Assimp::ByteSwap::Swap
static void Swap(float *fOut)
Definition: ByteSwapper.h:128
Assimp::Intern::ByteSwapper< T, false >
Definition: ByteSwapper.h:260
Assimp::ByteSwap::Swap
static void Swap(int32_t *fOut)
Definition: ByteSwapper.h:154
Assimp::ByteSwap::Swap
static void Swap(int16_t *fOut)
Definition: ByteSwapper.h:143
Assimp::Intern::Getter
Definition: ByteSwapper.h:267
Assimp::ByteSwap::Swap4
static void Swap4(void *_szOut)
Definition: ByteSwapper.h:92
Assimp::Intern::ByteSwapper
Definition: ByteSwapper.h:253
Assimp::ByteSwap::Swapped
static Type Swapped(Type tOut)
Definition: ByteSwapper.h:177
Assimp::ByteSwap::Swap
static void Swap(double *fOut)
Definition: ByteSwapper.h:135
Assimp::ByteSwap::Swap2
static void Swap2(void *_szOut)
Definition: ByteSwapper.h:76
Assimp::ByteSwap::Swap8
static void Swap8(void *_szOut)
Definition: ByteSwapper.h:109
Assimp
Definition: ai_assert.h:50
Assimp::ByteSwap
Definition: ByteSwapper.h:68