OpenShot Audio Library | OpenShotAudio: juce_ByteOrder.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_ByteOrder.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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//==============================================================================
32 class JUCE_API ByteOrder
33{
34public:
35 //==============================================================================
37 constexpr static uint16 swap (uint16 value) noexcept;
38
40 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 constexpr static uint32 littleEndianInt (const void* bytes) noexcept;
86
88 constexpr static uint64 littleEndianInt64 (const void* bytes) noexcept;
89
91 constexpr static uint16 littleEndianShort (const void* bytes) noexcept;
92
94 constexpr static int littleEndian24Bit (const void* bytes) noexcept;
95
97 static void littleEndian24BitToChars (int32 value, void* destBytes) noexcept;
98
99 //==============================================================================
101 constexpr static uint32 bigEndianInt (const void* bytes) noexcept;
102
104 constexpr static uint64 bigEndianInt64 (const void* bytes) noexcept;
105
107 constexpr static uint16 bigEndianShort (const void* bytes) noexcept;
108
110 constexpr static int bigEndian24Bit (const void* bytes) noexcept;
111
113 static void bigEndian24BitToChars (int32 value, void* destBytes) noexcept;
114
115 //==============================================================================
117 constexpr static uint16 makeInt (uint8 leastSig, uint8 mostSig) noexcept;
118
120 constexpr static uint32 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 mostSig) noexcept;
121
123 constexpr static uint64 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 byte3,
124 uint8 byte4, uint8 byte5, uint8 byte6, uint8 mostSig) noexcept;
125
126 //==============================================================================
128 constexpr static bool isBigEndian() noexcept
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//==============================================================================
143 constexpr inline uint16 ByteOrder::swap (uint16 v) noexcept { return static_cast<uint16> ((v << 8) | (v >> 8)); }
144 constexpr inline int16 ByteOrder::swap (int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
145 inline int32 ByteOrder::swap (int32 v) noexcept { return static_cast<int32> (swap (static_cast<uint32> (v))); }
146 inline int64 ByteOrder::swap (int64 v) noexcept { return static_cast<int64> (swap (static_cast<uint64> (v))); }
147 inline float ByteOrder::swap (float v) noexcept { union { uint32 asUInt; float asFloat; } n; n.asFloat = v; n.asUInt = swap (n.asUInt); return n.asFloat; }
148 inline 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
154 inline 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
170 inline 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
181 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
186 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
192 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
198 constexpr inline uint16 ByteOrder::littleEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1]); }
199 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]); }
201 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
206 constexpr inline uint16 ByteOrder::bigEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
207 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]); }
209 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
214 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])); }
215 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
217 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); }
218 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; }
219
220} // namespace juce
juce::ByteOrder
Definition juce_ByteOrder.h:33
juce::ByteOrder::bigEndianInt64
static constexpr uint64 bigEndianInt64(const void *bytes) noexcept
Definition juce_ByteOrder.h:209
juce::ByteOrder::bigEndianInt
static constexpr uint32 bigEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:207
juce::ByteOrder::littleEndianInt
static constexpr uint32 littleEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:199
juce::ByteOrder::swapIfLittleEndian
static Type swapIfLittleEndian(Type value) noexcept
Definition juce_ByteOrder.h:74
juce::ByteOrder::littleEndian24BitToChars
static void littleEndian24BitToChars(int32 value, void *destBytes) noexcept
Definition juce_ByteOrder.h:217
juce::ByteOrder::bigEndianShort
static constexpr uint16 bigEndianShort(const void *bytes) noexcept
Definition juce_ByteOrder.h:206
juce::ByteOrder::littleEndianShort
static constexpr uint16 littleEndianShort(const void *bytes) noexcept
Definition juce_ByteOrder.h:198
juce::ByteOrder::makeInt
static constexpr uint16 makeInt(uint8 leastSig, uint8 mostSig) noexcept
Definition juce_ByteOrder.h:181
juce::ByteOrder::swap
static constexpr uint16 swap(uint16 value) noexcept
Definition juce_ByteOrder.h:143
juce::ByteOrder::littleEndianInt64
static constexpr uint64 littleEndianInt64(const void *bytes) noexcept
Definition juce_ByteOrder.h:201
juce::ByteOrder::bigEndian24BitToChars
static void bigEndian24BitToChars(int32 value, void *destBytes) noexcept
Definition juce_ByteOrder.h:218
juce::ByteOrder::littleEndian24Bit
static constexpr int littleEndian24Bit(const void *bytes) noexcept
Definition juce_ByteOrder.h:214
juce::ByteOrder::swapIfBigEndian
static Type swapIfBigEndian(Type value) noexcept
Definition juce_ByteOrder.h:63
juce::ByteOrder::isBigEndian
static constexpr bool isBigEndian() noexcept
Definition juce_ByteOrder.h:128
juce::ByteOrder::bigEndian24Bit
static constexpr int bigEndian24Bit(const void *bytes) noexcept
Definition juce_ByteOrder.h:215