EndianPortable.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. //
  21. // EndianPortable.c
  22. //
  23. // Copyright 2011 Apple Inc. All rights reserved.
  24. //
  25. #include <stdio.h>
  26. #include "EndianPortable.h"
  27. #define BSWAP16(x) (((x << 8) | ((x >> 8) & 0x00ff)))
  28. #define BSWAP32(x) (((x << 24) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff)))
  29. #define BSWAP64(x) ((((int64_t)x << 56) | (((int64_t)x << 40) & 0x00ff000000000000LL) | \
  30. (((int64_t)x << 24) & 0x0000ff0000000000LL) | (((int64_t)x << 8) & 0x000000ff00000000LL) | \
  31. (((int64_t)x >> 8) & 0x00000000ff000000LL) | (((int64_t)x >> 24) & 0x0000000000ff0000LL) | \
  32. (((int64_t)x >> 40) & 0x000000000000ff00LL) | (((int64_t)x >> 56) & 0x00000000000000ffLL)))
  33. #if defined(__i386__)
  34. #define TARGET_RT_LITTLE_ENDIAN 1
  35. #elif defined(__x86_64__)
  36. #define TARGET_RT_LITTLE_ENDIAN 1
  37. #elif defined (TARGET_OS_WIN32)
  38. #define TARGET_RT_LITTLE_ENDIAN 1
  39. #elif defined (__arm__) || defined(__aarch64__)
  40. #define TARGET_RT_LITTLE_ENDIAN 1
  41. #endif
  42. uint16_t Swap16NtoB(uint16_t inUInt16)
  43. {
  44. #if TARGET_RT_LITTLE_ENDIAN
  45. return BSWAP16(inUInt16);
  46. #else
  47. return inUInt16;
  48. #endif
  49. }
  50. uint16_t Swap16BtoN(uint16_t inUInt16)
  51. {
  52. #if TARGET_RT_LITTLE_ENDIAN
  53. return BSWAP16(inUInt16);
  54. #else
  55. return inUInt16;
  56. #endif
  57. }
  58. uint32_t Swap32NtoB(uint32_t inUInt32)
  59. {
  60. #if TARGET_RT_LITTLE_ENDIAN
  61. return BSWAP32(inUInt32);
  62. #else
  63. return inUInt32;
  64. #endif
  65. }
  66. uint32_t Swap32BtoN(uint32_t inUInt32)
  67. {
  68. #if TARGET_RT_LITTLE_ENDIAN
  69. return BSWAP32(inUInt32);
  70. #else
  71. return inUInt32;
  72. #endif
  73. }
  74. uint64_t Swap64BtoN(uint64_t inUInt64)
  75. {
  76. #if TARGET_RT_LITTLE_ENDIAN
  77. return BSWAP64(inUInt64);
  78. #else
  79. return inUInt64;
  80. #endif
  81. }
  82. uint64_t Swap64NtoB(uint64_t inUInt64)
  83. {
  84. #if TARGET_RT_LITTLE_ENDIAN
  85. return BSWAP64(inUInt64);
  86. #else
  87. return inUInt64;
  88. #endif
  89. }
  90. float SwapFloat32BtoN(float in)
  91. {
  92. #if TARGET_RT_LITTLE_ENDIAN
  93. union {
  94. float f;
  95. int32_t i;
  96. } x;
  97. x.f = in;
  98. x.i = BSWAP32(x.i);
  99. return x.f;
  100. #else
  101. return in;
  102. #endif
  103. }
  104. float SwapFloat32NtoB(float in)
  105. {
  106. #if TARGET_RT_LITTLE_ENDIAN
  107. union {
  108. float f;
  109. int32_t i;
  110. } x;
  111. x.f = in;
  112. x.i = BSWAP32(x.i);
  113. return x.f;
  114. #else
  115. return in;
  116. #endif
  117. }
  118. double SwapFloat64BtoN(double in)
  119. {
  120. #if TARGET_RT_LITTLE_ENDIAN
  121. union {
  122. double f;
  123. int64_t i;
  124. } x;
  125. x.f = in;
  126. x.i = BSWAP64(x.i);
  127. return x.f;
  128. #else
  129. return in;
  130. #endif
  131. }
  132. double SwapFloat64NtoB(double in)
  133. {
  134. #if TARGET_RT_LITTLE_ENDIAN
  135. union {
  136. double f;
  137. int64_t i;
  138. } x;
  139. x.f = in;
  140. x.i = BSWAP64(x.i);
  141. return x.f;
  142. #else
  143. return in;
  144. #endif
  145. }
  146. void Swap16(uint16_t * inUInt16)
  147. {
  148. *inUInt16 = BSWAP16(*inUInt16);
  149. }
  150. void Swap24(uint8_t * inUInt24)
  151. {
  152. uint8_t tempVal = inUInt24[0];
  153. inUInt24[0] = inUInt24[2];
  154. inUInt24[2] = tempVal;
  155. }
  156. void Swap32(uint32_t * inUInt32)
  157. {
  158. *inUInt32 = BSWAP32(*inUInt32);
  159. }