-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBits.hpp
More file actions
371 lines (319 loc) · 12 KB
/
Copy pathBits.hpp
File metadata and controls
371 lines (319 loc) · 12 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#pragma once
#include <stdint.h>
#include "LifeAPI.h"
inline void HalfAdd(uint64_t &outbit, uint64_t &outcarry, const uint64_t ina,
const uint64_t inb) {
outbit = ina ^ inb;
outcarry = ina & inb;
}
inline void FullAdd(uint64_t &outbit, uint64_t &outcarry, const uint64_t ina,
const uint64_t inb, const uint64_t inc) {
uint64_t halftotal = ina ^ inb;
outbit = halftotal ^ inc;
uint64_t halfcarry1 = ina & inb;
uint64_t halfcarry2 = inc & halftotal;
outcarry = halfcarry1 | halfcarry2;
}
inline void HalfAdd(LifeState &outbit,
LifeState &outcarry,
const LifeState &ina,
const LifeState &inb) {
outbit = ina ^ inb;
outcarry = ina & inb;
}
inline void FullAdd(LifeState &outbit,
LifeState &outcarry,
const LifeState &ina,
const LifeState &inb,
const LifeState &inc) {
LifeState halftotal = ina ^ inb;
outbit = halftotal ^ inc;
LifeState halfcarry1 = ina & inb;
LifeState halfcarry2 = inc & halftotal;
outcarry = halfcarry1 | halfcarry2;
}
inline void HalfAdd(uint64_t mask,
LifeState &outbit,
LifeState &outcarry,
const LifeState &ina,
const LifeState &inb) {
for (auto s : StripIterator(mask)) {
#pragma clang loop vectorize_width(4)
for (int i = 0; i < 4; i++) {
outbit[s][i] = ina[s][i] ^ inb[s][i];
outcarry[s][i] = ina[s][i] & inb[s][i];
}
}
}
inline void FullAdd(uint64_t mask,
LifeState &outbit,
LifeState &outcarry,
const LifeState &ina,
const LifeState &inb,
const LifeState &inc) {
for (auto s : StripIterator(mask)) {
#pragma clang loop vectorize_width(4)
for (int i = 0; i < 4; i++) {
uint64_t halftotal = ina[s][i] ^ inb[s][i];
outbit[s][i] = halftotal ^ inc[s][i];
uint64_t halfcarry1 = ina[s][i] & inb[s][i];
uint64_t halfcarry2 = inc[s][i] & halftotal;
outcarry[s][i] = halfcarry1 | halfcarry2;
}
}
}
inline void FullAdd(std::array<uint64_t, 4> &__restrict__ outbit,
std::array<uint64_t, 4> &__restrict__ outcarry,
const std::array<uint64_t, 4> &__restrict__ ina,
const std::array<uint64_t, 4> &__restrict__ inb,
const std::array<uint64_t, 4> &__restrict__ inc) {
for(unsigned i = 0; i < 4; i++) {
uint64_t halftotal = ina[i] ^ inb[i];
outbit[i] = halftotal ^ inc[i];
uint64_t halfcarry1 = ina[i] & inb[i];
uint64_t halfcarry2 = inc[i] & halftotal;
outcarry[i] = halfcarry1 | halfcarry2;
}
}
inline void FourBitAdd(const std::array<uint64_t, 4> &__restrict__ a3,
const std::array<uint64_t, 4> &__restrict__ a2,
const std::array<uint64_t, 4> &__restrict__ a1,
const std::array<uint64_t, 4> &__restrict__ a0,
const std::array<uint64_t, 4> &__restrict__ b3,
const std::array<uint64_t, 4> &__restrict__ b2,
const std::array<uint64_t, 4> &__restrict__ b1,
const std::array<uint64_t, 4> &__restrict__ b0,
const std::array<uint64_t, 4> &__restrict__ incarry,
std::array<uint64_t, 4> &__restrict__ result3,
std::array<uint64_t, 4> &__restrict__ result2,
std::array<uint64_t, 4> &__restrict__ result1,
std::array<uint64_t, 4> &__restrict__ result0) {
std::array<uint64_t, 4> carry = incarry;
FullAdd(result0, carry, a0, b0, carry);
FullAdd(result1, carry, a1, b1, carry);
FullAdd(result2, carry, a2, b2, carry);
FullAdd(result3, carry, a3, b3, carry);
}
inline void FourBitAdd(const std::array<uint64_t, 4> &__restrict__ a3,
const std::array<uint64_t, 4> &__restrict__ a2,
const std::array<uint64_t, 4> &__restrict__ a1,
const std::array<uint64_t, 4> &__restrict__ a0,
const std::array<uint64_t, 4> &__restrict__ b3,
const std::array<uint64_t, 4> &__restrict__ b2,
const std::array<uint64_t, 4> &__restrict__ b1,
const std::array<uint64_t, 4> &__restrict__ b0,
std::array<uint64_t, 4> &__restrict__ result3,
std::array<uint64_t, 4> &__restrict__ result2,
std::array<uint64_t, 4> &__restrict__ result1,
std::array<uint64_t, 4> &__restrict__ result0) {
FourBitAdd(a3, a2, a1, a0, b3, b2, b1, b0, {0, 0, 0, 0}, result3, result2, result1, result0);
}
inline void FourBitSubtract(const std::array<uint64_t, 4> &__restrict__ a3,
const std::array<uint64_t, 4> &__restrict__ a2,
const std::array<uint64_t, 4> &__restrict__ a1,
const std::array<uint64_t, 4> &__restrict__ a0,
const std::array<uint64_t, 4> &__restrict__ b3,
const std::array<uint64_t, 4> &__restrict__ b2,
const std::array<uint64_t, 4> &__restrict__ b1,
const std::array<uint64_t, 4> &__restrict__ b0,
std::array<uint64_t, 4> &__restrict__ result3,
std::array<uint64_t, 4> &__restrict__ result2,
std::array<uint64_t, 4> &__restrict__ result1,
std::array<uint64_t, 4> &__restrict__ result0) {
FourBitAdd(a3, a2, a1, a0,
{~b3[0], ~b3[1], ~b3[2], ~b3[3]},
{~b2[0], ~b2[1], ~b2[2], ~b2[3]},
{~b1[0], ~b1[1], ~b1[2], ~b1[3]},
{~b0[0], ~b0[1], ~b0[2], ~b0[3]},
{~0ULL, ~0ULL, ~0ULL, ~0ULL}, result3, result2, result1, result0);
}
struct NeighbourCount {
LifeState bit3;
LifeState bit2;
LifeState bit1;
LifeState bit0;
NeighbourCount()
: bit3{false}, bit2{false}, bit1{false}, bit0{false} {}
NeighbourCount operator~() const {
NeighbourCount result;
result.bit3 = ~bit3;
result.bit2 = ~bit2;
result.bit1 = ~bit1;
result.bit0 = ~bit0;
return result;
}
static void CountRows(const LifeState &state,
uint64_t (&col0)[N + 2],
uint64_t (&col1)[N + 2]) {
for (unsigned i = 0; i < N; i++) {
uint64_t a = state.state[i];
uint64_t l = std::rotl(a, 1);
uint64_t r = std::rotr(a, 1);
col0[i+1] = l ^ r ^ a;
col1[i+1] = ((l ^ r) & a) | (l & r);
}
col0[0] = col0[N]; col0[N+1] = col0[1];
col1[0] = col1[N]; col1[N+1] = col1[1];
}
NeighbourCount(const LifeState &state)
: bit3{false}, bit2{false}, bit1{false}, bit0{false} {
uint64_t col0[N + 2];
uint64_t col1[N + 2];
CountRows(state, col0, col1);
for (unsigned i = 0; i < N; i++) {
uint64_t u_on0 = col0[i];
uint64_t c_on0 = col0[i+1];
uint64_t l_on0 = col0[i+2];
uint64_t u_on1 = col1[i];
uint64_t c_on1 = col1[i+1];
uint64_t l_on1 = col1[i+2];
uint64_t on3, on2, on1, on0;
uint64_t uc0, uc1, uc2, uc_carry0;
HalfAdd(uc0, uc_carry0, u_on0, c_on0);
FullAdd(uc1, uc2, u_on1, c_on1, uc_carry0);
uint64_t on_carry1, on_carry0;
HalfAdd(on0, on_carry0, uc0, l_on0);
FullAdd(on1, on_carry1, uc1, l_on1, on_carry0);
HalfAdd(on2, on3, uc2, on_carry1);
bit3.state[i] = on3;
bit2.state[i] = on2;
bit1.state[i] = on1;
bit0.state[i] = on0;
}
}
inline NeighbourCount Add(const NeighbourCount &other, const LifeState &incarry) const {
NeighbourCount result;
LifeState carry = incarry;
FullAdd(result.bit0, carry, bit0, other.bit0, carry);
FullAdd(result.bit1, carry, bit1, other.bit1, carry);
FullAdd(result.bit2, carry, bit2, other.bit2, carry);
FullAdd(result.bit3, carry, bit3, other.bit3, carry);
return result;
}
inline NeighbourCount operator+(const NeighbourCount &other) const {
return Add(other, LifeState());
}
inline NeighbourCount Subtract(const NeighbourCount &other) const {
return Add(~other, ~LifeState());
}
inline NeighbourCount operator-(const NeighbourCount &other) const {
return Add(~other, ~LifeState());
}
inline NeighbourCount Add(uint64_t mask, const NeighbourCount &other, const LifeState &incarry) const {
NeighbourCount result;
LifeState carry = incarry;
LifeState carry2(false);
FullAdd(mask, result.bit0, carry2, bit0, other.bit0, carry);
FullAdd(mask, result.bit1, carry, bit1, other.bit1, carry2);
FullAdd(mask, result.bit2, carry2, bit2, other.bit2, carry);
FullAdd(mask, result.bit3, carry, bit3, other.bit3, carry2);
return result;
}
inline NeighbourCount Subtract(uint64_t mask, const NeighbourCount &other) const {
return Add(mask, ~other, ~LifeState());
}
};
std::array<uint64_t, 4> inline CountNeighbourhoodColumn(const LifeState &state, int column) {
std::array<uint64_t, 4> nearby;
for (int i = 0; i < 4; i++) {
int c = (column + i - 1 + N) % N;
nearby[i] = state[c];
}
std::array<uint64_t, 4> col0;
std::array<uint64_t, 4> col1;
for (unsigned i = 0; i < 4; i++) {
uint64_t a = nearby[i];
uint64_t l = std::rotl(a, 1);
uint64_t r = std::rotr(a, 1);
col0[i] = l ^ r ^ a;
col1[i] = ((l ^ r) & a) | (l & r);
}
std::array<uint64_t, 4> result;
{
int idxU = 0;
int i = 1;
int idxB = 2;
uint64_t u_on1 = col1[idxU];
uint64_t u_on0 = col0[idxU];
uint64_t c_on1 = col1[i];
uint64_t c_on0 = col0[i];
uint64_t l_on1 = col1[idxB];
uint64_t l_on0 = col0[idxB];
uint64_t uc0, uc1, uc2, uc_carry0;
HalfAdd(uc0, uc_carry0, u_on0, c_on0);
FullAdd(uc1, uc2, u_on1, c_on1, uc_carry0);
uint64_t on_carry1, on_carry0;
HalfAdd(result[3], on_carry0, uc0, l_on0);
FullAdd(result[2], on_carry1, uc1, l_on1, on_carry0);
HalfAdd(result[1], result[0], uc2, on_carry1);
}
return result;
}
void inline CountNeighbourhoodStrip(
const std::array<uint64_t, 6> &state,
std::array<uint64_t, 4> &bit3,
std::array<uint64_t, 4> &bit2,
std::array<uint64_t, 4> &bit1,
std::array<uint64_t, 4> &bit0) {
std::array<uint64_t, 6> col0;
std::array<uint64_t, 6> col1;
for (unsigned i = 0; i < 6; i++) {
uint64_t a = state[i];
uint64_t l = std::rotl(a, 1);
uint64_t r = std::rotr(a, 1);
col0[i] = l ^ r ^ a;
col1[i] = ((l ^ r) & a) | (l & r);
}
#pragma clang loop vectorize(enable)
for (unsigned i = 1; i < 5; i++) {
int idxU = i-1;
int idxB = i+1;
uint64_t u_on1 = col1[idxU];
uint64_t u_on0 = col0[idxU];
uint64_t c_on1 = col1[i];
uint64_t c_on0 = col0[i];
uint64_t l_on1 = col1[idxB];
uint64_t l_on0 = col0[idxB];
uint64_t uc0, uc1, uc2, uc_carry0;
HalfAdd(uc0, uc_carry0, u_on0, c_on0);
FullAdd(uc1, uc2, u_on1, c_on1, uc_carry0);
uint64_t on_carry1, on_carry0;
HalfAdd(bit0[i-1], on_carry0, uc0, l_on0);
FullAdd(bit1[i-1], on_carry1, uc1, l_on1, on_carry0);
HalfAdd(bit2[i-1], bit3[i-1], uc2, on_carry1);
}
}
template <uint32_t max>
class LifeCountdown {
public:
static constexpr uint32_t lmax = (max == 0) ? 0 : 32 - __builtin_clz(max);
LifeState started;
LifeState finished;
std::array<LifeState, lmax> counter = {0};
uint32_t n;
LifeCountdown() : started{}, finished{}, counter{}, n{0} {};
LifeCountdown(uint32_t n) : started{}, finished{}, counter{}, n{n} {};
void Start(const LifeState &state) {
LifeState newStarted = state & ~started;
for (unsigned i = 0; i < lmax; i++) {
if ((n >> i) & 1) {
counter[i] |= newStarted;
}
}
started |= state;
}
void Reset(const LifeState &state) {
for (unsigned i = 0; i < lmax; i++) {
counter[i] &= ~state;
}
started &= ~state;
}
void Tick() {
auto carry = started;
for (unsigned i = 0; i < lmax; i++) {
counter[i] ^= carry;
carry &= counter[i];
}
finished |= carry;
}
};