endian.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. return U16(((in_swap >> 8) & 0x00ff) |
  45. ((in_swap << 8) & 0xff00));
  46. }
  47. inline S16 endianSwap(const S16 in_swap)
  48. {
  49. return S16(endianSwap(U16(in_swap)));
  50. }
  51. /**
  52. Convert the byte ordering on the U32 to and from big/little endian format.
  53. @param in_swap Any U32
  54. @returns swapped U32.
  55. */
  56. inline U32 endianSwap(const U32 in_swap)
  57. {
  58. return U32(((in_swap >> 24) & 0x000000ff) |
  59. ((in_swap >> 8) & 0x0000ff00) |
  60. ((in_swap << 8) & 0x00ff0000) |
  61. ((in_swap << 24) & 0xff000000));
  62. }
  63. inline S32 endianSwap(const S32 in_swap)
  64. {
  65. return S32(endianSwap(U32(in_swap)));
  66. }
  67. inline U64 endianSwap(const U64 in_swap)
  68. {
  69. U32 *inp = (U32 *) &in_swap;
  70. U64 ret;
  71. U32 *outp = (U32 *) &ret;
  72. outp[0] = endianSwap(inp[1]);
  73. outp[1] = endianSwap(inp[0]);
  74. return ret;
  75. }
  76. inline S64 endianSwap(const S64 in_swap)
  77. {
  78. return S64(endianSwap(U64(in_swap)));
  79. }
  80. inline F32 endianSwap(const F32 in_swap)
  81. {
  82. U32 result = endianSwap(* ((U32 *) &in_swap) );
  83. return * ((F32 *) &result);
  84. }
  85. inline F64 endianSwap(const F64 in_swap)
  86. {
  87. U64 result = endianSwap(* ((U64 *) &in_swap) );
  88. return * ((F64 *) &result);
  89. }
  90. //------------------------------------------------------------------------------
  91. // Endian conversions
  92. #ifdef TORQUE_LITTLE_ENDIAN
  93. #define TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \
  94. inline type convertHostToLEndian(type i) { return i; } \
  95. inline type convertLEndianToHost(type i) { return i; } \
  96. inline type convertHostToBEndian(type i) { return endianSwap(i); } \
  97. inline type convertBEndianToHost(type i) { return endianSwap(i); }
  98. #elif defined(TORQUE_BIG_ENDIAN)
  99. #define TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(type) \
  100. inline type convertHostToLEndian(type i) { return endianSwap(i); } \
  101. inline type convertLEndianToHost(type i) { return endianSwap(i); } \
  102. inline type convertHostToBEndian(type i) { return i; } \
  103. inline type convertBEndianToHost(type i) { return i; }
  104. #else
  105. #error "Endian define not set!"
  106. #endif
  107. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U8)
  108. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S8)
  109. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U16)
  110. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S16)
  111. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U32)
  112. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S32)
  113. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(U64)
  114. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(S64)
  115. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(F32)
  116. TORQUE_DECLARE_TEMPLATIZED_ENDIAN_CONV(F64)
  117. #endif