optimize pointon functions

This commit is contained in:
Alug committed 2024-09-02 14:22:58 +02:00
1 parent eed32527cd
commit f95c751ff2
2 files changed
+12 -24

No files matched your search

+8 -21
View File
@@ -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
//
+4 -3
View File
@@ -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)));
}
//