Merge branch 'improve-point-in-subsector-performance' into 'next'

Avoid branch prediction slowdowns in R_PointOnSide

See merge request STJr/SRB2!2168
This commit is contained in:
Eidolon 2023-10-05 12:26:51 +00:00
commit 219dac16cb

View file

@ -249,7 +249,7 @@ static void FlipCam2_OnChange(void)
//
// killough 5/2/98: reformatted
//
INT32 R_PointOnSide(fixed_t x, fixed_t y, node_t *node)
INT32 R_PointOnSide(fixed_t x, fixed_t y, node_t *restrict node)
{
if (!node->dx)
return x <= node->x ? node->dy > 0 : node->dy < 0;
@ -261,9 +261,10 @@ INT32 R_PointOnSide(fixed_t x, fixed_t y, node_t *node)
fixed_t dy = (y >> 1) - (node->y >> 1);
// Try to quickly decide by looking at sign bits.
if ((node->dy ^ node->dx ^ dx ^ dy) < 0)
return (node->dy ^ dx) < 0; // (left is negative)
return FixedMul(dy, node->dx>>FRACBITS) >= FixedMul(node->dy>>FRACBITS, dx);
// also use a mask to avoid branch prediction
INT32 mask = (node->dy ^ node->dx ^ dx ^ dy) >> 31;
return (mask & ((node->dy ^ dx) < 0)) | // (left is negative)
(~mask & (FixedMul(dy, node->dx>>FRACBITS) >= FixedMul(node->dy>>FRACBITS, dx)));
}
// killough 5/2/98: reformatted