Browse Source

separate endian header in sauerlib

master
Daniel Kolesa 4 months ago
parent
commit
7f728f23d8
10 changed files with 98 additions and 46 deletions
  1. +2
    -1
      src/engine/movie.cc
  2. +2
    -0
      src/engine/octaedit.cc
  3. +1
    -0
      src/engine/rendermodel.cc
  4. +2
    -1
      src/engine/texture.cc
  5. +1
    -0
      src/engine/worldio.cc
  6. +86
    -0
      src/sauerlib/endian.hh
  7. +1
    -0
      src/sauerlib/stream.hh
  8. +1
    -0
      src/sauerlib/tools.cc
  9. +0
    -44
      src/sauerlib/tools.hh
  10. +2
    -0
      src/shared/zip.cc

+ 2
- 1
src/engine/movie.cc View File

@@ -12,6 +12,7 @@
#include <cassert>

#include <sauerlib/stream.hh>
#include <sauerlib/endian.hh>

#include <shared/command.hh>

@@ -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;


+ 2
- 0
src/engine/octaedit.cc View File

@@ -5,6 +5,8 @@

#include <zlib.h>

#include <sauerlib/endian.hh>

#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>


+ 1
- 0
src/engine/rendermodel.cc View File

@@ -5,6 +5,7 @@
#include <new>
#include <algorithm>

#include <sauerlib/endian.hh>
#include <sauerlib/encoding.hh>
#include <sauerlib/stream.hh>



+ 2
- 1
src/engine/texture.cc View File

@@ -9,6 +9,7 @@
#include <zlib.h>

#include <sauerlib/encoding.hh>
#include <sauerlib/endian.hh>

#include <shared/command.hh>
#include <shared/igame.hh>
@@ -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


+ 1
- 0
src/engine/worldio.cc View File

@@ -6,6 +6,7 @@

#include <algorithm>

#include <sauerlib/endian.hh>
#include <sauerlib/encoding.hh>

#include <shared/command.hh>


+ 86
- 0
src/sauerlib/endian.hh View File

@@ -0,0 +1,86 @@
#ifndef SAUERLIB_ENDIAN_HH
#define SAUERLIB_ENDIAN_HH

#include <cstdint>

/* 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<typename T> 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>(uint16_t n) {
return endianswap16(n);
}
template<> inline int16_t endianswap<int16_t>(int16_t n) {
return endianswap16(n);
}
template<> inline uint32_t endianswap<uint32_t>(uint32_t n) {
return endianswap32(n);
}
template<> inline int32_t endianswap<int32_t>(int32_t n) {
return endianswap32(n);
}
template<> inline uint64_t endianswap<uint64_t>(uint64_t n) {
return endianswap64(n);
}
template<> inline int64_t endianswap<int64_t>(int64_t n) {
return endianswap64(n);
}
template<> inline double endianswap<double>(double n) {
union { double t; uint i; } conv;
conv.t = n;
conv.i = endianswap64(conv.i);
return conv.t;
}
template<typename T> inline void endianswap(T *buf, size_t len) {
for (T *end = &buf[len]; buf < end; buf++) {
*buf = endianswap(*buf);
}
}

#ifdef SAUERLIB_BIG_ENDIAN
template<typename T> inline T lilswap(T n) { return endianswap(n); }
template<typename T> inline T bigswap(T n) { return n; }
template<typename T> inline void lilswap(T *buf, size_t len) {
endianswap(buf, len);
}
template<typename T> inline void bigswap(T *buf, size_t len) {}
#else
template<typename T> inline T lilswap(T n) { return n; }
template<typename T> inline T bigswap(T n) { return endianswap(n); }
template<typename T> inline void lilswap(T *buf, size_t len) {}
template<typename T> inline void bigswap(T *buf, size_t len) {
endianswap(buf, len);
}
#endif

#endif

+ 1
- 0
src/sauerlib/stream.hh View File

@@ -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


+ 1
- 0
src/sauerlib/tools.cc View File

@@ -3,6 +3,7 @@
#include "tools.hh"

#include "encoding.hh"
#include "endian.hh"

#include <ctime>



+ 0
- 44
src/sauerlib/tools.hh View File

@@ -1046,50 +1046,6 @@ template <class T, int SIZE> struct reversequeue : queue<T, SIZE>
const T &operator[](int offset) const { return queue<T, SIZE>::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<class T> 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>(ushort n) { return endianswap16(n); }
template<> inline short endianswap<short>(short n) { return endianswap16(n); }
template<> inline uint endianswap<uint>(uint n) { return endianswap32(n); }
template<> inline int endianswap<int>(int n) { return endianswap32(n); }
template<> inline ullong endianswap<ullong>(ullong n) { return endianswap64(n); }
template<> inline llong endianswap<llong>(llong n) { return endianswap64(n); }
template<> inline double endianswap<double>(double n) { union { double t; uint i; } conv; conv.t = n; conv.i = endianswap64(conv.i); return conv.t; }
template<class T> inline void endianswap(T *buf, size_t len) { for(T *end = &buf[len]; buf < end; buf++) *buf = endianswap(*buf); }
template<class T> inline T endiansame(T n) { return n; }
template<class T> 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<class T> inline T lilswap(T n) { return islittleendian() ? n : endianswap(n); }
template<class T> inline void lilswap(T *buf, size_t len) { if(!islittleendian()) endianswap(buf, len); }
template<class T> inline T bigswap(T n) { return islittleendian() ? endianswap(n) : n; }
template<class T> 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);


+ 2
- 0
src/shared/zip.cc View File

@@ -4,6 +4,8 @@

#include <algorithm>

#include <sauerlib/endian.hh>

#include "command.hh"

#include <engine/console.hh> /* conoutf */


Loading…
Cancel
Save