-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife-wasm.c
174 lines (147 loc) · 4.83 KB
/
life-wasm.c
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
// Compiling with imported memory:
// c4wa-compile -P -Xmodule.memoryStatus=import:mem -Xmodule.importName=js -Xmodule.dataSize=0 -Xmodule.stackSize=0 life-wasm.c -o life-wasm.wat
// wat2wasm --enable-bulk-memory life-wasm.wat
// special mode for wasmer compatibility (exported memory, no bulk mem funcs)
// c4wa-compile -P -DNO_BULK_MEM -Xmodule.memoryStatus=export:emem -Xmodule.importName=js -Xmodule.dataSize=0 -Xmodule.stackSize=0 life-wasm.c -o life-wasmer.wat
// wat2wasm life-wasmer.wat
#define cell_t int
const unsigned int hash_rand = 179424673;
static int g_count = 0;
static unsigned int g_hash = 0;
void life_prepare (
cell_t * cells,
int X,
int Y ) {
int cnt = 0;
unsigned int hash = 0;
for(int y = 0; y < Y; y ++)
for(int x = 0; x < X; x ++) {
int idx = X * y + x;
if (cells[idx] == 1) {
cnt ++;
hash ^= (unsigned int)idx * hash_rand;
for (int dx = -1; dx <= 1; dx ++)
for (int dy = -1; dy <= 1; dy ++) {
int didx = X * ((y + dy + Y) % Y) + ((x + dx + X) % X);
if (cells[didx] == 0)
cells[didx] = 2;
}
}
}
g_count = cnt;
g_hash = hash;
}
void life_step (
cell_t * cells,
cell_t * cellsnew,
int X,
int Y
) {
int x, y, n, newv;
int n00, n01, n02, n10, n12, n20, n21, n22;
int v00, v01, v02, v10, v11, v12, v20, v21, v22;
int cnt = 0;
unsigned int hash = 0;
cell_t * p = cells - 1;
int ind = -1;
#ifdef NO_BULK_MEM
p = cellsnew;
for(x = 0; x < X*Y; x ++) {
*p = 0;
p ++;
}
p = cells - 1;
#else
memset ( (char *)cellsnew, (char)0, X * Y * sizeof(cell_t) );
#endif
/* Assuming there could be many empty cells, optimize looping the
* best we can */
do {
do {
p ++;
ind ++;
if (ind == X * Y) {
g_count = cnt;
g_hash = hash;
return;
}
}
while (*p == (char)0);
y = ind / X; x = ind - y * X;
if ( x > 0 & x < X - 1 & y > 0 & y < Y - 1 ) {
n00 = X * (y - 1) + (x - 1);
n01 = n00 + 1;
n02 = n01 + 1;
n10 = ind - 1;
n12 = ind + 1;
n20 = n10 + X;
n21 = n20 + 1;
n22 = n21 + 1;
}
else {
#define N(dy,dx) (X * ((y + dy + Y) % Y) + ((x + dx + X) % X))
n00 = N(-1,-1);
n01 = N(-1,0);
n02 = N(-1,1);
n10 = N(0,-1);
n12 = N(0,1);
n20 = N(1,-1);
n21 = N(1,0);
n22 = N(1,1);
#undef N
}
v00 = (1 == cells[n00]);
v01 = (1 == cells[n01]);
v02 = (1 == cells[n02]);
v10 = (1 == cells[n10]);
v11 = (1 == *p);
v12 = (1 == cells[n12]);
v20 = (1 == cells[n20]);
v21 = (1 == cells[n21]);
v22 = (1 == cells[n22]);
n = v00 + v01 + v02 + v10 + v12 + v20 + v21 + v22;
newv = (n == 3) | ((n == 2) & v11);
if ( newv ) {
cnt ++;
hash ^= (unsigned int)ind * hash_rand;
cellsnew[ind] = (cell_t)newv;
if (cellsnew[n00] != 1) cellsnew[n00] = 2;
if (cellsnew[n01] != 1) cellsnew[n01] = 2;
if (cellsnew[n02] != 1) cellsnew[n02] = 2;
if (cellsnew[n10] != 1) cellsnew[n10] = 2;
if (cellsnew[n12] != 1) cellsnew[n12] = 2;
if (cellsnew[n20] != 1) cellsnew[n20] = 2;
if (cellsnew[n21] != 1) cellsnew[n21] = 2;
if (cellsnew[n22] != 1) cellsnew[n22] = 2;
}
}
while(1);
}
void log(int, int); // compatibility
int callback(int /* X */, int /* Y */, int /* iteration */,
int /* count */, unsigned int /* hash */);
extern int run(int X, int Y, int N) {
cell_t * pos_0 = alloc(0, 2*X*Y, cell_t);
cell_t * pos_1 = pos_0 + X*Y;
unsigned int hash_1, hash_2, hash_3, hash_4;
hash_1 = hash_2 = hash_3 = hash_4 = (unsigned int) 0;
life_prepare(pos_0, X, Y);
for (int i = 0; i < N; i ++) {
if (i % 2 == 0)
life_step(pos_0, pos_1, X, Y);
else
life_step(pos_1, pos_0, X, Y);
if(1 == callback(X, Y, i + 1, g_count, g_hash))
return i + 1;
if (g_hash == hash_1 ||
g_hash == hash_2 ||
g_hash == hash_3 ||
g_hash == hash_4)
return i + 1;
hash_1 = hash_2;
hash_2 = hash_3;
hash_3 = hash_4;
hash_4 = g_hash;
}
return N;
}