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.

253 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. #if defined(CARLA_OS_BSD)
  24. # include <sys/endian.h>
  25. #elif defined(CARLA_OS_LINUX)
  26. # include <byteswap.h>
  27. #elif defined(CARLA_OS_MAC)
  28. # include <libkern/OSByteOrder.h>
  29. #endif
  30. namespace water {
  31. //==============================================================================
  32. /** Contains static methods for converting the byte order between different
  33. endiannesses.
  34. */
  35. class ByteOrder
  36. {
  37. public:
  38. //==============================================================================
  39. /** Swaps the upper and lower bytes of a 16-bit integer. */
  40. static uint16 swap (uint16 value) noexcept;
  41. /** Reverses the order of the 4 bytes in a 32-bit integer. */
  42. static uint32 swap (uint32 value) noexcept;
  43. /** Reverses the order of the 8 bytes in a 64-bit integer. */
  44. static uint64 swap (uint64 value) noexcept;
  45. //==============================================================================
  46. /** Swaps the byte order of a 16-bit unsigned int if the CPU is big-endian */
  47. static uint16 swapIfBigEndian (uint16 value) noexcept;
  48. /** Swaps the byte order of a 32-bit unsigned int if the CPU is big-endian */
  49. static uint32 swapIfBigEndian (uint32 value) noexcept;
  50. /** Swaps the byte order of a 64-bit unsigned int if the CPU is big-endian */
  51. static uint64 swapIfBigEndian (uint64 value) noexcept;
  52. /** Swaps the byte order of a 16-bit signed int if the CPU is big-endian */
  53. static int16 swapIfBigEndian (int16 value) noexcept;
  54. /** Swaps the byte order of a 32-bit signed int if the CPU is big-endian */
  55. static int32 swapIfBigEndian (int32 value) noexcept;
  56. /** Swaps the byte order of a 64-bit signed int if the CPU is big-endian */
  57. static int64 swapIfBigEndian (int64 value) noexcept;
  58. /** Swaps the byte order of a 32-bit float if the CPU is big-endian */
  59. static float swapIfBigEndian (float value) noexcept;
  60. /** Swaps the byte order of a 64-bit float if the CPU is big-endian */
  61. static double swapIfBigEndian (double value) noexcept;
  62. /** Swaps the byte order of a 16-bit unsigned int if the CPU is little-endian */
  63. static uint16 swapIfLittleEndian (uint16 value) noexcept;
  64. /** Swaps the byte order of a 32-bit unsigned int if the CPU is little-endian */
  65. static uint32 swapIfLittleEndian (uint32 value) noexcept;
  66. /** Swaps the byte order of a 64-bit unsigned int if the CPU is little-endian */
  67. static uint64 swapIfLittleEndian (uint64 value) noexcept;
  68. /** Swaps the byte order of a 16-bit signed int if the CPU is little-endian */
  69. static int16 swapIfLittleEndian (int16 value) noexcept;
  70. /** Swaps the byte order of a 32-bit signed int if the CPU is little-endian */
  71. static int32 swapIfLittleEndian (int32 value) noexcept;
  72. /** Swaps the byte order of a 64-bit signed int if the CPU is little-endian */
  73. static int64 swapIfLittleEndian (int64 value) noexcept;
  74. /** Swaps the byte order of a 32-bit float if the CPU is little-endian */
  75. static float swapIfLittleEndian (float value) noexcept;
  76. /** Swaps the byte order of a 64-bit float if the CPU is little-endian */
  77. static double swapIfLittleEndian (double value) noexcept;
  78. //==============================================================================
  79. /** Turns 4 bytes into a little-endian integer. */
  80. static uint32 littleEndianInt (const void* bytes) noexcept;
  81. /** Turns 8 bytes into a little-endian integer. */
  82. static uint64 littleEndianInt64 (const void* bytes) noexcept;
  83. /** Turns 2 bytes into a little-endian integer. */
  84. static uint16 littleEndianShort (const void* bytes) noexcept;
  85. /** Turns 4 bytes into a big-endian integer. */
  86. static uint32 bigEndianInt (const void* bytes) noexcept;
  87. /** Turns 8 bytes into a big-endian integer. */
  88. static uint64 bigEndianInt64 (const void* bytes) noexcept;
  89. /** Turns 2 bytes into a big-endian integer. */
  90. static uint16 bigEndianShort (const void* bytes) noexcept;
  91. //==============================================================================
  92. /** Converts 3 little-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
  93. static int littleEndian24Bit (const void* bytes) noexcept;
  94. /** Converts 3 big-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
  95. static int bigEndian24Bit (const void* bytes) noexcept;
  96. /** Copies a 24-bit number to 3 little-endian bytes. */
  97. static void littleEndian24BitToChars (int value, void* destBytes) noexcept;
  98. /** Copies a 24-bit number to 3 big-endian bytes. */
  99. static void bigEndian24BitToChars (int value, void* destBytes) noexcept;
  100. //==============================================================================
  101. /** Returns true if the current CPU is big-endian. */
  102. static bool isBigEndian() noexcept;
  103. private:
  104. ByteOrder() WATER_DELETED_FUNCTION;
  105. CARLA_DECLARE_NON_COPYABLE (ByteOrder)
  106. };
  107. //==============================================================================
  108. inline uint16 ByteOrder::swap (uint16 n) noexcept
  109. {
  110. #if defined(CARLA_OS_BSD)
  111. return bswap16 (n);
  112. #elif defined(CARLA_OS_LINUX)
  113. return bswap_16 (n);
  114. #else
  115. return static_cast<uint16> ((n << 8) | (n >> 8));
  116. #endif
  117. }
  118. inline uint32 ByteOrder::swap (uint32 n) noexcept
  119. {
  120. #if defined(CARLA_OS_BSD)
  121. return bswap32 (n);
  122. #elif defined(CARLA_OS_LINUX)
  123. return bswap_32 (n);
  124. #elif defined(CARLA_OS_MAC)
  125. return OSSwapInt32 (n);
  126. #elif defined(__i386__) || defined(__x86_64__)
  127. asm("bswap %%eax" : "=a"(n) : "a"(n));
  128. return n;
  129. #else
  130. return (n << 24) | (n >> 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8);
  131. #endif
  132. }
  133. inline uint64 ByteOrder::swap (uint64 value) noexcept
  134. {
  135. #if defined(CARLA_OS_BSD)
  136. return bswap64 (value);
  137. #elif defined(CARLA_OS_LINUX)
  138. return bswap_64 (value);
  139. #elif defined(CARLA_OS_MAC)
  140. return OSSwapInt64 (value);
  141. #else
  142. return (((uint64) swap ((uint32) value)) << 32) | swap ((uint32) (value >> 32));
  143. #endif
  144. }
  145. #if defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  146. inline uint16 ByteOrder::swapIfBigEndian (const uint16 v) noexcept { return swap (v); }
  147. inline uint32 ByteOrder::swapIfBigEndian (const uint32 v) noexcept { return swap (v); }
  148. inline uint64 ByteOrder::swapIfBigEndian (const uint64 v) noexcept { return swap (v); }
  149. inline int16 ByteOrder::swapIfBigEndian (const int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  150. inline int32 ByteOrder::swapIfBigEndian (const int32 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  151. inline int64 ByteOrder::swapIfBigEndian (const int64 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  152. 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; }
  153. 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; }
  154. inline uint16 ByteOrder::swapIfLittleEndian (const uint16 v) noexcept { return v; }
  155. inline uint32 ByteOrder::swapIfLittleEndian (const uint32 v) noexcept { return v; }
  156. inline uint64 ByteOrder::swapIfLittleEndian (const uint64 v) noexcept { return v; }
  157. inline int16 ByteOrder::swapIfLittleEndian (const int16 v) noexcept { return v; }
  158. inline int32 ByteOrder::swapIfLittleEndian (const int32 v) noexcept { return v; }
  159. inline int64 ByteOrder::swapIfLittleEndian (const int64 v) noexcept { return v; }
  160. inline float ByteOrder::swapIfLittleEndian (const float v) noexcept { return v; }
  161. inline double ByteOrder::swapIfLittleEndian (const double v) noexcept { return v; }
  162. inline uint32 ByteOrder::littleEndianInt (const void* const bytes) noexcept { return swap (*static_cast<const uint32*> (bytes)); }
  163. inline uint64 ByteOrder::littleEndianInt64 (const void* const bytes) noexcept { return swap (*static_cast<const uint64*> (bytes)); }
  164. inline uint16 ByteOrder::littleEndianShort (const void* const bytes) noexcept { return swap (*static_cast<const uint16*> (bytes)); }
  165. inline uint32 ByteOrder::bigEndianInt (const void* const bytes) noexcept { return *static_cast<const uint32*> (bytes); }
  166. inline uint64 ByteOrder::bigEndianInt64 (const void* const bytes) noexcept { return *static_cast<const uint64*> (bytes); }
  167. inline uint16 ByteOrder::bigEndianShort (const void* const bytes) noexcept { return *static_cast<const uint16*> (bytes); }
  168. inline bool ByteOrder::isBigEndian() noexcept { return true; }
  169. #else
  170. inline uint16 ByteOrder::swapIfBigEndian (const uint16 v) noexcept { return v; }
  171. inline uint32 ByteOrder::swapIfBigEndian (const uint32 v) noexcept { return v; }
  172. inline uint64 ByteOrder::swapIfBigEndian (const uint64 v) noexcept { return v; }
  173. inline int16 ByteOrder::swapIfBigEndian (const int16 v) noexcept { return v; }
  174. inline int32 ByteOrder::swapIfBigEndian (const int32 v) noexcept { return v; }
  175. inline int64 ByteOrder::swapIfBigEndian (const int64 v) noexcept { return v; }
  176. inline float ByteOrder::swapIfBigEndian (const float v) noexcept { return v; }
  177. inline double ByteOrder::swapIfBigEndian (const double v) noexcept { return v; }
  178. inline uint16 ByteOrder::swapIfLittleEndian (const uint16 v) noexcept { return swap (v); }
  179. inline uint32 ByteOrder::swapIfLittleEndian (const uint32 v) noexcept { return swap (v); }
  180. inline uint64 ByteOrder::swapIfLittleEndian (const uint64 v) noexcept { return swap (v); }
  181. inline int16 ByteOrder::swapIfLittleEndian (const int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
  182. inline int32 ByteOrder::swapIfLittleEndian (const int32 v) noexcept { return static_cast<int32> (swap (static_cast<uint32> (v))); }
  183. inline int64 ByteOrder::swapIfLittleEndian (const int64 v) noexcept { return static_cast<int64> (swap (static_cast<uint64> (v))); }
  184. 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; }
  185. 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; }
  186. inline uint32 ByteOrder::littleEndianInt (const void* const bytes) noexcept { return *static_cast<const uint32*> (bytes); }
  187. inline uint64 ByteOrder::littleEndianInt64 (const void* const bytes) noexcept { return *static_cast<const uint64*> (bytes); }
  188. inline uint16 ByteOrder::littleEndianShort (const void* const bytes) noexcept { return *static_cast<const uint16*> (bytes); }
  189. inline uint32 ByteOrder::bigEndianInt (const void* const bytes) noexcept { return swap (*static_cast<const uint32*> (bytes)); }
  190. inline uint64 ByteOrder::bigEndianInt64 (const void* const bytes) noexcept { return swap (*static_cast<const uint64*> (bytes)); }
  191. inline uint16 ByteOrder::bigEndianShort (const void* const bytes) noexcept { return swap (*static_cast<const uint16*> (bytes)); }
  192. inline bool ByteOrder::isBigEndian() noexcept { return false; }
  193. #endif
  194. 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]); }
  195. 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]); }
  196. 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); }
  197. 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; }
  198. }
  199. #endif // WATER_BYTEORDER_H_INCLUDED