From 86166f5e67e817143e4dc5424cf71211614de79e Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 1 Nov 2021 20:25:38 +0100 Subject: [PATCH] Revert "- `xs_Float.h`: Make all inlines return an unsigned value, and change previous unsigned inlines to signed." Revert "- `xs_Float.h`: Add `getint()` getter to `_xs_doubleints` struct." Revert "- SW: When adjusting horizon in `DoPlayerDeathHoriz()`, just use integer horizon values and not Q16.16." Revert "- Duke (RR): Clean up some unnecessary `FixedToFloat()` usage with the `fixedhoriz` `asbuildf()` method." Revert "- `binaryangle.h`: Use `constexpr` on inline functions where it was previously not possible to do so." Revert "- `m_fixed.h`: Use `constexpr` on inline functions where it was previously not possible to do so." Revert "- `xs_Float.h`: Convert header to `constexpr`." This does nor work as it violates the constexpr rules for unions. The code will error out on compilation for accessing an inactive member of a union. --- source/common/thirdparty/xs_Float.h | 184 +++++++++++++++------------- source/common/utility/m_fixed.h | 6 +- source/core/binaryangle.h | 18 +-- source/games/duke/src/player_r.cpp | 4 +- source/games/sw/src/player.cpp | 4 +- 5 files changed, 116 insertions(+), 100 deletions(-) diff --git a/source/common/thirdparty/xs_Float.h b/source/common/thirdparty/xs_Float.h index 2692d0de2..6c676ee3a 100644 --- a/source/common/thirdparty/xs_Float.h +++ b/source/common/thirdparty/xs_Float.h @@ -42,16 +42,10 @@ typedef double real64; -struct _xs_doubleints +union _xs_doubleints { - union - { - real64 val; - uint32_t ival[2]; - }; - - constexpr _xs_doubleints(real64 v) : val(v) {} - constexpr uint32_t getint() const { return ival[_xs_iman_]; } + real64 val; + uint32_t ival[2]; }; #if 0 @@ -62,9 +56,73 @@ struct _xs_doubleints // ==================================================================================================================== // Constants // ==================================================================================================================== -constexpr real64 _xs_doublemagic = real64 (6755399441055744.0); //2^52 * 1.5, uses limited precisicion to floor -constexpr real64 _xs_doublemagicdelta = (1.5e-8); //almost .5f = .5f + 1e^(number of exp bit) -constexpr real64 _xs_doublemagicroundeps = (.5f-_xs_doublemagicdelta); //almost .5f = .5f - 1e^(number of exp bit) +const real64 _xs_doublemagic = real64 (6755399441055744.0); //2^52 * 1.5, uses limited precisicion to floor +const real64 _xs_doublemagicdelta = (1.5e-8); //almost .5f = .5f + 1e^(number of exp bit) +const real64 _xs_doublemagicroundeps = (.5f-_xs_doublemagicdelta); //almost .5f = .5f - 1e^(number of exp bit) + + +// ==================================================================================================================== +// Prototypes +// ==================================================================================================================== +static int32_t xs_CRoundToInt (real64 val, real64 dmr = _xs_doublemagic); +static int32_t xs_ToInt (real64 val, real64 dme = -_xs_doublemagicroundeps); +static int32_t xs_FloorToInt (real64 val, real64 dme = _xs_doublemagicroundeps); +static int32_t xs_CeilToInt (real64 val, real64 dme = _xs_doublemagicroundeps); +static int32_t xs_RoundToInt (real64 val); + +//int32_t versions +finline static int32_t xs_CRoundToInt (int32_t val) {return val;} +finline static int32_t xs_ToInt (int32_t val) {return val;} + + + +// ==================================================================================================================== +// Fix Class +// ==================================================================================================================== +template class xs_Fix +{ +public: + typedef int32_t Fix; + + // ==================================================================================================================== + // Basic Conversion from Numbers + // ==================================================================================================================== + finline static Fix ToFix (int32_t val) {return val<>N;} + + + +protected: + // ==================================================================================================================== + // Helper function - mainly to preserve _xs_DEFAULT_CONVERSION + // ==================================================================================================================== + finline static int32_t xs_ConvertToFixed (real64 val) + { + #if _xs_DEFAULT_CONVERSION==0 + return xs_CRoundToInt(val, _xs_doublemagic/(1< class xs_Fix +finline static uint32_t xs_CeilToUInt(real64 val) { -public: - typedef int32_t Fix; + return (uint32_t)xs_CeilToInt(val); +} - // ==================================================================================================================== - // Basic Conversion from Numbers - // ==================================================================================================================== - finline static constexpr Fix ToFix (int32_t val) {return val<>N;} - - - -protected: - // ==================================================================================================================== - // Helper function - mainly to preserve _xs_DEFAULT_CONVERSION - // ==================================================================================================================== - finline static constexpr int32_t xs_ConvertToFixed (real64 val) - { - #if _xs_DEFAULT_CONVERSION==0 - return xs_CRoundToUInt(val, _xs_doublemagic/(1<::ToFix(f); } @@ -32,10 +32,10 @@ inline constexpr double FixedToFloat(fixed_t f) inline constexpr int32_t FixedToInt(fixed_t f) { - return (f + (FRACUNIT >> 1)) >> FRACBITS; + return (f + FRACUNIT/2) >> FRACBITS; } -inline constexpr unsigned FloatToAngle(double f) +inline unsigned FloatToAngle(double f) { return xs_CRoundToInt((f)* (0x40000000 / 90.)); } diff --git a/source/core/binaryangle.h b/source/core/binaryangle.h index 8137b1642..42b45e31a 100644 --- a/source/core/binaryangle.h +++ b/source/core/binaryangle.h @@ -118,9 +118,9 @@ class binangle friend constexpr binangle bamang(uint32_t v); friend constexpr binangle q16ang(uint32_t v); friend constexpr binangle buildang(uint32_t v); - friend constexpr binangle buildfang(double v); - friend constexpr binangle radang(double v); - friend constexpr binangle degang(double v); + friend binangle buildfang(double v); + friend binangle radang(double v); + friend binangle degang(double v); friend FSerializer &Serialize(FSerializer &arc, const char *key, binangle &obj, binangle *defval); @@ -207,9 +207,9 @@ public: inline constexpr binangle bamang(uint32_t v) { return binangle(v); } inline constexpr binangle q16ang(uint32_t v) { return binangle(v << 5); } inline constexpr binangle buildang(uint32_t v) { return binangle(v << BAMBITS); } -inline constexpr binangle buildfang(double v) { return binangle(xs_CRoundToUInt(v * BAMUNIT)); } -inline constexpr binangle radang(double v) { return binangle(xs_CRoundToUInt(v * (0x80000000u / pi::pi()))); } -inline constexpr binangle degang(double v) { return binangle(FloatToAngle(v)); } +inline binangle buildfang(double v) { return binangle(xs_CRoundToUInt(v * BAMUNIT)); } +inline binangle radang(double v) { return binangle(xs_CRoundToUInt(v * (0x80000000u / pi::pi()))); } +inline binangle degang(double v) { return binangle(FloatToAngle(v)); } inline FSerializer &Serialize(FSerializer &arc, const char *key, binangle &obj, binangle *defval) { @@ -226,7 +226,7 @@ inline FSerializer &Serialize(FSerializer &arc, const char *key, binangle &obj, inline double HorizToPitch(double horiz) { return atan2(horiz, 128) * (180. / pi::pi()); } inline double HorizToPitch(fixed_t q16horiz) { return atan2(q16horiz, IntToFixed(128)) * (180. / pi::pi()); } inline fixed_t PitchToHoriz(double pitch) { return xs_CRoundToInt(IntToFixed(128) * tan(pitch * (pi::pi() / 180.))); } -inline constexpr int32_t PitchToBAM(double pitch) { return int(clamp(pitch * (1073741823.5 / 45.), -INT32_MAX, INT32_MAX)); } +inline int32_t PitchToBAM(double pitch) { return xs_CRoundToInt(clamp(pitch * (1073741823.5 / 45.), -INT32_MAX, INT32_MAX)); } inline constexpr double BAMToPitch(int32_t bam) { return bam * (45. / 1073741823.5); } @@ -244,7 +244,7 @@ class fixedhoriz friend constexpr fixedhoriz q16horiz(fixed_t v); friend constexpr fixedhoriz buildhoriz(int v); - friend constexpr fixedhoriz buildfhoriz(double v); + friend fixedhoriz buildfhoriz(double v); friend fixedhoriz pitchhoriz(double v); friend fixedhoriz bamhoriz(int32_t v); @@ -343,7 +343,7 @@ public: inline constexpr fixedhoriz q16horiz(fixed_t v) { return fixedhoriz(v); } inline constexpr fixedhoriz buildhoriz(int v) { return fixedhoriz(IntToFixed(v)); } -inline constexpr fixedhoriz buildfhoriz(double v) { return fixedhoriz(FloatToFixed(v)); } +inline fixedhoriz buildfhoriz(double v) { return fixedhoriz(FloatToFixed(v)); } inline fixedhoriz pitchhoriz(double v) { return fixedhoriz(PitchToHoriz(v)); } inline fixedhoriz bamhoriz(int32_t v) { return pitchhoriz(BAMToPitch(v)); } diff --git a/source/games/duke/src/player_r.cpp b/source/games/duke/src/player_r.cpp index 6027be8ab..e0abf909e 100644 --- a/source/games/duke/src/player_r.cpp +++ b/source/games/duke/src/player_r.cpp @@ -1742,7 +1742,7 @@ static void onMotorcycle(int snum, ESyncBits &actions) } if (horiz != FRACUNIT) { - p->horizon.addadjustment(horiz - p->horizon.horiz.asbuildf()); + p->horizon.addadjustment(horiz - FixedToFloat(p->horizon.horiz.asq16())); } int currSpeed = int(p->MotoSpeed); @@ -2010,7 +2010,7 @@ static void onBoat(int snum, ESyncBits &actions) } if (horiz != FRACUNIT) { - p->horizon.addadjustment(horiz - p->horizon.horiz.asbuildf()); + p->horizon.addadjustment(horiz - FixedToFloat(p->horizon.horiz.asq16())); } if (p->MotoSpeed > 0 && p->on_ground == 1 && (p->vehTurnLeft || p->vehTurnRight)) diff --git a/source/games/sw/src/player.cpp b/source/games/sw/src/player.cpp index 32efb8132..e6acd7fc3 100644 --- a/source/games/sw/src/player.cpp +++ b/source/games/sw/src/player.cpp @@ -6038,12 +6038,12 @@ DoPlayerBeginDie(PLAYERp pp) void DoPlayerDeathHoriz(PLAYERp pp, short target, short speed) { - if ((pp->horizon.horiz.asbuild() - target) > 1) + if ((pp->horizon.horiz.asq16() - IntToFixed(target)) > FRACUNIT) { pp->horizon.addadjustment(-speed); } - if ((target - pp->horizon.horiz.asbuild()) > 1) + if ((IntToFixed(target) - pp->horizon.horiz.asq16()) > FRACUNIT) { pp->horizon.addadjustment(speed); }