From e7798d12dc34feb8e086d416de508d766a108c78 Mon Sep 17 00:00:00 2001 From: terminx Date: Thu, 25 Oct 2018 23:33:52 +0000 Subject: [PATCH] Additional const and constexpr usage in compat.h and pragmas git-svn-id: https://svn.eduke32.com/eduke32@7132 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/build/include/compat.h | 8 ++-- source/build/include/pragmas.h | 51 +++++++++++++------------- source/build/include/pragmas_x86_gcc.h | 6 --- source/build/src/pragmas.cpp | 35 +++++++----------- 4 files changed, 43 insertions(+), 57 deletions(-) diff --git a/source/build/include/compat.h b/source/build/include/compat.h index 5a6236872..8fe5bd678 100644 --- a/source/build/include/compat.h +++ b/source/build/include/compat.h @@ -958,9 +958,9 @@ static FORCE_INLINE void B_BUF16(void * const buf, uint16_t const x) { *(uint16_ static FORCE_INLINE void B_BUF32(void * const buf, uint32_t const x) { *(uint32_t *) buf = x; } static FORCE_INLINE void B_BUF64(void * const buf, uint64_t const x) { *(uint64_t *) buf = x; } -static FORCE_INLINE uint16_t B_UNBUF16(void const * const buf) { return *(uint16_t const *) buf; } -static FORCE_INLINE uint32_t B_UNBUF32(void const * const buf) { return *(uint32_t const *) buf; } -static FORCE_INLINE uint64_t B_UNBUF64(void const * const buf) { return *(uint64_t const *) buf; } +static FORCE_INLINE CONSTEXPR uint16_t B_UNBUF16(void const * const buf) { return *(uint16_t const *) buf; } +static FORCE_INLINE CONSTEXPR uint32_t B_UNBUF32(void const * const buf) { return *(uint32_t const *) buf; } +static FORCE_INLINE CONSTEXPR uint64_t B_UNBUF64(void const * const buf) { return *(uint64_t const *) buf; } #else static FORCE_INLINE void B_BUF16(void * const vbuf, uint16_t const x) { @@ -1013,7 +1013,7 @@ static FORCE_INLINE uint64_t B_UNBUF64(void const * const vbuf) ////////// Abstract data operations ////////// -#define ABSTRACT_DECL static FORCE_INLINE WARN_UNUSED_RESULT +#define ABSTRACT_DECL static FORCE_INLINE WARN_UNUSED_RESULT CONSTEXPR #ifdef __cplusplus template ABSTRACT_DECL T clamp(T in, X min, Y max) { return in <= (T) min ? (T) min : (in >= (T) max ? (T) max : in); } diff --git a/source/build/include/pragmas.h b/source/build/include/pragmas.h index 955abf209..f8e588808 100644 --- a/source/build/include/pragmas.h +++ b/source/build/include/pragmas.h @@ -19,7 +19,6 @@ extern "C" { EDUKE32_SCALER_PRAGMA(25) EDUKE32_SCALER_PRAGMA(26) EDUKE32_SCALER_PRAGMA(27) EDUKE32_SCALER_PRAGMA(28) \ EDUKE32_SCALER_PRAGMA(29) EDUKE32_SCALER_PRAGMA(30) EDUKE32_SCALER_PRAGMA(31) -extern int32_t dmval; #if !defined(NOASM) && defined __cplusplus extern "C" { #endif @@ -44,7 +43,7 @@ extern libdivide_s32_t divtable32[DIVTABLESIZE]; extern void initdivtables(void); #if defined(__arm__) || defined(LIBDIVIDE_ALWAYS) -static inline uint32_t divideu32(uint32_t n, uint32_t d) +static inline uint32_t divideu32(uint32_t const n, uint32_t const d) { static libdivide_u32_t udiv; static uint32_t lastd; @@ -57,11 +56,11 @@ skip: return libdivide_u32_do(n, &udiv); } -static inline int64_t tabledivide64(int64_t n, int32_t d) +static inline int64_t tabledivide64(int64_t const n, int32_t const d) { static libdivide_s64_t sdiv; static int32_t lastd; - auto const dptr = ((unsigned)d < DIVTABLESIZE) ? (libdivide_s64_t *)&divtable64[d] : &sdiv; + auto *const dptr = ((unsigned)d < DIVTABLESIZE) ? &divtable64[d] : &sdiv; if (d == lastd || dptr != &sdiv) goto skip; @@ -71,11 +70,11 @@ skip: return libdivide_s64_do(n, dptr); } -static inline int32_t tabledivide32(int32_t n, int32_t d) +static inline int32_t tabledivide32(int32_t const n, int32_t const d) { static libdivide_s32_t sdiv; static int32_t lastd; - auto const dptr = ((unsigned)d < DIVTABLESIZE) ? (libdivide_s32_t *)&divtable32[d] : &sdiv; + auto *const dptr = ((unsigned)d < DIVTABLESIZE) ? &divtable32[d] : &sdiv; if (d == lastd || dptr != &sdiv) goto skip; @@ -85,16 +84,16 @@ skip: return libdivide_s32_do(n, dptr); } #else -static FORCE_INLINE uint32_t divideu32(uint32_t n, uint32_t d) { return n / d; } +static FORCE_INLINE CONSTEXPR uint32_t divideu32(uint32_t const n, uint32_t const d) { return n / d; } -static inline int64_t tabledivide64(int64_t n, int32_t d) +static inline int64_t tabledivide64(int64_t const n, int32_t const d) { - return ((unsigned)d < DIVTABLESIZE) ? libdivide_s64_do(n, (libdivide_s64_t *)&divtable64[d]) : n / d; + return ((unsigned)d < DIVTABLESIZE) ? libdivide_s64_do(n, &divtable64[d]) : n / d; } -static inline int32_t tabledivide32(int32_t n, int32_t d) +static inline int32_t tabledivide32(int32_t const n, int32_t const d) { - return ((unsigned)d < DIVTABLESIZE) ? libdivide_s32_do(n, (libdivide_s32_t *)&divtable32[d]) : n / d; + return ((unsigned)d < DIVTABLESIZE) ? libdivide_s32_do(n, &divtable32[d]) : n / d; } #endif @@ -112,7 +111,7 @@ static inline int32_t divscale(int32_t eax, int32_t ebx, int32_t ecx) } #endif -#define EDUKE32_SCALER_PRAGMA(a) \ +#define EDUKE32_SCALER_PRAGMA(a) \ static FORCE_INLINE int32_t divscale##a(int32_t eax, int32_t ebx) { return divscale(eax, ebx, a); } EDUKE32_GENERATE_PRAGMAS EDUKE32_SCALER_PRAGMA(32) #undef EDUKE32_SCALER_PRAGMA @@ -164,16 +163,16 @@ static FORCE_INLINE int32_t sqr(int32_t a) { return a * a; } #ifndef pragmas_have_mulscale -#define EDUKE32_SCALER_PRAGMA(a) \ - static FORCE_INLINE int32_t mulscale##a(int32_t eax, int32_t edx) { return dw((qw(eax) * edx) >> by(a)); } \ - static FORCE_INLINE int32_t dmulscale##a(int32_t eax, int32_t edx, int32_t esi, int32_t edi) \ - { \ - return dw(((qw(eax) * edx) + (qw(esi) * edi)) >> by(a)); \ - } \ - static FORCE_INLINE int32_t tmulscale##a(int32_t eax, int32_t edx, int32_t ebx, int32_t ecx, int32_t esi, int32_t edi) \ - { \ - return dw(((qw(eax) * edx) + (qw(ebx) * ecx) + (qw(esi) * edi)) >> by(a)); \ - } \ +#define EDUKE32_SCALER_PRAGMA(a) \ + static FORCE_INLINE CONSTEXPR int32_t mulscale##a(int32_t eax, int32_t edx) { return dw((qw(eax) * edx) >> by(a)); } \ + static FORCE_INLINE CONSTEXPR int32_t dmulscale##a(int32_t eax, int32_t edx, int32_t esi, int32_t edi) \ + { \ + return dw(((qw(eax) * edx) + (qw(esi) * edi)) >> by(a)); \ + } \ + static FORCE_INLINE CONSTEXPR int32_t tmulscale##a(int32_t eax, int32_t edx, int32_t ebx, int32_t ecx, int32_t esi, int32_t edi) \ + { \ + return dw(((qw(eax) * edx) + (qw(ebx) * ecx) + (qw(esi) * edi)) >> by(a)); \ + } EDUKE32_GENERATE_PRAGMAS EDUKE32_SCALER_PRAGMA(32) @@ -255,7 +254,7 @@ static FORCE_INLINE void swapchar2(void *a, void *b, int32_t s) } #endif -static FORCE_INLINE char readpixel(void *s) { return *(char *)s; } +static FORCE_INLINE CONSTEXPR char readpixel(void *s) { return *(char *)s; } static FORCE_INLINE void drawpixel(void *s, char a) { *(char *)s = a; } #ifndef pragmas_have_klabs @@ -266,12 +265,12 @@ static FORCE_INLINE int32_t klabs(int32_t a) } #endif #ifndef pragmas_have_ksgn -static FORCE_INLINE int32_t ksgn(int32_t a) { return (a > 0) - (a < 0); } +static FORCE_INLINE CONSTEXPR int32_t ksgn(int32_t a) { return (a > 0) - (a < 0); } #endif #ifndef pragmas_have_mulscale -static FORCE_INLINE int32_t mulscale(int32_t eax, int32_t edx, int32_t ecx) { return dw((qw(eax) * edx) >> by(ecx)); } -static FORCE_INLINE int32_t dmulscale(int32_t eax, int32_t edx, int32_t esi, int32_t edi, int32_t ecx) +static FORCE_INLINE CONSTEXPR int32_t mulscale(int32_t eax, int32_t edx, int32_t ecx) { return dw((qw(eax) * edx) >> by(ecx)); } +static FORCE_INLINE CONSTEXPR int32_t dmulscale(int32_t eax, int32_t edx, int32_t esi, int32_t edi, int32_t ecx) { return dw(((qw(eax) * edx) + (qw(esi) * edi)) >> by(ecx)); } diff --git a/source/build/include/pragmas_x86_gcc.h b/source/build/include/pragmas_x86_gcc.h index f65cdee15..7d7be1fb5 100644 --- a/source/build/include/pragmas_x86_gcc.h +++ b/source/build/include/pragmas_x86_gcc.h @@ -8,12 +8,6 @@ #ifndef pragmas_x86_h_ #define pragmas_x86_h_ -#ifndef UNDERSCORES -#define _DMVAL "dmval" -#else -#define _DMVAL "_dmval" -#endif - #define pragmas_have_mulscale #define mulscale(a,d,c) \ diff --git a/source/build/src/pragmas.cpp b/source/build/src/pragmas.cpp index 65d17093a..cc593a9af 100644 --- a/source/build/src/pragmas.cpp +++ b/source/build/src/pragmas.cpp @@ -15,15 +15,10 @@ libdivide_s32_t divtable32[DIVTABLESIZE]; void initdivtables(void) { - libdivide_s64_t d; - libdivide_s32_t d32; - - for (bssize_t i=1; i 0) { @@ -316,9 +309,9 @@ void swapbuf4(void *a, void *b, int32_t c) void clearbufbyte(void *D, int32_t c, int32_t a) { // Cringe City - int32_t const m[4] = { 0xffl, 0xff00l, 0xff0000l, (int32_t)0xff000000l }; - int32_t z = 0; - auto p = (char *)D; + constexpr int32_t m[4] = { 0xffl, 0xff00l, 0xff0000l, (int32_t)0xff000000l }; + int z = 0; + auto *p = (char *)D; while ((c--) > 0) { @@ -331,8 +324,8 @@ void clearbufbyte(void *D, int32_t c, int32_t a) #ifndef pragmas_have_copybufbyte void copybufbyte(const void *s, void *d, int32_t c) { - auto src = (const char *)s; - auto dst = (char *)d; + auto *src = (const char *)s; + auto *dst = (char *)d; while (c--) *dst++ = *src++; @@ -385,8 +378,8 @@ void copybufreverse(const void *S, void *D, int32_t c) #elif !defined pragmas_have_copybufreverse void copybufreverse(const void *s, void *d, int32_t c) { - auto src = (const char *)s; - auto dst = (char *)d; + auto *src = (const char *)s; + auto *dst = (char *)d; while (c--) *dst++ = *src--;