From: DeaDDooMER Date: Tue, 6 Apr 2021 09:54:14 +0000 (+0300) Subject: common: move endianness converters to common X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=2cd3d04c04fb347e6eea354dccf14099f56bd352;p=flatwaifu.git common: move endianness converters to common --- diff --git a/src/common/endianness.h b/src/common/endianness.h new file mode 100644 index 0000000..f4708b2 --- /dev/null +++ b/src/common/endianness.h @@ -0,0 +1,47 @@ +#ifndef COMMON_ENDIANNESS_H_INCLUDED +#define COMMON_ENDIANNESS_H_INCLUDED + +#include + +#define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0) + +static inline int16_t short2swap (int16_t x) { + union { + uint8_t a[2]; + int16_t x; + } y; + y.x = x; + SWAP_VAR(y.a[0], y.a[1]); + return y.x; +} + +static inline int32_t int2swap (int32_t x) { + union { + uint8_t a[4]; + int32_t x; + } y; + y.x = x; + SWAP_VAR(y.a[0], y.a[3]); + SWAP_VAR(y.a[1], y.a[2]); + return y.x; +} + +#undef SWAP_VAR + +static inline int16_t short2host (int16_t x) { +#if __BIG_ENDIAN__ + return short2swap(x); +#else + return x; +#endif +} + +static inline int32_t int2host (int32_t x) { +#if __BIG_ENDIAN__ + return int2swap(x); +#else + return x; +#endif +} + +#endif /* COMMON_ENDIANNESS_H_INCLUDED */ \ No newline at end of file diff --git a/src/common/streams.c b/src/common/streams.c index 615274c..46e756b 100644 --- a/src/common/streams.c +++ b/src/common/streams.c @@ -1,10 +1,9 @@ -#include "streams.h" +#include "common/streams.h" +#include "common/endianness.h" #include #include -#include "misc.h" // endianness conversion - void stream_read (void *data, size_t size, size_t n, Reader *r) { r->read(r, data, size, n); } diff --git a/src/gl/render.c b/src/gl/render.c index 948a7b5..e29dd23 100644 --- a/src/gl/render.c +++ b/src/gl/render.c @@ -38,6 +38,7 @@ #include "switch.h" // sw_secrets #include "cp866.h" +#include "common/endianness.h" #ifdef __APPLE__ # include diff --git a/src/misc.h b/src/misc.h index 20ec630..b863460 100644 --- a/src/misc.h +++ b/src/misc.h @@ -57,7 +57,4 @@ int Z_moveobj (obj_t *p); void Z_splash (obj_t *p, int n); void Z_calc_time(dword t, word *h, word *m, word *s); -int16_t short2host (int16_t x); -int32_t int2host (int32_t x); - #endif /* MISC_H_INCLUDED */ diff --git a/src/miscc.c b/src/miscc.c index b2bbbaf..184e369 100644 --- a/src/miscc.c +++ b/src/miscc.c @@ -389,44 +389,3 @@ void Z_calc_time(dword t,word *h,word *m,word *s) t = t / 60; *h = t; } - -#define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0) - -static int16_t short2swap (int16_t x) { - union { - uint8_t a[2]; - int16_t x; - } y; - y.x = x; - SWAP_VAR(y.a[0], y.a[1]); - return y.x; -} - -static int32_t int2swap (int32_t x) { - union { - uint8_t a[4]; - int32_t x; - } y; - y.x = x; - SWAP_VAR(y.a[0], y.a[3]); - SWAP_VAR(y.a[1], y.a[2]); - return y.x; -} - -#undef SWAP_VAR - -int16_t short2host (int16_t x) { -#if __BIG_ENDIAN__ - return short2swap(x); -#else - return x; -#endif -} - -int32_t int2host (int32_t x) { -#if __BIG_ENDIAN__ - return int2swap(x); -#else - return x; -#endif -} diff --git a/src/my.c b/src/my.c index b3eb964..a7adf3c 100644 --- a/src/my.c +++ b/src/my.c @@ -18,7 +18,8 @@ #include "glob.h" #include "error.h" -#include "misc.h" +#include "common/endianness.h" + #include #include #include diff --git a/src/openal/sound.c b/src/openal/sound.c index 8d0b342..7bb60a0 100644 --- a/src/openal/sound.c +++ b/src/openal/sound.c @@ -18,9 +18,10 @@ #include "files.h" // F_findres F_getreslen #include "memory.h" // M_lock M_unlock -#include "misc.h" // int2host #include "error.h" // logo +#include "common/endianness.h" + #ifdef __APPLE__ # include # include diff --git a/src/sdlmixer/sound.c b/src/sdlmixer/sound.c index ec27e45..a5da8c5 100644 --- a/src/sdlmixer/sound.c +++ b/src/sdlmixer/sound.c @@ -16,11 +16,12 @@ #include "glob.h" #include "sound.h" #include "music.h" -#include "misc.h" // int2host #include "memory.h" // M_lock M_unlock #include "files.h" // F_findres #include "error.h" +#include "common/endianness.h" + #include "SDL.h" #include "SDL_mixer.h" #include diff --git a/src/soft/vga.c b/src/soft/vga.c index 63b5f65..5f90785 100644 --- a/src/soft/vga.c +++ b/src/soft/vga.c @@ -24,6 +24,8 @@ #include "files.h" #include "system.h" +#include "common/endianness.h" + #include #include