Path: blob/master/thirdparty/sdl/include/SDL3/SDL_endian.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryEndian23*24* Functions converting endian-specific values to different byte orders.25*26* These functions either unconditionally swap byte order (SDL_Swap16,27* SDL_Swap32, SDL_Swap64, SDL_SwapFloat), or they swap to/from the system's28* native byte order (SDL_Swap16LE, SDL_Swap16BE, SDL_Swap32LE, SDL_Swap32BE,29* SDL_Swap32LE, SDL_Swap32BE, SDL_SwapFloatLE, SDL_SwapFloatBE). In the30* latter case, the functionality is provided by macros that become no-ops if31* a swap isn't necessary: on an x86 (littleendian) processor, SDL_Swap32LE32* does nothing, but SDL_Swap32BE reverses the bytes of the data. On a PowerPC33* processor (bigendian), the macros behavior is reversed.34*35* The swap routines are inline functions, and attempt to use compiler36* intrinsics, inline assembly, and other magic to make byteswapping37* efficient.38*/3940#ifndef SDL_endian_h_41#define SDL_endian_h_4243#include <SDL3/SDL_stdinc.h>4445#if defined(_MSC_VER) && (_MSC_VER >= 1400)46/* As of Clang 11, '_m_prefetchw' is conflicting with the winnt.h's version,47so we define the needed '_m_prefetch' here as a pseudo-header, until the issue is fixed. */48#ifdef __clang__49#ifndef __PRFCHWINTRIN_H50#define __PRFCHWINTRIN_H51static __inline__ void __attribute__((__always_inline__, __nodebug__))52_m_prefetch(void *__P)53{54__builtin_prefetch(__P, 0, 3 /* _MM_HINT_T0 */);55}56#endif /* __PRFCHWINTRIN_H */57#endif /* __clang__ */5859#include <intrin.h>60#endif6162/**63* \name The two types of endianness64*/65/* @{ */666768/**69* A value to represent littleendian byteorder.70*71* This is used with the preprocessor macro SDL_BYTEORDER, to determine a72* platform's byte ordering:73*74* ```c75* #if SDL_BYTEORDER == SDL_LIL_ENDIAN76* SDL_Log("This system is littleendian.");77* #endif78* ```79*80* \since This macro is available since SDL 3.2.0.81*82* \sa SDL_BYTEORDER83* \sa SDL_BIG_ENDIAN84*/85#define SDL_LIL_ENDIAN 12348687/**88* A value to represent bigendian byteorder.89*90* This is used with the preprocessor macro SDL_BYTEORDER, to determine a91* platform's byte ordering:92*93* ```c94* #if SDL_BYTEORDER == SDL_BIG_ENDIAN95* SDL_Log("This system is bigendian.");96* #endif97* ```98*99* \since This macro is available since SDL 3.2.0.100*101* \sa SDL_BYTEORDER102* \sa SDL_LIL_ENDIAN103*/104#define SDL_BIG_ENDIAN 4321105106/* @} */107108#ifndef SDL_BYTEORDER109#ifdef SDL_WIKI_DOCUMENTATION_SECTION110111/**112* A macro that reports the target system's byte order.113*114* This is set to either SDL_LIL_ENDIAN or SDL_BIG_ENDIAN (and maybe other115* values in the future, if something else becomes popular). This can be116* tested with the preprocessor, so decisions can be made at compile time.117*118* ```c119* #if SDL_BYTEORDER == SDL_BIG_ENDIAN120* SDL_Log("This system is bigendian.");121* #endif122* ```123*124* \since This macro is available since SDL 3.2.0.125*126* \sa SDL_LIL_ENDIAN127* \sa SDL_BIG_ENDIAN128*/129#define SDL_BYTEORDER SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN130#elif defined(SDL_PLATFORM_LINUX)131#include <endian.h>132#define SDL_BYTEORDER __BYTE_ORDER133#elif defined(SDL_PLATFORM_SOLARIS)134#include <sys/byteorder.h>135#if defined(_LITTLE_ENDIAN)136#define SDL_BYTEORDER SDL_LIL_ENDIAN137#elif defined(_BIG_ENDIAN)138#define SDL_BYTEORDER SDL_BIG_ENDIAN139#else140#error Unsupported endianness141#endif142#elif defined(SDL_PLATFORM_OPENBSD) || defined(__DragonFly__)143#include <endian.h>144#define SDL_BYTEORDER BYTE_ORDER145#elif defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_NETBSD)146#include <sys/endian.h>147#define SDL_BYTEORDER BYTE_ORDER148/* predefs from newer gcc and clang versions: */149#elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__)150#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)151#define SDL_BYTEORDER SDL_LIL_ENDIAN152#elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)153#define SDL_BYTEORDER SDL_BIG_ENDIAN154#else155#error Unsupported endianness156#endif /**/157#else158#if defined(__hppa__) || \159defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \160(defined(__MIPS__) && defined(__MIPSEB__)) || \161defined(__ppc__) || defined(__POWERPC__) || defined(__powerpc__) || defined(__PPC__) || \162defined(__sparc__) || defined(__sparc)163#define SDL_BYTEORDER SDL_BIG_ENDIAN164#else165#define SDL_BYTEORDER SDL_LIL_ENDIAN166#endif167#endif /* SDL_PLATFORM_LINUX */168#endif /* !SDL_BYTEORDER */169170#ifndef SDL_FLOATWORDORDER171#ifdef SDL_WIKI_DOCUMENTATION_SECTION172173/**174* A macro that reports the target system's floating point word order.175*176* This is set to either SDL_LIL_ENDIAN or SDL_BIG_ENDIAN (and maybe other177* values in the future, if something else becomes popular). This can be178* tested with the preprocessor, so decisions can be made at compile time.179*180* ```c181* #if SDL_FLOATWORDORDER == SDL_BIG_ENDIAN182* SDL_Log("This system's floats are bigendian.");183* #endif184* ```185*186* \since This macro is available since SDL 3.2.0.187*188* \sa SDL_LIL_ENDIAN189* \sa SDL_BIG_ENDIAN190*/191#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN192/* predefs from newer gcc versions: */193#elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__FLOAT_WORD_ORDER__)194#if (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)195#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN196#elif (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)197#define SDL_FLOATWORDORDER SDL_BIG_ENDIAN198#else199#error Unsupported endianness200#endif /**/201#elif defined(__MAVERICK__)202/* For Maverick, float words are always little-endian. */203#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN204#elif (defined(__arm__) || defined(__thumb__)) && !defined(__VFP_FP__) && !defined(__ARM_EABI__)205/* For FPA, float words are always big-endian. */206#define SDL_FLOATWORDORDER SDL_BIG_ENDIAN207#else208/* By default, assume that floats words follow the memory system mode. */209#define SDL_FLOATWORDORDER SDL_BYTEORDER210#endif /* __FLOAT_WORD_ORDER__ */211#endif /* !SDL_FLOATWORDORDER */212213214#include <SDL3/SDL_begin_code.h>215/* Set up for C function definitions, even when using C++ */216#ifdef __cplusplus217extern "C" {218#endif219220/* various modern compilers may have builtin swap */221#if defined(__GNUC__) || defined(__clang__)222# define HAS_BUILTIN_BSWAP16 (SDL_HAS_BUILTIN(__builtin_bswap16)) || \223(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))224# define HAS_BUILTIN_BSWAP32 (SDL_HAS_BUILTIN(__builtin_bswap32)) || \225(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))226# define HAS_BUILTIN_BSWAP64 (SDL_HAS_BUILTIN(__builtin_bswap64)) || \227(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))228229/* this one is broken */230# define HAS_BROKEN_BSWAP (__GNUC__ == 2 && __GNUC_MINOR__ <= 95)231#else232# define HAS_BUILTIN_BSWAP16 0233# define HAS_BUILTIN_BSWAP32 0234# define HAS_BUILTIN_BSWAP64 0235# define HAS_BROKEN_BSWAP 0236#endif237238/* Byte swap 16-bit integer. */239#ifndef SDL_WIKI_DOCUMENTATION_SECTION240#if HAS_BUILTIN_BSWAP16241#define SDL_Swap16(x) __builtin_bswap16(x)242#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)243#pragma intrinsic(_byteswap_ushort)244#define SDL_Swap16(x) _byteswap_ushort(x)245#elif defined(__i386__) && !HAS_BROKEN_BSWAP246SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)247{248__asm__("xchgb %b0,%h0": "=q"(x):"0"(x));249return x;250}251#elif defined(__x86_64__)252SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)253{254__asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));255return x;256}257#elif (defined(__powerpc__) || defined(__ppc__))258SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)259{260int result;261262__asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));263return (Uint16)result;264}265#elif (defined(__m68k__) && !defined(__mcoldfire__))266SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)267{268__asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");269return x;270}271#elif defined(__WATCOMC__) && defined(__386__)272extern __inline Uint16 SDL_Swap16(Uint16);273#pragma aux SDL_Swap16 = \274"xchg al, ah" \275parm [ax] \276modify [ax];277#else278SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)279{280return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));281}282#endif283#endif284285/* Byte swap 32-bit integer. */286#ifndef SDL_WIKI_DOCUMENTATION_SECTION287#if HAS_BUILTIN_BSWAP32288#define SDL_Swap32(x) __builtin_bswap32(x)289#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)290#pragma intrinsic(_byteswap_ulong)291#define SDL_Swap32(x) _byteswap_ulong(x)292#elif defined(__i386__) && !HAS_BROKEN_BSWAP293SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)294{295__asm__("bswap %0": "=r"(x):"0"(x));296return x;297}298#elif defined(__x86_64__)299SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)300{301__asm__("bswapl %0": "=r"(x):"0"(x));302return x;303}304#elif (defined(__powerpc__) || defined(__ppc__))305SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)306{307Uint32 result;308309__asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));310__asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));311__asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));312return result;313}314#elif (defined(__m68k__) && !defined(__mcoldfire__))315SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)316{317__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");318return x;319}320#elif defined(__WATCOMC__) && defined(__386__)321extern __inline Uint32 SDL_Swap32(Uint32);322#pragma aux SDL_Swap32 = \323"bswap eax" \324parm [eax] \325modify [eax];326#else327SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)328{329return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |330((x >> 8) & 0x0000FF00) | (x >> 24)));331}332#endif333#endif334335/* Byte swap 64-bit integer. */336#ifndef SDL_WIKI_DOCUMENTATION_SECTION337#if HAS_BUILTIN_BSWAP64338#define SDL_Swap64(x) __builtin_bswap64(x)339#elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)340#pragma intrinsic(_byteswap_uint64)341#define SDL_Swap64(x) _byteswap_uint64(x)342#elif defined(__i386__) && !HAS_BROKEN_BSWAP343SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)344{345union {346struct {347Uint32 a, b;348} s;349Uint64 u;350} v;351v.u = x;352__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"353: "=r"(v.s.a), "=r"(v.s.b)354: "0" (v.s.a), "1"(v.s.b));355return v.u;356}357#elif defined(__x86_64__)358SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)359{360__asm__("bswapq %0": "=r"(x):"0"(x));361return x;362}363#elif defined(__WATCOMC__) && defined(__386__)364extern __inline Uint64 SDL_Swap64(Uint64);365#pragma aux SDL_Swap64 = \366"bswap eax" \367"bswap edx" \368"xchg eax,edx" \369parm [eax edx] \370modify [eax edx];371#else372SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)373{374Uint32 hi, lo;375376/* Separate into high and low 32-bit values and swap them */377lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);378x >>= 32;379hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);380x = SDL_Swap32(lo);381x <<= 32;382x |= SDL_Swap32(hi);383return (x);384}385#endif386#endif387388/**389* Byte-swap a floating point number.390*391* This will always byte-swap the value, whether it's currently in the native392* byteorder of the system or not. You should use SDL_SwapFloatLE or393* SDL_SwapFloatBE instead, in most cases.394*395* Note that this is a forced-inline function in a header, and not a public396* API function available in the SDL library (which is to say, the code is397* embedded in the calling program and the linker and dynamic loader will not398* be able to find this function inside SDL itself).399*400* \param x the value to byte-swap.401* \returns x, with its bytes in the opposite endian order.402*403* \threadsafety It is safe to call this function from any thread.404*405* \since This function is available since SDL 3.2.0.406*/407SDL_FORCE_INLINE float SDL_SwapFloat(float x)408{409union {410float f;411Uint32 ui32;412} swapper;413swapper.f = x;414swapper.ui32 = SDL_Swap32(swapper.ui32);415return swapper.f;416}417418/* remove extra macros */419#undef HAS_BROKEN_BSWAP420#undef HAS_BUILTIN_BSWAP16421#undef HAS_BUILTIN_BSWAP32422#undef HAS_BUILTIN_BSWAP64423424425#ifdef SDL_WIKI_DOCUMENTATION_SECTION426427/**428* Byte-swap an unsigned 16-bit number.429*430* This will always byte-swap the value, whether it's currently in the native431* byteorder of the system or not. You should use SDL_Swap16LE or SDL_Swap16BE432* instead, in most cases.433*434* Note that this is a forced-inline function in a header, and not a public435* API function available in the SDL library (which is to say, the code is436* embedded in the calling program and the linker and dynamic loader will not437* be able to find this function inside SDL itself).438*439* \param x the value to byte-swap.440* \returns `x`, with its bytes in the opposite endian order.441*442* \threadsafety It is safe to call this function from any thread.443*444* \since This function is available since SDL 3.2.0.445*/446SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x) { return x_but_byteswapped; }447448/**449* Byte-swap an unsigned 32-bit number.450*451* This will always byte-swap the value, whether it's currently in the native452* byteorder of the system or not. You should use SDL_Swap32LE or SDL_Swap32BE453* instead, in most cases.454*455* Note that this is a forced-inline function in a header, and not a public456* API function available in the SDL library (which is to say, the code is457* embedded in the calling program and the linker and dynamic loader will not458* be able to find this function inside SDL itself).459*460* \param x the value to byte-swap.461* \returns `x`, with its bytes in the opposite endian order.462*463* \threadsafety It is safe to call this function from any thread.464*465* \since This function is available since SDL 3.2.0.466*/467SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x) { return x_but_byteswapped; }468469/**470* Byte-swap an unsigned 64-bit number.471*472* This will always byte-swap the value, whether it's currently in the native473* byteorder of the system or not. You should use SDL_Swap64LE or SDL_Swap64BE474* instead, in most cases.475*476* Note that this is a forced-inline function in a header, and not a public477* API function available in the SDL library (which is to say, the code is478* embedded in the calling program and the linker and dynamic loader will not479* be able to find this function inside SDL itself).480*481* \param x the value to byte-swap.482* \returns `x`, with its bytes in the opposite endian order.483*484* \threadsafety It is safe to call this function from any thread.485*486* \since This function is available since SDL 3.2.0.487*/488SDL_FORCE_INLINE Uint32 SDL_Swap64(Uint64 x) { return x_but_byteswapped; }489490/**491* Swap a 16-bit value from littleendian to native byte order.492*493* If this is running on a littleendian system, `x` is returned unchanged.494*495* This macro never references `x` more than once, avoiding side effects.496*497* \param x the value to swap, in littleendian byte order.498* \returns `x` in native byte order.499*500* \threadsafety It is safe to call this macro from any thread.501*502* \since This macro is available since SDL 3.2.0.503*/504#define SDL_Swap16LE(x) SwapOnlyIfNecessary(x)505506/**507* Swap a 32-bit value from littleendian to native byte order.508*509* If this is running on a littleendian system, `x` is returned unchanged.510*511* This macro never references `x` more than once, avoiding side effects.512*513* \param x the value to swap, in littleendian byte order.514* \returns `x` in native byte order.515*516* \threadsafety It is safe to call this macro from any thread.517*518* \since This macro is available since SDL 3.2.0.519*/520#define SDL_Swap32LE(x) SwapOnlyIfNecessary(x)521522/**523* Swap a 64-bit value from littleendian to native byte order.524*525* If this is running on a littleendian system, `x` is returned unchanged.526*527* This macro never references `x` more than once, avoiding side effects.528*529* \param x the value to swap, in littleendian byte order.530* \returns `x` in native byte order.531*532* \threadsafety It is safe to call this macro from any thread.533*534* \since This macro is available since SDL 3.2.0.535*/536#define SDL_Swap64LE(x) SwapOnlyIfNecessary(x)537538/**539* Swap a floating point value from littleendian to native byte order.540*541* If this is running on a littleendian system, `x` is returned unchanged.542*543* This macro never references `x` more than once, avoiding side effects.544*545* \param x the value to swap, in littleendian byte order.546* \returns `x` in native byte order.547*548* \threadsafety It is safe to call this macro from any thread.549*550* \since This macro is available since SDL 3.2.0.551*/552#define SDL_SwapFloatLE(x) SwapOnlyIfNecessary(x)553554/**555* Swap a 16-bit value from bigendian to native byte order.556*557* If this is running on a bigendian system, `x` is returned unchanged.558*559* This macro never references `x` more than once, avoiding side effects.560*561* \param x the value to swap, in bigendian byte order.562* \returns `x` in native byte order.563*564* \threadsafety It is safe to call this macro from any thread.565*566* \since This macro is available since SDL 3.2.0.567*/568#define SDL_Swap16BE(x) SwapOnlyIfNecessary(x)569570/**571* Swap a 32-bit value from bigendian to native byte order.572*573* If this is running on a bigendian system, `x` is returned unchanged.574*575* This macro never references `x` more than once, avoiding side effects.576*577* \param x the value to swap, in bigendian byte order.578* \returns `x` in native byte order.579*580* \threadsafety It is safe to call this macro from any thread.581*582* \since This macro is available since SDL 3.2.0.583*/584#define SDL_Swap32BE(x) SwapOnlyIfNecessary(x)585586/**587* Swap a 64-bit value from bigendian to native byte order.588*589* If this is running on a bigendian system, `x` is returned unchanged.590*591* This macro never references `x` more than once, avoiding side effects.592*593* \param x the value to swap, in bigendian byte order.594* \returns `x` in native byte order.595*596* \threadsafety It is safe to call this macro from any thread.597*598* \since This macro is available since SDL 3.2.0.599*/600#define SDL_Swap64BE(x) SwapOnlyIfNecessary(x)601602/**603* Swap a floating point value from bigendian to native byte order.604*605* If this is running on a bigendian system, `x` is returned unchanged.606*607* This macro never references `x` more than once, avoiding side effects.608*609* \param x the value to swap, in bigendian byte order.610* \returns `x` in native byte order.611*612* \threadsafety It is safe to call this macro from any thread.613*614* \since This macro is available since SDL 3.2.0.615*/616#define SDL_SwapFloatBE(x) SwapOnlyIfNecessary(x)617618#elif SDL_BYTEORDER == SDL_LIL_ENDIAN619#define SDL_Swap16LE(x) (x)620#define SDL_Swap32LE(x) (x)621#define SDL_Swap64LE(x) (x)622#define SDL_SwapFloatLE(x) (x)623#define SDL_Swap16BE(x) SDL_Swap16(x)624#define SDL_Swap32BE(x) SDL_Swap32(x)625#define SDL_Swap64BE(x) SDL_Swap64(x)626#define SDL_SwapFloatBE(x) SDL_SwapFloat(x)627#else628#define SDL_Swap16LE(x) SDL_Swap16(x)629#define SDL_Swap32LE(x) SDL_Swap32(x)630#define SDL_Swap64LE(x) SDL_Swap64(x)631#define SDL_SwapFloatLE(x) SDL_SwapFloat(x)632#define SDL_Swap16BE(x) (x)633#define SDL_Swap32BE(x) (x)634#define SDL_Swap64BE(x) (x)635#define SDL_SwapFloatBE(x) (x)636#endif637638/* Ends C function definitions when using C++ */639#ifdef __cplusplus640}641#endif642#include <SDL3/SDL_close_code.h>643644#endif /* SDL_endian_h_ */645646647