platformEndian.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PLATFORM_ENDIAN_H_
  23. #define _PLATFORM_ENDIAN_H_
  24. #ifdef TORQUE_LITTLE_ENDIAN
  25. inline U16 convertHostToLEndian(U16 i) { return i; }
  26. inline U16 convertLEndianToHost(U16 i) { return i; }
  27. inline U32 convertHostToLEndian(U32 i) { return i; }
  28. inline U32 convertLEndianToHost(U32 i) { return i; }
  29. inline S16 convertHostToLEndian(S16 i) { return i; }
  30. inline S16 convertLEndianToHost(S16 i) { return i; }
  31. inline S32 convertHostToLEndian(S32 i) { return i; }
  32. inline S32 convertLEndianToHost(S32 i) { return i; }
  33. inline F32 convertHostToLEndian(F32 i) { return i; }
  34. inline F32 convertLEndianToHost(F32 i) { return i; }
  35. inline F64 convertHostToLEndian(F64 i) { return i; }
  36. inline F64 convertLEndianToHost(F64 i) { return i; }
  37. inline U16 convertHostToBEndian(U16 i)
  38. {
  39. return U16((i << 8) | (i >> 8));
  40. }
  41. inline U16 convertBEndianToHost(U16 i)
  42. {
  43. return U16((i << 8) | (i >> 8));
  44. }
  45. inline S16 convertHostToBEndian(S16 i)
  46. {
  47. return S16(convertHostToBEndian(U16(i)));
  48. }
  49. inline S16 convertBEndianToHost(S16 i)
  50. {
  51. return S16(convertBEndianToHost(S16(i)));
  52. }
  53. inline U32 convertHostToBEndian(U32 i)
  54. {
  55. return ((i << 24) & 0xff000000) |
  56. ((i << 8) & 0x00ff0000) |
  57. ((i >> 8) & 0x0000ff00) |
  58. ((i >> 24) & 0x000000ff);
  59. }
  60. inline U32 convertBEndianToHost(U32 i)
  61. {
  62. return ((i << 24) & 0xff000000) |
  63. ((i << 8) & 0x00ff0000) |
  64. ((i >> 8) & 0x0000ff00) |
  65. ((i >> 24) & 0x000000ff);
  66. }
  67. inline S32 convertHostToBEndian(S32 i)
  68. {
  69. return S32(convertHostToBEndian(U32(i)));
  70. }
  71. inline S32 convertBEndianToHost(S32 i)
  72. {
  73. return S32(convertBEndianToHost(U32(i)));
  74. }
  75. inline U64 convertBEndianToHost(U64 i)
  76. {
  77. U32 *inp = (U32 *) &i;
  78. U64 ret;
  79. U32 *outp = (U32 *) &ret;
  80. outp[0] = convertBEndianToHost(inp[1]);
  81. outp[1] = convertBEndianToHost(inp[0]);
  82. return ret;
  83. }
  84. inline U64 convertHostToBEndian(U64 i)
  85. {
  86. U32 *inp = (U32 *) &i;
  87. U64 ret;
  88. U32 *outp = (U32 *) &ret;
  89. outp[0] = convertHostToBEndian(inp[1]);
  90. outp[1] = convertHostToBEndian(inp[0]);
  91. return ret;
  92. }
  93. inline F64 convertBEndianToHost(F64 in_swap)
  94. {
  95. U64 result = convertBEndianToHost(* ((U64 *) &in_swap) );
  96. return * ((F64 *) &result);
  97. }
  98. inline F64 convertHostToBEndian(F64 in_swap)
  99. {
  100. U64 result = convertHostToBEndian(* ((U64 *) &in_swap) );
  101. return * ((F64 *) &result);
  102. }
  103. #elif defined(TORQUE_BIG_ENDIAN)
  104. inline U16 convertHostToBEndian(U16 i) { return i; }
  105. inline U16 convertBEndianToHost(U16 i) { return i; }
  106. inline U32 convertHostToBEndian(U32 i) { return i; }
  107. inline U32 convertBEndianToHost(U32 i) { return i; }
  108. inline S16 convertHostToBEndian(S16 i) { return i; }
  109. inline S16 convertBEndianToHost(S16 i) { return i; }
  110. inline S32 convertHostToBEndian(S32 i) { return i; }
  111. inline S32 convertBEndianToHost(S32 i) { return i; }
  112. inline U16 convertHostToLEndian(U16 i)
  113. {
  114. return (i << 8) | (i >> 8);
  115. }
  116. inline U16 convertLEndianToHost(U16 i)
  117. {
  118. return (i << 8) | (i >> 8);
  119. }
  120. inline U32 convertHostToLEndian(U32 i)
  121. {
  122. return ((i << 24) & 0xff000000) |
  123. ((i << 8) & 0x00ff0000) |
  124. ((i >> 8) & 0x0000ff00) |
  125. ((i >> 24) & 0x000000ff);
  126. }
  127. inline U32 convertLEndianToHost(U32 i)
  128. {
  129. return ((i << 24) & 0xff000000) |
  130. ((i << 8) & 0x00ff0000) |
  131. ((i >> 8) & 0x0000ff00) |
  132. ((i >> 24) & 0x000000ff);
  133. }
  134. inline U64 convertLEndianToHost(U64 i)
  135. {
  136. U32 *inp = (U32 *) &i;
  137. U64 ret;
  138. U32 *outp = (U32 *) &ret;
  139. outp[0] = convertLEndianToHost(inp[1]);
  140. outp[1] = convertLEndianToHost(inp[0]);
  141. return ret;
  142. }
  143. inline U64 convertHostToLEndian(U64 i)
  144. {
  145. U32 *inp = (U32 *) &i;
  146. U64 ret;
  147. U32 *outp = (U32 *) &ret;
  148. outp[0] = convertHostToLEndian(inp[1]);
  149. outp[1] = convertHostToLEndian(inp[0]);
  150. return ret;
  151. }
  152. inline F64 convertLEndianToHost(F64 in_swap)
  153. {
  154. U64 result = convertLEndianToHost(* ((U64 *) &in_swap) );
  155. return * ((F64 *) &result);
  156. }
  157. inline F64 convertHostToLEndian(F64 in_swap)
  158. {
  159. U64 result = convertHostToLEndian(* ((U64 *) &in_swap) );
  160. return * ((F64 *) &result);
  161. }
  162. inline F32 convertHostToLEndian(F32 i)
  163. {
  164. U32 result = convertHostToLEndian( *reinterpret_cast<U32*>(&i) );
  165. return *reinterpret_cast<F32*>(&result);
  166. }
  167. inline F32 convertLEndianToHost(F32 i)
  168. {
  169. U32 result = convertLEndianToHost( *reinterpret_cast<U32*>(&i) );
  170. return *reinterpret_cast<F32*>(&result);
  171. }
  172. inline S16 convertHostToLEndian(S16 i) { return S16(convertHostToLEndian(U16(i))); }
  173. inline S16 convertLEndianToHost(S16 i) { return S16(convertLEndianToHost(U16(i))); }
  174. inline S32 convertHostToLEndian(S32 i) { return S32(convertHostToLEndian(U32(i))); }
  175. inline S32 convertLEndianToHost(S32 i) { return S32(convertLEndianToHost(U32(i))); }
  176. #else
  177. #error "Endian define not set"
  178. #endif
  179. #endif // _PLATFORM_ENDIAN_H_