OpenShot Library | libopenshot-audio  0.2.0
juce_ByteOrder.h
1 
2 /** @weakgroup juce_core-memory
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /** Contains static methods for converting the byte order between different
32  endiannesses.
33 
34  @tags{Core}
35 */
37 {
38 public:
39  //==============================================================================
40  /** Swaps the upper and lower bytes of a 16-bit integer. */
41  JUCE_CONSTEXPR static uint16 swap (uint16 value) noexcept;
42 
43  /** Swaps the upper and lower bytes of a 16-bit integer. */
44  JUCE_CONSTEXPR static int16 swap (int16 value) noexcept;
45 
46  /** Reverses the order of the 4 bytes in a 32-bit integer. */
47  static uint32 swap (uint32 value) noexcept;
48 
49  /** Reverses the order of the 4 bytes in a 32-bit integer. */
50  static int32 swap (int32 value) noexcept;
51 
52  /** Reverses the order of the 8 bytes in a 64-bit integer. */
53  static uint64 swap (uint64 value) noexcept;
54 
55  /** Reverses the order of the 8 bytes in a 64-bit integer. */
56  static int64 swap (int64 value) noexcept;
57 
58  /** Returns a garbled float which has the reverse byte-order of the original. */
59  static float swap (float value) noexcept;
60 
61  /** Returns a garbled double which has the reverse byte-order of the original. */
62  static double swap (double value) noexcept;
63 
64  //==============================================================================
65  /** Swaps the byte order of a signed or unsigned integer if the CPU is big-endian */
66  template <typename Type>
67  static Type swapIfBigEndian (Type value) noexcept
68  {
69  #if JUCE_LITTLE_ENDIAN
70  return value;
71  #else
72  return swap (value);
73  #endif
74  }
75 
76  /** Swaps the byte order of a signed or unsigned integer if the CPU is little-endian */
77  template <typename Type>
78  static Type swapIfLittleEndian (Type value) noexcept
79  {
80  #if JUCE_LITTLE_ENDIAN
81  return swap (value);
82  #else
83  return value;
84  #endif
85  }
86 
87  //==============================================================================
88  /** Turns 4 bytes into a little-endian integer. */
89  JUCE_CONSTEXPR static uint32 littleEndianInt (const void* bytes) noexcept;
90 
91  /** Turns 8 bytes into a little-endian integer. */
92  JUCE_CONSTEXPR static uint64 littleEndianInt64 (const void* bytes) noexcept;
93 
94  /** Turns 2 bytes into a little-endian integer. */
95  JUCE_CONSTEXPR static uint16 littleEndianShort (const void* bytes) noexcept;
96 
97  /** Converts 3 little-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
98  JUCE_CONSTEXPR static int littleEndian24Bit (const void* bytes) noexcept;
99 
100  /** Copies a 24-bit number to 3 little-endian bytes. */
101  static void littleEndian24BitToChars (int32 value, void* destBytes) noexcept;
102 
103  //==============================================================================
104  /** Turns 4 bytes into a big-endian integer. */
105  JUCE_CONSTEXPR static uint32 bigEndianInt (const void* bytes) noexcept;
106 
107  /** Turns 8 bytes into a big-endian integer. */
108  JUCE_CONSTEXPR static uint64 bigEndianInt64 (const void* bytes) noexcept;
109 
110  /** Turns 2 bytes into a big-endian integer. */
111  JUCE_CONSTEXPR static uint16 bigEndianShort (const void* bytes) noexcept;
112 
113  /** Converts 3 big-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits). */
114  JUCE_CONSTEXPR static int bigEndian24Bit (const void* bytes) noexcept;
115 
116  /** Copies a 24-bit number to 3 big-endian bytes. */
117  static void bigEndian24BitToChars (int32 value, void* destBytes) noexcept;
118 
119  //==============================================================================
120  /** Constructs a 16-bit integer from its constituent bytes, in order of significance. */
121  JUCE_CONSTEXPR static uint16 makeInt (uint8 leastSig, uint8 mostSig) noexcept;
122 
123  /** Constructs a 32-bit integer from its constituent bytes, in order of significance. */
124  JUCE_CONSTEXPR static uint32 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 mostSig) noexcept;
125 
126  /** Constructs a 64-bit integer from its constituent bytes, in order of significance. */
127  JUCE_CONSTEXPR static uint64 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 byte3,
128  uint8 byte4, uint8 byte5, uint8 byte6, uint8 mostSig) noexcept;
129 
130  //==============================================================================
131  /** Returns true if the current CPU is big-endian. */
132  JUCE_CONSTEXPR static bool isBigEndian() noexcept
133  {
134  #if JUCE_LITTLE_ENDIAN
135  return false;
136  #else
137  return true;
138  #endif
139  }
140 
141 private:
142  ByteOrder() = delete;
143 };
144 
145 
146 //==============================================================================
147 JUCE_CONSTEXPR inline uint16 ByteOrder::swap (uint16 v) noexcept { return static_cast<uint16> ((v << 8) | (v >> 8)); }
148 JUCE_CONSTEXPR inline int16 ByteOrder::swap (int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
149 inline int32 ByteOrder::swap (int32 v) noexcept { return static_cast<int32> (swap (static_cast<uint32> (v))); }
150 inline int64 ByteOrder::swap (int64 v) noexcept { return static_cast<int64> (swap (static_cast<uint64> (v))); }
151 inline float ByteOrder::swap (float v) noexcept { union { uint32 asUInt; float asFloat; } n; n.asFloat = v; n.asUInt = swap (n.asUInt); return n.asFloat; }
152 inline double ByteOrder::swap (double v) noexcept { union { uint64 asUInt; double asFloat; } n; n.asFloat = v; n.asUInt = swap (n.asUInt); return n.asFloat; }
153 
154 #if JUCE_MSVC && ! defined (__INTEL_COMPILER)
155  #pragma intrinsic (_byteswap_ulong)
156 #endif
157 
158 inline uint32 ByteOrder::swap (uint32 n) noexcept
159 {
160  #if JUCE_MAC || JUCE_IOS
161  return OSSwapInt32 (n);
162  #elif (JUCE_GCC || JUCE_CLANG) && JUCE_INTEL && ! JUCE_NO_INLINE_ASM
163  asm("bswap %%eax" : "=a"(n) : "a"(n));
164  return n;
165  #elif JUCE_MSVC
166  return _byteswap_ulong (n);
167  #elif JUCE_ANDROID
168  return bswap_32 (n);
169  #else
170  return (n << 24) | (n >> 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8);
171  #endif
172 }
173 
174 inline uint64 ByteOrder::swap (uint64 value) noexcept
175 {
176  #if JUCE_MAC || JUCE_IOS
177  return OSSwapInt64 (value);
178  #elif JUCE_MSVC
179  return _byteswap_uint64 (value);
180  #else
181  return (((uint64) swap ((uint32) value)) << 32) | swap ((uint32) (value >> 32));
182  #endif
183 }
184 
185 JUCE_CONSTEXPR inline uint16 ByteOrder::makeInt (uint8 b0, uint8 b1) noexcept
186 {
187  return static_cast<uint16> (static_cast<uint16> (b0) | (static_cast<uint16> (b1) << 8));
188 }
189 
190 JUCE_CONSTEXPR inline uint32 ByteOrder::makeInt (uint8 b0, uint8 b1, uint8 b2, uint8 b3) noexcept
191 {
192  return static_cast<uint32> (b0) | (static_cast<uint32> (b1) << 8)
193  | (static_cast<uint32> (b2) << 16) | (static_cast<uint32> (b3) << 24);
194 }
195 
196 JUCE_CONSTEXPR inline uint64 ByteOrder::makeInt (uint8 b0, uint8 b1, uint8 b2, uint8 b3, uint8 b4, uint8 b5, uint8 b6, uint8 b7) noexcept
197 {
198  return static_cast<uint64> (b0) | (static_cast<uint64> (b1) << 8) | (static_cast<uint64> (b2) << 16) | (static_cast<uint64> (b3) << 24)
199  | (static_cast<uint64> (b4) << 32) | (static_cast<uint64> (b5) << 40) | (static_cast<uint64> (b6) << 48) | (static_cast<uint64> (b7) << 56);
200 }
201 
202 JUCE_CONSTEXPR inline uint16 ByteOrder::littleEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1]); }
203 JUCE_CONSTEXPR inline uint32 ByteOrder::littleEndianInt (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1],
204  static_cast<const uint8*> (bytes)[2], static_cast<const uint8*> (bytes)[3]); }
205 JUCE_CONSTEXPR inline uint64 ByteOrder::littleEndianInt64 (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1],
206  static_cast<const uint8*> (bytes)[2], static_cast<const uint8*> (bytes)[3],
207  static_cast<const uint8*> (bytes)[4], static_cast<const uint8*> (bytes)[5],
208  static_cast<const uint8*> (bytes)[6], static_cast<const uint8*> (bytes)[7]); }
209 
210 JUCE_CONSTEXPR inline uint16 ByteOrder::bigEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
211 JUCE_CONSTEXPR inline uint32 ByteOrder::bigEndianInt (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[3], static_cast<const uint8*> (bytes)[2],
212  static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
213 JUCE_CONSTEXPR inline uint64 ByteOrder::bigEndianInt64 (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[7], static_cast<const uint8*> (bytes)[6],
214  static_cast<const uint8*> (bytes)[5], static_cast<const uint8*> (bytes)[4],
215  static_cast<const uint8*> (bytes)[3], static_cast<const uint8*> (bytes)[2],
216  static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
217 
218 JUCE_CONSTEXPR inline int32 ByteOrder::littleEndian24Bit (const void* bytes) noexcept { return (int32) ((((uint32) static_cast<const int8*> (bytes)[2]) << 16) | (((uint32) static_cast<const uint8*> (bytes)[1]) << 8) | ((uint32) static_cast<const uint8*> (bytes)[0])); }
219 JUCE_CONSTEXPR inline int32 ByteOrder::bigEndian24Bit (const void* bytes) noexcept { return (int32) ((((uint32) static_cast<const int8*> (bytes)[0]) << 16) | (((uint32) static_cast<const uint8*> (bytes)[1]) << 8) | ((uint32) static_cast<const uint8*> (bytes)[2])); }
220 
221 inline void ByteOrder::littleEndian24BitToChars (int32 value, void* 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); }
222 inline void ByteOrder::bigEndian24BitToChars (int32 value, void* 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; }
223 
224 } // namespace juce
225 
226 /** @}*/
static Type swapIfBigEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is big-endian.
Contains static methods for converting the byte order between different endiannesses.
#define JUCE_API
This macro is added to all JUCE public class declarations.
static JUCE_CONSTEXPR bool isBigEndian() noexcept
Returns true if the current CPU is big-endian.
static JUCE_CONSTEXPR int littleEndian24Bit(const void *bytes) noexcept
Converts 3 little-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits)...
static JUCE_CONSTEXPR uint16 makeInt(uint8 leastSig, uint8 mostSig) noexcept
Constructs a 16-bit integer from its constituent bytes, in order of significance. ...
static JUCE_CONSTEXPR uint32 bigEndianInt(const void *bytes) noexcept
Turns 4 bytes into a big-endian integer.
static JUCE_CONSTEXPR int bigEndian24Bit(const void *bytes) noexcept
Converts 3 big-endian bytes into a signed 24-bit value (which is sign-extended to 32 bits)...
static void bigEndian24BitToChars(int32 value, void *destBytes) noexcept
Copies a 24-bit number to 3 big-endian bytes.
static JUCE_CONSTEXPR uint16 swap(uint16 value) noexcept
Swaps the upper and lower bytes of a 16-bit integer.
static void littleEndian24BitToChars(int32 value, void *destBytes) noexcept
Copies a 24-bit number to 3 little-endian bytes.
static Type swapIfLittleEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is little-endian. ...
static JUCE_CONSTEXPR uint32 littleEndianInt(const void *bytes) noexcept
Turns 4 bytes into a little-endian integer.
static JUCE_CONSTEXPR uint16 littleEndianShort(const void *bytes) noexcept
Turns 2 bytes into a little-endian integer.
static JUCE_CONSTEXPR uint16 bigEndianShort(const void *bytes) noexcept
Turns 2 bytes into a big-endian integer.
static JUCE_CONSTEXPR uint64 bigEndianInt64(const void *bytes) noexcept
Turns 8 bytes into a big-endian integer.
static JUCE_CONSTEXPR uint64 littleEndianInt64(const void *bytes) noexcept
Turns 8 bytes into a little-endian integer.