pal_endian.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  4. //
  5. /*++
  6. --*/
  7. #ifndef __PAL_ENDIAN_H__
  8. #define __PAL_ENDIAN_H__
  9. #ifdef __cplusplus
  10. extern "C++" {
  11. inline UINT16 SWAP16(UINT16 x)
  12. {
  13. return (x >> 8) | (x << 8);
  14. }
  15. inline UINT32 SWAP32(UINT32 x)
  16. {
  17. return (x >> 24) |
  18. ((x >> 8) & 0x0000FF00L) |
  19. ((x & 0x0000FF00L) << 8) |
  20. (x << 24);
  21. }
  22. }
  23. #endif // __cplusplus
  24. #if BIGENDIAN
  25. #ifdef __cplusplus
  26. extern "C++" {
  27. inline UINT16 VAL16(UINT16 x)
  28. {
  29. return SWAP16(x);
  30. }
  31. inline UINT32 VAL32(UINT32 x)
  32. {
  33. return SWAP32(x);
  34. }
  35. inline UINT64 VAL64(UINT64 x)
  36. {
  37. return ((UINT64)VAL32(x) << 32) | VAL32(x >> 32);
  38. }
  39. inline void SwapString(WCHAR *szString)
  40. {
  41. unsigned i;
  42. for (i = 0; szString[i] != L'\0'; i++)
  43. {
  44. szString[i] = VAL16(szString[i]);
  45. }
  46. }
  47. inline void SwapStringLength(WCHAR *szString, ULONG StringLength)
  48. {
  49. unsigned i;
  50. for (i = 0; i < StringLength; i++)
  51. {
  52. szString[i] = VAL16(szString[i]);
  53. }
  54. }
  55. inline void SwapGuid(GUID *pGuid)
  56. {
  57. pGuid->Data1 = VAL32(pGuid->Data1);
  58. pGuid->Data2 = VAL16(pGuid->Data2);
  59. pGuid->Data3 = VAL16(pGuid->Data3);
  60. }
  61. };
  62. #else // __cplusplus
  63. /* C Version of VAL functionality. Swap functions omitted for lack of use in C code */
  64. #define VAL16(x) (((x) >> 8) | ((x) << 8))
  65. #define VAL32(y) (((y) >> 24) | (((y) >> 8) & 0x0000FF00L) | (((y) & 0x0000FF00L) << 8) | ((y) << 24))
  66. #define VAL64(z) (((UINT64)VAL32(z) << 32) | VAL32((z) >> 32))
  67. #endif // __cplusplus
  68. #else // !BIGENDIAN
  69. #define VAL16(x) x
  70. #define VAL32(x) x
  71. #define VAL64(x) x
  72. #define SwapString(x)
  73. #define SwapStringLength(x, y)
  74. #define SwapGuid(x)
  75. #endif // !BIGENDIAN
  76. #ifdef BIT64
  77. #define VALPTR(x) VAL64(x)
  78. #else
  79. #define VALPTR(x) VAL32(x)
  80. #endif
  81. #if defined(ALIGN_ACCESS) && !defined(_MSC_VER)
  82. #ifdef __cplusplus
  83. extern "C++" {
  84. // Get Unaligned values from a potentially unaligned object
  85. inline UINT16 GET_UNALIGNED_16(const void *pObject)
  86. {
  87. UINT16 temp;
  88. memcpy(&temp, pObject, sizeof(temp));
  89. return temp;
  90. }
  91. inline UINT32 GET_UNALIGNED_32(const void *pObject)
  92. {
  93. UINT32 temp;
  94. memcpy(&temp, pObject, sizeof(temp));
  95. return temp;
  96. }
  97. inline UINT64 GET_UNALIGNED_64(const void *pObject)
  98. {
  99. UINT64 temp;
  100. memcpy(&temp, pObject, sizeof(temp));
  101. return temp;
  102. }
  103. // Set Value on an potentially unaligned object
  104. inline void SET_UNALIGNED_16(void *pObject, UINT16 Value)
  105. {
  106. memcpy(pObject, &Value, sizeof(UINT16));
  107. }
  108. inline void SET_UNALIGNED_32(void *pObject, UINT32 Value)
  109. {
  110. memcpy(pObject, &Value, sizeof(UINT32));
  111. }
  112. inline void SET_UNALIGNED_64(void *pObject, UINT64 Value)
  113. {
  114. memcpy(pObject, &Value, sizeof(UINT64));
  115. }
  116. }
  117. #endif // __cplusplus
  118. #else
  119. // Get Unaligned values from a potentially unaligned object
  120. #define GET_UNALIGNED_16(_pObject) (*(UINT16 UNALIGNED *)(_pObject))
  121. #define GET_UNALIGNED_32(_pObject) (*(UINT32 UNALIGNED *)(_pObject))
  122. #define GET_UNALIGNED_64(_pObject) (*(UINT64 UNALIGNED *)(_pObject))
  123. // Set Value on an potentially unaligned object
  124. #define SET_UNALIGNED_16(_pObject, _Value) (*(UNALIGNED UINT16 *)(_pObject)) = (UINT16)(_Value)
  125. #define SET_UNALIGNED_32(_pObject, _Value) (*(UNALIGNED UINT32 *)(_pObject)) = (UINT32)(_Value)
  126. #define SET_UNALIGNED_64(_pObject, _Value) (*(UNALIGNED UINT64 *)(_pObject)) = (UINT64)(_Value)
  127. #endif
  128. // Get Unaligned values from a potentially unaligned object and swap the value
  129. #define GET_UNALIGNED_VAL16(_pObject) VAL16(GET_UNALIGNED_16(_pObject))
  130. #define GET_UNALIGNED_VAL32(_pObject) VAL32(GET_UNALIGNED_32(_pObject))
  131. #define GET_UNALIGNED_VAL64(_pObject) VAL64(GET_UNALIGNED_64(_pObject))
  132. // Set a swap Value on an potentially unaligned object
  133. #define SET_UNALIGNED_VAL16(_pObject, _Value) SET_UNALIGNED_16(_pObject, VAL16((UINT16)_Value))
  134. #define SET_UNALIGNED_VAL32(_pObject, _Value) SET_UNALIGNED_32(_pObject, VAL32((UINT32)_Value))
  135. #define SET_UNALIGNED_VAL64(_pObject, _Value) SET_UNALIGNED_64(_pObject, VAL64((UINT64)_Value))
  136. #endif // __PAL_ENDIAN_H__