EndianMath.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (C) 2000 Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4. #ifndef ENDIAN_H__
  5. #define ENDIAN_H__
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include "basictypes.h"
  10. #include "BaseMath.h"
  11. class Endian
  12. {
  13. public:
  14. inline static INT16 FASTCALL Flip(const INT16 x)
  15. {
  16. INT16 a = (x >> 8) & 0x000FF;
  17. INT16 b = (x << 8) & 0x0FF00;
  18. INT16 rval = (a | b);
  19. return rval;
  20. }
  21. inline static UINT16 FASTCALL Flip(const UINT16 x)
  22. {
  23. UINT16 a = (x >> 8) & 0x000FF;
  24. UINT16 b = (x << 8) & 0x0FF00;
  25. UINT16 rval = (a | b);
  26. return rval;
  27. }
  28. inline static INT32 FASTCALL Flip(const INT32 x)
  29. {
  30. INT32 a = (x >> 24) & 0x0000000FF;
  31. INT32 b = (x >> 8) & 0x00000FF00;
  32. INT32 c = (x << 8) & 0x000FF0000;
  33. INT32 d = (x << 24) & 0x0FF000000;
  34. INT32 rval = (a | b | c | d);
  35. return rval;
  36. }
  37. inline static UINT32 FASTCALL Flip(const UINT32 x)
  38. {
  39. INT32 a = (x >> 24) & 0x0000000FF;
  40. INT32 b = (x >> 8) & 0x00000FF00;
  41. INT32 c = (x << 8) & 0x000FF0000;
  42. INT32 d = (x << 24) & 0x0FF000000;
  43. INT32 rval = (a | b | c | d);
  44. return rval;
  45. }
  46. #if 0
  47. inline static INT64 FASTCALL Flip(const INT64 x)
  48. {
  49. INT64 a = (x >> 56) & 0x000000000000000FF;
  50. INT64 b = (x >> 40) & 0x0000000000000FF00;
  51. INT64 c = (x >> 24) & 0x00000000000FF0000;
  52. INT64 d = (x >> 8 ) & 0x000000000FF000000;
  53. INT64 e = (x << 8 ) & 0x0000000FF00000000;
  54. INT64 f = (x << 24) & 0x00000FF0000000000;
  55. INT64 g = (x << 40) & 0x000FF000000000000;
  56. INT64 h = (x << 56) & 0x0FF00000000000000;
  57. INT64 rval = (a | b | c | d | e | f | g | h);
  58. return rval;
  59. }
  60. inline static UINT64 FASTCALL Flip(const UINT64 x)
  61. {
  62. UINT64 a = (x >> 56) & 0x000000000000000FF;
  63. UINT64 b = (x >> 40) & 0x0000000000000FF00;
  64. UINT64 c = (x >> 24) & 0x00000000000FF0000;
  65. UINT64 d = (x >> 8 ) & 0x000000000FF000000;
  66. UINT64 e = (x << 8 ) & 0x0000000FF00000000;
  67. UINT64 f = (x << 24) & 0x00000FF0000000000;
  68. UINT64 g = (x << 40) & 0x000FF000000000000;
  69. UINT64 h = (x << 56) & 0x0FF00000000000000;
  70. UINT64 rval = (a | b | c | d | e | f | g | h);
  71. return rval;
  72. }
  73. #endif
  74. inline static float FASTCALL Flip(const float x)
  75. {
  76. union floatflipper
  77. {
  78. struct _x_t
  79. {
  80. BYTE _v[4];
  81. } _x;
  82. float _f;
  83. };
  84. floatflipper tmp;
  85. tmp._f = x;
  86. SWAP(tmp._x._v[0], tmp._x._v[3]);
  87. SWAP(tmp._x._v[1], tmp._x._v[2]);
  88. return tmp._f;
  89. }
  90. inline static double FASTCALL Flip(const double x)
  91. {
  92. union floatflipper
  93. {
  94. struct _x_t
  95. {
  96. BYTE _v[8];
  97. } _x;
  98. double _d;
  99. };
  100. floatflipper tmp;
  101. tmp._d = x;
  102. SWAP(tmp._x._v[0], tmp._x._v[7]);
  103. SWAP(tmp._x._v[1], tmp._x._v[6]);
  104. SWAP(tmp._x._v[2], tmp._x._v[5]);
  105. SWAP(tmp._x._v[3], tmp._x._v[4]);
  106. return tmp._d;
  107. }
  108. inline static void FlipArray(unsigned size, INT16* x)
  109. {
  110. for (unsigned i=0 ; i<size ; i++, x++)
  111. {
  112. *x = Flip(*x);
  113. }
  114. }
  115. inline static void FlipArray(unsigned size, UINT16* x)
  116. {
  117. for (unsigned i=0 ; i<size ; i++, x++)
  118. {
  119. *x = Flip(*x);
  120. }
  121. }
  122. inline static void FlipArray(unsigned size, INT32* x)
  123. {
  124. for (unsigned i=0 ; i<size ; i++, x++)
  125. {
  126. *x = Flip(*x);
  127. }
  128. }
  129. inline static void FlipArray(unsigned size, UINT32* x)
  130. {
  131. for (unsigned i=0 ; i<size ; i++, x++)
  132. {
  133. *x = Flip(*x);
  134. }
  135. }
  136. #if 0
  137. inline static void FlipArray(unsigned size, INT64* x)
  138. {
  139. for (unsigned i=0 ; i<size ; i++, x++)
  140. {
  141. *x = Flip(*x);
  142. }
  143. }
  144. inline static void FlipArray(unsigned size, UINT64* x)
  145. {
  146. for (unsigned i=0 ; i<size ; i++, x++)
  147. {
  148. *x = Flip(*x);
  149. }
  150. }
  151. #endif
  152. inline static void FlipArray(unsigned size, float* x)
  153. {
  154. for (unsigned i=0 ; i<size ; i++, x++)
  155. {
  156. *x = Flip(*x);
  157. }
  158. }
  159. inline static void FlipArray(unsigned size, double* x)
  160. {
  161. for (unsigned i=0 ; i<size ; i++, x++)
  162. {
  163. *x = Flip(*x);
  164. }
  165. }
  166. };
  167. #endif // ENDIAN_H__