Skip to content

Commit 6bffbb2

Browse files
authored
Fix pointer aliasing in kdtree
This commit addresses two problems related the the fast log2 computation in the kdtree : * The code was written with Real = double in mind which may not always be the case. The variable is now explicitly set to double. * GCC issues a warning for the reinterpret-cast when compiling with -fstrict-aliasing. The portable solution here is to use memcpy (according to http://dbp-consulting.com/tutorials/StrictAliasing.html) which compilers will optimize.
1 parent 36f571d commit 6bffbb2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Demos/Simulation/kdTree.inl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include "BoundingSphere.h"
3+
#include <cstring>
34
#include "omp.h"
45

56
template<typename HullType> void
@@ -127,8 +128,10 @@ KDTree<HullType>::traverse_breadth_first_parallel(TraversalPredicate pred,
127128
#endif
128129

129130
// compute ceiling of Log2
130-
Real d = maxThreads - 1;
131-
const unsigned int targetDepth = (*reinterpret_cast<long long*>(&d) >> 52) - 1022;
131+
// assuming double and long long have the same size.
132+
double d = maxThreads - 1;
133+
long long ll; memcpy( &ll, &d, sizeof(d));
134+
const unsigned int targetDepth = (ll >> 52) - 1022ll;
132135

133136
traverse_breadth_first(
134137
[&start_nodes, &maxThreads, &targetDepth](unsigned int node_index, unsigned int depth)
@@ -207,4 +210,4 @@ KDTree<HullType>::update()
207210
compute_hull_approx(nd.begin, nd.n, m_hulls[node_index]);
208211
}
209212
);
210-
}
213+
}

0 commit comments

Comments
 (0)