From f95c751ff233a02857b220ba303ac5e98ecf9716 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 2 Sep 2024 14:22:58 +0200 Subject: [PATCH] optimize pointon functions --- src/p_maputl.c | 29 ++++++++--------------------- src/r_main.c | 7 ++++--- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/p_maputl.c b/src/p_maputl.c index fc7ea89ec..63430f35f 100644 --- a/src/p_maputl.c +++ b/src/p_maputl.c @@ -136,21 +136,16 @@ void P_ClosestPointOnLine3D(fixed_t x, fixed_t y, fixed_t z, line_t *line, verte // FUNCINLINE ATTRINLINE PUREFUNC INT32 P_PointOnLineSide(fixed_t x, fixed_t y, const line_t *line) { - fixed_t dx, dy, left, right; - if (!line->dx) return x <= line->v1->x ? line->dy > 0 : line->dy < 0; if (!line->dy) return y <= line->v1->y ? line->dx < 0 : line->dx > 0; - dx = (x - line->v1->x); - dy = (y - line->v1->y); + x -= line->v1->x; + y -= line->v1->y; - left = FixedMul(line->dy>>FRACBITS, dx); - right = FixedMul(dy, line->dx>>FRACBITS); - - return right < left ? 0 : 1; + return FixedMul(y, line->dx>>FRACBITS) > FixedMul(line->dy>>FRACBITS, x); } // @@ -210,28 +205,20 @@ PUREFUNC INT32 P_BoxOnLineSide(fixed_t *tmbox, const line_t *ld) // FUNCINLINE static ATTRINLINE PUREFUNC INT32 P_PointOnDivlineSide(fixed_t x, fixed_t y, const divline_t *line) { - fixed_t dx, dy, left, right; - if (!line->dx) return x <= line->x ? line->dy > 0 : line->dy < 0; if (!line->dy) return y <= line->y ? line->dx < 0 : line->dx > 0; - dx = (x - line->x); - dy = (y - line->y); + x -= line->x; + 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; + INT32 mask = ((line->dy ^ line->dx ^ x ^ y) & 0x80000000); + return (mask & ((line->dy ^ x) & 0x80000000)) | + (~mask & (FixedMul(y>>8, line->dx>>8) > FixedMul(line->dy>>8, x>>8))); } - // // P_MakeDivline // diff --git a/src/r_main.c b/src/r_main.c index 8ea5bdd7d..834a28bb3 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -400,9 +400,10 @@ FUNCINLINE ATTRINLINE PUREFUNC INT32 R_PointOnSegSide(fixed_t x, fixed_t y, cons y -= ly; // Try to quickly decide by looking at sign bits. - if ((ldy ^ ldx ^ x ^ y) < 0) - return (ldy ^ x) < 0; // (left is negative) - return FixedMul(y, ldx>>FRACBITS) >= FixedMul(ldy>>FRACBITS, x); + // also use a mask to avoid branch prediction + INT32 mask = (ldy ^ ldx ^ x ^ y) >> 31; + return (mask & ((ldy ^ x) < 0)) | // (left is negative) + (~mask & (FixedMul(y, ldx>>FRACBITS) >= FixedMul(ldy>>FRACBITS, x))); } //