SDL 3.0
SDL_endian.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * \file SDL_endian.h
24 *
25 * Functions for reading and writing endian-specific values
26 */
27
28#ifndef SDL_endian_h_
29#define SDL_endian_h_
30
31#include <SDL3/SDL_stdinc.h>
32
33#if defined(_MSC_VER) && (_MSC_VER >= 1400)
34/* As of Clang 11, '_m_prefetchw' is conflicting with the winnt.h's version,
35 so we define the needed '_m_prefetch' here as a pseudo-header, until the issue is fixed. */
36#ifdef __clang__
37#ifndef __PRFCHWINTRIN_H
38#define __PRFCHWINTRIN_H
39static __inline__ void __attribute__((__always_inline__, __nodebug__))
40_m_prefetch(void *__P)
41{
42 __builtin_prefetch(__P, 0, 3 /* _MM_HINT_T0 */);
43}
44#endif /* __PRFCHWINTRIN_H */
45#endif /* __clang__ */
46
47#include <intrin.h>
48#endif
49
50/**
51 * \name The two types of endianness
52 */
53/* @{ */
54#define SDL_LIL_ENDIAN 1234
55#define SDL_BIG_ENDIAN 4321
56/* @} */
57
58#ifndef SDL_BYTEORDER
59#ifdef SDL_PLATFORM_LINUX
60#include <endian.h>
61#define SDL_BYTEORDER __BYTE_ORDER
62#elif defined(SDL_PLATFORM_OPENBSD) || defined(__DragonFly__)
63#include <endian.h>
64#define SDL_BYTEORDER BYTE_ORDER
65#elif defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_NETBSD)
66#include <sys/endian.h>
67#define SDL_BYTEORDER BYTE_ORDER
68/* predefs from newer gcc and clang versions: */
69#elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__)
70#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
71#define SDL_BYTEORDER SDL_LIL_ENDIAN
72#elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
73#define SDL_BYTEORDER SDL_BIG_ENDIAN
74#else
75#error Unsupported endianness
76#endif /**/
77#else
78#if defined(__hppa__) || \
79 defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
80 (defined(__MIPS__) && defined(__MIPSEB__)) || \
81 defined(__ppc__) || defined(__POWERPC__) || defined(__powerpc__) || defined(__PPC__) || \
82 defined(__sparc__)
83#define SDL_BYTEORDER SDL_BIG_ENDIAN
84#else
85#define SDL_BYTEORDER SDL_LIL_ENDIAN
86#endif
87#endif /* SDL_PLATFORM_LINUX */
88#endif /* !SDL_BYTEORDER */
89
90#ifndef SDL_FLOATWORDORDER
91/* predefs from newer gcc versions: */
92#if defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__FLOAT_WORD_ORDER__)
93#if (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)
94#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
95#elif (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
96#define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
97#else
98#error Unsupported endianness
99#endif /**/
100#elif defined(__MAVERICK__)
101/* For Maverick, float words are always little-endian. */
102#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
103#elif (defined(__arm__) || defined(__thumb__)) && !defined(__VFP_FP__) && !defined(__ARM_EABI__)
104/* For FPA, float words are always big-endian. */
105#define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
106#else
107/* By default, assume that floats words follow the memory system mode. */
108#define SDL_FLOATWORDORDER SDL_BYTEORDER
109#endif /* __FLOAT_WORD_ORDER__ */
110#endif /* !SDL_FLOATWORDORDER */
111
112
113#include <SDL3/SDL_begin_code.h>
114/* Set up for C function definitions, even when using C++ */
115#ifdef __cplusplus
116extern "C" {
117#endif
118
119/**
120 * \file SDL_endian.h
121 */
122
123/* various modern compilers may have builtin swap */
124#if defined(__GNUC__) || defined(__clang__)
125# define HAS_BUILTIN_BSWAP16 (SDL_HAS_BUILTIN(__builtin_bswap16)) || \
126 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
127# define HAS_BUILTIN_BSWAP32 (SDL_HAS_BUILTIN(__builtin_bswap32)) || \
128 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
129# define HAS_BUILTIN_BSWAP64 (SDL_HAS_BUILTIN(__builtin_bswap64)) || \
130 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
131
132 /* this one is broken */
133# define HAS_BROKEN_BSWAP (__GNUC__ == 2 && __GNUC_MINOR__ <= 95)
134#else
135# define HAS_BUILTIN_BSWAP16 0
136# define HAS_BUILTIN_BSWAP32 0
137# define HAS_BUILTIN_BSWAP64 0
138# define HAS_BROKEN_BSWAP 0
139#endif
140
141/**
142 * Byte swap 16-bit integer.
143 */
144#if HAS_BUILTIN_BSWAP16
145#define SDL_Swap16(x) __builtin_bswap16(x)
146#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
147#pragma intrinsic(_byteswap_ushort)
148#define SDL_Swap16(x) _byteswap_ushort(x)
149#elif defined(__i386__) && !HAS_BROKEN_BSWAP
152{
153 __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
154 return x;
155}
156#elif defined(__x86_64__)
159{
160 __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
161 return x;
162}
163#elif (defined(__powerpc__) || defined(__ppc__))
166{
167 int result;
168
169 __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
170 return (Uint16)result;
171}
172#elif (defined(__m68k__) && !defined(__mcoldfire__))
175{
176 __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
177 return x;
178}
179#elif defined(__WATCOMC__) && defined(__386__)
180extern __inline Uint16 SDL_Swap16(Uint16);
181#pragma aux SDL_Swap16 = \
182 "xchg al, ah" \
183 parm [ax] \
184 modify [ax];
185#else
188{
189 return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
190}
191#endif
192
193/**
194 * Byte swap 32-bit integer.
195 */
196#if HAS_BUILTIN_BSWAP32
197#define SDL_Swap32(x) __builtin_bswap32(x)
198#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
199#pragma intrinsic(_byteswap_ulong)
200#define SDL_Swap32(x) _byteswap_ulong(x)
201#elif defined(__i386__) && !HAS_BROKEN_BSWAP
204{
205 __asm__("bswap %0": "=r"(x):"0"(x));
206 return x;
207}
208#elif defined(__x86_64__)
211{
212 __asm__("bswapl %0": "=r"(x):"0"(x));
213 return x;
214}
215#elif (defined(__powerpc__) || defined(__ppc__))
218{
219 Uint32 result;
220
221 __asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));
222 __asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));
223 __asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
224 return result;
225}
226#elif (defined(__m68k__) && !defined(__mcoldfire__))
229{
230 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
231 return x;
232}
233#elif defined(__WATCOMC__) && defined(__386__)
234extern __inline Uint32 SDL_Swap32(Uint32);
235#pragma aux SDL_Swap32 = \
236 "bswap eax" \
237 parm [eax] \
238 modify [eax];
239#else
242{
243 return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
244 ((x >> 8) & 0x0000FF00) | (x >> 24)));
245}
246#endif
247
248/**
249 * Byte swap 64-bit integer.
250 */
251#if HAS_BUILTIN_BSWAP64
252#define SDL_Swap64(x) __builtin_bswap64(x)
253#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
254#pragma intrinsic(_byteswap_uint64)
255#define SDL_Swap64(x) _byteswap_uint64(x)
256#elif defined(__i386__) && !HAS_BROKEN_BSWAP
259{
260 union {
261 struct {
262 Uint32 a, b;
263 } s;
264 Uint64 u;
265 } v;
266 v.u = x;
267 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
268 : "=r"(v.s.a), "=r"(v.s.b)
269 : "0" (v.s.a), "1"(v.s.b));
270 return v.u;
271}
272#elif defined(__x86_64__)
275{
276 __asm__("bswapq %0": "=r"(x):"0"(x));
277 return x;
278}
279#elif defined(__WATCOMC__) && defined(__386__)
280extern __inline Uint64 SDL_Swap64(Uint64);
281#pragma aux SDL_Swap64 = \
282 "bswap eax" \
283 "bswap edx" \
284 "xchg eax,edx" \
285 parm [eax edx] \
286 modify [eax edx];
287#else
290{
291 Uint32 hi, lo;
292
293 /* Separate into high and low 32-bit values and swap them */
294 lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
295 x >>= 32;
296 hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
297 x = SDL_Swap32(lo);
298 x <<= 32;
299 x |= SDL_Swap32(hi);
300 return (x);
301}
302#endif
303
304
305/**
306 * Byte swap floating point number.
307 */
310{
311 union {
312 float f;
313 Uint32 ui32;
314 } swapper;
315 swapper.f = x;
316 swapper.ui32 = SDL_Swap32(swapper.ui32);
317 return swapper.f;
318}
319
320/* remove extra macros */
321#undef HAS_BROKEN_BSWAP
322#undef HAS_BUILTIN_BSWAP16
323#undef HAS_BUILTIN_BSWAP32
324#undef HAS_BUILTIN_BSWAP64
325
326/**
327 * \name Swap to native
328 * Byteswap item from the specified endianness to the native endianness.
329 */
330
331/**
332 * \def SDL_SwapLE16
333 * Swap 16-bit little endian integer to 16-bit native endian integer.
334 */
335
336/**
337 * \def SDL_SwapLE32
338 * Swap 32-bit little endian integer to 32-bit native endian integer.
339 */
340
341/**
342 * \def SDL_SwapLE64
343 * Swap 64-bit little endian integer to 64-bit native endian integer.
344 */
345
346/**
347 * \def SDL_SwapFloatLE
348 * Swap little endian float to native endian float.
349 */
350/**
351 * \def SDL_SwapBE16
352 * Swap 16-bit big endian integer to 16-bit native endian integer.
353 */
354
355/**
356 * \def SDL_SwapBE32
357 * Swap 32-bit big endian integer to 32-bit native endian integer.
358 */
359
360/**
361 * \def SDL_SwapBE64
362 * Swap 64-bit big endian integer to 64-bit native endian integer.
363 */
364
365/**
366 * \def SDL_SwapFloatBE
367 * Swap endian float to native endian float.
368 */
369
370/* @{ */
371#if SDL_BYTEORDER == SDL_LIL_ENDIAN
372#define SDL_SwapLE16(X) (X)
373#define SDL_SwapLE32(X) (X)
374#define SDL_SwapLE64(X) (X)
375#define SDL_SwapFloatLE(X) (X)
376#define SDL_SwapBE16(X) SDL_Swap16(X)
377#define SDL_SwapBE32(X) SDL_Swap32(X)
378#define SDL_SwapBE64(X) SDL_Swap64(X)
379#define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
380#else
381#define SDL_SwapLE16(X) SDL_Swap16(X)
382#define SDL_SwapLE32(X) SDL_Swap32(X)
383#define SDL_SwapLE64(X) SDL_Swap64(X)
384#define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
385#define SDL_SwapBE16(X) (X)
386#define SDL_SwapBE32(X) (X)
387#define SDL_SwapBE64(X) (X)
388#define SDL_SwapFloatBE(X) (X)
389#endif
390/* @} *//* Swap to native */
391
392/* Ends C function definitions when using C++ */
393#ifdef __cplusplus
394}
395#endif
396#include <SDL3/SDL_close_code.h>
397
398#endif /* SDL_endian_h_ */
#define __inline__
#define SDL_FORCE_INLINE
SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
Definition SDL_endian.h:241
SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
Definition SDL_endian.h:187
SDL_FORCE_INLINE float SDL_SwapFloat(float x)
Definition SDL_endian.h:309
SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
Definition SDL_endian.h:289
uint16_t Uint16
Definition SDL_stdinc.h:162
#define SDL_static_cast(type, expression)
Definition SDL_stdinc.h:115
uint64_t Uint64
Definition SDL_stdinc.h:187
uint32_t Uint32
Definition SDL_stdinc.h:174