software renderer: handle some float type related issues with slope drawers

there were a bunch of cases where inf and nan float results were casted and used with integer things, which well is undefined behavior in c

yes i did use ceepeepee for that crap, cause the cases i found both gladly were ceepeepee files and this wouldve been really annoying without templating types
if someone really wants that crap to use outside ceepeepee, feel free to make a bunch of wrappers cause im too lazy for that stuff
This commit is contained in:
Alug committed 2025-11-05 23:42:06 +01:00
1 parent 37c54d7abe
commit d85011cd0f
3 files changed
+85 -14

No files matched your search

+67
View File
@@ -147,6 +147,73 @@ public:
template <class T>
NotNull(T) -> NotNull<T>;
// does a super safe cast from floating point to interger types
// this prob could be better since i suck at ceepeepee
// but atleast we can just template shit to our desired types
template <typename FloatT, typename IntT>
static inline IntT floattoint(FloatT val)
{
// :chaosleep:
static_assert(std::is_floating_point_v<FloatT>, "Input must be a floating-point type");
static_assert(std::is_integral_v<IntT>, "Output must be a integer type");
if (std::fpclassify(val) == FP_NAN)
return 0; // if stuffs not a number we sure screwed up lmao
// treat inifinity as "int min/max"
if (std::fpclassify(val) == FP_INFINITE)
{
// yes infinity can be signed kek
if (std::signbit(val))
return std::numeric_limits<IntT>::min(); // negative infinity, so return min
else
return std::numeric_limits<IntT>::max();
}
// make sure we fit into our targettype uwu
if (val > static_cast<FloatT>(std::numeric_limits<IntT>::max()))
return std::numeric_limits<IntT>::max();
else if (val < static_cast<FloatT>(std::numeric_limits<IntT>::min()))
return std::numeric_limits<IntT>::min();
return static_cast<IntT>(val);
}
// safe float to fixed
// which handles nan and inf
// pretty much the same stuff as the above, but uh, idk how id use the above with this
template <typename FloatT> // can use both float and doubles yay
static inline int32_t floattofixed(FloatT val)
{
// :chaosleep:
static_assert(std::is_floating_point_v<FloatT>, "Input must be a floating-point type");
// fixed_t is just an int32_t, i dont wanna include doomtype here if it can be avoided
if (std::fpclassify(val) == FP_NAN)
return 0; // if stuffs not a number we sure screwed up lmao
// treat inifinity as "int min/max"
if (std::fpclassify(val) == FP_INFINITE)
{
// yes infinity can be signed kek
if (std::signbit(val))
return std::numeric_limits<int32_t>::min(); // negative infinity, so return min
else
return std::numeric_limits<int32_t>::max();
}
const FloatT ret = (val * 65536.0);
// make sure we fit into our targettype uwu
if (ret > static_cast<FloatT>(std::numeric_limits<int32_t>::max()))
return std::numeric_limits<int32_t>::max();
else if (ret < static_cast<FloatT>(std::numeric_limits<int32_t>::min()))
return std::numeric_limits<int32_t>::min();
return static_cast<int32_t>(ret);
}
} // namespace srb2
#endif // __SRB2_CXXUTIL_HPP__
+12 -10
View File
@@ -28,6 +28,8 @@
#include <vector>
#include <algorithm>
#include "cxxutil.hpp"
// ==========================================================================
// SPANS
// ==========================================================================
@@ -315,10 +317,10 @@ static void R_DrawTiltedSpanTemplate(drawspandata_t* ds)
endz = 1.f/iz;
endu = uz*endz;
endv = vz*endz;
stepu = (INT64)((endu - startu) * INVSPAN);
stepv = (INT64)((endv - startv) * INVSPAN);
u = (INT64)(startu);
v = (INT64)(startv);
stepu = srb2::floattoint<float, INT64>((endu - startu) * INVSPAN);
stepv = srb2::floattoint<float, INT64>((endv - startv) * INVSPAN);
u = srb2::floattoint<float, INT64>(startu);
v = srb2::floattoint<float, INT64>(startv);
const INT32 x1 = ds->x1;
@@ -345,8 +347,8 @@ static void R_DrawTiltedSpanTemplate(drawspandata_t* ds)
{
if (width == 1)
{
u = (INT64)(startu);
v = (INT64)(startv);
u = srb2::floattoint<float, INT64>(startu);
v = srb2::floattoint<float, INT64>(startv);
bit = ((v >> nflatyshift) & nflatmask) | (u >> nflatxshift);
colormap = planezlight[tiltlight[ds->x1]] + colormap_diff;
if constexpr (Type & DS_RIPPLE)
@@ -366,10 +368,10 @@ static void R_DrawTiltedSpanTemplate(drawspandata_t* ds)
endu = uz*endz;
endv = vz*endz;
left = 1.f/left;
stepu = (INT64)((endu - startu) * left);
stepv = (INT64)((endv - startv) * left);
u = (INT64)(startu);
v = (INT64)(startv);
stepu = srb2::floattoint<float, INT64>((endu - startu) * left);
stepv = srb2::floattoint<float, INT64>((endv - startv) * left);
u = srb2::floattoint<float, INT64>(startu);
v = srb2::floattoint<float, INT64>(startv);
for (; width != 0; width--)
{
+6 -4
View File
@@ -30,6 +30,8 @@
#include "core/memory.h"
#include "cxxutil.hpp"
#define HEIGHTBITS 12
#define HEIGHTUNIT (1<<HEIGHTBITS)
@@ -1890,8 +1892,8 @@ void R_StoreWallRange(INT32 start, INT32 stop)
det = a1*b2 - a2*b1;
ds_p->leftpos.x = segleft.x = DoubleToFixed((b2*c1 - b1*c2)/det);
ds_p->leftpos.y = segleft.y = DoubleToFixed((a1*c2 - a2*c1)/det);
ds_p->leftpos.x = segleft.x = srb2::floattofixed<double>((b2*c1 - b1*c2)/det);
ds_p->leftpos.y = segleft.y = srb2::floattofixed<double>((a1*c2 - a2*c1)/det);
// right
temp = xtoviewangle[stop]+viewangle;
@@ -1902,8 +1904,8 @@ void R_StoreWallRange(INT32 start, INT32 stop)
det = a1*b2 - a2*b1;
ds_p->rightpos.x = segright.x = DoubleToFixed((b2*c1 - b1*c2)/det);
ds_p->rightpos.y = segright.y = DoubleToFixed((a1*c2 - a2*c1)/det);
ds_p->rightpos.x = segright.x = srb2::floattofixed<double>((b2*c1 - b1*c2)/det);
ds_p->rightpos.y = segright.y = srb2::floattofixed<double>((a1*c2 - a2*c1)/det);
}
#define SLOPEPARAMS(slope, end1, end2, normalheight) \