From 2d5cf03ceba24ca76bf0f93b2007aa11b8c2b20e Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 18 Jan 2025 11:44:34 +0100 Subject: [PATCH] attempt optimizing P_PointOn stuff again this time non inversed and wrong lel --- src/p_maputl.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/p_maputl.c b/src/p_maputl.c index 13dd85592..62a50656c 100644 --- a/src/p_maputl.c +++ b/src/p_maputl.c @@ -136,7 +136,7 @@ void P_ClosestPointOnLine3D(fixed_t x, fixed_t y, fixed_t z, line_t *line, verte // INT32 P_PointOnLineSide(fixed_t x, fixed_t y, const line_t *line) { - fixed_t dx, dy, left, right; + fixed_t dx, dy; if (!line->dx) return x <= line->v1->x ? line->dy > 0 : line->dy < 0; @@ -147,10 +147,7 @@ INT32 P_PointOnLineSide(fixed_t x, fixed_t y, const line_t *line) dx = (x - line->v1->x); dy = (y - line->v1->y); - left = FixedMul(line->dy>>FRACBITS, dx); - right = FixedMul(dy, line->dx>>FRACBITS); - - return right < left ? 0 : 1; + return FixedMul(dy, line->dx>>FRACBITS) >= FixedMul(line->dy>>FRACBITS, dx) ? 1 : 0; } // @@ -210,7 +207,7 @@ INT32 P_BoxOnLineSide(fixed_t *tmbox, const line_t *ld) // static INT32 P_PointOnDivlineSide(fixed_t x, fixed_t y, divline_t *line) { - fixed_t dx, dy, left, right; + fixed_t dx, dy; if (!line->dx) return x <= line->x ? line->dy > 0 : line->dy < 0; @@ -221,17 +218,13 @@ static INT32 P_PointOnDivlineSide(fixed_t x, fixed_t y, divline_t *line) dx = (x - line->x); dy = (y - line->y); - // try to quickly decide by looking at sign bits - if ((line->dy ^ line->dx ^ dx ^ dy) & 0x80000000) - return ((line->dy ^ dx) & 0x80000000) ? 1 : 0; - - left = FixedMul(line->dy>>8, dx>>8); - right = FixedMul(dy>>8, line->dx>>8); - - return right < left ? 0 : 1; + // Try to quickly decide by looking at sign bits. + // also use a mask to avoid branch prediction + INT32 mask = ((line->dy ^ line->dx ^ dx ^ dy) & 0x80000000); + return ((mask & ((line->dy ^ dx) & 0x80000000)) | // (left is negative) + (~mask & (FixedMul(dy>>8, line->dx>>8) >= FixedMul(line->dy>>8, dx>>8)))) ? 1 : 0; } - // // P_MakeDivline //