From 8fd7e7ba107c17e30d368001d0a4ab99ad4cfd6a Mon Sep 17 00:00:00 2001 From: Alison Watson Date: Tue, 3 Dec 2019 13:13:00 -0500 Subject: [PATCH] use platform specific endian swap functions (in our case SDL's) --- CMakeLists.txt | 1 + source/common.c | 98 --------------------------------------------- source/common.h | 22 +++------- source/host.c | 1 - source/snd_mp3.c | 17 ++++---- source/snd_vorbis.c | 2 +- 6 files changed, 15 insertions(+), 126 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a39687..0fc079d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,7 @@ set(srcs_windows set(srcs_sdl source/sdl/cd_sdl.c + source/sdl/endian_sdl.h source/sdl/gl_vidsdl.c source/sdl/in_sdl.c source/sdl/main_sdl.c diff --git a/source/common.c b/source/common.c index 5b04423..4bc2db2 100644 --- a/source/common.c +++ b/source/common.c @@ -508,67 +508,6 @@ float Q_atof(const char *str) return val * sign; } -/* -============================================================================ - - BYTE ORDER FUNCTIONS - -============================================================================ -*/ - -bool host_bigendian; - -int16_t (*BigShort)(int16_t l); -int32_t (*BigLong)(int32_t l); -float (*BigFloat)(float l); - -int16_t (*LittleShort)(int16_t l); -int32_t (*LittleLong)(int32_t l); -float (*LittleFloat)(float l); - -static int16_t ShortSwap(int16_t l) -{ - int32_t copy; - ((byte *)©)[0] = ((byte *)&l)[1]; - ((byte *)©)[1] = ((byte *)&l)[0]; - return copy; -} - -static int16_t ShortNoSwap(int16_t l) -{ - return l; -} - -static int32_t LongSwap(int32_t l) -{ - int32_t copy; - ((byte *)©)[0] = ((byte *)&l)[3]; - ((byte *)©)[1] = ((byte *)&l)[2]; - ((byte *)©)[2] = ((byte *)&l)[1]; - ((byte *)©)[3] = ((byte *)&l)[0]; - return copy; -} - -static int32_t LongNoSwap(int32_t l) -{ - return l; -} - -static float FloatSwap(float f) -{ - float copy; - ((byte *)©)[0] = ((byte *)&f)[3]; - ((byte *)©)[1] = ((byte *)&f)[2]; - ((byte *)©)[2] = ((byte *)&f)[1]; - ((byte *)©)[3] = ((byte *)&f)[0]; - return copy; -} - -static float FloatNoSwap(float f) -{ - return f; -} - /* ============================================================================== @@ -1250,43 +1189,6 @@ void COM_InitArgv(int32_t argc, char **argv) } } -/* -================ -COM_Init -================ -*/ -void COM_Init(void) -{ - int32_t i = 0x12345678; - - if(*(byte *)&i == 0x12) - host_bigendian = true; - else if(*(byte *)&i == 0x78) - host_bigendian = false; - else - Sys_Error("Unsupported endianness"); - - if(host_bigendian) - { - BigShort = ShortNoSwap; - BigLong = LongNoSwap; - BigFloat = FloatNoSwap; - LittleShort = ShortSwap; - LittleLong = LongSwap; - LittleFloat = FloatSwap; - } - else - { - BigShort = ShortSwap; - BigLong = LongSwap; - BigFloat = FloatSwap; - LittleShort = ShortNoSwap; - LittleLong = LongNoSwap; - LittleFloat = FloatNoSwap; - } -} - - /* ============ va diff --git a/source/common.h b/source/common.h index eda2117..aada6f1 100644 --- a/source/common.h +++ b/source/common.h @@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef spingle__common_h #define spingle__common_h +#include "sdl/endian_sdl.h" #define q_minfunc(typ, name) \ static inline typ name(typ a, typ b) {return a < b ? a : b;} @@ -66,11 +67,11 @@ GenericTypes(CLAMPfunc, CLAMP) typedef struct sizebuf_s { - bool allowoverflow; // if false, do a Sys_Error - bool overflowed; // set to true if the buffer size failed - byte *data; - int32_t maxsize; - int32_t cursize; + bool allowoverflow; // if false, do a Sys_Error + bool overflowed; // set to true if the buffer size failed + byte *data; + int32_t maxsize; + int32_t cursize; } sizebuf_t; void SZ_Alloc(sizebuf_t *buf, int32_t startsize); @@ -95,16 +96,6 @@ void InsertLinkAfter(link_t *l, link_t *after); //============================================================================ -extern bool host_bigendian; - -extern int16_t (*BigShort)(int16_t l); -extern int32_t (*BigLong)(int32_t l); -extern float (*BigFloat)(float l); - -extern int16_t (*LittleShort)(int16_t l); -extern int32_t (*LittleLong)(int32_t l); -extern float (*LittleFloat)(float l); - static inline int16_t ReadBigShort(byte const *bytes) { int16_t v; @@ -243,7 +234,6 @@ extern int32_t safemode; int32_t COM_CheckParm(const char *parm); -void COM_Init(void); void COM_InitArgv(int32_t argc, char **argv); void COM_InitFilesystem(void); diff --git a/source/host.c b/source/host.c index 6c9ce3a..7e4281c 100644 --- a/source/host.c +++ b/source/host.c @@ -826,7 +826,6 @@ void Host_Init(void) Cmd_Init(); LOG_Init(host_parms); Cvar_Init(); //johnfitz - COM_Init(); COM_InitFilesystem(); Host_InitLocal(); W_LoadWadFile(); //johnfitz -- filename is now hard-coded for honesty diff --git a/source/snd_mp3.c b/source/snd_mp3.c index 46fce43..2181c6e 100644 --- a/source/snd_mp3.c +++ b/source/snd_mp3.c @@ -317,16 +317,13 @@ static int32_t mp3_decode(snd_stream_t *stream, byte *buf, int32_t len) sample = 0x7FFF; else sample >>= (MAD_F_FRACBITS + 1 - 16); - if(host_bigendian) - { - *buf++ = (sample >> 8) & 0xFF; - *buf++ = sample & 0xFF; - } - else - { - *buf++ = sample & 0xFF; - *buf++ = (sample >> 8) & 0xFF; - } +#if HOST_BIGENDIAN + *buf++ = (sample >> 8) & 0xFF; + *buf++ = sample & 0xFF; +#else + *buf++ = sample & 0xFF; + *buf++ = (sample >> 8) & 0xFF; +#endif i++; } p->cursamp++; diff --git a/source/snd_vorbis.c b/source/snd_vorbis.c index 1abb98e..93abfe8 100644 --- a/source/snd_vorbis.c +++ b/source/snd_vorbis.c @@ -155,7 +155,7 @@ static int32_t S_VORBIS_CodecReadStream(snd_stream_t *stream, int32_t bytes, voi * the channels are interleaved in the output buffer. */ res = ov_read((OggVorbis_File *)stream->priv, ptr, rem, - host_bigendian, + HOST_BIGENDIAN, VORBIS_SAMPLEWIDTH, VORBIS_SIGNED_DATA, & section);