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/* Byte swap 16-bit integer. */
142#if HAS_BUILTIN_BSWAP16
143#define SDL_Swap16(x) __builtin_bswap16(x)
144#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
145#pragma intrinsic(_byteswap_ushort)
146#define SDL_Swap16(x) _byteswap_ushort(x)
147#elif defined(__i386__) && !HAS_BROKEN_BSWAP
149{
150 __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
151 return x;
152}
153#elif defined(__x86_64__)
155{
156 __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
157 return x;
158}
159#elif (defined(__powerpc__) || defined(__ppc__))
161{
162 int result;
163
164 __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
165 return (Uint16)result;
166}
167#elif (defined(__m68k__) && !defined(__mcoldfire__))
169{
170 __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
171 return x;
172}
173#elif defined(__WATCOMC__) && defined(__386__)
174extern __inline Uint16 SDL_Swap16(Uint16);
175#pragma aux SDL_Swap16 = \
176 "xchg al, ah" \
177 parm [ax] \
178 modify [ax];
179#else
181{
182 return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
183}
184#endif
185
186/* Byte swap 32-bit integer. */
187#if HAS_BUILTIN_BSWAP32
188#define SDL_Swap32(x) __builtin_bswap32(x)
189#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
190#pragma intrinsic(_byteswap_ulong)
191#define SDL_Swap32(x) _byteswap_ulong(x)
192#elif defined(__i386__) && !HAS_BROKEN_BSWAP
194{
195 __asm__("bswap %0": "=r"(x):"0"(x));
196 return x;
197}
198#elif defined(__x86_64__)
200{
201 __asm__("bswapl %0": "=r"(x):"0"(x));
202 return x;
203}
204#elif (defined(__powerpc__) || defined(__ppc__))
206{
207 Uint32 result;
208
209 __asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));
210 __asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));
211 __asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
212 return result;
213}
214#elif (defined(__m68k__) && !defined(__mcoldfire__))
216{
217 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
218 return x;
219}
220#elif defined(__WATCOMC__) && defined(__386__)
221extern __inline Uint32 SDL_Swap32(Uint32);
222#pragma aux SDL_Swap32 = \
223 "bswap eax" \
224 parm [eax] \
225 modify [eax];
226#else
228{
229 return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
230 ((x >> 8) & 0x0000FF00) | (x >> 24)));
231}
232#endif
233
234/* Byte swap 64-bit integer. */
235#if HAS_BUILTIN_BSWAP64
236#define SDL_Swap64(x) __builtin_bswap64(x)
237#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
238#pragma intrinsic(_byteswap_uint64)
239#define SDL_Swap64(x) _byteswap_uint64(x)
240#elif defined(__i386__) && !HAS_BROKEN_BSWAP
242{
243 union {
244 struct {
245 Uint32 a, b;
246 } s;
247 Uint64 u;
248 } v;
249 v.u = x;
250 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
251 : "=r"(v.s.a), "=r"(v.s.b)
252 : "0" (v.s.a), "1"(v.s.b));
253 return v.u;
254}
255#elif defined(__x86_64__)
257{
258 __asm__("bswapq %0": "=r"(x):"0"(x));
259 return x;
260}
261#elif defined(__WATCOMC__) && defined(__386__)
262extern __inline Uint64 SDL_Swap64(Uint64);
263#pragma aux SDL_Swap64 = \
264 "bswap eax" \
265 "bswap edx" \
266 "xchg eax,edx" \
267 parm [eax edx] \
268 modify [eax edx];
269#else
271{
272 Uint32 hi, lo;
273
274 /* Separate into high and low 32-bit values and swap them */
275 lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
276 x >>= 32;
277 hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
278 x = SDL_Swap32(lo);
279 x <<= 32;
280 x |= SDL_Swap32(hi);
281 return (x);
282}
283#endif
284
285
286/**
287 * Byte-swap a floating point number.
288 *
289 * This will always byte-swap the value, whether it's currently in the native
290 * byteorder of the system or not. You should use SDL_SwapFloatLE or
291 * SDL_SwapFloatBE instead, in most cases.
292 *
293 * Note that this is a forced-inline function in a header, and not a public
294 * API function available in the SDL library (which is to say, the code is
295 * embedded in the calling program and the linker and dynamic loader will not
296 * be able to find this function inside SDL itself).
297 *
298 * \param x the value to byte-swap.
299 * \returns x, with its bytes in the opposite endian order.
300 *
301 * \threadsafety It is safe to call this function from any thread.
302 *
303 * \since This function is available since SDL 3.0.0.
304 */
306{
307 union {
308 float f;
309 Uint32 ui32;
310 } swapper;
311 swapper.f = x;
312 swapper.ui32 = SDL_Swap32(swapper.ui32);
313 return swapper.f;
314}
315
316/* remove extra macros */
317#undef HAS_BROKEN_BSWAP
318#undef HAS_BUILTIN_BSWAP16
319#undef HAS_BUILTIN_BSWAP32
320#undef HAS_BUILTIN_BSWAP64
321
322
323#ifdef SDL_WIKI_DOCUMENTATION_SECTION
324
325/**
326 * Byte-swap an unsigned 16-bit number.
327 *
328 * This will always byte-swap the value, whether it's currently in the native
329 * byteorder of the system or not. You should use SDL_SwapLE16 or SDL_SwapBE16
330 * instead, in most cases.
331 *
332 * Note that this is a forced-inline function in a header, and not a public
333 * API function available in the SDL library (which is to say, the code is
334 * embedded in the calling program and the linker and dynamic loader will not
335 * be able to find this function inside SDL itself).
336 *
337 * \param x the value to byte-swap.
338 * \returns `x`, with its bytes in the opposite endian order.
339 *
340 * \threadsafety It is safe to call this function from any thread.
341 *
342 * \since This function is available since SDL 3.0.0.
343 */
344SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x) { return x_but_byteswapped; }
345
346/**
347 * Byte-swap an unsigned 32-bit number.
348 *
349 * This will always byte-swap the value, whether it's currently in the native
350 * byteorder of the system or not. You should use SDL_SwapLE32 or SDL_SwapBE32
351 * instead, in most cases.
352 *
353 * Note that this is a forced-inline function in a header, and not a public
354 * API function available in the SDL library (which is to say, the code is
355 * embedded in the calling program and the linker and dynamic loader will not
356 * be able to find this function inside SDL itself).
357 *
358 * \param x the value to byte-swap.
359 * \returns `x`, with its bytes in the opposite endian order.
360 *
361 * \threadsafety It is safe to call this function from any thread.
362 *
363 * \since This function is available since SDL 3.0.0.
364 */
365SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x) { return x_but_byteswapped; }
366
367/**
368 * Byte-swap an unsigned 64-bit number.
369 *
370 * This will always byte-swap the value, whether it's currently in the native
371 * byteorder of the system or not. You should use SDL_SwapLE64 or SDL_SwapBE64
372 * instead, in most cases.
373 *
374 * Note that this is a forced-inline function in a header, and not a public
375 * API function available in the SDL library (which is to say, the code is
376 * embedded in the calling program and the linker and dynamic loader will not
377 * be able to find this function inside SDL itself).
378 *
379 * \param x the value to byte-swap.
380 * \returns `x`, with its bytes in the opposite endian order.
381 *
382 * \threadsafety It is safe to call this function from any thread.
383 *
384 * \since This function is available since SDL 3.0.0.
385 */
386SDL_FORCE_INLINE Uint32 SDL_Swap64(Uint64 x) { return x_but_byteswapped; }
387
388/**
389 * Swap a 16-bit value from littleendian to native byte order.
390 *
391 * If this is running on a littleendian system, `x` is returned unchanged.
392 *
393 * This macro never references `x` more than once, avoiding side effects.
394 *
395 * \param x the value to swap, in littleendian byte order.
396 * \returns `x` in native byte order.
397 *
398 * \since This macro is available since SDL 3.0.0.
399 */
400#define SDL_SwapLE16(x) SwapOnlyIfNecessary(x)
401
402/**
403 * Swap a 32-bit value from littleendian to native byte order.
404 *
405 * If this is running on a littleendian system, `x` is returned unchanged.
406 *
407 * This macro never references `x` more than once, avoiding side effects.
408 *
409 * \param x the value to swap, in littleendian byte order.
410 * \returns `x` in native byte order.
411 *
412 * \since This macro is available since SDL 3.0.0.
413 */
414#define SDL_SwapLE32(x) SwapOnlyIfNecessary(x)
415
416/**
417 * Swap a 64-bit value from littleendian to native byte order.
418 *
419 * If this is running on a littleendian system, `x` is returned unchanged.
420 *
421 * This macro never references `x` more than once, avoiding side effects.
422 *
423 * \param x the value to swap, in littleendian byte order.
424 * \returns `x` in native byte order.
425 *
426 * \since This macro is available since SDL 3.0.0.
427 */
428#define SDL_SwapLE64(x) SwapOnlyIfNecessary(x)
429
430/**
431 * Swap a floating point value from littleendian to native byte order.
432 *
433 * If this is running on a littleendian system, `x` is returned unchanged.
434 *
435 * This macro never references `x` more than once, avoiding side effects.
436 *
437 * \param x the value to swap, in littleendian byte order.
438 * \returns `x` in native byte order.
439 *
440 * \since This macro is available since SDL 3.0.0.
441 */
442#define SDL_SwapFloatLE(x) SwapOnlyIfNecessary(x)
443
444/**
445 * Swap a 16-bit value from bigendian to native byte order.
446 *
447 * If this is running on a bigendian system, `x` is returned unchanged.
448 *
449 * This macro never references `x` more than once, avoiding side effects.
450 *
451 * \param x the value to swap, in bigendian byte order.
452 * \returns `x` in native byte order.
453 *
454 * \since This macro is available since SDL 3.0.0.
455 */
456#define SDL_SwapBE16(x) SwapOnlyIfNecessary(x)
457
458/**
459 * Swap a 32-bit value from bigendian to native byte order.
460 *
461 * If this is running on a bigendian system, `x` is returned unchanged.
462 *
463 * This macro never references `x` more than once, avoiding side effects.
464 *
465 * \param x the value to swap, in bigendian byte order.
466 * \returns `x` in native byte order.
467 *
468 * \since This macro is available since SDL 3.0.0.
469 */
470#define SDL_SwapBE32(x) SwapOnlyIfNecessary(x)
471
472/**
473 * Swap a 64-bit value from bigendian to native byte order.
474 *
475 * If this is running on a bigendian system, `x` is returned unchanged.
476 *
477 * This macro never references `x` more than once, avoiding side effects.
478 *
479 * \param x the value to swap, in bigendian byte order.
480 * \returns `x` in native byte order.
481 *
482 * \since This macro is available since SDL 3.0.0.
483 */
484#define SDL_SwapBE64(x) SwapOnlyIfNecessary(x)
485
486/**
487 * Swap a floating point value from bigendian to native byte order.
488 *
489 * If this is running on a bigendian system, `x` is returned unchanged.
490 *
491 * This macro never references `x` more than once, avoiding side effects.
492 *
493 * \param x the value to swap, in bigendian byte order.
494 * \returns `x` in native byte order.
495 *
496 * \since This macro is available since SDL 3.0.0.
497 */
498#define SDL_SwapFloatBE(x) SwapOnlyIfNecessary(x)
499
500#elif SDL_BYTEORDER == SDL_LIL_ENDIAN
501#define SDL_SwapLE16(x) (x)
502#define SDL_SwapLE32(x) (x)
503#define SDL_SwapLE64(x) (x)
504#define SDL_SwapFloatLE(x) (x)
505#define SDL_SwapBE16(x) SDL_Swap16(x)
506#define SDL_SwapBE32(x) SDL_Swap32(x)
507#define SDL_SwapBE64(x) SDL_Swap64(x)
508#define SDL_SwapFloatBE(x) SDL_SwapFloat(x)
509#else
510#define SDL_SwapLE16(x) SDL_Swap16(x)
511#define SDL_SwapLE32(x) SDL_Swap32(x)
512#define SDL_SwapLE64(x) SDL_Swap64(x)
513#define SDL_SwapFloatLE(x) SDL_SwapFloat(x)
514#define SDL_SwapBE16(x) (x)
515#define SDL_SwapBE32(x) (x)
516#define SDL_SwapBE64(x) (x)
517#define SDL_SwapFloatBE(x) (x)
518#endif
519
520/* Ends C function definitions when using C++ */
521#ifdef __cplusplus
522}
523#endif
524#include <SDL3/SDL_close_code.h>
525
526#endif /* SDL_endian_h_ */
#define __inline__
#define SDL_FORCE_INLINE
SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
Definition SDL_endian.h:227
SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
Definition SDL_endian.h:180
SDL_FORCE_INLINE float SDL_SwapFloat(float x)
Definition SDL_endian.h:305
SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
Definition SDL_endian.h:270
uint16_t Uint16
Definition SDL_stdinc.h:206
#define SDL_static_cast(type, expression)
Definition SDL_stdinc.h:127
uint64_t Uint64
Definition SDL_stdinc.h:242
uint32_t Uint32
Definition SDL_stdinc.h:224