endian.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 _ENDIAN_H_
  23. #define _ENDIAN_H_
  24. #ifndef _TORQUE_TYPES_H_
  25. #include "platform/types.h"
  26. #endif
  27. //------------------------------------------------------------------------------
  28. // Endian conversions
  29. inline U8 endianSwap(const U8 in_swap)
  30. {
  31. return in_swap;
  32. }
  33. inline S8 endianSwap(const S8 in_swap)
  34. {
  35. return in_swap;
  36. }
  37. /**
  38. Convert the byte ordering on the U16 to and from big/little endian format.
  39. @param in_swap Any U16
  40. @returns swapped U16.
  41. */
  42. inline U16 endianSwap(const U16 in_swap)
  43. {
  44. #ifdef TORQUE_U16_ENDIANSWAP_BUILTIN
  45. return TORQUE_U16_ENDIANSWAP_BUILTIN(in_swap);
  46. #else
  47. return U16(((in_swap >> 8) & 0x00ff) |
  48. ((in_swap << 8) & 0xff00));
  49. #endif
  50. }
  51. inline S16 endianSwap(const S16 in_swap)
  52. {
  53. return S16(endianSwap(U16(in_swap)));
  54. }
  55. /**
  56. Convert the byte ordering on the U32 to and from big/little endian format.
  57. @param in_swap Any U32
  58. @returns swapped U32.
  59. */
  60. inline U32 endianSwap(const U32 in_swap)
  61. {
  62. #ifdef TORQUE_U32_ENDIANSWAP_BUILTIN
  63. return TORQUE_U32_ENDIANSWAP_BUILTIN(in_swap);
  64. #else
  65. return U32(((in_swap >> 24) & 0x000000ff) |
  66. ((in_swap >> 8) & 0x0000ff00) |
  67. ((in_swap << 8) & 0x00ff0000) |
  68. ((in_swap << 24) & 0xff000000));
  69. #endif
  70. }
  71. inline S32 endianSwap(const S32 in_swap)
  72. {
  73. return S32(endianSwap(U32(in_swap)));
  74. }
  75. inline U64 endianSwap(const U64 in_swap)
  76. {
  77. #ifdef TORQUE_U64_ENDIANSWAP_BUILTIN
  78. return TORQUE_U64_ENDIANSWAP_BUILTIN(in_swap);
  79. #else
  80. U32 *inp = (U32 *) &in_swap;
  81. U64 ret;
  82. U32 *outp = (U32 *) &ret;
  83. outp[0] = endianSwap(inp[1]);
  84. outp[1] = endianSwap(inp[0]);
  85. return ret;
  86. #endif
  87. }
  88. inline S64 endianSwap(const S64 in_swap)
  89. {
  90. return S64(endianSwap(U64(in_swap)));
  91. }
  92. inline F32 endianSwap(const F32 in_swap)
  93. {
  94. U32 result = endianSwap(* ((U32 *) &in_swap) );
  95. return * ((F32 *) &result);
  96. }
  97. inline F64 endianSwap(const F64 in_swap)
  98. {
  99. U64 result = endianSwap(* ((U64 *) &in_swap) );
  100. return * ((F64 *) &result);
  101. }
  102. //------------------------------------------------------------------------------
  103. // Endian conversions
  104. #ifdef TORQUE_LITTLE_ENDIAN
  105. #define TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \
  106. inline type convertHostToLEndian(type i) { return i; } \
  107. inline type convertLEndianToHost(type i) { return i; } \
  108. inline type convertHostToBEndian(type i) { return endianSwap(i); } \
  109. inline type convertBEndianToHost(type i) { return endianSwap(i); }
  110. #elif defined(TORQUE_BIG_ENDIAN)
  111. #define TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \
  112. inline type convertHostToLEndian(type i) { return endianSwap(i); } \
  113. inline type convertLEndianToHost(type i) { return endianSwap(i); } \
  114. inline type convertHostToBEndian(type i) { return i; } \
  115. inline type convertBEndianToHost(type i) { return i; }
  116. #else
  117. #error "Endian define not set!"
  118. #endif
  119. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U8)
  120. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S8)
  121. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U16)
  122. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S16)
  123. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U32)
  124. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S32)
  125. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U64)
  126. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S64)
  127. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(F32)
  128. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(F64)
  129. #endif