diff --git a/src/p_maputl.c b/src/p_maputl.c index 0e9c2d12c..17df33a91 100644 --- a/src/p_maputl.c +++ b/src/p_maputl.c @@ -136,10 +136,33 @@ 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, line_t *line) { - return - !line->dx ? x <= line->v1->x ? line->dy > 0 : line->dy < 0 : - !line->dy ? y <= line->v1->y ? line->dx < 0 : line->dx > 0 : - ((long long) y - line->v1->y) * line->dx >= ((long long) x - line->v1->x) * line->dy; + const vertex_t *v1 = line->v1; + fixed_t dx, dy, left, right; + + if (!line->dx) + { + if (x <= v1->x) + return (line->dy > 0); + + return (line->dy < 0); + } + if (!line->dy) + { + if (y <= v1->y) + return (line->dx < 0); + + return (line->dx > 0); + } + + dx = (x - v1->x); + dy = (y - v1->y); + + left = FixedMul(line->dy>>FRACBITS, dx); + right = FixedMul(dy, line->dx>>FRACBITS); + + if (right < left) + return 0; // front side + return 1; // back side } // @@ -149,27 +172,48 @@ INT32 P_PointOnLineSide(fixed_t x, fixed_t y, line_t *line) // INT32 P_BoxOnLineSide(fixed_t *tmbox, line_t *ld) { - switch (ld->slopetype) - { - int p; - default: // shut up compiler warnings -- killough - case ST_HORIZONTAL: - return - (tmbox[BOXBOTTOM] > ld->v1->y) == (p = tmbox[BOXTOP] > ld->v1->y) ? - p ^ (ld->dx < 0) : -1; - case ST_VERTICAL: - return - (tmbox[BOXLEFT] < ld->v1->x) == (p = tmbox[BOXRIGHT] < ld->v1->x) ? - p ^ (ld->dy < 0) : -1; - case ST_POSITIVE: - return - P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXBOTTOM], ld) == - (p = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXTOP], ld)) ? p : -1; - case ST_NEGATIVE: - return - (P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXBOTTOM], ld)) == - (p = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXTOP], ld)) ? p : -1; - } + INT32 p1, p2; + + switch (ld->slopetype) + { + case ST_HORIZONTAL: + p1 = tmbox[BOXTOP] > ld->v1->y; + p2 = tmbox[BOXBOTTOM] > ld->v1->y; + if (ld->dx < 0) + { + p1 ^= 1; + p2 ^= 1; + } + break; + + case ST_VERTICAL: + p1 = tmbox[BOXRIGHT] < ld->v1->x; + p2 = tmbox[BOXLEFT] < ld->v1->x; + if (ld->dy < 0) + { + p1 ^= 1; + p2 ^= 1; + } + break; + + case ST_POSITIVE: + p1 = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXTOP], ld); + p2 = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXBOTTOM], ld); + break; + + case ST_NEGATIVE: + p1 = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXTOP], ld); + p2 = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXBOTTOM], ld); + break; + + default: + I_Error("P_BoxOnLineSide: unknown slopetype %d\n", ld->slopetype); + return -1; + } + + if (p1 == p2) + return p1; + return -1; } // @@ -178,11 +222,40 @@ INT32 P_BoxOnLineSide(fixed_t *tmbox, line_t *ld) // static INT32 P_PointOnDivlineSide(fixed_t x, fixed_t y, divline_t *line) { - return - !line->dx ? x <= line->x ? line->dy > 0 : line->dy < 0 : - !line->dy ? y <= line->y ? line->dx < 0 : line->dx > 0 : - (line->dy^line->dx^(x -= line->x)^(y -= line->y)) < 0 ? (line->dy^x) < 0 : - (long long) y * line->dx >= (long long) x * line->dy; + fixed_t dx, dy, left, right; + + if (!line->dx) + { + if (x <= line->x) + return line->dy > 0; + + return line->dy < 0; + } + if (!line->dy) + { + if (y <= line->y) + return line->dx < 0; + + return line->dx > 0; + } + + 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) + { + if ((line->dy ^ dx) & 0x80000000) + return 1; // left is negative + return 0; + } + + left = FixedMul(line->dy>>8, dx>>8); + right = FixedMul(dy>>8, line->dx>>8); + + if (right < left) + return 0; // front side + return 1; // back side } // @@ -203,13 +276,18 @@ void P_MakeDivline(line_t *li, divline_t *dl) // fixed_t P_InterceptVector(divline_t *v2, divline_t *v1) { - /* cph - This was introduced at prboom_4_compatibility - no precision/overflow problems */ - int64_t den = (int64_t)v1->dy * v2->dx - (int64_t)v1->dx * v2->dy; - den >>= 16; - if (!den) - return 0; - return (fixed_t)(((int64_t)(v1->x - v2->x) * v1->dy - (int64_t)(v1->y - v2->y) * v1->dx) / den); - } + fixed_t frac, num, den; + + den = FixedMul(v1->dy>>8, v2->dx) - FixedMul(v1->dx>>8, v2->dy); + + if (!den) + return 0; + + num = FixedMul((v1->x - v2->x)>>8, v1->dy) + FixedMul((v2->y - v1->y)>>8, v1->dx); + frac = FixedDiv(num, den); + + return frac; +} // // P_LineOpening diff --git a/src/r_bsp.c b/src/r_bsp.c index b2f0d42ee..2de30d2ab 100644 --- a/src/r_bsp.c +++ b/src/r_bsp.c @@ -410,8 +410,8 @@ static void R_AddLine(seg_t *line) return; // big room fix - angle1 = R_PointToAngleEx(viewx, viewy, line->v1->x, line->v1->y); - angle2 = R_PointToAngleEx(viewx, viewy, line->v2->x, line->v2->y); + angle1 = R_PointToAngle64(line->v1->x, line->v1->y); + angle2 = R_PointToAngle64(line->v2->x, line->v2->y); curline = line; // Clip to view edges. @@ -613,8 +613,8 @@ static boolean R_CheckBBox(const fixed_t *bspcoord) check = checkcoord[boxpos]; // big room fix - angle1 = R_PointToAngleEx(viewx, viewy, bspcoord[check[0]], bspcoord[check[1]]) - viewangle; - angle2 = R_PointToAngleEx(viewx, viewy, bspcoord[check[2]], bspcoord[check[3]]) - viewangle; + angle1 = R_PointToAngle64(bspcoord[check[0]], bspcoord[check[1]]) - viewangle; + angle2 = R_PointToAngle64(bspcoord[check[2]], bspcoord[check[3]]) - viewangle; if ((signed)angle1 < (signed)angle2) { diff --git a/src/r_main.c b/src/r_main.c index 60616811b..a62c441f2 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -329,18 +329,19 @@ static void FlipCam4_OnChange(void) FUNCINLINE ATTRINLINE 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; + return x <= node->x ? node->dy > 0 : node->dy < 0; if (!node->dy) - return y <= node->y ? node->dx < 0 : node->dx > 0; + return y <= node->y ? node->dx < 0 : node->dx > 0; x -= node->x; y -= node->y; - // Try to quickly decide by looking at sign bits. - if ((node->dy ^ node->dx ^ x ^ y) < 0) - return (node->dy ^ x) < 0; // (left is negative) - return (long long) y * node->dx >= (long long) x * node->dy; + // Try to quickly decide by looking at sign bits. + // also use a mask to avoid branch prediction + INT32 mask = (node->dy ^ node->dx ^ x ^ y) >> 31; + 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 @@ -363,7 +364,7 @@ FUNCINLINE ATTRINLINE INT32 R_PointOnSegSide(fixed_t x, fixed_t y, seg_t *line) // Try to quickly decide by looking at sign bits. if ((ldy ^ ldx ^ x ^ y) < 0) return (ldy ^ x) < 0; // (left is negative) - return (long long) y * ldx >= (long long) x * ldy; + return FixedMul(y, ldx>>FRACBITS) >= FixedMul(ldy>>FRACBITS, x); } // @@ -395,6 +396,23 @@ FUNCINLINE ATTRINLINE angle_t R_PointToAngle(fixed_t x, fixed_t y) 0; } +// This version uses 64-bit variables to avoid overflows with large values. +angle_t R_PointToAngle64(INT64 x, INT64 y) +{ + return (y -= viewy, (x -= viewx) || y) ? + x >= 0 ? + y >= 0 ? + (x > y) ? tantoangle[SlopeDivEx(y,x)] : // octant 0 + ANGLE_90-tantoangle[SlopeDivEx(x,y)] : // octant 1 + x > (y = -y) ? 0-tantoangle[SlopeDivEx(y,x)] : // octant 8 + ANGLE_270+tantoangle[SlopeDivEx(x,y)] : // octant 7 + y >= 0 ? (x = -x) > y ? ANGLE_180-tantoangle[SlopeDivEx(y,x)] : // octant 3 + ANGLE_90 + tantoangle[SlopeDivEx(x,y)] : // octant 2 + (x = -x) > (y = -y) ? ANGLE_180+tantoangle[SlopeDivEx(y,x)] : // octant 4 + ANGLE_270-tantoangle[SlopeDivEx(x,y)] : // octant 5 + 0; +} + angle_t R_PointToAngle2(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y) { return (y -= pviewy, (x -= pviewx) || y) ? @@ -1106,10 +1124,6 @@ boolean R_IsPointInSector(sector_t *sector, fixed_t x, fixed_t y) FUNCINLINE ATTRINLINE subsector_t *R_PointInSubsector(fixed_t x, fixed_t y) { size_t nodenum = numnodes-1; - - // special case for trivial maps (single subsector, no nodes) - if (numnodes == 0) - return subsectors; while (!(nodenum & NF_SUBSECTOR)) nodenum = nodes[nodenum].children[R_PointOnSide(x, y, nodes+nodenum)]; diff --git a/src/r_main.h b/src/r_main.h index 826ece458..5fbd00142 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -68,6 +68,7 @@ extern lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ]; INT32 R_PointOnSide(fixed_t x, fixed_t y, node_t *node); INT32 R_PointOnSegSide(fixed_t x, fixed_t y, seg_t *line); angle_t R_PointToAngle(fixed_t x, fixed_t y); +angle_t R_PointToAngle64(INT64 x, INT64 y); angle_t R_PointToAngle2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1); angle_t R_PointToAngleEx(INT32 x2, INT32 y2, INT32 x1, INT32 y1); angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y);