[Mesa-dev] [PATCH] mesa: Fix test for big-endian architecture in compiler.h
Jonathan Gray
jsg at jsg.id.au
Tue Feb 16 09:13:45 UTC 2016
On Tue, Feb 16, 2016 at 10:37:47AM +0200, Oded Gabbay wrote:
> On Tue, Feb 16, 2016 at 9:23 AM, Jonathan Gray <jsg at jsg.id.au> wrote:
> > On Fri, Feb 12, 2016 at 10:01:21AM +0100, Jochen Rollwagen wrote:
> >> Hi,
> >>
> >> i think i found & fixed a bug in mesa concerning tests for big-endian
> >> machines. The defines tested don't exist or are wrongly defined so the test
> >> (probably) never fires. The gcc defines on my machine concerning big-endian
> >> are
> >>
> >> jochen at mac-mini:~/sources/mesa$ gcc -dM -E - < /dev/null | grep BIG
> >> #define __BIGGEST_ALIGNMENT__ 16
> >> #define __BIG_ENDIAN__ 1
> >> #define __FLOAT_WORD_ORDER__ __ORDER_BIG_ENDIAN__
> >> #define _BIG_ENDIAN 1
> >> #define __ORDER_BIG_ENDIAN__ 4321
> >> #define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
> >>
> >> The tested values in current mesa are quite different :-)
> >>
> >> The following patch fixes this.
> >
> > I think you have this backwards.
> >
> > On OpenBSD/sparc64
> > $ gcc -dM -E - < /dev/null | grep BIG
> > $
> > $ sysctl hw.byteorder
> > hw.byteorder=4321
> >
> > endian.h defines BYTE_ORDER and it should be included to test it.
> >
> > I was under the impression the headers on linux had similiar defines.
> >
> > Look at how src/gallium/include/pipe/p_config.h does it.
> > _______________________________________________
> > mesa-dev mailing list
> > mesa-dev at lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>
> After looking at it last night, I also think a better solution will be
> to just include <endian.h> in compiler.h file
>
> Oded
Right, I suspect it will end up looking something like the following
untested diff.
It would be nice if this didn't have to be duplicated in two places
in Mesa though.
diff --git a/src/mesa/main/compiler.h b/src/mesa/main/compiler.h
index c5ee741..10babd1 100644
--- a/src/mesa/main/compiler.h
+++ b/src/mesa/main/compiler.h
@@ -52,28 +52,60 @@ extern "C" {
* Try to use a runtime test instead.
* For now, only used by some DRI hardware drivers for color/texel packing.
*/
-#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
-#if defined(__linux__)
+
+#ifdef __OpenBSD__
+#include <endian.h>
+#define CPU_TO_LE32( x ) htole32( x )
+#define LE32_TO_CPU( x ) letoh32( x )
+#if BYTE_ORDER == BIG_ENDIAN
+#define MESA_BIG_ENDIAN 1
+#else
+#define MESA_LITTLE_ENDIAN 1
+#endif
+#endif /* __OpenBSD__ */
+
+#ifdef __GLIBC__
+#include <endian.h>
#include <byteswap.h>
+#if __BYTE_ORDER == __BIG_ENDIAN
#define CPU_TO_LE32( x ) bswap_32( x )
-#elif defined(__APPLE__)
-#include <CoreFoundation/CFByteOrder.h>
-#define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
-#elif defined(__OpenBSD__)
-#include <sys/types.h>
-#define CPU_TO_LE32( x ) htole32( x )
-#else /*__linux__ */
-#include <sys/endian.h>
-#define CPU_TO_LE32( x ) bswap32( x )
-#endif /*__linux__*/
+#define LE32_TO_CPU( x ) bswap_32( x )
#define MESA_BIG_ENDIAN 1
#else
#define CPU_TO_LE32( x ) ( x )
+#define LE32_TO_CPU( x ) ( x )
#define MESA_LITTLE_ENDIAN 1
#endif
-#define LE32_TO_CPU( x ) CPU_TO_LE32( x )
+#endif /* __GLIBC__ */
+#ifdef __APPLE__
+#include <machine/endian.h>
+#define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
+#define LE32_TO_CPU( x ) CFSwapInt32LittleToHost( x )
+#if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
+#define MESA_BIG_ENDIAN 1
+#else
+#define MESA_LITTLE_ENDIAN 1
+#endif
+#endif /* __APPLE__ */
+
+#ifdef __sun
+#include <sys/isa_defs.h>
+#include <sys/byteorder.h>
+#define CPU_TO_LE32( x ) LE_32( x )
+#define LE32_TO_CPU( x ) LE_32( x )
+#if defined(_BIG_ENDIAN)
+#define MESA_BIG_ENDIAN 1
+#else
+#define MESA_LITTLE_ENDIAN 1
+#endif
+#endif /* __sun */
+#if !defined(MESA_BIG_ENDIAN) && !defined(MESA_LITTLE_ENDIAN)
+#define CPU_TO_LE32( x ) ( x )
+#define LE32_TO_CPU( x ) ( x )
+#define MESA_LITTLE_ENDIAN 1
+#endif
#define IEEE_ONE 0x3f800000
More information about the mesa-dev
mailing list