-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.wat
356 lines (283 loc) · 15.1 KB
/
life.wat
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
(module
(import "js" "log" (func $log (param i32) (param i32)))
;; callback(X, Y, iteration, count, hash); return 0 = continue, 1 = stop
(import "js" "callback" (func $callback (param i32) (param i32) (param i32) (param i32) (param i32) (result i32)))
(memory (import "js" "mem") 1)
;; ~~~ wasmer ~~~
;; (memory (export "emem") 1)
;; width, height, n_iters -> [actual number of iterations]
(func (export "run") (param $X i32) (param $Y i32) (param $reqiter i32) (result i32)
(local $ii i32)
(local $jj i32)
(local $XY i32)
(local $x i32)
(local $y i32)
(local $dx0 i32)
(local $dx1 i32)
(local $dy0 i32)
(local $dy1 i32)
(local $iter i32)
(local $src i32)
(local $dst i32)
(local $cnt i32)
(local $tcnt i32)
(local $cell i32)
(local $isrc i32)
(local $idst i32)
(local $hash_1 i32)
(local $hash_2 i32)
(local $hash_3 i32)
(local $hash_4 i32)
(local $hash i32)
(set_local $hash_1 (i32.const 0))
(set_local $hash_2 (i32.const 0))
(set_local $hash_3 (i32.const 0))
(set_local $hash_4 (i32.const 0))
(set_local $XY (i32.mul (get_local $X) (get_local $Y))) ;; $XY := $X * $Y
;;
;; Initialization
;;
(set_local $ii (i32.const -1)) ;; ii := -1
(block $break (loop $cont
(set_local $ii (i32.add (get_local $ii) (i32.const 1))) ;; $ii += 1
(br_if $break (i32.ge_s (get_local $ii) (get_local $XY))) ;; break if $ii>=$XY
;; continue if memory[4*$ii] != 1
(br_if $cont (i32.ne (i32.const 1) (i32.load (i32.mul (get_local $ii) (i32.const 4)))))
;; $hash_4 = $hash_4 ^ ($ii * 179424673)
(set_local $hash_4 (i32.xor (get_local $hash_4) (i32.mul (get_local $ii) (i32.const 179424673))))
;; $x := $X + $ii % $X, $y := $Y + $ii / $X
(set_local $x (i32.rem_s (get_local $ii) (get_local $X)))
(set_local $y (i32.div_s (get_local $ii) (get_local $X)))
;; $dx0 := -1; if ($x == 0) then $dx0 += $X
(set_local $dx0 (i32.const -1))
(if (i32.eqz (get_local $x)) (then (set_local $dx0 (i32.add (get_local $dx0) (get_local $X)))))
;; $dx1 := 1; if ($x == $X - 1) then $dx1 -= $X
(set_local $dx1 (i32.const 1))
(if (i32.eq (get_local $x) (i32.sub (get_local $X) (i32.const 1))) (then (set_local $dx1 (i32.sub (get_local $dx1) (get_local $X)))))
;; $dy0 := -$X; if ($y == 0) then $dy0 += $XY
(set_local $dy0 (i32.sub (i32.const 0) (get_local $X)))
(if (i32.eqz (get_local $y)) (then (set_local $dy0 (i32.add (get_local $dy0) (get_local $XY)))))
;; $dy1 := $X; if ($y == $Y - 1) then $dy1 -= $XY
(set_local $dy1 (get_local $X))
(if (i32.eq (get_local $y) (i32.sub (get_local $Y) (i32.const 1))) (then (set_local $dy1 (i32.sub (get_local $dy1) (get_local $XY)))))
;; push $ii+$dx0+$dy0, $ii+$dx0+$dy1, $ii+$dx1+$dy0, $ii+$dx1+$dy1
(i32.add (get_local $ii) (i32.add (get_local $dx0) (get_local $dy0)))
(i32.add (get_local $ii) (i32.add (get_local $dx0) (get_local $dy1)))
(i32.add (get_local $ii) (i32.add (get_local $dx1) (get_local $dy0)))
(i32.add (get_local $ii) (i32.add (get_local $dx1) (get_local $dy1)))
;; push $ii+$dx0, $ii+$dx1, $ii+$dy0, $ii+$dy1
(i32.add (get_local $ii) (get_local $dx0))
(i32.add (get_local $ii) (get_local $dx1))
(i32.add (get_local $ii) (get_local $dy0))
(i32.add (get_local $ii) (get_local $dy1))
;; $jj = 4 * [pop]; if memory[$jj]==0 then memory[$jj]:=2
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
;; repeat 7 more
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.eqz (i32.load)) (then (i32.store (get_local $jj) (i32.const 2))))
(br $cont) ;; continue
))
;;
;; Main iteration loop
;;
(set_local $iter (i32.const 0)) ;; $iter := 0
(block $return (loop $main
;; (call $log (i32.const 130) (get_local $iter))
(br_if $return (i32.ge_s (get_local $iter) (get_local $reqiter))) ;; break if $iter>=$reqiter
;; (call $log (i32.const 131) (get_local $iter))
(set_local $iter (i32.add (get_local $iter) (i32.const 1))) ;; $iter += 1
(if (i32.eqz (i32.rem_s (get_local $iter) (i32.const 2)))
(then (set_local $src (get_local $XY)) (set_local $dst (i32.const 0)))
(else (set_local $dst (get_local $XY)) (set_local $src (i32.const 0))))
;; ~~~ wasmer ~~~
(;
(set_local $ii (i32.mul (get_local $XY) (i32.const 4))) ;; $ii := $XY * 4
(set_local $idst (i32.mul (get_local $dst) (i32.const 4))) ;; $idst := $dst * 4
(loop $fill
(set_local $ii (i32.sub (get_local $ii) (i32.const 4))) ;; $ii -= 4
;; mem[$idst + $ii] := 0
(i32.store (i32.add (get_local $idst) (get_local $ii)) (i32.const 0))
;; continue if $ii > 0
(br_if $fill (i32.gt_s (get_local $ii) (i32.const 0))))
;)
(memory.fill (i32.mul (i32.const 4) (get_local $dst)) (i32.const 0) (i32.mul (i32.const 4) (get_local $XY)))
(set_local $tcnt (i32.const 0)) ;; $tcnt := 0
(set_local $hash (i32.const 0))
(set_local $ii (i32.const -1)) ;; ii := -1
(block $break (loop $cont
;; (call $log (i32.const 80) (i32.load (i32.const 128)))
;; (call $log (i32.const 130) (get_local $iter))
(set_local $ii (i32.add (get_local $ii) (i32.const 1))) ;; $ii += 1
(br_if $break (i32.ge_s (get_local $ii) (get_local $XY))) ;; break if $ii>=$XY
;; $isrc := $ii + $src, $idst := $ii + $dst
(set_local $isrc (i32.add (get_local $ii) (get_local $src)))
(set_local $idst (i32.add (get_local $ii) (get_local $dst)))
;; memory[4 * $idst] := 0
;; (i32.store (i32.mul (i32.const 4) (get_local $idst)) (i32.const 0))
;; $cell = memory[4 * $isrc]
(set_local $cell (i32.load (i32.mul (get_local $isrc) (i32.const 4))))
;; continue if $cell == 0
(br_if $cont (i32.eqz (get_local $cell)))
;; $x := $X + $ii % $X, $y := $Y + $ii / $X
(set_local $x (i32.rem_s (get_local $ii) (get_local $X)))
(set_local $y (i32.div_s (get_local $ii) (get_local $X)))
;; $dx0 := -1; if ($x == 0) then $dx0 += $X
(set_local $dx0 (i32.const -1))
(if (i32.eqz (get_local $x)) (then (set_local $dx0 (i32.add (get_local $dx0) (get_local $X)))))
;; $dx1 := 1; if ($x == $X - 1) then $dx1 -= $X
(set_local $dx1 (i32.const 1))
(if (i32.eq (get_local $x) (i32.sub (get_local $X) (i32.const 1))) (then (set_local $dx1 (i32.sub (get_local $dx1) (get_local $X)))))
;; $dy0 := -$X; if ($y == 0) then $dy0 += $XY
(set_local $dy0 (i32.sub (i32.const 0) (get_local $X)))
(if (i32.eqz (get_local $y)) (then (set_local $dy0 (i32.add (get_local $dy0) (get_local $XY)))))
;; $dy1 := $X; if ($y == $Y - 1) then $dy1 -= $XY
(set_local $dy1 (get_local $X))
(if (i32.eq (get_local $y) (i32.sub (get_local $Y) (i32.const 1))) (then (set_local $dy1 (i32.sub (get_local $dy1) (get_local $XY)))))
;; push $isrc+$dx0+$dy0, $isrc+$dx0+$dy1, $isrc+$dx1+$dy0, $isrc+$dx1+$dy1
(i32.add (get_local $isrc) (i32.add (get_local $dx0) (get_local $dy0)))
(i32.add (get_local $isrc) (i32.add (get_local $dx0) (get_local $dy1)))
(i32.add (get_local $isrc) (i32.add (get_local $dx1) (get_local $dy0)))
(i32.add (get_local $isrc) (i32.add (get_local $dx1) (get_local $dy1)))
;; push $isrc+$dx0, $isrc+$dx1, $isrc+$dy0, $isrc+$dy1
(i32.add (get_local $isrc) (get_local $dx0))
(i32.add (get_local $isrc) (get_local $dx1))
(i32.add (get_local $isrc) (get_local $dy0))
(i32.add (get_local $isrc) (get_local $dy1))
(set_local $cnt (i32.const 0)) ;; $cnt := 0
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(set_local $cnt (i32.add (i32.eq (i32.load (i32.mul (i32.const 4))) (i32.const 1)) (get_local $cnt)))
(;
(call $log (i32.const 1) (get_local $x))
(call $log (i32.const 2) (get_local $y))
(call $log (i32.const 3) (get_local $cnt))
;)
(;
;; if memory[[pop]] == 1 then $cnt += 1 [repeat 8 times]
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
(if (i32.eq (i32.const 1) (i32.load)) (then (set_local $cnt (i32.add (i32.const 1) (get_local $cnt)))))
;)
;; main Conway formula for next-gen of cells
;; (exactly 3 neighnours OR exactly 2 neighbours + old cell)
(if (i32.or (i32.eq (i32.const 3) (get_local $cnt))
(i32.and (i32.eq (i32.const 2) (get_local $cnt))
(i32.eq (i32.const 1) (get_local $cell))))
(then
;; (call $log (i32.const 100) (get_local $idst))
;; $tcnt += 1
;; *** WARNING *** WARNING *** WARNING *** WARNING ***
;; For some reason, the next line triggers HUGE performance
;; degradation in node.js (up to 10%); тhere in no impact in wasmtime
;; It is safe to comment it out, the only result would be
;; callback function will always get '0' as 'count'
(set_local $tcnt (i32.add (get_local $tcnt) (i32.const 1)))
;; memory[4 * $idst] := 1
(i32.store (i32.mul (i32.const 4) (get_local $idst)) (i32.const 1))
;; $hash = $hash ^ ($ii * 179424673)
(set_local $hash (i32.xor (get_local $hash) (i32.mul (get_local $ii) (i32.const 179424673))))
;; (call $log (i32.const 1) (get_local $ii))
;; (call $log (i32.const 2) (get_local $hash))
;; push $idst+$dx0+$dy0, $idst+$dx0+$dy1, $idst+$dx1+$dy0, $idst+$dx1+$dy1
(i32.add (get_local $idst) (i32.add (get_local $dx0) (get_local $dy0)))
(i32.add (get_local $idst) (i32.add (get_local $dx0) (get_local $dy1)))
(i32.add (get_local $idst) (i32.add (get_local $dx1) (get_local $dy0)))
(i32.add (get_local $idst) (i32.add (get_local $dx1) (get_local $dy1)))
;; push $idst+$dx0, $idst+$dx1, $idst+$dy0, $idst+$dy1
(i32.add (get_local $idst) (get_local $dx0))
(i32.add (get_local $idst) (get_local $dx1))
(i32.add (get_local $idst) (get_local $dy0))
(i32.add (get_local $idst) (get_local $dy1))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
(i32.mul (i32.const 4))
(tee_local $jj)
(if (i32.ne (i32.load) (i32.const 1)) (then (i32.store (get_local $jj) (i32.const 2))))
;; (call $log (i32.const 10) (i32.div_s (get_local $jj) (i32.const 4)))
;; (call $log (i32.const 20) (i32.load (get_local $jj)))
))
(br $cont) ;; continue
))
;; (call $log (i32.const 0) (get_local $hash))
;; (call $log (i32.const 132) (get_local $iter))
(br_if $return (call $callback (get_local $X) (get_local $Y) (get_local $iter) (get_local $tcnt) (get_local $hash)))
;; (call $log (i32.const 133) (get_local $iter))
(br_if $return
(i32.or (i32.eq (get_local $hash_1) (get_local $hash))
(i32.or (i32.eq (get_local $hash_2) (get_local $hash))
(i32.or (i32.eq (get_local $hash_3) (get_local $hash))
(i32.eq (get_local $hash_4) (get_local $hash))))))
;; (call $log (i32.const 134) (get_local $iter))
(set_local $hash_1 (get_local $hash_2))
(set_local $hash_2 (get_local $hash_3))
(set_local $hash_3 (get_local $hash_4))
(set_local $hash_4 (get_local $hash))
(br $main) ;; continue [main loop]
))
;; returning number of actually executed iterations
(get_local $iter)
))