-
Notifications
You must be signed in to change notification settings - Fork 62
/
morton_common.h
49 lines (45 loc) · 1.23 KB
/
morton_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
// Libmorton - Common helper methods needed in Morton encoding/decoding
#include <stdint.h>
#if _MSC_VER
#include <intrin.h>
#endif
namespace libmorton {
template<typename morton>
inline bool findFirstSetBitZeroIdx(const morton x, unsigned long* firstbit_location) {
#if _MSC_VER && !_WIN64
// 32 BIT on 32 BIT
if (sizeof(morton) <= 4) {
return _BitScanReverse(firstbit_location, x) != 0;
}
// 64 BIT on 32 BIT
else {
*firstbit_location = 0;
if (_BitScanReverse(firstbit_location, (x >> 32))) { // check first part
*firstbit_location += 32;
return true;
}
return _BitScanReverse(firstbit_location, (x & 0xFFFFFFFF)) != 0;
}
#elif _MSC_VER && _WIN64
// 32 or 64 BIT on 64 BIT
return _BitScanReverse64(firstbit_location, x) != 0;
#elif __GNUC__
if (x == 0) {
return false;
}
else {
*firstbit_location = static_cast<unsigned long>((sizeof(morton) * 8) - __builtin_clzll(x) - 1);
return true;
}
#endif
}
template<typename morton>
inline bool findFirstSetBit(const morton x, unsigned long* firstbit_location) {
if (findFirstSetBitZeroIdx(x, firstbit_location)) {
*firstbit_location += 1;
return true;
}
return false;
}
}