Use FORCE_INLINE and CONSTEXPR where appropriate in the Q16 fixed point math library

git-svn-id: https://svn.eduke32.com/eduke32@7588 1a8010ca-5511-0410-912e-c29ae57300e0

# Conflicts:
#	platform/Windows/build.vcxproj.filters
This commit is contained in:
terminx 2019-04-18 17:24:17 +00:00 committed by Christoph Oelckers
parent e5bd221746
commit e59f8e3b71
7 changed files with 92 additions and 98 deletions

View file

@ -241,8 +241,8 @@ engine_objs := \
miniz.c \
miniz_tinfl.c \
miniz_tdef.c \
fix16.c \
fix16_str.c \
fix16.cpp \
fix16_str.cpp \
engine_editor_objs := \
build.cpp \

View file

@ -211,8 +211,8 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\source\build\src\engine.cpp" />
<ClCompile Include="..\..\source\build\src\fix16.c" />
<ClCompile Include="..\..\source\build\src\fix16_str.c" />
<ClCompile Include="..\..\source\build\src\fix16.cpp" />
<ClCompile Include="..\..\source\build\src\fix16_str.cpp" />
<ClCompile Include="..\..\source\build\src\glbuild.cpp" />
<ClCompile Include="..\..\source\build\src\glsurface.cpp" />
<ClCompile Include="..\..\source\build\src\gtkbits.cpp">

View file

@ -47,11 +47,11 @@ $(engine_obj)/wiibits.$o: $(engine_src)/wiibits.cpp $(engine_inc)/wiibits.h
$(engine_obj)/winbits.$o: $(engine_src)/winbits.cpp $(engine_inc)/winbits.h
$(engine_obj)/xxhash.$o: $(engine_src)/xxhash.c $(engine_inc)/xxhash.h
$(engine_obj)/pngwrite.$o: $(engine_src)/pngwrite.cpp $(engine_inc)/pngwrite.h
$(engine_obj)/fix16.$o: $(engine_src)/fix16.c $(engine_inc)/fix16.h $(engine_inc)/fix16_int64.h
$(engine_obj)/fix16.$o: $(engine_src)/fix16.cpp $(engine_inc)/fix16.h $(engine_inc)/fix16_int64.h
$(engine_obj)/miniz.$o: $(engine_src)/miniz.c $(engine_inc)/miniz.h $(engine_inc)/miniz_common.h $(engine_inc)/miniz_tinfl.h $(engine_inc)/miniz_tdef.h
$(engine_obj)/miniz_tinfl.$o: $(engine_src)/miniz_tinfl.c $(engine_inc)/miniz.h $(engine_inc)/miniz_common.h $(engine_inc)/miniz_tinfl.h $(engine_inc)/miniz_tdef.h
$(engine_obj)/miniz_tdef.$o: $(engine_src)/miniz_tdef.c $(engine_inc)/miniz.h $(engine_inc)/miniz_common.h $(engine_inc)/miniz_tinfl.h $(engine_inc)/miniz_tdef.h
$(engine_obj)/fix16_str.$o: $(engine_src)/fix16_str.c $(engine_inc)/fix16.h
$(engine_obj)/fix16_str.$o: $(engine_src)/fix16_str.cpp $(engine_inc)/fix16.h
$(engine_obj)/lunatic.$o: $(engine_src)/lunatic.cpp $(engine_inc)/lunatic.h $(engine_inc)/cache1d.h $(engine_inc)/osd.h

View file

