-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgol.cpp
69 lines (48 loc) · 1.67 KB
/
gol.cpp
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
#include "core.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <chrono>
#include <iostream>
using namespace std;
void step_into(word_t *hv, word_t *target) {
word_t nbs [8][WORDS];
word_t* nbs_lookup [8] = {nbs[0], nbs[1], nbs[2], nbs[3],
nbs[4], nbs[5], nbs[6], nbs[7]};
bhv::roll_word_bits_into(hv, -1, nbs[0]);
bhv::roll_word_bits_into(hv, 1, nbs[1]);
bhv::roll_words_into(hv, -1, nbs[2]);
bhv::roll_words_into(hv, 1, nbs[3]);
bhv::roll_words_into(nbs[0], 1, nbs[4]);
bhv::roll_words_into(nbs[0], -1, nbs[5]);
bhv::roll_words_into(nbs[1], 1, nbs[6]);
bhv::roll_words_into(nbs[1], -1, nbs[7]);
word_t alive [WORDS];
word_t dead [WORDS];
bhv::window_into(nbs_lookup, 8, 2, 3, alive);
bhv::window_into(nbs_lookup, 8, 3, 3, dead);
bhv::select_into(hv, alive, dead, target);
}
int main() {
FILE* file = fopen("gol1000.pbm", "rb");
if (file == nullptr) {
perror("Failed to open file");
return 1;
}
size_t n;
word_t** hvs = bhv::load_pbm(file, &n);
assert(n == 1001);
fclose(file);
word_t* petri_dish_hv = hvs[0];
for (size_t i = 0; i < 30; ++i)
step_into(petri_dish_hv, petri_dish_hv);
cout << bhv::eq(petri_dish_hv, hvs[30]) << endl;
auto t0 = chrono::high_resolution_clock::now();
for (size_t i = 0; i < 1000000; ++i)
step_into(petri_dish_hv, petri_dish_hv);
auto t1 = chrono::high_resolution_clock::now();
cout << (1000000.)/((double)chrono::duration_cast<chrono::nanoseconds>(t1 - t0).count()/1e9) << " fps" << endl;
for (size_t i = 0; i < n; ++i)
free(hvs[i]);
return 0;
}