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