-
Notifications
You must be signed in to change notification settings - Fork 6
/
Chunk.h
205 lines (167 loc) · 5.45 KB
/
Chunk.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#pragma once
#include "Utils.h"
#include <array>
#include <cstring>
using x2_t = uint8_t;
using x4_t = std::array<x2_t, 8>;
using x8_t = std::array<x4_t, 8>;
using x16_t = std::array<x8_t, 8>;
static constexpr int x16Index(int y) {
return y >> 4;
}
static constexpr int x8Index(int x, int y, int z) {
return ((x & 8) >> 1) | ((y & 8) >> 2) | ((z & 8) >> 3);
}
static constexpr int x4Index(int x, int y, int z) {
return ((x & 4)) | ((y & 4) >> 1) | ((z & 4) >> 2);
}
static constexpr int x2Index(int x, int y, int z) {
return ((x & 2) << 1) | ((y & 2)) | ((z & 2) >> 1);
}
static constexpr int bitIndex(int x, int y, int z) {
return ((x & 1) << 2) | ((y & 1) << 1) | ((z & 1));
}
constexpr auto makeX2Index() {
std::array<std::array<std::array<uint16_t, 64>, 8>, 8> out{};
for (int x = 0; x < 16; x += 2) {
for (int z = 0; z < 16; z += 2) {
for (int y = 0; y < 128; y+= 2) {
const auto idx = (x16Index(y) * sizeof(x16_t))
+ (x8Index(x, y, z) * sizeof(x8_t))
+ (x4Index(x, y, z) * sizeof(x4_t))
+ (x2Index(x, y, z) * sizeof(x2_t));
out[x / 2][z / 2][y / 2] = idx;
}
}
}
return out;
}
constexpr auto X2_INDEX = makeX2Index();
template<typename T> requires (sizeof(T) >= sizeof(int64_t) && sizeof(T) <= 32 && sizeof(T) % sizeof(int64_t) == 0)
inline bool isEmpty(const T& input) {
constexpr auto arrSize = sizeof(T) / sizeof(int64_t);
auto* nums = reinterpret_cast<const int64_t*>(&input);
int64_t out = 0;
for (int i = 0; i < arrSize; i++) {
out |= nums[i];
}
return out == 0;
}
// https://gcc.godbolt.org/z/nsvzefd6r
template<typename T> requires (sizeof(T) > 32 && sizeof(T) % 32 == 0)
inline bool isEmpty(const T& input) {
using block = std::array<std::byte, 32>; // size of avx2 register (256 bits)
constexpr auto arrSize = sizeof(T) / sizeof(block);
auto* blocks = reinterpret_cast<const block*>(&input);
for (int i = 0; i < arrSize; i++) {
block data = blocks[i];
if (!isEmpty(data)) return false;
}
return true;
}
struct Chunk {
std::array<x16_t, 8> data{};
//std::array<bool, 8> x16Empty{};
bool isFromJava{};
private:
#define CHUNK_GETBIT(x, y, z) \
auto& x2 = data \
[x16Index(y)] /* x16 */ \
[x8Index(x, y, z)] /* x8 */ \
[x4Index(x, y, z)] /* x4 */ \
[x2Index(x, y, z)]; /* x2 */ \
uint8_t bit = bitIndex(x, y, z); /* bit */
private:
bool getBit(int x, int y, int z) const {
CHUNK_GETBIT(x, y, z);
return (x2 >> bit) & 1;
}
#undef CHUNK_GETBIT
public:
void calcEmptyX16() {
for (int i = 0; i < 8; i++) {
//x16Empty[i] = ::isEmpty(data[i]);
}
}
const x16_t& getX16(int y) const {
return data[x16Index(y)];
}
const x8_t& getX8(int x, int y, int z) const {
return getX16(y)[x8Index(x, y, z)];
}
const x4_t& getX4(int x, int y, int z) const {
return getX8(x, y, z)[x4Index(x, y, z)];
}
// TODO: this doesn't modulo the input args like the other functions
const x2_t& getX2(int x, int y, int z) const {
auto x2Idx = X2_INDEX[x/2][z/2][y/2];
auto* asX2Array = reinterpret_cast<const x2_t*>(this->data.data());
return asX2Array[x2Idx];
}
x2_t& getX2(int x, int y, int z) {
auto x2Idx = X2_INDEX[x/2][z/2][y/2];
auto* asX2Array = reinterpret_cast<x2_t*>(this->data.data());
return asX2Array[x2Idx];
}
// for benchmarking
const x2_t& getX2Old(int x, int y, int z) const {
return getX4(x, y, z)[x2Index(x, y, z)];
}
template<Size>
bool isEmpty(int x, int y, int z) const = delete;
// for benchmarking (this actually compiled to cmov)
void setBlockOld(int x, int y, int z, bool solid) {
auto& x2 = getX2(x, y, z);
const uint8_t bit = bitIndex(x, y, z);
if (solid) {
x2 |= (1u << bit);
} else {
x2 &= ~(1u << bit);
}
}
void setBlock(int x, int y, int z, bool solid) {
auto& x2 = getX2(x, y, z);
const uint8_t bit = bitIndex(x, y, z);
// for some reason this compiled to a bit less code
x2 = solid ? (x2 | (1u << bit)) : (x2 & ~(1u << bit));
}
bool isSolid(const BlockPos& pos) const {
return isSolid(pos.x, pos.y, pos.z);
}
bool isSolid(int x, int y, int z) const {
return getBit(x, y, z) == 1;
}
};
template<>
inline bool Chunk::isEmpty<Size::X16>(int, int y, int) const {
//return x16Empty[x16Index(y)];
return ::isEmpty(data[x16Index(y)]);
}
template<>
inline bool Chunk::isEmpty<Size::X8>(int x, int y, int z) const {
const x8_t& x8 = getX8(x, y, z);
return ::isEmpty(x8);
}
template<>
inline bool Chunk::isEmpty<Size::X4>(int x, int y, int z) const {
const x4_t& x4 = getX4(x, y, z);
return ::isEmpty(x4);
}
template<>
inline bool Chunk::isEmpty<Size::X2>(int x, int y, int z) const {
return getX2(x, y, z) == 0;
}
template<>
inline bool Chunk::isEmpty<Size::X1>(int x, int y, int z) const {
return getBit(x, y, z) == 0;
}
static const Chunk AIR_CHUNK = [] {
Chunk out{};
out.calcEmptyX16();
return out;
}();
static const Chunk SOLID_CHUNK = [] {
Chunk out{};
memset(&out.data, 0xFF, sizeof(out.data));
return out;
}();