quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
mmorton.cc
Go to the documentation of this file.
1#include <mmorton.h>
2
3static constexpr morton_code_t B[] = {0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF};
4static constexpr morton_code_t M[] = {0x4444444444444444, 0x3030303030303030, 0x0F000F000F000F00, 0x00FF000000FF0000, 0x0000FFFF00000000};
5static constexpr morton_code_t S[] = {1, 2, 4, 8, 16};
6
9
10 morton_code_t x = static_cast<morton_code_t> (_x);
11 morton_code_t y = static_cast<morton_code_t> (_y);
12
13 x = (x | (x << S[3])) & B[3];
14 x = (x | (x << S[2])) & B[2];
15 x = (x | (x << S[1])) & B[1];
16 x = (x | (x << S[0])) & B[0];
17
18 y = (y | (y << S[3])) & B[3];
19 y = (y | (y << S[2])) & B[2];
20 y = (y | (y << S[1])) & B[1];
21 y = (y | (y << S[0])) & B[0];
22
23 return x | (y << 1);
24}
25
26// Below is a slower but easier to understand implementation
27// the two versions of this function should return the same results
28// which can be used for testing and debugging
29/*
30morton_code_t
31coord_2_morton (coord_t x, coord_t y) {
32
33 static coord_t W = 0;
34 static coord_t H = 0;
35 static morton_code_t xmask = 0;
36 static morton_code_t ymask = 0;
37
38 morton_code_t res = 0;
39
40 W = (1 << (max_level -1));
41 H = (1 << (max_level -1));
42
43 xmask = 1 << (2 * (max_level - 1));
44 ymask = xmask << 1;
45
46while (x || y) {
47
48
49 std::cout << "res = " << res << " x = " << x << " y = " << y << std::endl;
50 std::cout << "H = " << H << " W = " << W << std::endl;
51 std::cout << "xmask = " << xmask << " ymask = " << ymask << std::endl;
52
53
54 if (y / H > 0) {
55 res |= ymask;
56 y ^= H;
57 }
58
59 if (x / W > 0) {
60 res |= xmask;
61 x ^= W;
62 }
63
64 xmask = xmask >> 2;
65 ymask = ymask >> 2;
66 H = H >> 1;
67 W = W >> 1;
68 }
69
70 return (res);
71}
72*/
73
74void
76
77 morton_code_t x = code & B[0];
78 morton_code_t y = (code >> 1) & B[0];
79
80 x = x | ((x & M[0]) >> S[0]);
81 x = x | ((x & M[1]) >> S[1]);
82 x = x | ((x & M[2]) >> S[2]);
83 x = x | ((x & M[3]) >> S[3]);
84 x = x | ((x & M[4]) >> S[4]);
85 _x = x;
86
87 y = y | ((y & M[0]) >> S[0]);
88 y = y | ((y & M[1]) >> S[1]);
89 y = y | ((y & M[2]) >> S[2]);
90 y = y | ((y & M[3]) >> S[3]);
91 y = y | ((y & M[4]) >> S[4]);
92 _y = y;
93}
94
95// Below is a slower but easier to understand implementation
96// the two versions of this function should return the same results
97// which can be used for testing and debugging
98/*
99void
100morton_2_coord (morton_code_t code, coord_t &x, coord_t &y) {
101
102 static coord_t level = 0;
103
104 x = 0;
105 y = 0;
106 level = 0;
107
108 while (code) {
109 x |= (code & 1) << level;
110 code = code >> 1;
111 y |= (code & 1) << level++;
112 code = code >> 1;
113 }
114
115}
116*/
117
void morton_2_coord(morton_code_t code, coord_t &_x, coord_t &_y)
Definition mmorton.cc:75
morton_code_t coord_2_morton(coord_t _x, coord_t _y)
Definition mmorton.cc:8
static constexpr morton_code_t B[]
Definition mmorton.cc:3
static constexpr morton_code_t M[]
Definition mmorton.cc:4
static constexpr morton_code_t S[]
Definition mmorton.cc:5
uint_fast64_t morton_code_t
Definition mmorton.h:8
uint_least32_t coord_t
Definition mmorton.h:7