Skip to content

Commit

Permalink
Add SDL_FLOATWORDORDER for older ARM toolchains
Browse files Browse the repository at this point in the history
  • Loading branch information
ccawley2011 authored and slouken committed May 4, 2022
1 parent adac3bd commit b398a84
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
22 changes: 22 additions & 0 deletions include/SDL_endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ _m_prefetch(void *__P)
#endif /* __linux__ */
#endif /* !SDL_BYTEORDER */

#ifndef SDL_FLOATWORDORDER /* Not defined in SDL_config.h? */
/* predefs from newer gcc versions: */
#if defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__FLOAT_WORD_ORDER__)
#if (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
#elif (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
#define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
#else
#error Unsupported endianness
#endif /**/
#elif defined(__MAVERICK__)
/* For Maverick, float words are always little-endian. */
#define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
#elif (defined(__arm__) || defined(__thumb__)) && !defined(__VFP_FP__) && !defined(__ARM_EABI__)
/* For FPA, float words are always big-endian. */
#define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
#else
/* By default, assume that floats words follow the memory system mode. */
#define SDL_FLOATWORDORDER SDL_BYTEORDER
#endif /* __FLOAT_WORD_ORDER__ */
#endif /* !SDL_FLOATWORDORDER */


#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
Expand Down
3 changes: 2 additions & 1 deletion src/libm/math_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ typedef unsigned int u_int32_t;
* Math on arm is special:
* For FPA, float words are always big-endian.
* For VFP, floats words follow the memory system mode.
* For Maverick, float words are always little-endian.
*/

#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
#if (SDL_FLOATWORDORDER == SDL_BIG_ENDIAN)

typedef union
{
Expand Down
19 changes: 19 additions & 0 deletions test/testautomation_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ int platform_testTypes(void *arg)
int platform_testEndianessAndSwap(void *arg)
{
int real_byteorder;
int real_floatwordorder = 0;
Uint16 value = 0x1234;
Uint16 value16 = 0xCDAB;
Uint16 swapped16 = 0xABCD;
Uint32 value32 = 0xEFBEADDE;
Uint32 swapped32 = 0xDEADBEEF;

union {
double d;
Uint32 ui32[2];
} value_double;

Uint64 value64, swapped64;
value64 = 0xEFBEADDE;
value64 <<= 32;
value64 |= 0xCDAB3412;
swapped64 = 0x1234ABCD;
swapped64 <<= 32;
swapped64 |= 0xDEADBEEF;
value_double.d = 3.141593;

if ((*((char *) &value) >> 4) == 0x1) {
real_byteorder = SDL_BIG_ENDIAN;
Expand All @@ -80,6 +87,18 @@ int platform_testEndianessAndSwap(void *arg)
(SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
(real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big" );

if (value_double.ui32[0] == 0x82c2bd7f && value_double.ui32[1] == 0x400921fb) {
real_floatwordorder = SDL_LIL_ENDIAN;
} else if (value_double.ui32[0] == 0x400921fb && value_double.ui32[1] == 0x82c2bd7f) {
real_floatwordorder = SDL_BIG_ENDIAN;
}

/* Test endianness. */
SDLTest_AssertCheck( real_floatwordorder == SDL_FLOATWORDORDER,
"Machine detected as having %s endian float word order, appears to be %s endian.",
(SDL_FLOATWORDORDER == SDL_LIL_ENDIAN) ? "little" : "big",
(real_floatwordorder == SDL_LIL_ENDIAN) ? "little" : (real_floatwordorder == SDL_BIG_ENDIAN) ? "big" : "unknown" );

/* Test 16 swap. */
SDLTest_AssertCheck( SDL_Swap16(value16) == swapped16,
"SDL_Swap16(): 16 bit swapped: 0x%X => 0x%X",
Expand Down
23 changes: 23 additions & 0 deletions test/testplatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,25 @@ TestEndian(SDL_bool verbose)
int error = 0;
Uint16 value = 0x1234;
int real_byteorder;
int real_floatwordorder = 0;
Uint16 value16 = 0xCDAB;
Uint16 swapped16 = 0xABCD;
Uint32 value32 = 0xEFBEADDE;
Uint32 swapped32 = 0xDEADBEEF;
Uint64 value64, swapped64;

union {
double d;
Uint32 ui32[2];
} value_double;

value64 = 0xEFBEADDE;
value64 <<= 32;
value64 |= 0xCDAB3412;
swapped64 = 0x1234ABCD;
swapped64 <<= 32;
swapped64 |= 0xDEADBEEF;
value_double.d = 3.141593;

if (verbose) {
SDL_Log("Detected a %s endian machine.\n",
Expand All @@ -115,6 +122,22 @@ TestEndian(SDL_bool verbose)
}
++error;
}
if (verbose) {
SDL_Log("Detected a %s endian float word order machine.\n",
(SDL_FLOATWORDORDER == SDL_LIL_ENDIAN) ? "little" : "big");
}
if (value_double.ui32[0] == 0x82c2bd7f && value_double.ui32[1] == 0x400921fb) {
real_floatwordorder = SDL_LIL_ENDIAN;
} else if (value_double.ui32[0] == 0x400921fb && value_double.ui32[1] == 0x82c2bd7f) {
real_floatwordorder = SDL_BIG_ENDIAN;
}
if (real_floatwordorder != SDL_FLOATWORDORDER) {
if (verbose) {
SDL_Log("Actually a %s endian float word order machine!\n",
(real_floatwordorder == SDL_LIL_ENDIAN) ? "little" : (real_floatwordorder == SDL_BIG_ENDIAN) ? "big" : "unknown" );
}
++error;
}
if (verbose) {
SDL_Log("Value 16 = 0x%X, swapped = 0x%X\n", value16,
SDL_Swap16(value16));
Expand Down

0 comments on commit b398a84

Please sign in to comment.