Skip to content

Commit e01292d

Browse files
committed
Improved checkbits calculation in for-loops
1 parent c2ca688 commit e01292d

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

include/libmorton/morton3D.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#define EIGHTBITMASK (morton) 0x000000FF
1313
#define NINEBITMASK (morton) 0x000001FF
1414

15-
1615
namespace libmorton {
1716
// AVAILABLE METHODS FOR ENCODING
1817
template<typename morton, typename coord> inline morton m3D_e_sLUT(const coord x, const coord y, const coord z);
@@ -126,7 +125,7 @@ namespace libmorton {
126125
template<typename morton, typename coord>
127126
inline morton m3D_e_for(const coord x, const coord y, const coord z) {
128127
morton answer = 0;
129-
unsigned int checkbits = static_cast<unsigned int>(floor((sizeof(morton) * 8.0f / 3.0f)));
128+
unsigned int checkbits = (sizeof(morton) * 8) / 3;
130129
for (unsigned int i = 0; i < checkbits; ++i) {
131130
morton mshifted = static_cast<morton>(1) << i; // Here we need to cast 0x1 to 64bits, otherwise there is a bug when morton code is larger than 32 bits
132131
unsigned int shift = 2 * i; // because you have to shift back i and forth 3*i
@@ -143,7 +142,7 @@ namespace libmorton {
143142
inline morton m3D_e_for_ET(const coord x, const coord y, const coord z) {
144143
morton answer = 0;
145144
unsigned long x_max = 0, y_max = 0, z_max = 0;
146-
unsigned int checkbits = static_cast<unsigned int>(floor((sizeof(morton) * 8.0f / 3.0f)));
145+
unsigned int checkbits = (sizeof(morton) * 8) / 3;
147146
findFirstSetBit<morton>(x, &x_max);
148147
findFirstSetBit<morton>(y, &y_max);
149148
findFirstSetBit<morton>(z, &z_max);
@@ -158,13 +157,12 @@ namespace libmorton {
158157
return answer;
159158
}
160159

161-
162160
// HELPER METHOD for LUT decoding
163161
// todo: wouldn't this be better with 8-bit aligned decode LUT?
164162
template<typename morton, typename coord>
165163
inline coord morton3D_DecodeCoord_LUT256(const morton m, const uint_fast8_t *LUT, const unsigned int startshift) {
166164
morton a = 0;
167-
unsigned int loops = (sizeof(morton) <= 4) ? 4 : 7; // ceil for 32bit, floor for 64bit
165+
unsigned int loops = (sizeof(morton) <= 4) ? 4 : 7;
168166
for (unsigned int i = 0; i < loops; ++i) {
169167
a |= (morton)(LUT[(m >> ((i * 9) + startshift)) & NINEBITMASK] << morton(3 * i));
170168
}
@@ -254,7 +252,7 @@ namespace libmorton {
254252
template<typename morton, typename coord>
255253
inline void m3D_d_for(const morton m, coord& x, coord& y, coord& z) {
256254
x = 0; y = 0; z = 0;
257-
unsigned int checkbits = static_cast<unsigned int>(floor((sizeof(morton) * 8.0f / 3.0f)));
255+
unsigned int checkbits = (sizeof(morton) * 8) / 3;
258256
for (unsigned int i = 0; i <= checkbits; ++i) {
259257
morton selector = 1;
260258
unsigned int shift_selector = 3 * i;
@@ -271,8 +269,8 @@ namespace libmorton {
271269
x = 0; y = 0; z = 0;
272270
unsigned long firstbit_location = 0;
273271
if (!findFirstSetBit<morton>(m, &firstbit_location)) return;
274-
float defaultbits = floor((sizeof(morton) * 8.0f / 3.0f));
275-
unsigned int checkbits = static_cast<unsigned int>(std::min(defaultbits, firstbit_location / 3.0f));
272+
unsigned int defaultbits = (sizeof(morton) * 8) / 3;
273+
unsigned int checkbits = static_cast<unsigned int>(std::min((float) defaultbits, firstbit_location / 3.0f));
276274
for (unsigned int i = 0; i <= checkbits; ++i) {
277275
morton selector = 1;
278276
unsigned int shift_selector = 3 * i;
@@ -282,4 +280,4 @@ namespace libmorton {
282280
z |= (m & (selector << (shift_selector + 2))) >> (shiftback + 2);
283281
}
284282
}
285-
}
283+
}

0 commit comments

Comments
 (0)