LMMS
Loading...
Searching...
No Matches
juce_ByteOrder.h
Go to the documentation of this file.
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
23#if ! defined (DOXYGEN) && (JUCE_MAC || JUCE_IOS)
24 #include <libkern/OSByteOrder.h>
25#endif
26
27namespace juce
28{
29
30//==============================================================================
37{
38public:
39 //==============================================================================
41 constexpr static uint16 swap (uint16 value) noexcept;
42
44 constexpr static int16 swap (int16 value) noexcept;
45
47 static uint32 swap (uint32 value) noexcept;
48
50 static int32 swap (int32 value) noexcept;
51
53 static uint64 swap (uint64 value) noexcept;
54
56 static int64 swap (int64 value) noexcept;
57
59 static float swap (float value) noexcept;
60
62 static double swap (double value) noexcept;
63
64 //==============================================================================
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
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 //==============================================================================
89 constexpr static uint32 littleEndianInt (const void* bytes) noexcept;
90
92 constexpr static uint64 littleEndianInt64 (const void* bytes) noexcept;
93
95 constexpr static uint16 littleEndianShort (const void* bytes) noexcept;
96
98 constexpr static int littleEndian24Bit (const void* bytes) noexcept;
99
101 static void littleEndian24BitToChars (int32 value, void* destBytes) noexcept;
102
103 //==============================================================================
105 constexpr static uint32 bigEndianInt (const void* bytes) noexcept;
106
108 constexpr static uint64 bigEndianInt64 (const void* bytes) noexcept;
109
111 constexpr static uint16 bigEndianShort (const void* bytes) noexcept;
112
114 constexpr static int bigEndian24Bit (const void* bytes) noexcept;
115
117 static void bigEndian24BitToChars (int32 value, void* destBytes) noexcept;
118
119 //==============================================================================
121 constexpr static uint16 makeInt (uint8 leastSig, uint8 mostSig) noexcept;
122
124 constexpr static uint32 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 mostSig) noexcept;
125
127 constexpr static uint64 makeInt (uint8 leastSig, uint8 byte1, uint8 byte2, uint8 byte3,
128 uint8 byte4, uint8 byte5, uint8 byte6, uint8 mostSig) noexcept;
129
130 //==============================================================================
132 constexpr static bool isBigEndian() noexcept
133 {
134 #if JUCE_LITTLE_ENDIAN
135 return false;
136 #else
137 return true;
138 #endif
139 }
140
141private:
142 ByteOrder() = delete;
143};
144
145
146//==============================================================================
147constexpr inline uint16 ByteOrder::swap (uint16 v) noexcept { return static_cast<uint16> ((v << 8) | (v >> 8)); }
148constexpr inline int16 ByteOrder::swap (int16 v) noexcept { return static_cast<int16> (swap (static_cast<uint16> (v))); }
149inline int32 ByteOrder::swap (int32 v) noexcept { return static_cast<int32> (swap (static_cast<uint32> (v))); }
150inline int64 ByteOrder::swap (int64 v) noexcept { return static_cast<int64> (swap (static_cast<uint64> (v))); }
151inline float ByteOrder::swap (float v) noexcept { union { uint32 asUInt; float asFloat; } n; n.asFloat = v; n.asUInt = swap (n.asUInt); return n.asFloat; }
152inline 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
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
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
185constexpr 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
190constexpr 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
196constexpr 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
202constexpr inline uint16 ByteOrder::littleEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[0], static_cast<const uint8*> (bytes)[1]); }
203constexpr 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]); }
205constexpr 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
210constexpr inline uint16 ByteOrder::bigEndianShort (const void* bytes) noexcept { return makeInt (static_cast<const uint8*> (bytes)[1], static_cast<const uint8*> (bytes)[0]); }
211constexpr 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]); }
213constexpr 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
218constexpr 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])); }
219constexpr 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
221inline 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); }
222inline 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
#define noexcept
Definition DistrhoDefines.h:72
static void littleEndian24BitToChars(int value, void *destBytes) noexcept
Definition ByteOrder.h:247
static uint32 littleEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:236
static uint64 bigEndianInt64(const void *bytes) noexcept
Definition ByteOrder.h:240
static uint64 littleEndianInt64(const void *bytes) noexcept
Definition ByteOrder.h:237
static void bigEndian24BitToChars(int value, void *destBytes) noexcept
Definition ByteOrder.h:248
static uint16 bigEndianShort(const void *bytes) noexcept
Definition ByteOrder.h:241
static uint32 bigEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:239
static uint16 littleEndianShort(const void *bytes) noexcept
Definition ByteOrder.h:238
static uint16 swap(uint16 value) noexcept
Definition ByteOrder.h:151
static int littleEndian24Bit(const void *bytes) noexcept
Definition ByteOrder.h:245
static int bigEndian24Bit(const void *bytes) noexcept
Definition ByteOrder.h:246
static Type swapIfLittleEndian(Type value) noexcept
Definition juce_ByteOrder.h:78
static constexpr uint16 makeInt(uint8 leastSig, uint8 mostSig) noexcept
Definition juce_ByteOrder.h:185
static constexpr uint16 swap(uint16 value) noexcept
Definition juce_ByteOrder.h:147
static Type swapIfBigEndian(Type value) noexcept
Definition juce_ByteOrder.h:67
static constexpr bool isBigEndian() noexcept
Definition juce_ByteOrder.h:132
ByteOrder()=delete
unsigned v[N_MAX]
Definition inflate.c:1584
static PuglViewHint int value
Definition pugl.h:1708
#define JUCE_API
Definition juce_StandardHeader.h:152
Definition carla_juce.cpp:31
unsigned short uint16
Definition juce_MathsFunctions.h:41
unsigned long long uint64
Definition juce_MathsFunctions.h:56
unsigned int uint32
Definition juce_MathsFunctions.h:45
signed short int16
Definition juce_MathsFunctions.h:39
long long int64
Definition juce_MathsFunctions.h:54
signed char int8
Definition juce_MathsFunctions.h:35
signed int int32
Definition juce_MathsFunctions.h:43
unsigned char uint8
Definition juce_MathsFunctions.h:37
int n
Definition crypt.c:458