Allow tools to work properly on big endian systems

This commit is contained in:
A. Wilcox 2020-05-06 19:37:48 -05:00 committed by Christine Dodrill
parent c69c75bf9b
commit 504b225b62
4 changed files with 15 additions and 1 deletions

View File

@ -19,10 +19,17 @@ typedef unsigned int u32;
typedef unsigned long long u64; typedef unsigned long long u64;
typedef float f32; typedef float f32;
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define bswap16(x) x
#define bswap32(x) x
#define BSWAP16(x) x = x
#define BSWAP32(x) x = x
#else
#define bswap16(x) __builtin_bswap16(x) #define bswap16(x) __builtin_bswap16(x)
#define bswap32(x) __builtin_bswap32(x) #define bswap32(x) __builtin_bswap32(x)
#define BSWAP16(x) x = __builtin_bswap16(x) #define BSWAP16(x) x = __builtin_bswap16(x)
#define BSWAP32(x) x = __builtin_bswap32(x) #define BSWAP32(x) x = __builtin_bswap32(x)
#endif
#define BSWAP16_MANY(x, n) for (s32 _i = 0; _i < n; _i++) BSWAP16((x)[_i]) #define BSWAP16_MANY(x, n) for (s32 _i = 0; _i < n; _i++) BSWAP16((x)[_i])
#define NORETURN __attribute__((noreturn)) #define NORETURN __attribute__((noreturn))

View File

@ -15,8 +15,13 @@ typedef int s32;
typedef unsigned char u8; typedef unsigned char u8;
typedef unsigned int u32; typedef unsigned int u32;
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define BSWAP16(x)
#define BSWAP32(x)
#else
#define BSWAP16(x) x = __builtin_bswap16(x) #define BSWAP16(x) x = __builtin_bswap16(x)
#define BSWAP32(x) x = __builtin_bswap32(x) #define BSWAP32(x) x = __builtin_bswap32(x)
#endif
#define NORETURN __attribute__((noreturn)) #define NORETURN __attribute__((noreturn))
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))

View File

@ -14,7 +14,7 @@ typedef unsigned long long u64;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
#ifdef __sgi #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define BSWAP16(x) # define BSWAP16(x)
# define BSWAP32(x) # define BSWAP32(x)
# define BSWAP16_MANY(x, n) # define BSWAP16_MANY(x, n)

View File

@ -82,6 +82,7 @@ void swap_bytes(unsigned char *data, long length)
void reverse_endian(unsigned char *data, long length) void reverse_endian(unsigned char *data, long length)
{ {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
long i; long i;
unsigned char tmp; unsigned char tmp;
for (i = 0; i < length; i += 4) { for (i = 0; i < length; i += 4) {
@ -92,6 +93,7 @@ void reverse_endian(unsigned char *data, long length)
data[i+1] = data[i+2]; data[i+1] = data[i+2];
data[i+2] = tmp; data[i+2] = tmp;
} }
#endif
} }
long filesize(const char *filename) long filesize(const char *filename)