@ -1,6 +1,8 @@
#ifndef __libfixmath_fix16_h__
#define __libfixmath_fix16_h__
#include "compat.h"
#ifdef __cplusplus
extern "C"
{
@ -26,54 +28,46 @@ extern "C"
typedef int32_t fix16_t;
static const fix16_t FOUR_DIV_PI = 0x145F3; /*!< Fix16 value of 4/PI */
static const fix16_t _FOUR_DIV_PI2 = 0xFFFF9840; /*!< Fix16 value of -4/PI² */
static const fix16_t X4_CORRECTION_COMPONENT = 0x399A; /*!< Fix16 value of 0.225 */
static const fix16_t PI_DIV_4 = 0x0000C90F; /*!< Fix16 value of PI/4 */
static const fix16_t THREE_PI_DIV_4 = 0x00025B2F; /*!< Fix16 value of 3PI/4 */
static CONSTEXPR const fix16_t FIX16_MAX = 0x7FFFFFFF; /*!< the maximum value of fix16_t */
static CONSTEXPR const fix16_t FIX16_MIN = 0x80000000; /*!< the minimum value of fix16_t */
static CONSTEXPR const fix16_t FIX16_OVERFLOW = 0x80000000; /*!< the value used to indicate overflows when FIXMATH_NO_OVERFLOW is not specified */
static const fix16_t fix16_maximum = 0x7FFFFFFF; /*!< the maximum value of fix16_t */
static const fix16_t fix16_minimum = 0x80000000; /*!< the minimum value of fix16_t */
static const fix16_t fix16_overflow = 0x80000000; /*!< the value used to indicate overflows when FIXMATH_NO_OVERFLOW is not specified */
static const fix16_t fix16_pi = 205887; /*!< fix16_t value of pi */
static const fix16_t fix16_e = 178145; /*!< fix16_t value of e */
static const fix16_t fix16_one = 0x00010000; /*!< fix16_t value of 1 */
static CONSTEXPR const fix16_t fix16_one = 0x00010000; /*!< fix16_t value of 1 */
/* Conversion functions between fix16_t and float/integer.
* These are inlined to allow compiler to optimize away constant numbers
*/
static inline fix16_t fix16_from_int(int a) { return a * fix16_one; }
static inline float fix16_to_float(fix16_t a) { return (float)a / fix16_one; }
static inline double fix16_to_dbl(fix16_t a) { return (double)a / fix16_one; }
static FORCE_INLINE CONSTEXPR fix16_t fix16_from_int(int a) { return a * fix16_one; }
static FORCE_INLINE CONSTEXPR float fix16_to_float(fix16_t a) { return (float)a / fix16_one; }
static FORCE_INLINE CONSTEXPR double fix16_to_dbl(fix16_t a) { return (double)a / fix16_one; }
static inline int fix16_to_int(fix16_t a)
{
#ifdef FIXMATH_NO_ROUNDING
return (a >> 16);
#else
if (a >= 0)
return (a + (fix16_one >> 1)) / fix16_one;
return (a - (fix16_one >> 1)) / fix16_one;
if (a >= 0)
return (a + (fix16_one >> 1)) / fix16_one;
return (a - (fix16_one >> 1)) / fix16_one;
#endif
}
static inline fix16_t fix16_from_float(float a)
{
float temp = a * fix16_one;
float temp = a * fix16_one;
#ifndef FIXMATH_NO_ROUNDING
temp += (temp >= 0) ? 0.5f : -0.5f;
temp += (temp >= 0) ? 0.5f : -0.5f;
#endif
return (fix16_t)temp;
return (fix16_t)temp;
}
static inline fix16_t fix16_from_dbl(double a)
{
double temp = a * fix16_one;
double temp = a * fix16_one;
#ifndef FIXMATH_NO_ROUNDING
temp += (temp >= 0) ? 0.5f : -0.5f;
temp += (temp >= 0) ? 0.5f : -0.5f;
#endif
return (fix16_t)temp;
return (fix16_t)temp;
}
/* Macro for defining fix16_t constant values.
@ -87,24 +81,24 @@ static inline fix16_t fix16_from_dbl(double a)
*/
#define F16(x) ((fix16_t)(((x) >= 0) ? ((x) * 65536.0 + 0.5) : ((x) * 65536.0 - 0.5)))
static inline fix16_t fix16_abs(fix16_t x)
{ return (x < 0 ? -x : x); }
static inline fix16_t fix16_floor(fix16_t x)
{ return (x & 0xFFFF0000UL); }
static inline fix16_t fix16_ceil(fix16_t x)
{ return (x & 0xFFFF0000UL) + ((x & 0x0000FFFFUL) ? fix16_one : 0); }
static inline fix16_t fix16_min(fix16_t x, fix16_t y)
{ return (x < y ? x : y); }
static inline fix16_t fix16_max(fix16_t x, fix16_t y)
{ return (x > y ? x : y); }
static inline fix16_t fix16_clamp(fix16_t x, fix16_t lo, fix16_t hi)
{ return fix16_min(fix16_max(x, lo), hi); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_abs(fix16_t x)
{ return (x < 0 ? -x : x); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_floor(fix16_t x)
{ return (x & 0xFFFF0000UL); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_ceil(fix16_t x)
{ return (x & 0xFFFF0000UL) + ((x & 0x0000FFFFUL) ? fix16_one : 0); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_min(fix16_t x, fix16_t y)
{ return (x < y ? x : y); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_max(fix16_t x, fix16_t y)
{ return (x > y ? x : y); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_clamp(fix16_t x, fix16_t lo, fix16_t hi)
{ return fix16_min(fix16_max(x, lo), hi); }
/* Subtraction and addition with (optional) overflow detection. */
#ifdef FIXMATH_NO_OVERFLOW
static inline fix16_t fix16_add(fix16_t inArg0, fix16_t inArg1) { return (inArg0 + inArg1); }
static inline fix16_t fix16_sub(fix16_t inArg0, fix16_t inArg1) { return (inArg0 - inArg1); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_add(fix16_t inArg0, fix16_t inArg1) { return (inArg0 + inArg1); }
static FORCE_INLINE CONSTEXPR fix16_t fix16_sub(fix16_t inArg0, fix16_t inArg1) { return (inArg0 - inArg1); }
#else
@ -135,9 +129,9 @@ extern fix16_t fix16_smul(fix16_t inArg0, fix16_t inArg1) FIXMATH_FUNC_ATTRS;
extern fix16_t fix16_sdiv(fix16_t inArg0, fix16_t inArg1) FIXMATH_FUNC_ATTRS;
#endif
/*! Divides the first given fix16_t by the second and returns the result.
/*! Divides the first given fix16_t by the second and returns the remainder.
*/
extern fix16_t fix16_mod(fix16_t x, fix16_t y) FIXMATH_FUNC_ATTRS;
static FORCE_INLINE fix16_t fix16_mod(fix16_t x, fix16_t y) { return x %= y; }
@ -183,11 +177,11 @@ extern fix16_t fix16_atan2(fix16_t inY, fix16_t inX) FIXMATH_FUNC_ATTRS;
static const fix16_t fix16_rad_to_deg_mult = 3754936;
static inline fix16_t fix16_rad_to_deg(fix16_t radians)
{ return fix16_mul(radians, fix16_rad_to_deg_mult); }
{ return fix16_mul(radians, fix16_rad_to_deg_mult); }
static const fix16_t fix16_deg_to_rad_mult = 1144;
static inline fix16_t fix16_deg_to_rad(fix16_t degrees)
{ return fix16_mul(degrees, fix16_deg_to_rad_mult); }
{ return fix16_mul(degrees, fix16_deg_to_rad_mult); }
@ -198,7 +192,7 @@ extern fix16_t fix16_sqrt(fix16_t inValue) FIXMATH_FUNC_ATTRS;
/*! Returns the square of the given fix16_t.
*/
static inline fix16_t fix16_sq(fix16_t x)
{ return fix16_mul(x, x); }
{ return fix16_mul(x, x); }
/*! Returns the exponent (e^) of the given fix16_t.
*/

View file

@ -1,33 +1,35 @@
#ifndef __libfixmath_int64_h__
#define __libfixmath_int64_h__
#include "compat.h"
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef FIXMATH_NO_64BIT
static inline int64_t int64_const(int32_t hi, uint32_t lo) { return (((int64_t)hi << 32) | lo); }
static inline int64_t int64_from_int32(int32_t x) { return (int64_t)x; }
static inline int32_t int64_hi(int64_t x) { return (x >> 32); }
static inline uint32_t int64_lo(int64_t x) { return (x & ((1ULL << 32) - 1)); }
static FORCE_INLINE CONSTEXPR int64_t int64_const(int32_t hi, uint32_t lo) { return (((int64_t)hi << 32) | lo); }
static FORCE_INLINE CONSTEXPR int64_t int64_from_int32(int32_t x) { return (int64_t)x; }
static FORCE_INLINE CONSTEXPR int32_t int64_hi(int64_t x) { return (x >> 32); }
static FORCE_INLINE CONSTEXPR uint32_t int64_lo(int64_t x) { return (x & ((1ULL << 32) - 1)); }
static inline int64_t int64_add(int64_t x, int64_t y) { return (x + y); }
static inline int64_t int64_neg(int64_t x) { return (-x); }
static inline int64_t int64_sub(int64_t x, int64_t y) { return (x - y); }
static inline int64_t int64_shift(int64_t x, int8_t y) { return (y < 0 ? (x >> -y) : (x << y)); }
static FORCE_INLINE CONSTEXPR int64_t int64_add(int64_t x, int64_t y) { return (x + y); }
static FORCE_INLINE CONSTEXPR int64_t int64_neg(int64_t x) { return (-x); }
static FORCE_INLINE CONSTEXPR int64_t int64_sub(int64_t x, int64_t y) { return (x - y); }
static FORCE_INLINE CONSTEXPR int64_t int64_shift(int64_t x, int8_t y) { return (y < 0 ? (x >> -y) : (x << y)); }
static inline int64_t int64_mul_i32_i32(int32_t x, int32_t y) { return (x * y); }
static inline int64_t int64_mul_i64_i32(int64_t x, int32_t y) { return (x * y); }
static FORCE_INLINE CONSTEXPR int64_t int64_mul_i32_i32(int32_t x, int32_t y) { return (x * y); }
static FORCE_INLINE CONSTEXPR int64_t int64_mul_i64_i32(int64_t x, int32_t y) { return (x * y); }
static inline int64_t int64_div_i64_i32(int64_t x, int32_t y) { return (x / y); }
static FORCE_INLINE CONSTEXPR int64_t int64_div_i64_i32(int64_t x, int32_t y) { return (x / y); }
static inline int int64_cmp_eq(int64_t x, int64_t y) { return (x == y); }
static inline int int64_cmp_ne(int64_t x, int64_t y) { return (x != y); }
static inline int int64_cmp_gt(int64_t x, int64_t y) { return (x > y); }
static inline int int64_cmp_ge(int64_t x, int64_t y) { return (x >= y); }
static inline int int64_cmp_lt(int64_t x, int64_t y) { return (x < y); }
static inline int int64_cmp_le(int64_t x, int64_t y) { return (x <= y); }
static FORCE_INLINE CONSTEXPR int int64_cmp_eq(int64_t x, int64_t y) { return (x == y); }
static FORCE_INLINE CONSTEXPR int int64_cmp_ne(int64_t x, int64_t y) { return (x != y); }
static FORCE_INLINE CONSTEXPR int int64_cmp_gt(int64_t x, int64_t y) { return (x > y); }
static FORCE_INLINE CONSTEXPR int int64_cmp_ge(int64_t x, int64_t y) { return (x >= y); }
static FORCE_INLINE CONSTEXPR int int64_cmp_lt(int64_t x, int64_t y) { return (x < y); }
static FORCE_INLINE CONSTEXPR int int64_cmp_le(int64_t x, int64_t y) { return (x <= y); }
#else
typedef struct {
@ -35,17 +37,17 @@ typedef struct {
uint32_t lo;
} __int64_t;
static inline __int64_t int64_const(int32_t hi, uint32_t lo) { return (__int64_t){ hi, lo }; }
static inline __int64_t int64_from_int32(int32_t x) { return (__int64_t){ (x < 0 ? -1 : 0), x }; }
static inline int32_t int64_hi(__int64_t x) { return x.hi; }
static inline uint32_t int64_lo(__int64_t x) { return x.lo; }
static FORCE_INLINE CONSTEXPR __int64_t int64_const(int32_t hi, uint32_t lo) { return (__int64_t){ hi, lo }; }
static FORCE_INLINE CONSTEXPR __int64_t int64_from_int32(int32_t x) { return (__int64_t){ (x < 0 ? -1 : 0), x }; }
static FORCE_INLINE CONSTEXPR int32_t int64_hi(__int64_t x) { return x.hi; }
static FORCE_INLINE CONSTEXPR uint32_t int64_lo(__int64_t x) { return x.lo; }
static inline int int64_cmp_eq(__int64_t x, __int64_t y) { return ((x.hi == y.hi) && (x.lo == y.lo)); }
static inline int int64_cmp_ne(__int64_t x, __int64_t y) { return ((x.hi != y.hi) || (x.lo != y.lo)); }
static inline int int64_cmp_gt(__int64_t x, __int64_t y) { return ((x.hi > y.hi) || ((x.hi == y.hi) && (x.lo > y.lo))); }
static inline int int64_cmp_ge(__int64_t x, __int64_t y) { return ((x.hi > y.hi) || ((x.hi == y.hi) && (x.lo >= y.lo))); }
static inline int int64_cmp_lt(__int64_t x, __int64_t y) { return ((x.hi < y.hi) || ((x.hi == y.hi) && (x.lo < y.lo))); }
static inline int int64_cmp_le(__int64_t x, __int64_t y) { return ((x.hi < y.hi) || ((x.hi == y.hi) && (x.lo <= y.lo))); }
static FORCE_INLINE CONSTEXPR int int64_cmp_eq(__int64_t x, __int64_t y) { return ((x.hi == y.hi) && (x.lo == y.lo)); }
static FORCE_INLINE CONSTEXPR int int64_cmp_ne(__int64_t x, __int64_t y) { return ((x.hi != y.hi) || (x.lo != y.lo)); }
static FORCE_INLINE CONSTEXPR int int64_cmp_gt(__int64_t x, __int64_t y) { return ((x.hi > y.hi) || ((x.hi == y.hi) && (x.lo > y.lo))); }
static FORCE_INLINE CONSTEXPR int int64_cmp_ge(__int64_t x, __int64_t y) { return ((x.hi > y.hi) || ((x.hi == y.hi) && (x.lo >= y.lo))); }
static FORCE_INLINE CONSTEXPR int int64_cmp_lt(__int64_t x, __int64_t y) { return ((x.hi < y.hi) || ((x.hi == y.hi) && (x.lo < y.lo))); }
static FORCE_INLINE CONSTEXPR int int64_cmp_le(__int64_t x, __int64_t y) { return ((x.hi < y.hi) || ((x.hi == y.hi) && (x.lo <= y.lo))); }
static inline __int64_t int64_add(__int64_t x, __int64_t y) {
__int64_t ret;

View file

@ -15,7 +15,7 @@ fix16_t fix16_add(fix16_t a, fix16_t b)
// Overflow can only happen if sign of a == sign of b, and then
// it causes sign of sum != sign of a.
if (!((_a ^ _b) & 0x80000000) && ((_a ^ sum) & 0x80000000))
return fix16_overflow;
return FIX16_OVERFLOW;
return sum;
}
@ -28,7 +28,7 @@ fix16_t fix16_sub(fix16_t a, fix16_t b)
// Overflow can only happen if sign of a != sign of b, and then
// it causes sign of diff != sign of a.
if (((_a ^ _b) & 0x80000000) && ((_a ^ diff) & 0x80000000))
return fix16_overflow;
return FIX16_OVERFLOW;
return diff;
}
@ -38,8 +38,8 @@ fix16_t fix16_sadd(fix16_t a, fix16_t b)
{
fix16_t result = fix16_add(a, b);
if (result == fix16_overflow)
return (a >= 0) ? fix16_maximum : fix16_minimum;
if (result == FIX16_OVERFLOW)
return (a >= 0) ? FIX16_MAX : FIX16_MIN;
return result;
}
@ -48,8 +48,8 @@ fix16_t fix16_ssub(fix16_t a, fix16_t b)
{
fix16_t result = fix16_sub(a, b);
if (result == fix16_overflow)
return (a >= 0) ? fix16_maximum : fix16_minimum;
if (result == FIX16_OVERFLOW)
return (a >= 0) ? FIX16_MAX : FIX16_MIN;
return result;
}
@ -76,7 +76,7 @@ fix16_t fix16_mul(fix16_t inArg0, fix16_t inArg1)
{
#ifndef FIXMATH_NO_OVERFLOW
if (~upper)
return fix16_overflow;
return FIX16_OVERFLOW;
#endif
#ifndef FIXMATH_NO_ROUNDING
@ -88,7 +88,7 @@ fix16_t fix16_mul(fix16_t inArg0, fix16_t inArg1)
{
#ifndef FIXMATH_NO_OVERFLOW
if (upper)
return fix16_overflow;
return FIX16_OVERFLOW;
#endif
}
@ -108,12 +108,12 @@ fix16_t fix16_smul(fix16_t inArg0, fix16_t inArg1)
{
fix16_t result = fix16_mul(inArg0, inArg1);
if (result == fix16_overflow)
if (result == FIX16_OVERFLOW)
{
if ((inArg0 >= 0) == (inArg1 >= 0))
return fix16_maximum;
return FIX16_MAX;
else
return fix16_minimum;
return FIX16_MIN;
}
return result;
@ -144,7 +144,7 @@ fix16_t fix16_div(fix16_t a, fix16_t b)
// computed all the bits in (a<<17)/b. Usually this takes 1-3 iterations.
if (b == 0)
return fix16_minimum;
return FIX16_MIN;
uint32_t remainder = (a >= 0) ? a : (-a);
uint32_t divider = (b >= 0) ? b : (-b);
@ -182,7 +182,7 @@ fix16_t fix16_div(fix16_t a, fix16_t b)
#ifndef FIXMATH_NO_OVERFLOW
if (div & ~(0xFFFFFFFF >> bit_pos))
return fix16_overflow;
return FIX16_OVERFLOW;
#endif
remainder <<= 1;
@ -200,8 +200,8 @@ fix16_t fix16_div(fix16_t a, fix16_t b)
if ((a ^ b) & 0x80000000)
{
#ifndef FIXMATH_NO_OVERFLOW
if (result == fix16_minimum)
return fix16_overflow;
if (result == FIX16_MIN)
return FIX16_OVERFLOW;
#endif
result = -result;
@ -216,20 +216,18 @@ fix16_t fix16_sdiv(fix16_t inArg0, fix16_t inArg1)
{
fix16_t result = fix16_div(inArg0, inArg1);
if (result == fix16_overflow)
if (result == FIX16_OVERFLOW)
{
if ((inArg0 >= 0) == (inArg1 >= 0))
return fix16_maximum;
return FIX16_MAX;
else
return fix16_minimum;
return FIX16_MIN;
}
return result;
}
#endif
fix16_t fix16_mod(fix16_t x, fix16_t y) { return x %= y; }
#ifndef FIXMATH_NO_64BIT

View file

@ -79,7 +79,7 @@ fix16_t fix16_from_str(const char *buf)
if (count == 0 || count > 5
|| intpart > 32768 || (!negative && intpart > 32767))
return fix16_overflow;
return FIX16_OVERFLOW;
fix16_t value = intpart << 16;
@ -104,7 +104,7 @@ fix16_t fix16_from_str(const char *buf)
while (*buf != '\0')
{
if (!isdigit(*buf) && !isspace(*buf))
return fix16_overflow;
return FIX16_OVERFLOW;
buf++;
}