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.

ByteOrder.h 14KB

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