shithub: nanobsp

Download patch

ref: 75cce63effe86ed759d7dbbb5755a0117aba2f16
parent: 1e63f7e03429f976fee4e0b493e9bbfe0be40edb
author: Andrew Apted <ajapted@gmail.com>
date: Wed Dec 20 07:27:53 EST 2023

use higher precision for `along` value in BSP_ComputeIntersection().

The previous calculation could produce zero when splitting a long seg
very close to the end, causing a bad split where the new piece was
identical to the old piece, which in turn lead to an infinite loop
which either overflowed the stack or ran out of memory.

--- a/nano_bsp.c
+++ b/nano_bsp.c
@@ -557,17 +557,18 @@
 		}
 	}
 
-	fixed_t along = FixedDiv (a, a + b);
+	// this is higher precision: 2.30 instead of 16.16
+	fixed_t along = ((int64_t)a << 30) / (int64_t)(a + b);
 
 	if (seg->v1->x == seg->v2->x)
 		*x = seg->v1->x;
 	else
-		*x = seg->v1->x + FixedMul (seg->v2->x - seg->v1->x, along);
+		*x = seg->v1->x + (((int64_t)(seg->v2->x - seg->v1->x) * (int64_t)along) >> 30);
 
 	if (seg->v1->y == seg->v2->y)
 		*y = seg->v1->y;
 	else
-		*y = seg->v1->y + FixedMul (seg->v2->y - seg->v1->y, along);
+		*y = seg->v1->y + (((int64_t)(seg->v2->y - seg->v1->y) * (int64_t)along) >> 30);
 }
 
 //
--