Avoid branch prediction slowdowns in R_PointOnSide

# Conflicts:
#	src/r_main.c
This commit is contained in:
Gustaf Alhäll 2023-10-01 17:34:13 +02:00 committed by Eidolon
parent bcc8fc6438
commit 2f01c6fa16

View file

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