Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ByteOrder.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_BYTEORDER_H_INCLUDED
  24. #define JUCE_BYTEORDER_H_INCLUDED
  25. //==============================================================================
  26. /** Contains static methods for converting the byte order between different
  27. endiannesses.
  28. */
  29. class JUCE_API ByteOrder
  30. {
  31. public:
  32. //==============================================================================
  33. /** Swaps the upper and lower bytes of a 16-bit integer. */
  34. static uint16 swap (uint16 value) noexcept;
  35. /** Reverses the order of the 4 bytes in a 32-bit integer. */
  36. static uint32 swap (uint32 value) noexcept;
  37. /** Reverses the order of the 8 bytes in a 64-bit integer. */
  38. static uint64 swap (uint64 value) noexcept;
  39. //==============================================================================
  40. /** Swaps the byte order of a 16-bit unsigned int if the CPU is big-endian */
  41. static uint16 swapIfBigEndian (uint16 value) noexcept;
  42. /** Swaps the byte order of a 32-bit unsigned int if the CPU is big-endian */
  43. static uint32 swapIfBigEndian (uint32 value) noexcept;
  44. /** Swaps the byte order of a 64-bit unsigned int if the CPU is big-endian */
  45. static uint64 swapIfBigEndian (uint64 value) noexcept;
  46. /** Swaps the byte order of a 16-bit signed int if the CPU is big-endian */
  47. static int16 swapIfBigEndian (int16 value) noexcept;
  48. /** Swaps the byte order of a 32-bit signed int if the CPU is big-endian */
  49. static int32 swapIfBigEndian (int32 value) noexcept;
  50. /** Swaps the byte order of a 64-bit signed int if the CPU is big-endian */
  51. static int64 swapIfBigEndian (int64 value) noexcept;
  52. /** Swaps the byte order of a 32-bit float if the CPU is big-endian */
  53. static float swapIfBigEndian (float value) noexcept;
  54. /** Swaps the byte order of a 64-bit float if the CPU is big-endian */
  55. static double swapIfBigEndian (double value) noexcept;
  56. /** Swaps the byte order of a 16-bit unsigned int if the CPU is little-endian */
  57. static uint16 swapIfLittleEndian (uint16 value) noexcept;
  58. /** Swaps the byte order of a 32-bit unsigned int if the CPU is little-endian */
  59. static uint32 swapIfLittleEndian (uint32 value) noexcept;
  60. /** Swaps the byte order of a 64-bit unsigned int if the CPU is little-endian */
  61. static uint64 swapIfLittleEndian (uint64 value) noexcept;
  62. /** Swaps the byte order of a 16-bit signed int if the CPU is little-endian */
  63. static int16 swapIfLittleEndian (int16 value) noexcept;
  64. /** Swaps the byte order of a 32-bit signed int if the CPU is little-endian */
  65. static int32 swapIfLittleEndian (int32 value) noexcept;
  66. /** Swaps the byte order of a 64-bit signed int if the CPU is little-endian */
  67. static int64 swapIfLittleEndian (int64 value) noexcept;
  68. /** Swaps the byte order of a 32-bit float if the CPU is little-endian */
  69. static float swapIfLittleEndian (float value) noexcept;
  70. /** Swaps the byte order of a 64-bit float if the CPU is little-endian */
  71. static double swapIfLittleEndian (double value) noexcept;
  72. //==============================================================================
  73. /** Turns 4 bytes into a little-endian integer. */
  74. static uint32 littleEndianInt (const void* bytes) noexcept;
  75. /** Turns 8 bytes into a little-endian integer. */
  76. static uint64 littleEndianInt64 (const void* bytes) noexcept;
  77. /** Turns 2 bytes into a little-endian integer. */
  78. static uint16 littleEndianShort (const void* bytes) noexcept;
  79. /** Turns 4 bytes into a big-endian integer. */
  80. static uint32 bigEndianInt (const void* bytes) noexcept;
  81. /** Turns 8 bytes into a big-endian integer. */
  82. static uint64 bigEndianInt64 (const void* bytes) noexcept;
  83. /** Turns 2 bytes into a big-endian integer. */
  84. static uint16 bigEndianShort (const void* bytes) noexcept;
  85. //==============================================================================
  86. /** Converts 3 little-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
  87. static int littleEndian24Bit (const void* bytes) noexcept;
  88. /** Converts 3 big-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
  89. static int bigEndian24Bit (const void* bytes) noexcept;
  90. /** Copies a 24-bit number to 3 little-endian bytes. */
  91. static void littleEndian24BitToChars (int value, void* destBytes) noexcept;
  92. /** Copies a 24-bit number to 3 big-endian bytes. */
  93. static void bigEndian24BitToChars (int value, void* destBytes) noexcept;
  94. //==============================================================================
  95. /** Returns true if the current CPU is big-endian. */
  96. static bool isBigEndian() noexcept;
  97. private:
  98. ByteOrder() JUCE_DELETED_FUNCTION;
  99. JUCE_DECLARE_NON_COPYABLE (ByteOrder)
  100. };
  101. //==============================================================================
  102. #if JUCE_MSVC && ! defined (__INTEL_COMPILER)
  103. #pragma intrinsic (_byteswap_ulong)
  104. #endif
  105. inline uint16 ByteOrder::swap (uint16 n) noexcept
  106. {
  107. return static_cast<uint16> ((n << 8) | (n >> 8));
  108. }
  109. inline uint32 ByteOrder::swap (uint32 n) noexcept
  110. {
  111. #if JUCE_MAC || JUCE_IOS
  112. return OSSwapInt32 (n);
  113. #elif (JUCE_GCC || JUCE_CLANG) && JUCE_INTEL && ! JUCE_NO_INLINE_ASM
  114. asm("bswap %%eax" : "=a"(n) : "a"(n));
  115. return n;
  116. #elif JUCE_MSVC
  117. return _byteswap_ulong (n);
  118. #elif JUCE_ANDROID
  119. return bswap_32 (n);
  120. #else
  121. return (n << 24) | (n >> 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8);
  122. #endif
  123. }
  124. inline uint64 ByteOrder::swap (uint64 value) noexcept
  125. {
  126. #if JUCE_MAC || JUCE_IOS
  127. return OSSwapInt64 (value);
  128. #elif JUCE_MSVC
  129. return _byteswap_uint64 (value);
  130. #else
  131. return (((uint64) swap ((uint32) value)) << 32) | swap ((uint32) (value >> 32));
  132. #endif
  133. }
  134. #ifdef __LITTLE_ENDIAN__
  135. inline uint16 ByteOrder::swapIfBigEndian (const uint16 v) noexcept { return v; }
  136. inline uint32 ByteOrder::swapIfBigEndian (const uint32 v) noexcept { return v; }
  137. inline uint64 ByteOrder::swapIfBigEndian (const uint64 v) noexcept { return v; }
  138. inline int16 ByteOrder::swapIfBigEndian (const int16 v) noexcept { return v; }
  139. inline int32 ByteOrder::swapIfBigEndian (const int32 v) noexcept { return v; }
  140. inline int64 ByteOrder::swapIfBigEndian (const int64 v) noexcept { return v; }
  141. inline float ByteOrder::swapIfBigEndian (const float v) noexcept { return v; }
  142. inline double ByteOrder::swapIfBigEndian (const double v) noexcept { return v; }
  143. inline uint16 ByteOrder::swapIfLittleEndian (const uint16 v) noexcept { return swap (v); }
  144. inline uint32 ByteOrder::swapIfLittleEndian (const uint32 v) noexcept { return swap (v); }
  145. inline uint64 ByteOrder::swapIfLittleEndian (const uint64 v) noexcept { return swap (v); }
  146. inline int16 ByteOrder::swapIfLittleEndian (const int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  147. inline int32 ByteOrder::swapIfLittleEndian (const int32 v) noexcept { return static_cast<int32> (swap (static_cast<uint32> (v))); }
  148. inline int64 ByteOrder::swapIfLittleEndian (const int64 v) noexcept { return static_cast<int64> (swap (static_cast<uint64> (v))); }
  149. inline float ByteOrder::swapIfLittleEndian (const float v) noexcept { union { uint32 asUInt; float asFloat; } n; n.asFloat = v; n.asUInt = ByteOrder::swap (n.asUInt); return n.asFloat; }
  150. inline double ByteOrder::swapIfLittleEndian (const double v) noexcept { union { uint64 asUInt; double asFloat; } n; n.asFloat = v; n.asUInt = ByteOrder::swap (n.asUInt); return n.asFloat; }
  151. inline uint32 ByteOrder::littleEndianInt (const void* const bytes) noexcept { return *static_cast<const uint32*> (bytes); }
  152. inline uint64 ByteOrder::littleEndianInt64 (const void* const bytes) noexcept { return *static_cast<const uint64*> (bytes); }
  153. inline uint16 ByteOrder::littleEndianShort (const void* const bytes) noexcept { return *static_cast<const uint16*> (bytes); }
  154. inline uint32 ByteOrder::bigEndianInt (const void* const bytes) noexcept { return swap (*static_cast<const uint32*> (bytes)); }
  155. inline uint64 ByteOrder::bigEndianInt64 (const void* const bytes) noexcept { return swap (*static_cast<const uint64*> (bytes)); }
  156. inline uint16 ByteOrder::bigEndianShort (const void* const bytes) noexcept { return swap (*static_cast<const uint16*> (bytes)); }
  157. inline bool ByteOrder::isBigEndian() noexcept { return false; }
  158. #else
  159. inline uint16 ByteOrder::swapIfBigEndian (const uint16 v) noexcept { return swap (v); }
  160. inline uint32 ByteOrder::swapIfBigEndian (const uint32 v) noexcept { return swap (v); }
  161. inline uint64 ByteOrder::swapIfBigEndian (const uint64 v) noexcept { return swap (v); }
  162. inline int16 ByteOrder::swapIfBigEndian (const int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  163. inline int32 ByteOrder::swapIfBigEndian (const int32 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  164. inline int64 ByteOrder::swapIfBigEndian (const int64 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  165. inline float ByteOrder::swapIfBigEndian (const float v) noexcept { union { uint32 asUInt; float asFloat; } n; n.asFloat = v; n.asUInt = ByteOrder::swap (n.asUInt); return n.asFloat; }
  166. inline double ByteOrder::swapIfBigEndian (const double v) noexcept { union { uint64 asUInt; double asFloat; } n; n.asFloat = v; n.asUInt = ByteOrder::swap (n.asUInt); return n.asFloat; }
  167. inline uint16 ByteOrder::swapIfLittleEndian (const uint16 v) noexcept { return v; }
  168. inline uint32 ByteOrder::swapIfLittleEndian (const uint32 v) noexcept { return v; }
  169. inline uint64 ByteOrder::swapIfLittleEndian (const uint64 v) noexcept { return v; }
  170. inline int16 ByteOrder::swapIfLittleEndian (const int16 v) noexcept { return v; }
  171. inline int32 ByteOrder::swapIfLittleEndian (const int32 v) noexcept { return v; }
  172. inline int64 ByteOrder::swapIfLittleEndian (const int64 v) noexcept { return v; }
  173. inline float ByteOrder::swapIfLittleEndian (const float v) noexcept { return v; }
  174. inline double ByteOrder::swapIfLittleEndian (const double v) noexcept { return v; }
  175. inline uint32 ByteOrder::littleEndianInt (const void* const bytes) noexcept { return swap (*static_cast<const uint32*> (bytes)); }
  176. inline uint64 ByteOrder::littleEndianInt64 (const void* const bytes) noexcept { return swap (*static_cast<const uint64*> (bytes)); }
  177. inline uint16 ByteOrder::littleEndianShort (const void* const bytes) noexcept { return swap (*static_cast<const uint16*> (bytes)); }
  178. inline uint32 ByteOrder::bigEndianInt (const void* const bytes) noexcept { return *static_cast<const uint32*> (bytes); }
  179. inline uint64 ByteOrder::bigEndianInt64 (const void* const bytes) noexcept { return *static_cast<const uint64*> (bytes); }
  180. inline uint16 ByteOrder::bigEndianShort (const void* const bytes) noexcept { return *static_cast<const uint16*> (bytes); }
  181. inline bool ByteOrder::isBigEndian() noexcept { return true; }
  182. #endif
  183. inline int ByteOrder::littleEndian24Bit (const void* const bytes) noexcept { return (((int) static_cast<const int8*> (bytes)[2]) << 16) | (((int) static_cast<const uint8*> (bytes)[1]) << 8) | ((int) static_cast<const uint8*> (bytes)[0]); }
  184. inline int ByteOrder::bigEndian24Bit (const void* const bytes) noexcept { return (((int) static_cast<const int8*> (bytes)[0]) << 16) | (((int) static_cast<const uint8*> (bytes)[1]) << 8) | ((int) static_cast<const uint8*> (bytes)[2]); }
  185. inline void ByteOrder::littleEndian24BitToChars (const int value, void* const destBytes) noexcept { static_cast<uint8*> (destBytes)[0] = (uint8) value; static_cast<uint8*> (destBytes)[1] = (uint8) (value >> 8); static_cast<uint8*> (destBytes)[2] = (uint8) (value >> 16); }
  186. inline void ByteOrder::bigEndian24BitToChars (const int value, void* const destBytes) noexcept { static_cast<uint8*> (destBytes)[0] = (uint8) (value >> 16); static_cast<uint8*> (destBytes)[1] = (uint8) (value >> 8); static_cast<uint8*> (destBytes)[2] = (uint8) value; }
  187. #endif // JUCE_BYTEORDER_H_INCLUDED