diff --git a/src/box2d.rs b/src/box2d.rs index d3b30c4..825f585 100644 --- a/src/box2d.rs +++ b/src/box2d.rs @@ -161,10 +161,11 @@ where /// Returns `true` if the two boxes intersect. #[inline] pub fn intersects(&self, other: &Self) -> bool { - self.min.x < other.max.x - && self.max.x > other.min.x - && self.min.y < other.max.y - && self.max.y > other.min.y + // Use bitwise and instead of && to avoid emitting branches. + (self.min.x < other.max.x) + & (self.max.x > other.min.x) + & (self.min.y < other.max.y) + & (self.max.y > other.min.y) } /// Returns `true` if this box2d contains the point `p`. A point is considered @@ -172,14 +173,16 @@ where /// on the right or bottom edges. #[inline] pub fn contains(&self, p: Point2D) -> bool { - self.min.x <= p.x && p.x < self.max.x && self.min.y <= p.y && p.y < self.max.y + // Use bitwise and instead of && to avoid emitting branches. + (self.min.x <= p.x) & (p.x < self.max.x) & (self.min.y <= p.y) & (p.y < self.max.y) } /// Returns `true` if this box contains the point `p`. A point is considered /// in the box2d if it lies on any edge of the box2d. #[inline] pub fn contains_inclusive(&self, p: Point2D) -> bool { - self.min.x <= p.x && p.x <= self.max.x && self.min.y <= p.y && p.y <= self.max.y + // Use bitwise and instead of && to avoid emitting branches. + (self.min.x <= p.x) & (p.x <= self.max.x) & (self.min.y <= p.y) & (p.y <= self.max.y) } /// Returns `true` if this box contains the interior of the other box. Always @@ -188,10 +191,10 @@ where #[inline] pub fn contains_box(&self, other: &Self) -> bool { other.is_empty() - || (self.min.x <= other.min.x - && other.max.x <= self.max.x - && self.min.y <= other.min.y - && other.max.y <= self.max.y) + || ((self.min.x <= other.min.x) + & (other.max.x <= self.max.x) + & (self.min.y <= other.min.y) + & (other.max.y <= self.max.y)) } } diff --git a/src/box3d.rs b/src/box3d.rs index ac43ff1..710c862 100644 --- a/src/box3d.rs +++ b/src/box3d.rs @@ -147,24 +147,24 @@ where /// on the back, right or bottom faces. #[inline] pub fn contains(&self, other: Point3D) -> bool { - self.min.x <= other.x - && other.x < self.max.x - && self.min.y <= other.y - && other.y < self.max.y - && self.min.z <= other.z - && other.z < self.max.z + (self.min.x <= other.x) + & (other.x < self.max.x) + & (self.min.y <= other.y) + & (other.y < self.max.y) + & (self.min.z <= other.z) + & (other.z < self.max.z) } /// Returns `true` if this box3d contains the point `p`. A point is considered /// in the box3d if it lies on any face of the box3d. #[inline] pub fn contains_inclusive(&self, other: Point3D) -> bool { - self.min.x <= other.x - && other.x <= self.max.x - && self.min.y <= other.y - && other.y <= self.max.y - && self.min.z <= other.z - && other.z <= self.max.z + (self.min.x <= other.x) + & (other.x <= self.max.x) + & (self.min.y <= other.y) + & (other.y <= self.max.y) + & (self.min.z <= other.z) + & (other.z <= self.max.z) } /// Returns `true` if this box3d contains the interior of the other box3d. Always @@ -173,12 +173,12 @@ where #[inline] pub fn contains_box(&self, other: &Self) -> bool { other.is_empty() - || (self.min.x <= other.min.x - && other.max.x <= self.max.x - && self.min.y <= other.min.y - && other.max.y <= self.max.y - && self.min.z <= other.min.z - && other.max.z <= self.max.z) + || ((self.min.x <= other.min.x) + & (other.max.x <= self.max.x) + & (self.min.y <= other.min.y) + & (other.max.y <= self.max.y) + & (self.min.z <= other.min.z) + & (other.max.z <= self.max.z)) } }