From 2b14d85eca5ff8fe9af4d1cd8b93172c75fe8004 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Fri, 8 Dec 2023 10:32:13 -0800 Subject: [PATCH] Add more informative panic to `GridRotation::transform_vector`. In addition to useful information, this will simplify the machine code by panicking only in one place rather than three. --- all-is-cubes/src/math/rotation.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/all-is-cubes/src/math/rotation.rs b/all-is-cubes/src/math/rotation.rs index 3dfd151be..afc5852b5 100644 --- a/all-is-cubes/src/math/rotation.rs +++ b/all-is-cubes/src/math/rotation.rs @@ -361,13 +361,27 @@ impl GridRotation { #[inline] #[track_caller] // TODO: temporary for troubleshooting a panic in fuzzing pub fn transform_vector(self, vector: GridVector) -> GridVector { - let basis = self.to_basis(); + #[inline(always)] + fn inner(rotation: GridRotation, vector: GridVector) -> Option { + let basis = rotation.to_basis(); + + let mut result = GridVector::zero(); + result[basis.x.axis()] = vector.x.checked_mul(basis.x.signum())?; + result[basis.y.axis()] = vector.y.checked_mul(basis.y.signum())?; + result[basis.z.axis()] = vector.z.checked_mul(basis.z.signum())?; - let mut result = GridVector::zero(); - result[basis.x.axis()] = vector.x * basis.x.signum(); - result[basis.y.axis()] = vector.y * basis.y.signum(); - result[basis.z.axis()] = vector.z * basis.z.signum(); - result + Some(result) + } + + // Shared handling to print the vector, and also to generate only one set of panic code + // rather than three. + // TODO: when overflow_checks disabled, don't panic + match inner(self, vector) { + Some(v) => v, + None => panic!( + "overflow due to sign change in GridVector::transform_vector({self:?}, {vector:?})" + ), + } // Implementation note: The following code would seem to be simpler and avoid // a zero initialization,