ByteSwap.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Helper class tp perform various byte oder swappings
  34. (e.g. little to big endian) */
  35. #ifndef AI_BYTESWAP_H_INC
  36. #define AI_BYTESWAP_H_INC
  37. #include "../include/aiAssert.h"
  38. #include "../include/aiTypes.h"
  39. namespace Assimp
  40. {
  41. // ---------------------------------------------------------------------------
  42. /** \brief Defines some useful byte order swap routines.
  43. *
  44. * This can e.g. be used for the conversion from and to littel and big endian.
  45. * This is required by some loaders since some model formats
  46. */
  47. class ByteSwap
  48. {
  49. ByteSwap() {}
  50. public:
  51. //! Swap the byte oder of 2 byte of data
  52. //! \param szOut Buffer to be swapped
  53. static inline void Swap2(void* _szOut)
  54. {
  55. ai_assert(NULL != _szOut);
  56. int8_t* szOut = (int8_t*)_szOut;
  57. std::swap(szOut[0],szOut[1]);
  58. }
  59. //! Swap the byte oder of 4 byte of data
  60. //! \param szOut Buffer to be swapped
  61. static inline void Swap4(void* _szOut)
  62. {
  63. #if _MSC_VER >= 1400 && (defined _M_X86)
  64. __asm
  65. {
  66. mov edi, _szOut
  67. mov eax, dword_ptr[edi]
  68. bswap eax
  69. mov dword_ptr[edi], eax
  70. };
  71. #else
  72. ai_assert(NULL != _szOut);
  73. int8_t* szOut = (int8_t*)_szOut;
  74. std::swap(szOut[0],szOut[3]);
  75. std::swap(szOut[1],szOut[2]);
  76. #endif
  77. }
  78. //! Swap the byte oder of 8 byte of data
  79. //! \param szOut Buffer to be swapped
  80. static inline void Swap8(void* _szOut)
  81. {
  82. ai_assert(NULL != _szOut);
  83. int8_t* szOut = (int8_t*)_szOut;
  84. std::swap(szOut[0],szOut[7]);
  85. std::swap(szOut[1],szOut[6]);
  86. std::swap(szOut[2],szOut[5]);
  87. std::swap(szOut[3],szOut[4]);
  88. }
  89. //! Swap a single precision float
  90. //! \param fOut Float value to be swapped
  91. static inline void Swap(float* fOut)
  92. {
  93. Swap4(fOut);
  94. }
  95. //! Swap a double precision float
  96. //! \param fOut Double value to be swapped
  97. static inline void Swap(double* fOut)
  98. {
  99. Swap8(fOut);
  100. }
  101. //! Swap a 16 bit integer
  102. //! \param fOut Integer to be swapped
  103. static inline void Swap(int16_t* fOut)
  104. {
  105. Swap2(fOut);
  106. }
  107. //! Swap a 32 bit integer
  108. //! \param fOut Integer to be swapped
  109. static inline void Swap(int32_t* fOut)
  110. {
  111. Swap4(fOut);
  112. }
  113. //! Swap a 64 bit integer
  114. //! \param fOut Integer to be swapped
  115. static inline void Swap(int64_t* fOut)
  116. {
  117. Swap8(fOut);
  118. }
  119. };
  120. };
  121. // byteswap macros for BigEndian/LittleEndian support
  122. #if (defined AI_BUILD_BIG_ENDIAN)
  123. # define AI_LSWAP2(p)
  124. # define AI_LSWAP4(p)
  125. # define AI_LSWAP8(p)
  126. # define AI_LSWAP2P(p)
  127. # define AI_LSWAP4P(p)
  128. # define AI_LSWAP8P(p)
  129. # define LE_NCONST const
  130. # define AI_SWAP2(p) ByteSwap::Swap2(&(p))
  131. # define AI_SWAP4(p) ByteSwap::Swap4(&(p))
  132. # define AI_SWAP8(p) ByteSwap::Swap8(&(p))
  133. # define AI_SWAP2P(p) ByteSwap::Swap2((p))
  134. # define AI_SWAP4P(p) ByteSwap::Swap4((p))
  135. # define AI_SWAP8P(p) ByteSwap::Swap8((p))
  136. # define BE_NCONST
  137. #else
  138. # define AI_SWAP2(p)
  139. # define AI_SWAP4(p)
  140. # define AI_SWAP8(p)
  141. # define AI_SWAP2P(p)
  142. # define AI_SWAP4P(p)
  143. # define AI_SWAP8P(p)
  144. # define BE_NCONST const
  145. # define AI_LSWAP2(p) ByteSwap::Swap2(&(p))
  146. # define AI_LSWAP4(p) ByteSwap::Swap4(&(p))
  147. # define AI_LSWAP8(p) ByteSwap::Swap8(&(p))
  148. # define AI_LSWAP2P(p) ByteSwap::Swap2((p))
  149. # define AI_LSWAP4P(p) ByteSwap::Swap4((p))
  150. # define AI_LSWAP8P(p) ByteSwap::Swap8((p))
  151. # define LE_NCONST
  152. #endif
  153. #endif //!! AI_BYTESWAP_H_INC