Skip to content

Commit

Permalink
Add more informative panic to GridRotation::transform_vector.
Browse files Browse the repository at this point in the history
In addition to useful information, this will simplify the machine code
by panicking only in one place rather than three.
  • Loading branch information
kpreid committed Dec 8, 2023
1 parent 2d46ad2 commit 2b14d85
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions all-is-cubes/src/math/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GridVector> {
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,
Expand Down

0 comments on commit 2b14d85

Please sign in to comment.