fix: allow builtins for all archs (#4756) * fix: allow builtins for all archs * fix: typo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
diff --git a/include/SDL_endian.h b/include/SDL_endian.h
index 9f88cb1..db72928 100644
--- a/include/SDL_endian.h
+++ b/include/SDL_endian.h
@@ -91,25 +91,36 @@ extern "C" {
/**
* \file SDL_endian.h
*/
-#if (_SDL_HAS_BUILTIN(__builtin_bswap16)) || \
+
+/* various modern compilers may have builtin swap */
+#define HAS_BUILTIN_BSWAP16 (_SDL_HAS_BUILTIN(__builtin_bswap16)) || \
(defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
+#define HAS_BUILTIN_BSWAP32 (_SDL_HAS_BUILTIN(__builtin_bswap32)) || \
+ (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
+#define HAS_BUILTIN_BSWAP64 (_SDL_HAS_BUILTIN(__builtin_bswap64)) || \
+ (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
+
+/* this one is broken */
+#define HAS_BROKEN_BSWAP (defined(__GNUC__) && \
+ (__GNUC__ == 2 && __GNUC_MINOR__ <= 95))
+
+#if HAS_BUILTIN_BSWAP16
#define SDL_Swap16(x) __builtin_bswap16(x)
-#elif defined(__GNUC__) && defined(__i386__) && \
- !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
+#elif defined(__i386__) && !HAS_BROKEN_BSWAP
SDL_FORCE_INLINE Uint16
SDL_Swap16(Uint16 x)
{
__asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
return x;
}
-#elif defined(__GNUC__) && defined(__x86_64__)
+#elif defined(__x86_64__)
SDL_FORCE_INLINE Uint16
SDL_Swap16(Uint16 x)
{
__asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
return x;
}
-#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
+#elif (defined(__powerpc__) || defined(__ppc__))
SDL_FORCE_INLINE Uint16
SDL_Swap16(Uint16 x)
{
@@ -118,14 +129,14 @@ SDL_Swap16(Uint16 x)
__asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
return (Uint16)result;
}
-#elif defined(__GNUC__) && defined(__aarch64__)
+#elif defined(__aarch64__)
SDL_FORCE_INLINE Uint16
SDL_Swap16(Uint16 x)
{
__asm__("rev16 %w1, %w0" : "=r"(x) : "r"(x));
return x;
}
-#elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
+#elif (defined(__m68k__) && !defined(__mcoldfire__))
SDL_FORCE_INLINE Uint16
SDL_Swap16(Uint16 x)
{
@@ -149,25 +160,23 @@ SDL_Swap16(Uint16 x)
}
#endif
-#if (_SDL_HAS_BUILTIN(__builtin_bswap32)) || \
- (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
+#if HAS_BUILTIN_BSWAP32
#define SDL_Swap32(x) __builtin_bswap32(x)
-#elif defined(__GNUC__) && defined(__i386__) && \
- !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
+#elif defined(__i386__) && !HAS_BROKEN_BSWAP
SDL_FORCE_INLINE Uint32
SDL_Swap32(Uint32 x)
{
__asm__("bswap %0": "=r"(x):"0"(x));
return x;
}
-#elif defined(__GNUC__) && defined(__x86_64__)
+#elif defined(__x86_64__)
SDL_FORCE_INLINE Uint32
SDL_Swap32(Uint32 x)
{
__asm__("bswapl %0": "=r"(x):"0"(x));
return x;
}
-#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
+#elif (defined(__powerpc__) || defined(__ppc__))
SDL_FORCE_INLINE Uint32
SDL_Swap32(Uint32 x)
{
@@ -178,14 +187,14 @@ SDL_Swap32(Uint32 x)
__asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
return result;
}
-#elif defined(__GNUC__) && defined(__aarch64__)
+#elif defined(__aarch64__)
SDL_FORCE_INLINE Uint32
SDL_Swap32(Uint32 x)
{
__asm__("rev %w1, %w0": "=r"(x):"r"(x));
return x;
}
-#elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
+#elif (defined(__m68k__) && !defined(__mcoldfire__))
SDL_FORCE_INLINE Uint32
SDL_Swap32(Uint32 x)
{
@@ -210,11 +219,9 @@ SDL_Swap32(Uint32 x)
}
#endif
-#if (_SDL_HAS_BUILTIN(__builtin_bswap64)) || \
- (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
+#if HAS_BUILTIN_BSWAP64
#define SDL_Swap64(x) __builtin_bswap64(x)
-#elif defined(__GNUC__) && defined(__i386__) && \
- !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
+#elif defined(__i386__) && !HAS_BROKEN_BSWAP
SDL_FORCE_INLINE Uint64
SDL_Swap64(Uint64 x)
{
@@ -230,7 +237,7 @@ SDL_Swap64(Uint64 x)
: "0" (v.s.a), "1"(v.s.b));
return v.u;
}
-#elif defined(__GNUC__) && defined(__x86_64__)
+#elif defined(__x86_64__)
SDL_FORCE_INLINE Uint64
SDL_Swap64(Uint64 x)
{
@@ -278,6 +285,11 @@ SDL_SwapFloat(float x)
return swapper.f;
}
+/* remove extra macros */
+#undef HAS_BROKEN_BSWAP
+#undef HAS_BUILTIN_BSWAP16
+#undef HAS_BUILTIN_BSWAP32
+#undef HAS_BUILTIN_BSWAP64
/**
* \name Swap to native