From bde13b12cf5657fc1189418cea50a5617c8d0e9e Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 20 Apr 2018 18:56:51 +0200 Subject: [PATCH] Some final tweaks to the examples I promise --- examples/game-of-life/assembly/index.ts | 42 +- examples/game-of-life/build/optimized.wasm | Bin 728 -> 731 bytes examples/game-of-life/build/optimized.wat | 421 ++++---- examples/game-of-life/build/untouched.wat | 1035 ++++++++++---------- examples/game-of-life/index.html | 3 +- examples/game-of-life/package.json | 4 +- examples/mandelbrot/assembly/index.ts | 31 +- examples/mandelbrot/build/optimized.wasm | Bin 554 -> 556 bytes examples/mandelbrot/build/optimized.wat | 184 ++-- examples/mandelbrot/build/untouched.wat | 208 ++-- examples/mandelbrot/package.json | 4 +- tests/compiler/game-of-life.optimized.wat | 128 ++- tests/compiler/game-of-life.untouched.wat | 664 +++++++------ tests/compiler/mandelbrot.optimized.wat | 400 ++++++-- tests/compiler/mandelbrot.untouched.wat | 431 ++++++-- 15 files changed, 2068 insertions(+), 1487 deletions(-) diff --git a/examples/game-of-life/assembly/index.ts b/examples/game-of-life/assembly/index.ts index cb628af1..3c595a65 100644 --- a/examples/game-of-life/assembly/index.ts +++ b/examples/game-of-life/assembly/index.ts @@ -1,7 +1,8 @@ // see: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life -const RGB_ALIVE = 0xE692D3; -const RGB_DEAD = 0x851BA6; +const RGB_ALIVE = 0xE692D3; // LSB must be set +const RGB_DEAD = 0x851BA6; // LSB must not be set +const BIT_ROT = 10; // It's everywhere var w: i32, h: i32, s: i32; @@ -19,8 +20,8 @@ function set(x: u32, y: u32, v: u32): void { /** Sets an output pixel in the range [s, 2*s] while fading it out. */ @inline -function set_fade(x: u32, y: u32, v: u32): void { - var a = max((v >>> 24) - 7, 0); +function rot(x: u32, y: u32, v: u32): void { + var a = max((v >>> 24) - BIT_ROT, 0); set(x, y, (a << 24) | (v & 0x00ffffff)); } @@ -33,7 +34,7 @@ export function init(width: i32, height: i32): void { // Start by filling output with random live cells. for (let y = 0; y < h; ++y) { for (let x = 0; x < w; ++x) { - set(x, y, JSMath.random() > 0.15 ? RGB_DEAD & 0x00ffffff : RGB_ALIVE | 0xff000000); + set(x, y, Math.random() > 0.1 ? RGB_DEAD & 0x00ffffff : RGB_ALIVE | 0xff000000); } } } @@ -53,35 +54,24 @@ export function step(): void { xp1 = x == wm1 ? 0 : x + 1; // Every cell interacts with its eight neighbours, which are the cells that are horizontally, - // vertically, or diagonally adjacent: - let tl = get(xm1, ym1); - let tm = get(x , ym1); - let tr = get(xp1, ym1); - let ml = get(xm1, y ); - let mm = get(x , y ); - let mr = get(xp1, y ); - let bl = get(xm1, yp1); - let bm = get(x , yp1); - let br = get(xp1, yp1); - - // Least significant bit indicates alive or dead, others are ARGB. + // vertically, or diagonally adjacent. Least significant bit indicates alive or dead. let aliveNeighbors = ( - (tl & 1) + (tm & 1) + (tr & 1) + - (ml & 1) + (mr & 1) + - (bl & 1) + (bm & 1) + (br & 1) + (get(xm1, ym1) & 1) + (get(x , ym1) & 1) + (get(xp1, ym1) & 1) + + (get(xm1, y ) & 1) + (get(xp1, y ) & 1) + + (get(xm1, yp1) & 1) + (get(x , yp1) & 1) + (get(xp1, yp1) & 1) ); - let alive = mm & 1; - if (alive) { - // A live cell with 2 or 3 live neighbors lives on to the next generation. - if ((aliveNeighbors & 0b1110) == 0b0010) set_fade(x, y, mm); + let self = get(x, y); + if (self & 1) { + // A live cell with 2 or 3 live neighbors rots on to the next generation. + if ((aliveNeighbors & 0b1110) == 0b0010) rot(x, y, self); // A live cell with fewer than 2 or more than 3 live neighbors dies. else set(x, y, RGB_DEAD | 0xff000000); } else { // A dead cell with exactly 3 live neighbors becomes a live cell. if (aliveNeighbors == 3) set(x, y, RGB_ALIVE | 0xff000000); - // A dead cell with fewer or more than 3 live neighbors remains dead. - else set_fade(x, y, mm); + // A dead cell with fewer or more than 3 live neighbors just rots. + else rot(x, y, self); } } } diff --git a/examples/game-of-life/build/optimized.wasm b/examples/game-of-life/build/optimized.wasm index deb061db88f58c8a89fbf3a6748ee3ff1a8f85ec..d83782b617a676f61b1e6c52db69338c12a8d5ad 100644 GIT binary patch literal 731 zcmZ`%&2G~`5T2RcbxIdd#3>So&e}O}>X9p1300_~7ZCSY!~#2Y;y9*=s*+!V3%$3m zfOv$S`#|ZeleUQ}d>HM_em}D_hICmX0N@KXrnZGK;Kdk#4k$YQe3CZbqdJ{U<|UxY zW@k~EmGk-+Nu~%W>Z=w#*vHTtD4oyp22d}XtP;pQWB*1|jo2~_fpLf|@pMQY9Hbwleyv8iyhQ4Jm1SU*wd;X5wHqU`PG<3XijFq&h zW!V1Z!qbr+$ruCRevB#xKREOIQvpJ(S>o|)IS(44tX8YuqLx9MT~=7kg_Nxkka{B8 zZR{yawAW71IY|^Jac|_}!AUl`wbZZ^N8M6k*k8_n)L*lcw5syiw=bVQzM5A}Ugqc7 N>{$N}rw3Bm^HuGjC_!d%I(b=Q$G*AIhPu>je#o7egX(B>ao(%ecDr%Xl)H z=EO3YJotH%Ps`5&l@uiWuB_RS-h`khRXRy4Vl}Ul0xf?h4t^<7GS`OsWVp`;@vbi% zS&{=o5x-Ld6pVr+LeZtSr|tLiY@}-U{l^#Oxj)~&*4oNQV1n*4!;Usjpsfc-!8w!&83?rXcl@-9E5TzyRvHfu z41=O!_e@8JqH%*m$Fue62FTWk>8v#xzdBUg9nGE07D=|!-0AW6dRnA?h?R6^T4a05 z3L{}k4UBMyGbhf~;NK?r(Ep+Y-dyhh%uFtS`OgIS43_I9V5r7Qqz4^489 zwB6SJ6u4JPSU!{xp#*j8aifGS{(qs=@6M<9Conway's Game of Life - AssemblyScript