-
Notifications
You must be signed in to change notification settings - Fork 30
/
morton.h
74 lines (61 loc) · 2.13 KB
/
morton.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include <cstdint>
#include <tuple>
namespace morton2d {
constexpr static uint64_t encode1(uint64_t x)
{
x = (x | (x << 16)) & 0x0000FFFF0000FFFF;
x = (x | (x << 8)) & 0x00FF00FF00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x << 2)) & 0x3333333333333333;
x = (x | (x << 1)) & 0x5555555555555555;
return x;
}
constexpr static uint64_t encode(uint64_t x, uint64_t y)
{
return encode1(x) | encode1(y << 1);
}
constexpr static uint64_t decode1(uint64_t x)
{
x = x & 0x5555555555555555;
x = (x | (x >> 1)) & 0x3333333333333333;
x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x >> 4)) & 0x00FF00FF00FF00FF;
x = (x | (x >> 8)) & 0x0000FFFF0000FFFF;
x = (x | (x >> 16)) & 0xFFFFFFFFFFFFFFFF;
return x;
}
constexpr static std::tuple<uint64_t, uint64_t> decode(uint64_t d)
{
return {decode1(d), decode1(d >> 1)};
}
}
namespace morton3d {
constexpr static uint64_t encode1(uint64_t x)
{
x = (x | (x << 32)) & 0x7fff00000000ffff; // 0b0111111111111111000000000000000000000000000000001111111111111111
x = (x | (x << 16)) & 0x00ff0000ff0000ff; // 0b0000000011111111000000000000000011111111000000000000000011111111
x = (x | (x << 8)) & 0x700f00f00f00f00f; // 0b0111000000001111000000001111000000001111000000001111000000001111
x = (x | (x << 4)) & 0x30c30c30c30c30c3; // 0b0011000011000011000011000011000011000011000011000011000011000011
x = (x | (x << 2)) & 0x1249249249249249; // 0b0001001001001001001001001001001001001001001001001001001001001001
return x;
}
constexpr static uint64_t encode(uint64_t x, uint64_t y, uint64_t z)
{
return encode1(x) | encode1(y << 1) | encode1(z << 2);
}
constexpr static uint64_t decode1(uint64_t x)
{
x = x & 0x1249249249249249;
x = (x | (x >> 2)) & 0x30c30c30c30c30c3;
x = (x | (x >> 4)) & 0x700f00f00f00f00f;
x = (x | (x >> 8)) & 0x00ff0000ff0000ff;
x = (x | (x >> 16)) & 0x7fff00000000ffff;
x = (x | (x >> 32)) & 0xffffffffffffffff;
return x;
}
constexpr static std::tuple<uint64_t, uint64_t, uint64_t> decode(uint64_t d)
{
return {decode1(d), decode1(d >> 1), decode1(d >> 2)};
}
}