vrshoot

view libs/assimp/ByteSwap.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file Helper class tp perform various byte oder swappings
42 (e.g. little to big endian) */
43 #ifndef AI_BYTESWAP_H_INC
44 #define AI_BYTESWAP_H_INC
46 #include "assimp/ai_assert.h"
47 #include "assimp/types.h"
49 #if _MSC_VER >= 1400
50 #include <stdlib.h>
51 #endif
53 namespace Assimp {
54 // --------------------------------------------------------------------------------------
55 /** Defines some useful byte order swap routines.
56 *
57 * This is required to read big-endian model formats on little-endian machines,
58 * and vice versa. Direct use of this class is DEPRECATED. Use #StreamReader instead. */
59 // --------------------------------------------------------------------------------------
60 class ByteSwap
61 {
62 ByteSwap() {}
64 public:
66 // ----------------------------------------------------------------------
67 /** Swap two bytes of data
68 * @param[inout] _szOut A void* to save the reintcasts for the caller. */
69 static inline void Swap2(void* _szOut)
70 {
71 ai_assert(_szOut);
73 #if _MSC_VER >= 1400
74 uint16_t* const szOut = reinterpret_cast<uint16_t*>(_szOut);
75 *szOut = _byteswap_ushort(*szOut);
76 #else
77 uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
78 std::swap(szOut[0],szOut[1]);
79 #endif
80 }
82 // ----------------------------------------------------------------------
83 /** Swap four bytes of data
84 * @param[inout] _szOut A void* to save the reintcasts for the caller. */
85 static inline void Swap4(void* _szOut)
86 {
87 ai_assert(_szOut);
89 #if _MSC_VER >= 1400
90 uint32_t* const szOut = reinterpret_cast<uint32_t*>(_szOut);
91 *szOut = _byteswap_ulong(*szOut);
92 #else
93 uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
94 std::swap(szOut[0],szOut[3]);
95 std::swap(szOut[1],szOut[2]);
96 #endif
97 }
99 // ----------------------------------------------------------------------
100 /** Swap eight bytes of data
101 * @param[inout] _szOut A void* to save the reintcasts for the caller. */
102 static inline void Swap8(void* _szOut)
103 {
104 ai_assert(_szOut);
106 #if _MSC_VER >= 1400
107 uint64_t* const szOut = reinterpret_cast<uint64_t*>(_szOut);
108 *szOut = _byteswap_uint64(*szOut);
109 #else
110 uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
111 std::swap(szOut[0],szOut[7]);
112 std::swap(szOut[1],szOut[6]);
113 std::swap(szOut[2],szOut[5]);
114 std::swap(szOut[3],szOut[4]);
115 #endif
116 }
118 // ----------------------------------------------------------------------
119 /** ByteSwap a float. Not a joke.
120 * @param[inout] fOut ehm. .. */
121 static inline void Swap(float* fOut) {
122 Swap4(fOut);
123 }
125 // ----------------------------------------------------------------------
126 /** ByteSwap a double. Not a joke.
127 * @param[inout] fOut ehm. .. */
128 static inline void Swap(double* fOut) {
129 Swap8(fOut);
130 }
133 // ----------------------------------------------------------------------
134 /** ByteSwap an int16t. Not a joke.
135 * @param[inout] fOut ehm. .. */
136 static inline void Swap(int16_t* fOut) {
137 Swap2(fOut);
138 }
140 static inline void Swap(uint16_t* fOut) {
141 Swap2(fOut);
142 }
144 // ----------------------------------------------------------------------
145 /** ByteSwap an int32t. Not a joke.
146 * @param[inout] fOut ehm. .. */
147 static inline void Swap(int32_t* fOut){
148 Swap4(fOut);
149 }
151 static inline void Swap(uint32_t* fOut){
152 Swap4(fOut);
153 }
155 // ----------------------------------------------------------------------
156 /** ByteSwap an int64t. Not a joke.
157 * @param[inout] fOut ehm. .. */
158 static inline void Swap(int64_t* fOut) {
159 Swap8(fOut);
160 }
162 static inline void Swap(uint64_t* fOut) {
163 Swap8(fOut);
164 }
166 // ----------------------------------------------------------------------
167 //! Templatized ByteSwap
168 //! \returns param tOut as swapped
169 template<typename Type>
170 static inline Type Swapped(Type tOut)
171 {
172 return _swapper<Type,sizeof(Type)>()(tOut);
173 }
175 private:
177 template <typename T, size_t size> struct _swapper;
178 };
180 template <typename T> struct ByteSwap::_swapper<T,2> {
181 T operator() (T tOut) {
182 Swap2(&tOut);
183 return tOut;
184 }
185 };
187 template <typename T> struct ByteSwap::_swapper<T,4> {
188 T operator() (T tOut) {
189 Swap4(&tOut);
190 return tOut;
191 }
192 };
194 template <typename T> struct ByteSwap::_swapper<T,8> {
195 T operator() (T tOut) {
196 Swap8(&tOut);
197 return tOut;
198 }
199 };
202 // --------------------------------------------------------------------------------------
203 // ByteSwap macros for BigEndian/LittleEndian support
204 // --------------------------------------------------------------------------------------
205 #if (defined AI_BUILD_BIG_ENDIAN)
206 # define AI_LE(t) (t)
207 # define AI_BE(t) ByteSwap::Swapped(t)
208 # define AI_LSWAP2(p)
209 # define AI_LSWAP4(p)
210 # define AI_LSWAP8(p)
211 # define AI_LSWAP2P(p)
212 # define AI_LSWAP4P(p)
213 # define AI_LSWAP8P(p)
214 # define LE_NCONST const
215 # define AI_SWAP2(p) ByteSwap::Swap2(&(p))
216 # define AI_SWAP4(p) ByteSwap::Swap4(&(p))
217 # define AI_SWAP8(p) ByteSwap::Swap8(&(p))
218 # define AI_SWAP2P(p) ByteSwap::Swap2((p))
219 # define AI_SWAP4P(p) ByteSwap::Swap4((p))
220 # define AI_SWAP8P(p) ByteSwap::Swap8((p))
221 # define BE_NCONST
222 #else
223 # define AI_BE(t) (t)
224 # define AI_LE(t) ByteSwap::Swapped(t)
225 # define AI_SWAP2(p)
226 # define AI_SWAP4(p)
227 # define AI_SWAP8(p)
228 # define AI_SWAP2P(p)
229 # define AI_SWAP4P(p)
230 # define AI_SWAP8P(p)
231 # define BE_NCONST const
232 # define AI_LSWAP2(p) ByteSwap::Swap2(&(p))
233 # define AI_LSWAP4(p) ByteSwap::Swap4(&(p))
234 # define AI_LSWAP8(p) ByteSwap::Swap8(&(p))
235 # define AI_LSWAP2P(p) ByteSwap::Swap2((p))
236 # define AI_LSWAP4P(p) ByteSwap::Swap4((p))
237 # define AI_LSWAP8P(p) ByteSwap::Swap8((p))
238 # define LE_NCONST
239 #endif
242 namespace Intern {
244 // --------------------------------------------------------------------------------------------
245 template <typename T, bool doit>
246 struct ByteSwapper {
247 void operator() (T* inout) {
248 ByteSwap::Swap(inout);
249 }
250 };
252 template <typename T>
253 struct ByteSwapper<T,false> {
254 void operator() (T*) {
255 }
256 };
258 // --------------------------------------------------------------------------------------------
259 template <bool SwapEndianess, typename T, bool RuntimeSwitch>
260 struct Getter {
261 void operator() (T* inout, bool le) {
262 #ifdef AI_BUILD_BIG_ENDIAN
263 le = le;
264 #else
265 le = !le;
266 #endif
267 if (le) {
268 ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout);
269 }
270 else ByteSwapper<T,false> () (inout);
271 }
272 };
274 template <bool SwapEndianess, typename T>
275 struct Getter<SwapEndianess,T,false> {
277 void operator() (T* inout, bool /*le*/) {
278 // static branch
279 ByteSwapper<T,(SwapEndianess && sizeof(T)>1)> () (inout);
280 }
281 };
282 } // end Intern
283 } // end Assimp
285 #endif //!! AI_BYTESWAP_H_INC