From 7b26ecb5f559469da045ce39097db9d407030106 Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 19 Nov 2024 20:18:16 +0100 Subject: [PATCH] fixed float to integer conversion problems from dsda doom should hopefully make this behave the same everywhere --- src/hardware/hw_clip.c | 4 ++-- src/m_fixed.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/hardware/hw_clip.c b/src/hardware/hw_clip.c index 2c6e1d52e..12d870ea4 100644 --- a/src/hardware/hw_clip.c +++ b/src/hardware/hw_clip.c @@ -337,7 +337,7 @@ angle_t gld_FrustumAngle(angle_t tiltangle) floatangle = 2.0f + (45.0f + (tilt / 1.9f)) * clipfov / 90.0f; if (floatangle >= 180.0) return 0xffffffff; - a1 = (angle_t)(ANG1 * (int)floatangle); + a1 = (angle_t)xs_CRoundToInt(ANG1 * floatangle); return a1; } @@ -458,4 +458,4 @@ boolean gld_SphereInFrustum(float x, float y, float z, float radius) } return true; } -#endif \ No newline at end of file +#endif diff --git a/src/m_fixed.h b/src/m_fixed.h index d67777162..3f1acd7ae 100644 --- a/src/m_fixed.h +++ b/src/m_fixed.h @@ -247,6 +247,35 @@ FUNCMATH FUNCINLINE static ATTRINLINE fixed_t FixedRound(fixed_t x) return INT32_MAX; } +/** \brief The xs_CRoundToInt function + + \param val double number + + \return Round toward nearest, but ties round toward even +*/ +#if defined(SRB2_BIG_ENDIAN) + #define _xs_iexp_ 0 + #define _xs_iman_ 1 +#else + #define _xs_iexp_ 1 //intel is little endian + #define _xs_iman_ 0 +#endif //BigEndian_ + +typedef double real64; + +typedef union _xs_doubleints +{ + real64 val; + unsigned ival[2]; +} _xs_doubleints; + +FUNCMATH FUNCINLINE static ATTRINLINE int xs_CRoundToInt(real64 val) +{ + _xs_doubleints uval; + uval.val = val + 6755399441055744.0; + return uval.ival[_xs_iman_]; +} + typedef struct { fixed_t x;