diff --git a/src/engine/movie.cc b/src/engine/movie.cc index c042f36..0766a36 100644 --- a/src/engine/movie.cc +++ b/src/engine/movie.cc @@ -12,6 +12,7 @@ #include #include +#include #include @@ -615,7 +616,7 @@ struct aviwriter break; case AUDIO_U16MSB: for(ushort *dst = (ushort *)data, *end = (ushort *)&data[framesize]; dst < end; dst++) -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#ifdef SAUERLIB_BIG_ENDIAN *dst = endianswap(*dst) ^ 0x0080; #else *dst = endianswap(*dst) ^ 0x8000; diff --git a/src/engine/octaedit.cc b/src/engine/octaedit.cc index 0a0f59f..1bd99d1 100644 --- a/src/engine/octaedit.cc +++ b/src/engine/octaedit.cc @@ -5,6 +5,8 @@ #include +#include + #include #include #include diff --git a/src/engine/rendermodel.cc b/src/engine/rendermodel.cc index 15bc77f..b65780d 100644 --- a/src/engine/rendermodel.cc +++ b/src/engine/rendermodel.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include diff --git a/src/engine/texture.cc b/src/engine/texture.cc index 6ba0823..3caee97 100644 --- a/src/engine/texture.cc +++ b/src/engine/texture.cc @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -1255,7 +1256,7 @@ static Texture *newtexture(Texture *t, const char *rname, ImageData &s, int clam return t; } -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#ifdef SAUERLIB_BIG_ENDIAN #define RGBAMASKS 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff #define RGBMASKS 0xff0000, 0x00ff00, 0x0000ff, 0 #else diff --git a/src/engine/worldio.cc b/src/engine/worldio.cc index f0acd5b..faafa99 100644 --- a/src/engine/worldio.cc +++ b/src/engine/worldio.cc @@ -6,6 +6,7 @@ #include +#include #include #include diff --git a/src/sauerlib/endian.hh b/src/sauerlib/endian.hh new file mode 100644 index 0000000..ada9afc --- /dev/null +++ b/src/sauerlib/endian.hh @@ -0,0 +1,86 @@ +#ifndef SAUERLIB_ENDIAN_HH +#define SAUERLIB_ENDIAN_HH + +#include + +/* we only support modern toolchains, this should be enough */ +#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || \ + defined(__BIG_ENDIAN__) +#define SAUERLIB_BIG_ENDIAN 1 +#else +#define SAUERLIB_LITTLE_ENDIAN 1 +#endif + +#if defined(__GNUC__) +inline uint16_t endianswap16(uint16_t n) { return __builtin_bswap16(n); } +inline uint32_t endianswap32(uint32_t n) { return __builtin_bswap32(n); } +inline uint32_t endianswap64(uint64_t n) { return __builtin_bswap64(n); } +#elif defined(_MSC_VER) +inline uint16_t endianswap16(uint16_t n) { return _byteswap_ushort(n); } +inline uint32_t endianswap32(uint32_t n) { return _byteswap_ulong(n); } +inline uint32_t endianswap64(uint64_t n) { return _byteswap_uint64(n); } +#else +inline uint16_t endianswap16(uint16_t n) { return (n<<8) | (n>>8); } +inline uint32_t endianswap32(uint32_t n) { + return (n << 24) | (n >> 24) | ((n >> 8) & 0xFF00) | ((n << 8) & 0xFF0000); +} +inline uint64_t endianswap64(uint64_t n) { + return endianswap32(uint32_t(n >> 32)) | ( + uint64_t(endianswap32(uint32_t(n))) << 32 + ); +} +#endif + +template inline T endianswap(T n) { + union { T t; uint32_t i; } conv; + conv.t = n; + conv.i = endianswap32(conv.i); + return conv.t; +} +template<> inline uint16_t endianswap(uint16_t n) { + return endianswap16(n); +} +template<> inline int16_t endianswap(int16_t n) { + return endianswap16(n); +} +template<> inline uint32_t endianswap(uint32_t n) { + return endianswap32(n); +} +template<> inline int32_t endianswap(int32_t n) { + return endianswap32(n); +} +template<> inline uint64_t endianswap(uint64_t n) { + return endianswap64(n); +} +template<> inline int64_t endianswap(int64_t n) { + return endianswap64(n); +} +template<> inline double endianswap(double n) { + union { double t; uint i; } conv; + conv.t = n; + conv.i = endianswap64(conv.i); + return conv.t; +} +template inline void endianswap(T *buf, size_t len) { + for (T *end = &buf[len]; buf < end; buf++) { + *buf = endianswap(*buf); + } +} + +#ifdef SAUERLIB_BIG_ENDIAN +template inline T lilswap(T n) { return endianswap(n); } +template inline T bigswap(T n) { return n; } +template inline void lilswap(T *buf, size_t len) { + endianswap(buf, len); +} +template inline void bigswap(T *buf, size_t len) {} +#else +template inline T lilswap(T n) { return n; } +template inline T bigswap(T n) { return endianswap(n); } +template inline void lilswap(T *buf, size_t len) {} +template inline void bigswap(T *buf, size_t len) { + endianswap(buf, len); +} +#endif + +#endif diff --git a/src/sauerlib/stream.hh b/src/sauerlib/stream.hh index f816b22..6b38bc9 100644 --- a/src/sauerlib/stream.hh +++ b/src/sauerlib/stream.hh @@ -2,6 +2,7 @@ #define SAUERLIB_STREAM_HH #include "tools.hh" +#include "endian.hh" /* workaround for some C platforms that have these two functions as macros - not used anywhere */ #ifdef getchar diff --git a/src/sauerlib/tools.cc b/src/sauerlib/tools.cc index b51092a..125577f 100644 --- a/src/sauerlib/tools.cc +++ b/src/sauerlib/tools.cc @@ -3,6 +3,7 @@ #include "tools.hh" #include "encoding.hh" +#include "endian.hh" #include diff --git a/src/sauerlib/tools.hh b/src/sauerlib/tools.hh index e8ca511..c550ee9 100644 --- a/src/sauerlib/tools.hh +++ b/src/sauerlib/tools.hh @@ -1046,50 +1046,6 @@ template struct reversequeue : queue const T &operator[](int offset) const { return queue::added(offset); } }; -static inline bool islittleendian() { union { int i; uchar b[sizeof(int)]; } conv; conv.i = 1; return conv.b[0] != 0; } -#ifdef SDL_BYTEORDER -#define endianswap16 SDL_Swap16 -#define endianswap32 SDL_Swap32 -#define endianswap64 SDL_Swap64 -#else -inline ushort endianswap16(ushort n) { return (n<<8) | (n>>8); } -inline uint endianswap32(uint n) { return (n<<24) | (n>>24) | ((n>>8)&0xFF00) | ((n<<8)&0xFF0000); } -inline ullong endianswap64(ullong n) { return endianswap32(uint(n >> 32)) | ((ullong)endianswap32(uint(n)) << 32); } -#endif -template inline T endianswap(T n) { union { T t; uint i; } conv; conv.t = n; conv.i = endianswap32(conv.i); return conv.t; } -template<> inline ushort endianswap(ushort n) { return endianswap16(n); } -template<> inline short endianswap(short n) { return endianswap16(n); } -template<> inline uint endianswap(uint n) { return endianswap32(n); } -template<> inline int endianswap(int n) { return endianswap32(n); } -template<> inline ullong endianswap(ullong n) { return endianswap64(n); } -template<> inline llong endianswap(llong n) { return endianswap64(n); } -template<> inline double endianswap(double n) { union { double t; uint i; } conv; conv.t = n; conv.i = endianswap64(conv.i); return conv.t; } -template inline void endianswap(T *buf, size_t len) { for(T *end = &buf[len]; buf < end; buf++) *buf = endianswap(*buf); } -template inline T endiansame(T n) { return n; } -template inline void endiansame(T *buf, size_t len) {} -#ifdef SDL_BYTEORDER -#if SDL_BYTEORDER == SDL_LIL_ENDIAN -#define lilswap endiansame -#define bigswap endianswap -#else -#define lilswap endianswap -#define bigswap endiansame -#endif -#elif defined(__BYTE_ORDER__) -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -#define lilswap endiansame -#define bigswap endianswap -#else -#define lilswap endianswap -#define bigswap endiansame -#endif -#else -template inline T lilswap(T n) { return islittleendian() ? n : endianswap(n); } -template inline void lilswap(T *buf, size_t len) { if(!islittleendian()) endianswap(buf, len); } -template inline T bigswap(T n) { return islittleendian() ? endianswap(n) : n; } -template inline void bigswap(T *buf, size_t len) { if(islittleendian()) endianswap(buf, len); } -#endif - extern string homedir; extern char *makerelpath(const char *dir, const char *file, const char *prefix = nullptr, const char *cmd = nullptr); diff --git a/src/shared/zip.cc b/src/shared/zip.cc index aff461a..73bcf24 100644 --- a/src/shared/zip.cc +++ b/src/shared/zip.cc @@ -4,6 +4,8 @@ #include +#include + #include "command.hh" #include /* conoutf */