From 201f01b66c11aea0c80293c550d2a6f70903be6f Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 26 May 2007 23:18:46 +0000 Subject: [PATCH] hopefully speed up byte swaps --- include/QF/qendian.h | 37 +++++++++++++++++++++++++----- libs/audio/renderer/snd_resample.c | 30 ++++++++++-------------- libs/util/qendian.c | 12 +++++----- tools/qfmodelgen/source/lbmlib.c | 6 ++--- 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/include/QF/qendian.h b/include/QF/qendian.h index 7d7738c5e..794adfd40 100644 --- a/include/QF/qendian.h +++ b/include/QF/qendian.h @@ -76,12 +76,37 @@ extern qboolean bigendien; -short ShortSwap (short l); -short ShortNoSwap (short l); -int LongSwap (int l); -int LongNoSwap (int l); -float FloatSwap (float f); -float FloatNoSwap (float f); +short _ShortSwap (short l); +short _ShortNoSwap (short l); +int _LongSwap (int l); +int _LongNoSwap (int l); +float _FloatSwap (float f); +float _FloatNoSwap (float f); + + +#ifdef __GNUC__ +#define ShortSwap(l) ({ uint16_t x = (uint16_t) (l); \ + x = ( ((x >> 8) & 0xff) \ + | ((x << 8) & 0xff00)); \ + x; }) + +#define LongSwap(l) ({ uint32_t z = (uint32_t) (l); \ + z = (ShortSwap (z >> 16)) | (ShortSwap (z) << 16); \ + z; }) + +#define FloatSwap(l) ({ union { uint32_t i; float f; } y; \ + y.f = (l); \ + y.i = LongSwap (y.i); \ + y.f; }) +#else +#define ShortSwap(l) _ShortSwap (l) +#define LongSwap(l) _LongSwap (l) +#define FloatSwap(l) _FloatSwap (l) +#endif + +#define ShortNoSwap(l) (l) +#define LongNoSwap(l) (l) +#define FloatNoSwap(l) (l) // NOTE: these /always/ read and write /little/ endian entities. struct QFile_s; diff --git a/libs/audio/renderer/snd_resample.c b/libs/audio/renderer/snd_resample.c index 426b805e8..e73104b85 100644 --- a/libs/audio/renderer/snd_resample.c +++ b/libs/audio/renderer/snd_resample.c @@ -61,6 +61,15 @@ typedef struct { cvar_t *snd_loadas8bit; cvar_t *snd_interp; +static void +check_buffer_integrity (sfxbuffer_t *sc, int width, const char *func) +{ + byte *x = sc->data + sc->length * width; + if (memcmp (x, "\xde\xad\xbe\xef", 4)) + Sys_Error ("%s screwed the pooch %02x%02x%02x%02x", func, + x[0], x[1], x[2], x[3]); +} + void SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev) { @@ -195,12 +204,7 @@ general_Mono: } } } - { - byte *x = sc->data + sc->length * outwidth; - if (memcmp (x, "\xde\xad\xbe\xef", 4)) - Sys_Error ("SND_ResampleMono screwed the pooch %02x%02x%02x%02x", - x[0], x[1], x[2], x[3]); - } + check_buffer_integrity (sc, outwidth, __FUNCTION__); } void @@ -359,12 +363,7 @@ general_Stereo: } } } - { - byte *x = sc->data + sc->length * outwidth * 2; - if (memcmp (x, "\xde\xad\xbe\xef", 4)) - Sys_Error ("SND_ResampleStereo screwed the pooch %02x%02x%02x%02x", - x[0], x[1], x[2], x[3]); - } + check_buffer_integrity (sc, outwidth * 2, __FUNCTION__); } void @@ -429,10 +428,5 @@ SND_NoResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev) } } } - { - byte *x = sc->data + sc->length * outwidth * 2; - if (memcmp (x, "\xde\xad\xbe\xef", 4)) - Sys_Error ("SND_ResampleStereo screwed the pooch %02x%02x%02x%02x", - x[0], x[1], x[2], x[3]); - } + check_buffer_integrity (sc, outwidth * 2, __FUNCTION__); } diff --git a/libs/util/qendian.c b/libs/util/qendian.c index e8c9cd93a..69e8a1019 100644 --- a/libs/util/qendian.c +++ b/libs/util/qendian.c @@ -51,7 +51,7 @@ VISIBLE qboolean bigendien = true;; VISIBLE short -ShortSwap (short l) +_ShortSwap (short l) { byte b1, b2; @@ -62,13 +62,13 @@ ShortSwap (short l) } VISIBLE short -ShortNoSwap (short l) +_ShortNoSwap (short l) { return l; } VISIBLE int -LongSwap (int l) +_LongSwap (int l) { byte b1, b2, b3, b4; @@ -81,13 +81,13 @@ LongSwap (int l) } VISIBLE int -LongNoSwap (int l) +_LongNoSwap (int l) { return l; } VISIBLE float -FloatSwap (float f) +_FloatSwap (float f) { union { float f; @@ -103,7 +103,7 @@ FloatSwap (float f) } VISIBLE float -FloatNoSwap (float f) +_FloatNoSwap (float f) { return f; } diff --git a/tools/qfmodelgen/source/lbmlib.c b/tools/qfmodelgen/source/lbmlib.c index 1b25f9cf1..cf8b34b2b 100644 --- a/tools/qfmodelgen/source/lbmlib.c +++ b/tools/qfmodelgen/source/lbmlib.c @@ -499,9 +499,9 @@ WriteLBMfile (char *filename, byte *data, int width, int height, byte *palette) memset (&basebmhd, 0, sizeof (basebmhd)); basebmhd.w = BigShort ((short) width); basebmhd.h = BigShort ((short) height); - basebmhd.nPlanes = BigShort (8); - basebmhd.xAspect = BigShort (5); - basebmhd.yAspect = BigShort (6); + basebmhd.nPlanes = 8; + basebmhd.xAspect = 5; + basebmhd.yAspect = 6; basebmhd.pageWidth = BigShort ((short) width); basebmhd.pageHeight = BigShort ((short) height);