vrshoot
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/ByteSwap.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,285 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +/** @file Helper class tp perform various byte oder swappings 1.45 + (e.g. little to big endian) */ 1.46 +#ifndef AI_BYTESWAP_H_INC 1.47 +#define AI_BYTESWAP_H_INC 1.48 + 1.49 +#include "assimp/ai_assert.h" 1.50 +#include "assimp/types.h" 1.51 + 1.52 +#if _MSC_VER >= 1400 1.53 +#include <stdlib.h> 1.54 +#endif 1.55 + 1.56 +namespace Assimp { 1.57 +// -------------------------------------------------------------------------------------- 1.58 +/** Defines some useful byte order swap routines. 1.59 + * 1.60 + * This is required to read big-endian model formats on little-endian machines, 1.61 + * and vice versa. Direct use of this class is DEPRECATED. Use #StreamReader instead. */ 1.62 +// -------------------------------------------------------------------------------------- 1.63 +class ByteSwap 1.64 +{ 1.65 + ByteSwap() {} 1.66 + 1.67 +public: 1.68 + 1.69 + // ---------------------------------------------------------------------- 1.70 + /** Swap two bytes of data 1.71 + * @param[inout] _szOut A void* to save the reintcasts for the caller. */ 1.72 + static inline void Swap2(void* _szOut) 1.73 + { 1.74 + ai_assert(_szOut); 1.75 + 1.76 +#if _MSC_VER >= 1400 1.77 + uint16_t* const szOut = reinterpret_cast<uint16_t*>(_szOut); 1.78 + *szOut = _byteswap_ushort(*szOut); 1.79 +#else 1.80 + uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut); 1.81 + std::swap(szOut[0],szOut[1]); 1.82 +#endif 1.83 + } 1.84 + 1.85 + // ---------------------------------------------------------------------- 1.86 + /** Swap four bytes of data 1.87 + * @param[inout] _szOut A void* to save the reintcasts for the caller. */ 1.88 + static inline void Swap4(void* _szOut) 1.89 + { 1.90 + ai_assert(_szOut); 1.91 + 1.92 +#if _MSC_VER >= 1400 1.93 + uint32_t* const szOut = reinterpret_cast<uint32_t*>(_szOut); 1.94 + *szOut = _byteswap_ulong(*szOut); 1.95 +#else 1.96 + uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut); 1.97 + std::swap(szOut[0],szOut[3]); 1.98 + std::swap(szOut[1],szOut[2]); 1.99 +#endif 1.100 + } 1.101 + 1.102 + // ---------------------------------------------------------------------- 1.103 + /** Swap eight bytes of data 1.104 + * @param[inout] _szOut A void* to save the reintcasts for the caller. */ 1.105 + static inline void Swap8(void* _szOut) 1.106 + { 1.107 + ai_assert(_szOut); 1.108 + 1.109 +#if _MSC_VER >= 1400 1.110 + uint64_t* const szOut = reinterpret_cast<uint64_t*>(_szOut); 1.111 + *szOut = _byteswap_uint64(*szOut); 1.112 +#else 1.113 + uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut); 1.114 + std::swap(szOut[0],szOut[7]); 1.115 + std::swap(szOut[1],szOut[6]); 1.116 + std::swap(szOut[2],szOut[5]); 1.117 + std::swap(szOut[3],szOut[4]); 1.118 +#endif 1.119 + } 1.120 + 1.121 + // ---------------------------------------------------------------------- 1.122 + /** ByteSwap a float. Not a joke. 1.123 + * @param[inout] fOut ehm. .. */ 1.124 + static inline void Swap(float* fOut) { 1.125 + Swap4(fOut); 1.126 + } 1.127 + 1.128 + // ---------------------------------------------------------------------- 1.129 + /** ByteSwap a double. Not a joke. 1.130 + * @param[inout] fOut ehm. .. */ 1.131 + static inline void Swap(double* fOut) { 1.132 + Swap8(fOut); 1.133 + } 1.134 + 1.135 + 1.136 + // ---------------------------------------------------------------------- 1.137 + /** ByteSwap an int16t. Not a joke. 1.138 + * @param[inout] fOut ehm. .. */ 1.139 + static inline void Swap(int16_t* fOut) { 1.140 + Swap2(fOut); 1.141 + } 1.142 + 1.143 + static inline void Swap(uint16_t* fOut) { 1.144 + Swap2(fOut); 1.145 + } 1.146 + 1.147 + // ---------------------------------------------------------------------- 1.148 + /** ByteSwap an int32t. Not a joke. 1.149 + * @param[inout] fOut ehm. .. */ 1.150 + static inline void Swap(int32_t* fOut){ 1.151 + Swap4(fOut); 1.152 + } 1.153 + 1.154 + static inline void Swap(uint32_t* fOut){ 1.155 + Swap4(fOut); 1.156 + } 1.157 + 1.158 + // ---------------------------------------------------------------------- 1.159 + /** ByteSwap an int64t. Not a joke. 1.160 + * @param[inout] fOut ehm. .. */ 1.161 + static inline void Swap(int64_t* fOut) { 1.162 + Swap8(fOut); 1.163 + } 1.164 + 1.165 + static inline void Swap(uint64_t* fOut) { 1.166 + Swap8(fOut); 1.167 + } 1.168 + 1.169 + // ---------------------------------------------------------------------- 1.170 + //! Templatized ByteSwap 1.171 + //! \returns param tOut as swapped 1.172 + template<typename Type> 1.173 + static inline Type Swapped(Type tOut) 1.174 + { 1.175 + return _swapper<Type,sizeof(Type)>()(tOut); 1.176 + } 1.177 + 1.178 +private: 1.179 + 1.180 + template <typename T, size_t size> struct _swapper; 1.181 +}; 1.182 + 1.183 +template <typename T> struct ByteSwap::_swapper<T,2> { 1.184 + T operator() (T tOut) { 1.185 + Swap2(&tOut); 1.186 + return tOut; 1.187 + } 1.188 +}; 1.189 + 1.190 +template <typename T> struct ByteSwap::_swapper<T,4> { 1.191 + T operator() (T tOut) { 1.192 + Swap4(&tOut); 1.193 + return tOut; 1.194 + } 1.195 +}; 1.196 + 1.197 +template <typename T> struct ByteSwap::_swapper<T,8> { 1.198 + T operator() (T tOut) { 1.199 + Swap8(&tOut); 1.200 + return tOut; 1.201 + } 1.202 +}; 1.203 + 1.204 + 1.205 +// -------------------------------------------------------------------------------------- 1.206 +// ByteSwap macros for BigEndian/LittleEndian support 1.207 +// -------------------------------------------------------------------------------------- 1.208 +#if (defined AI_BUILD_BIG_ENDIAN) 1.209 +# define AI_LE(t) (t) 1.210 +# define AI_BE(t) ByteSwap::Swapped(t) 1.211 +# define AI_LSWAP2(p) 1.212 +# define AI_LSWAP4(p) 1.213 +# define AI_LSWAP8(p) 1.214 +# define AI_LSWAP2P(p) 1.215 +# define AI_LSWAP4P(p) 1.216 +# define AI_LSWAP8P(p) 1.217 +# define LE_NCONST const 1.218 +# define AI_SWAP2(p) ByteSwap::Swap2(&(p)) 1.219 +# define AI_SWAP4(p) ByteSwap::Swap4(&(p)) 1.220 +# define AI_SWAP8(p) ByteSwap::Swap8(&(p)) 1.221 +# define AI_SWAP2P(p) ByteSwap::Swap2((p)) 1.222 +# define AI_SWAP4P(p) ByteSwap::Swap4((p)) 1.223 +# define AI_SWAP8P(p) ByteSwap::Swap8((p)) 1.224 +# define BE_NCONST 1.225 +#else 1.226 +# define AI_BE(t) (t) 1.227 +# define AI_LE(t) ByteSwap::Swapped(t) 1.228 +# define AI_SWAP2(p) 1.229 +# define AI_SWAP4(p) 1.230 +# define AI_SWAP8(p) 1.231 +# define AI_SWAP2P(p) 1.232 +# define AI_SWAP4P(p) 1.233 +# define AI_SWAP8P(p) 1.234 +# define BE_NCONST const 1.235 +# define AI_LSWAP2(p) ByteSwap::Swap2(&(p)) 1.236 +# define AI_LSWAP4(p) ByteSwap::Swap4(&(p)) 1.237 +# define AI_LSWAP8(p) ByteSwap::Swap8(&(p)) 1.238 +# define AI_LSWAP2P(p) ByteSwap::Swap2((p)) 1.239 +# define AI_LSWAP4P(p) ByteSwap::Swap4((p)) 1.240 +# define AI_LSWAP8P(p) ByteSwap::Swap8((p)) 1.241 +# define LE_NCONST 1.242 +#endif 1.243 + 1.244 + 1.245 +namespace Intern { 1.246 + 1.247 +// -------------------------------------------------------------------------------------------- 1.248 +template <typename T, bool doit> 1.249 +struct ByteSwapper { 1.250 + void operator() (T* inout) { 1.251 + ByteSwap::Swap(inout); 1.252 + } 1.253 +}; 1.254 + 1.255 +template <typename T> 1.256 +struct ByteSwapper<T,false> { 1.257 + void operator() (T*) { 1.258 + } 1.259 +}; 1.260 + 1.261 +// -------------------------------------------------------------------------------------------- 1.262 +template <bool SwapEndianess, typename T, bool RuntimeSwitch> 1.263 +struct Getter { 1.264 + void operator() (T* inout, bool le) { 1.265 +#ifdef AI_BUILD_BIG_ENDIAN 1.266 + le = le; 1.267 +#else 1.268 + le = !le; 1.269 +#endif 1.270 + if (le) { 1.271 + ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout); 1.272 + } 1.273 + else ByteSwapper<T,false> () (inout); 1.274 + } 1.275 +}; 1.276 + 1.277 +template <bool SwapEndianess, typename T> 1.278 +struct Getter<SwapEndianess,T,false> { 1.279 + 1.280 + void operator() (T* inout, bool /*le*/) { 1.281 + // static branch 1.282 + ByteSwapper<T,(SwapEndianess && sizeof(T)>1)> () (inout); 1.283 + } 1.284 +}; 1.285 +} // end Intern 1.286 +} // end Assimp 1.287 + 1.288 +#endif //!! AI_BYTESWAP_H_INC