Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

`BVH` - std::signbit optimization

lawnjelly534ae8e255c4
diff --git a/core/math/bvh_abb.h b/core/math/bvh_abb.h
index e936a433f77..80e43a90fc0 100644
--- a/core/math/bvh_abb.h
+++ b/core/math/bvh_abb.h
@@ -206,45 +206,49 @@ struct BVH_ABB {
}
bool intersects_point(const POINT &p_pt) const {
- if (_any_lessthan(-p_pt, neg_max)) {
- return false;
- }
- if (_any_lessthan(p_pt, min)) {
- return false;
+ POINT diff_max = -p_pt - neg_max;
+ POINT diff_min = p_pt - min;
+ bool result = true;
+ for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) {
+ result &= !std::signbit(diff_max[axis]);
+ result &= !std::signbit(diff_min[axis]);
}
- return true;
+ return result;
}
// Very hot in profiling, make sure optimized
bool intersects(const BVH_ABB &p_o) const {
- if (_any_morethan(p_o.min, -neg_max)) {
- return false;
- }
- if (_any_morethan(min, -p_o.neg_max)) {
- return false;
+ POINT diff_max = -neg_max - p_o.min;
+ POINT diff_min = -p_o.neg_max - min;
+ bool result = true;
+ for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) {
+ result &= !std::signbit(diff_max[axis]);
+ result &= !std::signbit(diff_min[axis]);
}
- return true;
+ return result;
}
// for pre-swizzled tester (this object)
bool intersects_swizzled(const BVH_ABB &p_o) const {
- if (_any_lessthan(min, p_o.min)) {
- return false;
- }
- if (_any_lessthan(neg_max, p_o.neg_max)) {
- return false;
+ POINT diff_min = min - p_o.min;
+ POINT diff_max = neg_max - p_o.neg_max;
+ bool result = true;
+ for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) {
+ result &= !std::signbit(diff_min[axis]);
+ result &= !std::signbit(diff_max[axis]);
}
- return true;
+ return result;
}
bool is_other_within(const BVH_ABB &p_o) const {
- if (_any_lessthan(p_o.neg_max, neg_max)) {
- return false;
- }
- if (_any_lessthan(p_o.min, min)) {
- return false;
+ POINT diff_max = p_o.neg_max - neg_max;
+ POINT diff_min = p_o.min - min;
+ bool result = true;
+ for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) {
+ result &= !std::signbit(diff_max[axis]);
+ result &= !std::signbit(diff_min[axis]);
}
- return true;
+ return result;
}
void grow(const POINT &p_change) {
@@ -272,22 +276,4 @@ struct BVH_ABB {
}
min = neg_max;
}
-
- bool _any_morethan(const POINT &p_a, const POINT &p_b) const {
- for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) {
- if (p_a[axis] > p_b[axis]) {
- return true;
- }
- }
- return false;
- }
-
- bool _any_lessthan(const POINT &p_a, const POINT &p_b) const {
- for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) {
- if (p_a[axis] < p_b[axis]) {
- return true;
- }
- }
- return false;
- }
};