diff --git a/examples/mandelbrot/assembly/index.ts b/examples/mandelbrot/assembly/index.ts index 9c1b8326..880126dd 100644 --- a/examples/mandelbrot/assembly/index.ts +++ b/examples/mandelbrot/assembly/index.ts @@ -1,41 +1,47 @@ // see: https://en.wikipedia.org/wiki/Mandelbrot_set -/** Computes the number of iterations in the rectangle `width` x `height`, limited to `max`. */ -export function compute(width: u32, height: u32, limit: u32): void { +/** Number of discrete color values on the JS side. */ +const NUM_COLORS = 2048; + +/** Computes a single line in the rectangle `width` x `height`. */ +export function computeLine(y: u32, width: u32, height: u32, limit: u32): void { var translateX = width / 1.6; var translateY = height / 2.0; var scale = 10.0 / min(3 * width, 4 * height); - for (let y: u32 = 0; y < height; ++y) { - let imaginary = (y - translateY) * scale; - for (let x: u32 = 0; x < width; ++x) { - let real = (x - translateX) * scale; + var imaginary = (y - translateY) * scale; + for (let x: u32 = 0; x < width; ++x) { + let real = (x - translateX) * scale; - // Iterate until either the escape radius or iteration limit is exceeded - let ix = 0.0, iy = 0.0, ixsq: f64, iysq: f64; - let iteration: u32 = 0; - while ((ixsq = ix * ix) + (iysq = iy * iy) <= 4.0) { - let t = ixsq - iysq + real; - iy = 2.0 * ix * iy + imaginary; - ix = t; - if (iteration >= limit) break; - ++iteration; - } - - // Do a few extra iterations to reduce error margin - for (let i = 0; i < 4; ++i) { - let t = (ixsq = ix * ix) - (iysq = iy * iy) + real; - iy = 2.0 * ix * iy + imaginary; - ix = t; - } - - // Renormalize, see: http://linas.org/art-gallery/escape/escape.html - let frac: f64 = JSMath.log(JSMath.log(sqrt(ix * ix + iy * iy))) / JSMath.LN2; - if (frac > 0) { - let norm: f64 = max(min(((iteration + 1) - frac) / limit, 1.0), 0.0); - store((y * width + x) << 1, (2047 * norm)); - } else { - store((y * width + x) << 1, iteration * 2047 / limit); - } + // Iterate until either the escape radius or iteration limit is exceeded + let ix = 0.0, iy = 0.0, ixsq: f64, iysq: f64; + let iteration: u32 = 0; + while ((ixsq = ix * ix) + (iysq = iy * iy) <= 4.0) { + let ix_new = ixsq - iysq + real; + iy = 2.0 * ix * iy + imaginary; + ix = ix_new; + if (iteration >= limit) break; + ++iteration; } + + // Do a few extra iterations to reduce error margin + for (let i = 0; i < 5 && iteration < limit; ++i, ++iteration) { + let ix_new = ix * ix - iy * iy + real; + iy = 2.0 * ix * iy + imaginary; + ix = ix_new; + } + + // Renormalize, see: http://linas.org/art-gallery/escape/escape.html + let frac: f64 = JSMath.log(JSMath.log(sqrt(ix * ix + iy * iy))) / JSMath.LN2; + store((y * width + x) << 1, + isFinite(frac) + ? ((NUM_COLORS - 1) * clamp((iteration + 1 - frac) / limit, 0.0, 1.0)) + : (NUM_COLORS - 1) * iteration / limit + ); } } + +/** Clamps a value between the given minimum and maximum. */ +@inline +function clamp(value: T, minValue: T, maxValue: T): T { + return min(max(value, minValue), maxValue); +} diff --git a/examples/mandelbrot/build/optimized.wasm b/examples/mandelbrot/build/optimized.wasm index 06992d6c..56e54cb5 100644 Binary files a/examples/mandelbrot/build/optimized.wasm and b/examples/mandelbrot/build/optimized.wasm differ diff --git a/examples/mandelbrot/build/optimized.wat b/examples/mandelbrot/build/optimized.wat index 31a43f13..d4c83176 100644 --- a/examples/mandelbrot/build/optimized.wat +++ b/examples/mandelbrot/build/optimized.wat @@ -1,68 +1,75 @@ (module - (type $iiiv (func (param i32 i32 i32))) + (type $iiiiv (func (param i32 i32 i32 i32))) (type $FF (func (param f64) (result f64))) (import "JSMath" "log" (func $~lib/math/JSMath.log (param f64) (result f64))) (import "JSMath" "LN2" (global $~lib/math/JSMath.LN2 f64)) - (memory $0 1) - (export "compute" (func $assembly/index/compute)) + (import "env" "memory" (memory $0 1)) + (export "computeLine" (func $assembly/index/computeLine)) (export "memory" (memory $0)) - (func $assembly/index/compute (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 f64) + (func $assembly/index/computeLine (; 1 ;) (type $iiiiv) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (local $4 f64) - (local $5 i32) + (local $5 f64) (local $6 i32) - (local $7 i32) - (local $8 f64) + (local $7 f64) + (local $8 i32) (local $9 i32) (local $10 f64) (local $11 f64) (local $12 f64) (local $13 f64) (local $14 f64) - (local $15 f64) - ;;@ assembly/index.ts:5:2 - (set_local $13 - ;;@ assembly/index.ts:5:19 - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - ;;@ assembly/index.ts:5:27 - (f64.const 1.6) - ) - ) - ;;@ assembly/index.ts:6:2 - (set_local $14 - ;;@ assembly/index.ts:6:19 + ;;@ assembly/index.ts:8:2 + (set_local $12 + ;;@ assembly/index.ts:8:19 (f64.div (f64.convert_u/i32 (get_local $1) ) - ;;@ assembly/index.ts:6:28 - (f64.const 2) + ;;@ assembly/index.ts:8:27 + (f64.const 1.6) ) ) - ;;@ assembly/index.ts:7:2 + ;;@ assembly/index.ts:11:2 (set_local $10 - ;;@ assembly/index.ts:7:14 - (f64.div - (f64.const 10) - ;;@ assembly/index.ts:7:21 - (f64.min - ;;@ assembly/index.ts:7:25 - (f64.mul - (f64.const 3) - (f64.convert_u/i32 - ;;@ assembly/index.ts:7:29 - (get_local $0) - ) + ;;@ assembly/index.ts:11:18 + (f64.mul + (f64.sub + (f64.convert_u/i32 + ;;@ assembly/index.ts:11:19 + (get_local $0) ) - ;;@ assembly/index.ts:7:36 - (f64.mul - (f64.const 4) + ;;@ assembly/index.ts:9:19 + (f64.div (f64.convert_u/i32 - ;;@ assembly/index.ts:7:40 - (get_local $1) + (get_local $2) + ) + ;;@ assembly/index.ts:9:28 + (f64.const 2) + ) + ) + ;;@ assembly/index.ts:10:2 + (tee_local $13 + ;;@ assembly/index.ts:10:14 + (f64.div + (f64.const 10) + ;;@ assembly/index.ts:10:21 + (f64.min + ;;@ assembly/index.ts:10:25 + (f64.mul + (f64.const 3) + (f64.convert_u/i32 + ;;@ assembly/index.ts:10:29 + (get_local $1) + ) + ) + ;;@ assembly/index.ts:10:36 + (f64.mul + (f64.const 4) + (f64.convert_u/i32 + ;;@ assembly/index.ts:10:40 + (get_local $2) + ) + ) ) ) ) @@ -70,362 +77,324 @@ ) (loop $continue|0 (if - ;;@ assembly/index.ts:8:23 + ;;@ assembly/index.ts:12:23 (i32.lt_u - (get_local $5) - ;;@ assembly/index.ts:8:27 + (get_local $8) + ;;@ assembly/index.ts:12:27 (get_local $1) ) (block - ;;@ assembly/index.ts:9:4 + ;;@ assembly/index.ts:13:4 (set_local $11 - ;;@ assembly/index.ts:9:20 + ;;@ assembly/index.ts:13:15 (f64.mul (f64.sub (f64.convert_u/i32 - ;;@ assembly/index.ts:9:21 - (get_local $5) + ;;@ assembly/index.ts:13:16 + (get_local $8) ) - ;;@ assembly/index.ts:9:25 - (get_local $14) + ;;@ assembly/index.ts:13:20 + (get_local $12) ) - ;;@ assembly/index.ts:9:39 - (get_local $10) + ;;@ assembly/index.ts:13:34 + (get_local $13) ) ) - ;;@ assembly/index.ts:10:9 + (set_local $4 + ;;@ assembly/index.ts:16:13 + (f64.const 0) + ) + (set_local $5 + ;;@ assembly/index.ts:16:23 + (f64.const 0) + ) + ;;@ assembly/index.ts:17:4 (set_local $6 - ;;@ assembly/index.ts:10:22 + ;;@ assembly/index.ts:17:25 (i32.const 0) ) - (loop $continue|1 + ;;@ assembly/index.ts:18:4 + (block $break|1 + (loop $continue|1 + (if + ;;@ assembly/index.ts:18:11 + (f64.le + (f64.add + (tee_local $14 + ;;@ assembly/index.ts:18:19 + (f64.mul + (get_local $4) + ;;@ assembly/index.ts:18:24 + (get_local $4) + ) + ) + ;;@ assembly/index.ts:18:30 + (tee_local $7 + ;;@ assembly/index.ts:18:38 + (f64.mul + (get_local $5) + ;;@ assembly/index.ts:18:43 + (get_local $5) + ) + ) + ) + ;;@ assembly/index.ts:18:50 + (f64.const 4) + ) + (block + ;;@ assembly/index.ts:20:6 + (set_local $5 + ;;@ assembly/index.ts:20:11 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + ;;@ assembly/index.ts:20:17 + (get_local $4) + ) + ;;@ assembly/index.ts:20:22 + (get_local $5) + ) + ;;@ assembly/index.ts:20:27 + (get_local $10) + ) + ) + ;;@ assembly/index.ts:21:6 + (set_local $4 + ;;@ assembly/index.ts:19:19 + (f64.add + (f64.sub + (get_local $14) + ;;@ assembly/index.ts:19:26 + (get_local $7) + ) + ;;@ assembly/index.ts:19:33 + (get_local $11) + ) + ) + ;;@ assembly/index.ts:22:30 + (br_if $break|1 + ;;@ assembly/index.ts:22:10 + (i32.ge_u + (get_local $6) + ;;@ assembly/index.ts:22:23 + (get_local $3) + ) + ) + ;;@ assembly/index.ts:23:6 + (set_local $6 + (i32.add + ;;@ assembly/index.ts:23:8 + (get_local $6) + (i32.const 1) + ) + ) + (br $continue|1) + ) + ) + ) + ) + ;;@ assembly/index.ts:27:9 + (set_local $9 + ;;@ assembly/index.ts:27:17 + (i32.const 0) + ) + (loop $continue|2 (if - ;;@ assembly/index.ts:10:25 - (i32.lt_u - (get_local $6) - ;;@ assembly/index.ts:10:29 - (get_local $0) + ;;@ assembly/index.ts:27:20 + (i32.and + (if (result i32) + (tee_local $2 + (i32.lt_s + (get_local $9) + ;;@ assembly/index.ts:27:24 + (i32.const 5) + ) + ) + ;;@ assembly/index.ts:27:29 + (i32.lt_u + (get_local $6) + ;;@ assembly/index.ts:27:41 + (get_local $3) + ) + (get_local $2) + ) + (i32.const 1) ) (block - ;;@ assembly/index.ts:11:6 - (set_local $12 - ;;@ assembly/index.ts:11:17 - (f64.mul + ;;@ assembly/index.ts:28:6 + (set_local $7 + ;;@ assembly/index.ts:28:19 + (f64.add (f64.sub - (f64.convert_u/i32 - ;;@ assembly/index.ts:11:18 - (get_local $6) + (f64.mul + (get_local $4) + ;;@ assembly/index.ts:28:24 + (get_local $4) + ) + ;;@ assembly/index.ts:28:29 + (f64.mul + (get_local $5) + ;;@ assembly/index.ts:28:34 + (get_local $5) ) - ;;@ assembly/index.ts:11:22 - (get_local $13) ) - ;;@ assembly/index.ts:11:36 + ;;@ assembly/index.ts:28:39 + (get_local $11) + ) + ) + ;;@ assembly/index.ts:29:6 + (set_local $5 + ;;@ assembly/index.ts:29:11 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + ;;@ assembly/index.ts:29:17 + (get_local $4) + ) + ;;@ assembly/index.ts:29:22 + (get_local $5) + ) + ;;@ assembly/index.ts:29:27 (get_local $10) ) ) - (set_local $3 - ;;@ assembly/index.ts:14:15 - (f64.const 0) - ) + ;;@ assembly/index.ts:30:6 (set_local $4 - ;;@ assembly/index.ts:14:25 - (f64.const 0) + ;;@ assembly/index.ts:30:11 + (get_local $7) ) - ;;@ assembly/index.ts:15:6 - (set_local $7 - ;;@ assembly/index.ts:15:27 - (i32.const 0) - ) - ;;@ assembly/index.ts:16:6 - (block $break|2 - (loop $continue|2 - (if - ;;@ assembly/index.ts:16:13 - (f64.le - (f64.add - (tee_local $15 - ;;@ assembly/index.ts:16:21 - (f64.mul - (get_local $3) - ;;@ assembly/index.ts:16:26 - (get_local $3) - ) - ) - ;;@ assembly/index.ts:16:32 - (tee_local $8 - ;;@ assembly/index.ts:16:40 - (f64.mul - (get_local $4) - ;;@ assembly/index.ts:16:45 - (get_local $4) - ) - ) - ) - ;;@ assembly/index.ts:16:52 - (f64.const 4) - ) - (block - ;;@ assembly/index.ts:18:8 - (set_local $4 - ;;@ assembly/index.ts:18:13 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - ;;@ assembly/index.ts:18:19 - (get_local $3) - ) - ;;@ assembly/index.ts:18:24 - (get_local $4) - ) - ;;@ assembly/index.ts:18:29 - (get_local $11) - ) - ) - ;;@ assembly/index.ts:19:8 - (set_local $3 - ;;@ assembly/index.ts:17:16 - (f64.add - (f64.sub - (get_local $15) - ;;@ assembly/index.ts:17:23 - (get_local $8) - ) - ;;@ assembly/index.ts:17:30 - (get_local $12) - ) - ) - ;;@ assembly/index.ts:20:32 - (br_if $break|2 - ;;@ assembly/index.ts:20:12 - (i32.ge_u - (get_local $7) - ;;@ assembly/index.ts:20:25 - (get_local $2) - ) - ) - ;;@ assembly/index.ts:21:8 - (set_local $7 - (i32.add - ;;@ assembly/index.ts:21:10 - (get_local $7) - (i32.const 1) - ) - ) - (br $continue|2) - ) - ) - ) - ) - ;;@ assembly/index.ts:25:11 + ;;@ assembly/index.ts:27:48 (set_local $9 - ;;@ assembly/index.ts:25:19 - (i32.const 0) - ) - (loop $continue|3 - (if - ;;@ assembly/index.ts:25:22 - (i32.lt_s - (get_local $9) - ;;@ assembly/index.ts:25:26 - (i32.const 4) - ) - (block - ;;@ assembly/index.ts:26:8 - (set_local $8 - ;;@ assembly/index.ts:26:16 - (f64.add - (f64.sub - ;;@ assembly/index.ts:26:24 - (f64.mul - (get_local $3) - ;;@ assembly/index.ts:26:29 - (get_local $3) - ) - ;;@ assembly/index.ts:26:43 - (f64.mul - (get_local $4) - ;;@ assembly/index.ts:26:48 - (get_local $4) - ) - ) - ;;@ assembly/index.ts:26:54 - (get_local $12) - ) - ) - ;;@ assembly/index.ts:27:8 - (set_local $4 - ;;@ assembly/index.ts:27:13 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - ;;@ assembly/index.ts:27:19 - (get_local $3) - ) - ;;@ assembly/index.ts:27:24 - (get_local $4) - ) - ;;@ assembly/index.ts:27:29 - (get_local $11) - ) - ) - ;;@ assembly/index.ts:28:8 - (set_local $3 - ;;@ assembly/index.ts:28:13 - (get_local $8) - ) - ;;@ assembly/index.ts:25:29 - (set_local $9 - (i32.add - ;;@ assembly/index.ts:25:31 - (get_local $9) - (i32.const 1) - ) - ) - (br $continue|3) - ) + (i32.add + ;;@ assembly/index.ts:27:50 + (get_local $9) + (i32.const 1) ) ) - ;;@ assembly/index.ts:33:6 - (if - ;;@ assembly/index.ts:33:10 - (f64.gt - ;;@ assembly/index.ts:32:6 - (tee_local $8 - ;;@ assembly/index.ts:32:22 - (f64.div - ;;@ assembly/index.ts:32:29 - (call $~lib/math/JSMath.log - ;;@ assembly/index.ts:32:40 - (call $~lib/math/JSMath.log - ;;@ assembly/index.ts:32:44 - (f64.sqrt - ;;@ assembly/index.ts:32:49 - (f64.add - (f64.mul - (get_local $3) - ;;@ assembly/index.ts:32:54 - (get_local $3) - ) - ;;@ assembly/index.ts:32:59 - (f64.mul - (get_local $4) - ;;@ assembly/index.ts:32:64 - (get_local $4) - ) - ) - ) - ) - ) - ;;@ assembly/index.ts:32:72 - (get_global $~lib/math/JSMath.LN2) - ) - ) - ;;@ assembly/index.ts:33:17 - (f64.const 0) - ) - ;;@ assembly/index.ts:35:8 - (i32.store16 - ;;@ assembly/index.ts:35:19 - (i32.shl - (i32.add - ;;@ assembly/index.ts:35:20 - (i32.mul - (get_local $5) - ;;@ assembly/index.ts:35:24 - (get_local $0) - ) - ;;@ assembly/index.ts:35:32 - (get_local $6) - ) - ;;@ assembly/index.ts:35:38 - (i32.const 1) - ) - ;;@ assembly/index.ts:35:41 - (i32.trunc_u/f64 - ;;@ assembly/index.ts:35:47 - (f64.mul - (f64.const 2047) - ;;@ assembly/index.ts:34:24 - (f64.max - ;;@ assembly/index.ts:34:33 - (f64.min - ;;@ assembly/index.ts:34:42 - (f64.div - (f64.sub - ;;@ assembly/index.ts:34:43 - (f64.convert_u/i32 - ;;@ assembly/index.ts:34:49 - (i32.add - (get_local $7) - ;;@ assembly/index.ts:34:61 - (i32.const 1) - ) - ) - ;;@ assembly/index.ts:34:66 - (get_local $8) - ) - (f64.convert_u/i32 - ;;@ assembly/index.ts:34:74 - (get_local $2) - ) - ) - ;;@ assembly/index.ts:34:81 - (f64.const 1) - ) - ;;@ assembly/index.ts:34:87 - (f64.const 0) - ) - ) - ) - ) - ;;@ assembly/index.ts:37:8 - (i32.store16 - ;;@ assembly/index.ts:37:19 - (i32.shl - (i32.add - ;;@ assembly/index.ts:37:20 - (i32.mul - (get_local $5) - ;;@ assembly/index.ts:37:24 - (get_local $0) - ) - ;;@ assembly/index.ts:37:32 - (get_local $6) - ) - ;;@ assembly/index.ts:37:38 - (i32.const 1) - ) - ;;@ assembly/index.ts:37:41 - (i32.div_u - (i32.mul - (get_local $7) - ;;@ assembly/index.ts:37:53 - (i32.const 2047) - ) - ;;@ assembly/index.ts:37:60 - (get_local $2) - ) - ) - ) - ;;@ assembly/index.ts:10:36 + ;;@ assembly/index.ts:27:53 (set_local $6 (i32.add - ;;@ assembly/index.ts:10:38 + ;;@ assembly/index.ts:27:55 (get_local $6) (i32.const 1) ) ) - (br $continue|1) + (br $continue|2) ) ) ) - ;;@ assembly/index.ts:8:35 - (set_local $5 + ;;@ assembly/index.ts:35:4 + (i32.store16 + ;;@ assembly/index.ts:35:15 + (i32.shl + (i32.add + ;;@ assembly/index.ts:35:16 + (i32.mul + (get_local $0) + ;;@ assembly/index.ts:35:20 + (get_local $1) + ) + ;;@ assembly/index.ts:35:28 + (get_local $8) + ) + ;;@ assembly/index.ts:35:34 + (i32.const 1) + ) + ;;@ assembly/index.ts:36:6 + (if (result i32) + (f64.eq + (f64.sub + (tee_local $4 + ;;@ assembly/index.ts:34:4 + (tee_local $7 + ;;@ assembly/index.ts:34:20 + (f64.div + ;;@ assembly/index.ts:34:27 + (call $~lib/math/JSMath.log + ;;@ assembly/index.ts:34:38 + (call $~lib/math/JSMath.log + ;;@ assembly/index.ts:34:42 + (f64.sqrt + ;;@ assembly/index.ts:34:47 + (f64.add + (f64.mul + (get_local $4) + ;;@ assembly/index.ts:34:52 + (get_local $4) + ) + ;;@ assembly/index.ts:34:57 + (f64.mul + (get_local $5) + ;;@ assembly/index.ts:34:62 + (get_local $5) + ) + ) + ) + ) + ) + ;;@ assembly/index.ts:34:70 + (get_global $~lib/math/JSMath.LN2) + ) + ) + ) + (get_local $4) + ) + (f64.const 0) + ) + ;;@ assembly/index.ts:37:10 + (i32.trunc_u/f64 + ;;@ assembly/index.ts:37:16 + (f64.mul + (f64.const 2047) + (f64.min + (f64.max + (tee_local $7 + ;;@ assembly/index.ts:37:41 + (f64.div + (f64.sub + (f64.convert_u/i32 + ;;@ assembly/index.ts:37:42 + (i32.add + (get_local $6) + ;;@ assembly/index.ts:37:54 + (i32.const 1) + ) + ) + ;;@ assembly/index.ts:37:58 + (get_local $7) + ) + (f64.convert_u/i32 + ;;@ assembly/index.ts:37:66 + (get_local $3) + ) + ) + ) + (f64.const 0) + ) + (f64.const 1) + ) + ) + ) + ;;@ assembly/index.ts:38:10 + (i32.div_s + (i32.mul + ;;@ assembly/index.ts:38:29 + (get_local $6) + ;;@ assembly/index.ts:38:11 + (i32.const 2047) + ) + ;;@ assembly/index.ts:38:41 + (get_local $3) + ) + ) + ) + ;;@ assembly/index.ts:12:34 + (set_local $8 (i32.add - ;;@ assembly/index.ts:8:37 - (get_local $5) + ;;@ assembly/index.ts:12:36 + (get_local $8) (i32.const 1) ) ) diff --git a/examples/mandelbrot/build/untouched.wat b/examples/mandelbrot/build/untouched.wat index 5707bbcd..edc745e5 100644 --- a/examples/mandelbrot/build/untouched.wat +++ b/examples/mandelbrot/build/untouched.wat @@ -1,17 +1,51 @@ (module - (type $iiiv (func (param i32 i32 i32))) + (type $iiiiv (func (param i32 i32 i32 i32))) (type $FF (func (param f64) (result f64))) + (type $Fi (func (param f64) (result i32))) + (type $FFFF (func (param f64 f64 f64) (result f64))) (import "JSMath" "log" (func $~lib/math/JSMath.log (param f64) (result f64))) (import "JSMath" "LN2" (global $~lib/math/JSMath.LN2 f64)) + (import "env" "memory" (memory $0 1)) + (global $assembly/index/NUM_COLORS i32 (i32.const 2048)) (global $HEAP_BASE i32 (i32.const 4)) - (memory $0 1) - (export "compute" (func $assembly/index/compute)) + (export "computeLine" (func $assembly/index/computeLine)) (export "memory" (memory $0)) - (func $assembly/index/compute (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 f64) + (func $isFinite (; 1 ;) (type $Fi) (param $0 f64) (result i32) + ;;@ ~lib/builtins.ts:20:26 + (return + ;;@ ~lib/builtins.ts:20:9 + (f64.eq + (f64.sub + (get_local $0) + ;;@ ~lib/builtins.ts:20:17 + (get_local $0) + ) + ;;@ ~lib/builtins.ts:20:26 + (f64.const 0) + ) + ) + ) + (func $assembly/index/clamp (; 2 ;) (type $FFFF) (param $0 f64) (param $1 f64) (param $2 f64) (result f64) + ;;@ assembly/index.ts:46:43 + (return + ;;@ assembly/index.ts:46:9 + (f64.min + ;;@ assembly/index.ts:46:13 + (f64.max + ;;@ assembly/index.ts:46:17 + (get_local $0) + ;;@ assembly/index.ts:46:24 + (get_local $1) + ) + ;;@ assembly/index.ts:46:35 + (get_local $2) + ) + ) + ) + (func $assembly/index/computeLine (; 3 ;) (type $iiiiv) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (local $4 f64) (local $5 f64) - (local $6 i32) + (local $6 f64) (local $7 f64) (local $8 i32) (local $9 f64) @@ -22,447 +56,203 @@ (local $14 i32) (local $15 f64) (local $16 i32) - (local $17 f64) - ;;@ assembly/index.ts:5:2 - (set_local $3 - ;;@ assembly/index.ts:5:19 - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - ;;@ assembly/index.ts:5:27 - (f64.const 1.6) - ) - ) - ;;@ assembly/index.ts:6:2 + (local $17 i32) + ;;@ assembly/index.ts:8:2 (set_local $4 - ;;@ assembly/index.ts:6:19 + ;;@ assembly/index.ts:8:19 (f64.div (f64.convert_u/i32 (get_local $1) ) - ;;@ assembly/index.ts:6:28 + ;;@ assembly/index.ts:8:27 + (f64.const 1.6) + ) + ) + ;;@ assembly/index.ts:9:2 + (set_local $5 + ;;@ assembly/index.ts:9:19 + (f64.div + (f64.convert_u/i32 + (get_local $2) + ) + ;;@ assembly/index.ts:9:28 (f64.const 2) ) ) - ;;@ assembly/index.ts:7:2 - (set_local $5 - ;;@ assembly/index.ts:7:14 + ;;@ assembly/index.ts:10:2 + (set_local $6 + ;;@ assembly/index.ts:10:14 (f64.div (f64.const 10) - ;;@ assembly/index.ts:7:21 + ;;@ assembly/index.ts:10:21 (f64.min - ;;@ assembly/index.ts:7:25 + ;;@ assembly/index.ts:10:25 (f64.mul (f64.const 3) (f64.convert_u/i32 - ;;@ assembly/index.ts:7:29 - (get_local $0) + ;;@ assembly/index.ts:10:29 + (get_local $1) ) ) - ;;@ assembly/index.ts:7:36 + ;;@ assembly/index.ts:10:36 (f64.mul (f64.const 4) (f64.convert_u/i32 - ;;@ assembly/index.ts:7:40 - (get_local $1) + ;;@ assembly/index.ts:10:40 + (get_local $2) ) ) ) ) ) - ;;@ assembly/index.ts:8:2 + ;;@ assembly/index.ts:11:2 + (set_local $7 + ;;@ assembly/index.ts:11:18 + (f64.mul + (f64.sub + (f64.convert_u/i32 + ;;@ assembly/index.ts:11:19 + (get_local $0) + ) + ;;@ assembly/index.ts:11:23 + (get_local $5) + ) + ;;@ assembly/index.ts:11:37 + (get_local $6) + ) + ) + ;;@ assembly/index.ts:12:2 (block $break|0 - ;;@ assembly/index.ts:8:7 - (set_local $6 - ;;@ assembly/index.ts:8:20 + ;;@ assembly/index.ts:12:7 + (set_local $8 + ;;@ assembly/index.ts:12:20 (i32.const 0) ) (loop $continue|0 (if - ;;@ assembly/index.ts:8:23 + ;;@ assembly/index.ts:12:23 (i32.lt_u - (get_local $6) - ;;@ assembly/index.ts:8:27 + (get_local $8) + ;;@ assembly/index.ts:12:27 (get_local $1) ) (block (block - ;;@ assembly/index.ts:9:4 - (set_local $7 - ;;@ assembly/index.ts:9:20 + ;;@ assembly/index.ts:13:4 + (set_local $9 + ;;@ assembly/index.ts:13:15 (f64.mul (f64.sub (f64.convert_u/i32 - ;;@ assembly/index.ts:9:21 - (get_local $6) + ;;@ assembly/index.ts:13:16 + (get_local $8) ) - ;;@ assembly/index.ts:9:25 + ;;@ assembly/index.ts:13:20 (get_local $4) ) - ;;@ assembly/index.ts:9:39 - (get_local $5) + ;;@ assembly/index.ts:13:34 + (get_local $6) ) ) - ;;@ assembly/index.ts:10:4 - (block $break|1 - ;;@ assembly/index.ts:10:9 - (set_local $8 - ;;@ assembly/index.ts:10:22 - (i32.const 0) + ;;@ assembly/index.ts:16:4 + (block + (set_local $10 + ;;@ assembly/index.ts:16:13 + (f64.const 0) ) + (set_local $11 + ;;@ assembly/index.ts:16:23 + (f64.const 0) + ) + ) + ;;@ assembly/index.ts:17:4 + (set_local $14 + ;;@ assembly/index.ts:17:25 + (i32.const 0) + ) + ;;@ assembly/index.ts:18:4 + (block $break|1 (loop $continue|1 (if - ;;@ assembly/index.ts:10:25 - (i32.lt_u - (get_local $8) - ;;@ assembly/index.ts:10:29 - (get_local $0) - ) - (block - (block - ;;@ assembly/index.ts:11:6 - (set_local $9 - ;;@ assembly/index.ts:11:17 + ;;@ assembly/index.ts:18:11 + (f64.le + (f64.add + (tee_local $12 + ;;@ assembly/index.ts:18:19 (f64.mul - (f64.sub - (f64.convert_u/i32 - ;;@ assembly/index.ts:11:18 - (get_local $8) - ) - ;;@ assembly/index.ts:11:22 - (get_local $3) - ) - ;;@ assembly/index.ts:11:36 - (get_local $5) + (get_local $10) + ;;@ assembly/index.ts:18:24 + (get_local $10) ) ) - ;;@ assembly/index.ts:14:6 - (block - (set_local $10 - ;;@ assembly/index.ts:14:15 - (f64.const 0) - ) - (set_local $11 - ;;@ assembly/index.ts:14:25 - (f64.const 0) - ) - ) - ;;@ assembly/index.ts:15:6 - (set_local $14 - ;;@ assembly/index.ts:15:27 - (i32.const 0) - ) - ;;@ assembly/index.ts:16:6 - (block $break|2 - (loop $continue|2 - (if - ;;@ assembly/index.ts:16:13 - (f64.le - (f64.add - (tee_local $12 - ;;@ assembly/index.ts:16:21 - (f64.mul - (get_local $10) - ;;@ assembly/index.ts:16:26 - (get_local $10) - ) - ) - ;;@ assembly/index.ts:16:32 - (tee_local $13 - ;;@ assembly/index.ts:16:40 - (f64.mul - (get_local $11) - ;;@ assembly/index.ts:16:45 - (get_local $11) - ) - ) - ) - ;;@ assembly/index.ts:16:52 - (f64.const 4) - ) - (block - (block - ;;@ assembly/index.ts:17:8 - (set_local $15 - ;;@ assembly/index.ts:17:16 - (f64.add - (f64.sub - (get_local $12) - ;;@ assembly/index.ts:17:23 - (get_local $13) - ) - ;;@ assembly/index.ts:17:30 - (get_local $9) - ) - ) - ;;@ assembly/index.ts:18:8 - (set_local $11 - ;;@ assembly/index.ts:18:13 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - ;;@ assembly/index.ts:18:19 - (get_local $10) - ) - ;;@ assembly/index.ts:18:24 - (get_local $11) - ) - ;;@ assembly/index.ts:18:29 - (get_local $7) - ) - ) - ;;@ assembly/index.ts:19:8 - (set_local $10 - ;;@ assembly/index.ts:19:13 - (get_local $15) - ) - ;;@ assembly/index.ts:20:8 - (if - ;;@ assembly/index.ts:20:12 - (i32.ge_u - (get_local $14) - ;;@ assembly/index.ts:20:25 - (get_local $2) - ) - ;;@ assembly/index.ts:20:32 - (br $break|2) - ) - ;;@ assembly/index.ts:21:8 - (set_local $14 - (i32.add - ;;@ assembly/index.ts:21:10 - (get_local $14) - (i32.const 1) - ) - ) - ) - (br $continue|2) - ) - ) - ) - ) - ;;@ assembly/index.ts:25:6 - (block $break|3 - ;;@ assembly/index.ts:25:11 - (set_local $16 - ;;@ assembly/index.ts:25:19 - (i32.const 0) - ) - (loop $continue|3 - (if - ;;@ assembly/index.ts:25:22 - (i32.lt_s - (get_local $16) - ;;@ assembly/index.ts:25:26 - (i32.const 4) - ) - (block - (block - ;;@ assembly/index.ts:26:8 - (set_local $15 - ;;@ assembly/index.ts:26:16 - (f64.add - (f64.sub - (tee_local $12 - ;;@ assembly/index.ts:26:24 - (f64.mul - (get_local $10) - ;;@ assembly/index.ts:26:29 - (get_local $10) - ) - ) - ;;@ assembly/index.ts:26:35 - (tee_local $13 - ;;@ assembly/index.ts:26:43 - (f64.mul - (get_local $11) - ;;@ assembly/index.ts:26:48 - (get_local $11) - ) - ) - ) - ;;@ assembly/index.ts:26:54 - (get_local $9) - ) - ) - ;;@ assembly/index.ts:27:8 - (set_local $11 - ;;@ assembly/index.ts:27:13 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - ;;@ assembly/index.ts:27:19 - (get_local $10) - ) - ;;@ assembly/index.ts:27:24 - (get_local $11) - ) - ;;@ assembly/index.ts:27:29 - (get_local $7) - ) - ) - ;;@ assembly/index.ts:28:8 - (set_local $10 - ;;@ assembly/index.ts:28:13 - (get_local $15) - ) - ) - ;;@ assembly/index.ts:25:29 - (set_local $16 - (i32.add - ;;@ assembly/index.ts:25:31 - (get_local $16) - (i32.const 1) - ) - ) - (br $continue|3) - ) - ) - ) - ) - ;;@ assembly/index.ts:32:6 - (set_local $15 - ;;@ assembly/index.ts:32:22 - (f64.div - ;;@ assembly/index.ts:32:29 - (call $~lib/math/JSMath.log - ;;@ assembly/index.ts:32:40 - (call $~lib/math/JSMath.log - ;;@ assembly/index.ts:32:44 - (f64.sqrt - ;;@ assembly/index.ts:32:49 - (f64.add - (f64.mul - (get_local $10) - ;;@ assembly/index.ts:32:54 - (get_local $10) - ) - ;;@ assembly/index.ts:32:59 - (f64.mul - (get_local $11) - ;;@ assembly/index.ts:32:64 - (get_local $11) - ) - ) - ) - ) - ) - ;;@ assembly/index.ts:32:72 - (get_global $~lib/math/JSMath.LN2) - ) - ) - ;;@ assembly/index.ts:33:6 - (if - ;;@ assembly/index.ts:33:10 - (f64.gt - (get_local $15) - ;;@ assembly/index.ts:33:17 - (f64.const 0) - ) - ;;@ assembly/index.ts:33:20 - (block - ;;@ assembly/index.ts:34:8 - (set_local $17 - ;;@ assembly/index.ts:34:24 - (f64.max - ;;@ assembly/index.ts:34:33 - (f64.min - ;;@ assembly/index.ts:34:42 - (f64.div - (f64.sub - ;;@ assembly/index.ts:34:43 - (f64.convert_u/i32 - ;;@ assembly/index.ts:34:49 - (i32.add - (get_local $14) - ;;@ assembly/index.ts:34:61 - (i32.const 1) - ) - ) - ;;@ assembly/index.ts:34:66 - (get_local $15) - ) - (f64.convert_u/i32 - ;;@ assembly/index.ts:34:74 - (get_local $2) - ) - ) - ;;@ assembly/index.ts:34:81 - (f64.const 1) - ) - ;;@ assembly/index.ts:34:87 - (f64.const 0) - ) - ) - ;;@ assembly/index.ts:35:8 - (i32.store16 - ;;@ assembly/index.ts:35:19 - (i32.shl - (i32.add - ;;@ assembly/index.ts:35:20 - (i32.mul - (get_local $6) - ;;@ assembly/index.ts:35:24 - (get_local $0) - ) - ;;@ assembly/index.ts:35:32 - (get_local $8) - ) - ;;@ assembly/index.ts:35:38 - (i32.const 1) - ) - ;;@ assembly/index.ts:35:41 - (i32.trunc_u/f64 - ;;@ assembly/index.ts:35:47 - (f64.mul - (f64.convert_u/i32 - (i32.const 2047) - ) - ;;@ assembly/index.ts:35:54 - (get_local $17) - ) - ) - ) - ) - ;;@ assembly/index.ts:37:8 - (i32.store16 - ;;@ assembly/index.ts:37:19 - (i32.shl - (i32.add - ;;@ assembly/index.ts:37:20 - (i32.mul - (get_local $6) - ;;@ assembly/index.ts:37:24 - (get_local $0) - ) - ;;@ assembly/index.ts:37:32 - (get_local $8) - ) - ;;@ assembly/index.ts:37:38 - (i32.const 1) - ) - ;;@ assembly/index.ts:37:41 - (i32.div_u - (i32.mul - (get_local $14) - ;;@ assembly/index.ts:37:53 - (i32.const 2047) - ) - ;;@ assembly/index.ts:37:60 - (get_local $2) - ) + ;;@ assembly/index.ts:18:30 + (tee_local $13 + ;;@ assembly/index.ts:18:38 + (f64.mul + (get_local $11) + ;;@ assembly/index.ts:18:43 + (get_local $11) ) ) ) - ;;@ assembly/index.ts:10:36 - (set_local $8 - (i32.add - ;;@ assembly/index.ts:10:38 - (get_local $8) - (i32.const 1) + ;;@ assembly/index.ts:18:50 + (f64.const 4) + ) + (block + (block + ;;@ assembly/index.ts:19:6 + (set_local $15 + ;;@ assembly/index.ts:19:19 + (f64.add + (f64.sub + (get_local $12) + ;;@ assembly/index.ts:19:26 + (get_local $13) + ) + ;;@ assembly/index.ts:19:33 + (get_local $9) + ) + ) + ;;@ assembly/index.ts:20:6 + (set_local $11 + ;;@ assembly/index.ts:20:11 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + ;;@ assembly/index.ts:20:17 + (get_local $10) + ) + ;;@ assembly/index.ts:20:22 + (get_local $11) + ) + ;;@ assembly/index.ts:20:27 + (get_local $7) + ) + ) + ;;@ assembly/index.ts:21:6 + (set_local $10 + ;;@ assembly/index.ts:21:11 + (get_local $15) + ) + ;;@ assembly/index.ts:22:6 + (if + ;;@ assembly/index.ts:22:10 + (i32.ge_u + (get_local $14) + ;;@ assembly/index.ts:22:23 + (get_local $3) + ) + ;;@ assembly/index.ts:22:30 + (br $break|1) + ) + ;;@ assembly/index.ts:23:6 + (set_local $14 + (i32.add + ;;@ assembly/index.ts:23:8 + (get_local $14) + (i32.const 1) + ) ) ) (br $continue|1) @@ -470,12 +260,221 @@ ) ) ) + ;;@ assembly/index.ts:27:4 + (block $break|2 + ;;@ assembly/index.ts:27:9 + (set_local $16 + ;;@ assembly/index.ts:27:17 + (i32.const 0) + ) + (loop $continue|2 + (if + ;;@ assembly/index.ts:27:20 + (i32.and + (if (result i32) + (tee_local $17 + (i32.lt_s + (get_local $16) + ;;@ assembly/index.ts:27:24 + (i32.const 5) + ) + ) + ;;@ assembly/index.ts:27:29 + (i32.lt_u + (get_local $14) + ;;@ assembly/index.ts:27:41 + (get_local $3) + ) + (get_local $17) + ) + (i32.const 1) + ) + (block + (block + ;;@ assembly/index.ts:28:6 + (set_local $15 + ;;@ assembly/index.ts:28:19 + (f64.add + (f64.sub + (f64.mul + (get_local $10) + ;;@ assembly/index.ts:28:24 + (get_local $10) + ) + ;;@ assembly/index.ts:28:29 + (f64.mul + (get_local $11) + ;;@ assembly/index.ts:28:34 + (get_local $11) + ) + ) + ;;@ assembly/index.ts:28:39 + (get_local $9) + ) + ) + ;;@ assembly/index.ts:29:6 + (set_local $11 + ;;@ assembly/index.ts:29:11 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + ;;@ assembly/index.ts:29:17 + (get_local $10) + ) + ;;@ assembly/index.ts:29:22 + (get_local $11) + ) + ;;@ assembly/index.ts:29:27 + (get_local $7) + ) + ) + ;;@ assembly/index.ts:30:6 + (set_local $10 + ;;@ assembly/index.ts:30:11 + (get_local $15) + ) + ) + ;;@ assembly/index.ts:27:48 + (block + (set_local $16 + (i32.add + ;;@ assembly/index.ts:27:50 + (get_local $16) + (i32.const 1) + ) + ) + ;;@ assembly/index.ts:27:53 + (set_local $14 + (i32.add + ;;@ assembly/index.ts:27:55 + (get_local $14) + (i32.const 1) + ) + ) + ) + (br $continue|2) + ) + ) + ) + ) + ;;@ assembly/index.ts:34:4 + (set_local $15 + ;;@ assembly/index.ts:34:20 + (f64.div + ;;@ assembly/index.ts:34:27 + (call $~lib/math/JSMath.log + ;;@ assembly/index.ts:34:38 + (call $~lib/math/JSMath.log + ;;@ assembly/index.ts:34:42 + (f64.sqrt + ;;@ assembly/index.ts:34:47 + (f64.add + (f64.mul + (get_local $10) + ;;@ assembly/index.ts:34:52 + (get_local $10) + ) + ;;@ assembly/index.ts:34:57 + (f64.mul + (get_local $11) + ;;@ assembly/index.ts:34:62 + (get_local $11) + ) + ) + ) + ) + ) + ;;@ assembly/index.ts:34:70 + (get_global $~lib/math/JSMath.LN2) + ) + ) + ;;@ assembly/index.ts:35:4 + (i32.store16 + ;;@ assembly/index.ts:35:15 + (i32.shl + (i32.add + ;;@ assembly/index.ts:35:16 + (i32.mul + (get_local $0) + ;;@ assembly/index.ts:35:20 + (get_local $1) + ) + ;;@ assembly/index.ts:35:28 + (get_local $8) + ) + ;;@ assembly/index.ts:35:34 + (i32.const 1) + ) + ;;@ assembly/index.ts:36:6 + (if (result i32) + (call $isFinite + ;;@ assembly/index.ts:36:15 + (get_local $15) + ) + ;;@ assembly/index.ts:37:10 + (i32.trunc_u/f64 + ;;@ assembly/index.ts:37:16 + (f64.mul + (f64.convert_s/i32 + (i32.sub + ;;@ assembly/index.ts:37:17 + (i32.const 2048) + ;;@ assembly/index.ts:37:30 + (i32.const 1) + ) + ) + ;;@ assembly/index.ts:37:35 + (call $assembly/index/clamp + ;;@ assembly/index.ts:37:41 + (f64.div + (f64.sub + (f64.convert_u/i32 + ;;@ assembly/index.ts:37:42 + (i32.add + (get_local $14) + ;;@ assembly/index.ts:37:54 + (i32.const 1) + ) + ) + ;;@ assembly/index.ts:37:58 + (get_local $15) + ) + (f64.convert_u/i32 + ;;@ assembly/index.ts:37:66 + (get_local $3) + ) + ) + ;;@ assembly/index.ts:37:73 + (f64.const 0) + ;;@ assembly/index.ts:37:78 + (f64.const 1) + ) + ) + ) + ;;@ assembly/index.ts:38:10 + (i32.div_s + (i32.mul + (i32.sub + ;;@ assembly/index.ts:38:11 + (i32.const 2048) + ;;@ assembly/index.ts:38:24 + (i32.const 1) + ) + ;;@ assembly/index.ts:38:29 + (get_local $14) + ) + ;;@ assembly/index.ts:38:41 + (get_local $3) + ) + ) + ) ) - ;;@ assembly/index.ts:8:35 - (set_local $6 + ;;@ assembly/index.ts:12:34 + (set_local $8 (i32.add - ;;@ assembly/index.ts:8:37 - (get_local $6) + ;;@ assembly/index.ts:12:36 + (get_local $8) (i32.const 1) ) ) diff --git a/examples/mandelbrot/index.html b/examples/mandelbrot/index.html index 796cf9c8..b9461d96 100644 --- a/examples/mandelbrot/index.html +++ b/examples/mandelbrot/index.html @@ -1,60 +1,99 @@ -Mandelbrot Set - AssemblyScript +Mandelbrot set - AssemblyScript

- Mandelbrot Set in + Mandelbrot set in AssemblyScript ( source )

diff --git a/examples/mandelbrot/package.json b/examples/mandelbrot/package.json index b345c2f4..c5dd7007 100644 --- a/examples/mandelbrot/package.json +++ b/examples/mandelbrot/package.json @@ -3,8 +3,8 @@ "version": "1.0.0", "private": true, "scripts": { - "asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --measure", - "asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --sourceMap --validate --noDebug --measure", + "asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --importMemory --sourceMap --validate --measure", + "asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --importMemory --sourceMap --validate --noDebug --measure", "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized", "server": "http-server . -o -c-1" }, diff --git a/tests/compiler/mandelbrot.optimized.wat b/tests/compiler/mandelbrot.optimized.wat index 7e330679..634c1224 100644 --- a/tests/compiler/mandelbrot.optimized.wat +++ b/tests/compiler/mandelbrot.optimized.wat @@ -1,55 +1,80 @@ (module - (type $iiiv (func (param i32 i32 i32))) + (type $iiiiv (func (param i32 i32 i32 i32))) (type $FF (func (param f64) (result f64))) + (type $Fi (func (param f64) (result i32))) + (type $FFFF (func (param f64 f64 f64) (result f64))) (import "JSMath" "log" (func $~lib/math/JSMath.log (param f64) (result f64))) (import "JSMath" "LN2" (global $~lib/math/JSMath.LN2 f64)) (memory $0 1) - (export "compute" (func $../../examples/mandelbrot/assembly/index/compute)) + (export "computeLine" (func $../../examples/mandelbrot/assembly/index/computeLine)) (export "memory" (memory $0)) - (func $../../examples/mandelbrot/assembly/index/compute (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 f64) + (func $isFinite (; 1 ;) (type $Fi) (param $0 f64) (result i32) + (f64.eq + (f64.sub + (get_local $0) + (get_local $0) + ) + (f64.const 0) + ) + ) + (func $../../examples/mandelbrot/assembly/index/clamp (; 2 ;) (type $FFFF) (param $0 f64) (param $1 f64) (param $2 f64) (result f64) + (f64.min + (f64.max + (get_local $0) + (get_local $1) + ) + (get_local $2) + ) + ) + (func $../../examples/mandelbrot/assembly/index/computeLine (; 3 ;) (type $iiiiv) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (local $4 f64) - (local $5 i32) + (local $5 f64) (local $6 i32) - (local $7 i32) - (local $8 f64) + (local $7 f64) + (local $8 i32) (local $9 i32) (local $10 f64) (local $11 f64) (local $12 f64) (local $13 f64) (local $14 f64) - (local $15 f64) - (set_local $13 - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 1.6) - ) - ) - (set_local $14 + (set_local $12 (f64.div (f64.convert_u/i32 (get_local $1) ) - (f64.const 2) + (f64.const 1.6) ) ) (set_local $10 - (f64.div - (f64.const 10) - (f64.min - (f64.mul - (f64.const 3) - (f64.convert_u/i32 - (get_local $0) - ) + (f64.mul + (f64.sub + (f64.convert_u/i32 + (get_local $0) ) - (f64.mul - (f64.const 4) + (f64.div (f64.convert_u/i32 - (get_local $1) + (get_local $2) + ) + (f64.const 2) + ) + ) + (tee_local $13 + (f64.div + (f64.const 10) + (f64.min + (f64.mul + (f64.const 3) + (f64.convert_u/i32 + (get_local $1) + ) + ) + (f64.mul + (f64.const 4) + (f64.convert_u/i32 + (get_local $2) + ) + ) ) ) ) @@ -58,7 +83,7 @@ (loop $continue|0 (if (i32.lt_u - (get_local $5) + (get_local $8) (get_local $1) ) (block @@ -66,232 +91,137 @@ (f64.mul (f64.sub (f64.convert_u/i32 - (get_local $5) + (get_local $8) ) - (get_local $14) + (get_local $12) ) - (get_local $10) + (get_local $13) ) ) + (set_local $4 + (f64.const 0) + ) + (set_local $5 + (f64.const 0) + ) (set_local $6 (i32.const 0) ) - (loop $continue|1 + (block $break|1 + (loop $continue|1 + (if + (f64.le + (f64.add + (tee_local $14 + (f64.mul + (get_local $4) + (get_local $4) + ) + ) + (tee_local $7 + (f64.mul + (get_local $5) + (get_local $5) + ) + ) + ) + (f64.const 4) + ) + (block + (set_local $5 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + (get_local $4) + ) + (get_local $5) + ) + (get_local $10) + ) + ) + (set_local $4 + (f64.add + (f64.sub + (get_local $14) + (get_local $7) + ) + (get_local $11) + ) + ) + (br_if $break|1 + (i32.ge_u + (get_local $6) + (get_local $3) + ) + ) + (set_local $6 + (i32.add + (get_local $6) + (i32.const 1) + ) + ) + (br $continue|1) + ) + ) + ) + ) + (set_local $9 + (i32.const 0) + ) + (loop $continue|2 (if - (i32.lt_u - (get_local $6) - (get_local $0) + (i32.and + (if (result i32) + (tee_local $2 + (i32.lt_s + (get_local $9) + (i32.const 5) + ) + ) + (i32.lt_u + (get_local $6) + (get_local $3) + ) + (get_local $2) + ) + (i32.const 1) ) (block - (set_local $12 - (f64.mul + (set_local $7 + (f64.add (f64.sub - (f64.convert_u/i32 - (get_local $6) + (f64.mul + (get_local $4) + (get_local $4) ) - (get_local $13) + (f64.mul + (get_local $5) + (get_local $5) + ) + ) + (get_local $11) + ) + ) + (set_local $5 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + (get_local $4) + ) + (get_local $5) ) (get_local $10) ) ) - (set_local $3 - (f64.const 0) - ) (set_local $4 - (f64.const 0) - ) - (set_local $7 - (i32.const 0) - ) - (block $break|2 - (loop $continue|2 - (if - (f64.le - (f64.add - (tee_local $15 - (f64.mul - (get_local $3) - (get_local $3) - ) - ) - (tee_local $8 - (f64.mul - (get_local $4) - (get_local $4) - ) - ) - ) - (f64.const 4) - ) - (block - (set_local $4 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - (get_local $3) - ) - (get_local $4) - ) - (get_local $11) - ) - ) - (set_local $3 - (f64.add - (f64.sub - (get_local $15) - (get_local $8) - ) - (get_local $12) - ) - ) - (br_if $break|2 - (i32.ge_u - (get_local $7) - (get_local $2) - ) - ) - (set_local $7 - (i32.add - (get_local $7) - (i32.const 1) - ) - ) - (br $continue|2) - ) - ) - ) + (get_local $7) ) (set_local $9 - (i32.const 0) - ) - (loop $continue|3 - (if - (i32.lt_s - (get_local $9) - (i32.const 4) - ) - (block - (set_local $8 - (f64.add - (f64.sub - (f64.mul - (get_local $3) - (get_local $3) - ) - (f64.mul - (get_local $4) - (get_local $4) - ) - ) - (get_local $12) - ) - ) - (set_local $4 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - (get_local $3) - ) - (get_local $4) - ) - (get_local $11) - ) - ) - (set_local $3 - (get_local $8) - ) - (set_local $9 - (i32.add - (get_local $9) - (i32.const 1) - ) - ) - (br $continue|3) - ) - ) - ) - (if - (f64.gt - (tee_local $8 - (f64.div - (call $~lib/math/JSMath.log - (call $~lib/math/JSMath.log - (f64.sqrt - (f64.add - (f64.mul - (get_local $3) - (get_local $3) - ) - (f64.mul - (get_local $4) - (get_local $4) - ) - ) - ) - ) - ) - (get_global $~lib/math/JSMath.LN2) - ) - ) - (f64.const 0) - ) - (i32.store16 - (i32.shl - (i32.add - (i32.mul - (get_local $5) - (get_local $0) - ) - (get_local $6) - ) - (i32.const 1) - ) - (i32.trunc_u/f64 - (f64.mul - (f64.const 2047) - (f64.max - (f64.min - (f64.div - (f64.sub - (f64.convert_u/i32 - (i32.add - (get_local $7) - (i32.const 1) - ) - ) - (get_local $8) - ) - (f64.convert_u/i32 - (get_local $2) - ) - ) - (f64.const 1) - ) - (f64.const 0) - ) - ) - ) - ) - (i32.store16 - (i32.shl - (i32.add - (i32.mul - (get_local $5) - (get_local $0) - ) - (get_local $6) - ) - (i32.const 1) - ) - (i32.div_u - (i32.mul - (get_local $7) - (i32.const 2047) - ) - (get_local $2) - ) + (i32.add + (get_local $9) + (i32.const 1) ) ) (set_local $6 @@ -300,13 +230,81 @@ (i32.const 1) ) ) - (br $continue|1) + (br $continue|2) ) ) ) - (set_local $5 + (set_local $7 + (f64.div + (call $~lib/math/JSMath.log + (call $~lib/math/JSMath.log + (f64.sqrt + (f64.add + (f64.mul + (get_local $4) + (get_local $4) + ) + (f64.mul + (get_local $5) + (get_local $5) + ) + ) + ) + ) + ) + (get_global $~lib/math/JSMath.LN2) + ) + ) + (i32.store16 + (i32.shl + (i32.add + (i32.mul + (get_local $0) + (get_local $1) + ) + (get_local $8) + ) + (i32.const 1) + ) + (if (result i32) + (call $isFinite + (get_local $7) + ) + (i32.trunc_u/f64 + (f64.mul + (f64.const 2047) + (call $../../examples/mandelbrot/assembly/index/clamp + (f64.div + (f64.sub + (f64.convert_u/i32 + (i32.add + (get_local $6) + (i32.const 1) + ) + ) + (get_local $7) + ) + (f64.convert_u/i32 + (get_local $3) + ) + ) + (f64.const 0) + (f64.const 1) + ) + ) + ) + (i32.div_s + (i32.mul + (get_local $6) + (i32.const 2047) + ) + (get_local $3) + ) + ) + ) + (set_local $8 (i32.add - (get_local $5) + (get_local $8) (i32.const 1) ) ) diff --git a/tests/compiler/mandelbrot.ts b/tests/compiler/mandelbrot.ts index 10f2d29e..b545e849 100644 --- a/tests/compiler/mandelbrot.ts +++ b/tests/compiler/mandelbrot.ts @@ -1 +1 @@ -export { compute } from "../../examples/mandelbrot/assembly/index"; +export { computeLine } from "../../examples/mandelbrot/assembly/index"; diff --git a/tests/compiler/mandelbrot.untouched.wat b/tests/compiler/mandelbrot.untouched.wat index 3199d5e9..86ca2658 100644 --- a/tests/compiler/mandelbrot.untouched.wat +++ b/tests/compiler/mandelbrot.untouched.wat @@ -1,17 +1,41 @@ (module - (type $iiiv (func (param i32 i32 i32))) + (type $iiiiv (func (param i32 i32 i32 i32))) (type $FF (func (param f64) (result f64))) + (type $Fi (func (param f64) (result i32))) + (type $FFFF (func (param f64 f64 f64) (result f64))) (import "JSMath" "log" (func $~lib/math/JSMath.log (param f64) (result f64))) (import "JSMath" "LN2" (global $~lib/math/JSMath.LN2 f64)) + (global $../../examples/mandelbrot/assembly/index/NUM_COLORS i32 (i32.const 2048)) (global $HEAP_BASE i32 (i32.const 4)) (memory $0 1) - (export "compute" (func $../../examples/mandelbrot/assembly/index/compute)) + (export "computeLine" (func $../../examples/mandelbrot/assembly/index/computeLine)) (export "memory" (memory $0)) - (func $../../examples/mandelbrot/assembly/index/compute (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 f64) + (func $isFinite (; 1 ;) (type $Fi) (param $0 f64) (result i32) + (return + (f64.eq + (f64.sub + (get_local $0) + (get_local $0) + ) + (f64.const 0) + ) + ) + ) + (func $../../examples/mandelbrot/assembly/index/clamp (; 2 ;) (type $FFFF) (param $0 f64) (param $1 f64) (param $2 f64) (result f64) + (return + (f64.min + (f64.max + (get_local $0) + (get_local $1) + ) + (get_local $2) + ) + ) + ) + (func $../../examples/mandelbrot/assembly/index/computeLine (; 3 ;) (type $iiiiv) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (local $4 f64) (local $5 f64) - (local $6 i32) + (local $6 f64) (local $7 f64) (local $8 i32) (local $9 f64) @@ -22,317 +46,145 @@ (local $14 i32) (local $15 f64) (local $16 i32) - (local $17 f64) - (set_local $3 - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 1.6) - ) - ) + (local $17 i32) (set_local $4 (f64.div (f64.convert_u/i32 (get_local $1) ) - (f64.const 2) + (f64.const 1.6) ) ) (set_local $5 + (f64.div + (f64.convert_u/i32 + (get_local $2) + ) + (f64.const 2) + ) + ) + (set_local $6 (f64.div (f64.const 10) (f64.min (f64.mul (f64.const 3) (f64.convert_u/i32 - (get_local $0) + (get_local $1) ) ) (f64.mul (f64.const 4) (f64.convert_u/i32 - (get_local $1) + (get_local $2) ) ) ) ) ) + (set_local $7 + (f64.mul + (f64.sub + (f64.convert_u/i32 + (get_local $0) + ) + (get_local $5) + ) + (get_local $6) + ) + ) (block $break|0 - (set_local $6 + (set_local $8 (i32.const 0) ) (loop $continue|0 (if (i32.lt_u - (get_local $6) + (get_local $8) (get_local $1) ) (block (block - (set_local $7 + (set_local $9 (f64.mul (f64.sub (f64.convert_u/i32 - (get_local $6) + (get_local $8) ) (get_local $4) ) - (get_local $5) + (get_local $6) ) ) - (block $break|1 - (set_local $8 - (i32.const 0) + (block + (set_local $10 + (f64.const 0) ) + (set_local $11 + (f64.const 0) + ) + ) + (set_local $14 + (i32.const 0) + ) + (block $break|1 (loop $continue|1 (if - (i32.lt_u - (get_local $8) - (get_local $0) - ) - (block - (block - (set_local $9 + (f64.le + (f64.add + (tee_local $12 (f64.mul - (f64.sub - (f64.convert_u/i32 - (get_local $8) - ) - (get_local $3) - ) - (get_local $5) + (get_local $10) + (get_local $10) ) ) - (block - (set_local $10 - (f64.const 0) - ) - (set_local $11 - (f64.const 0) - ) - ) - (set_local $14 - (i32.const 0) - ) - (block $break|2 - (loop $continue|2 - (if - (f64.le - (f64.add - (tee_local $12 - (f64.mul - (get_local $10) - (get_local $10) - ) - ) - (tee_local $13 - (f64.mul - (get_local $11) - (get_local $11) - ) - ) - ) - (f64.const 4) - ) - (block - (block - (set_local $15 - (f64.add - (f64.sub - (get_local $12) - (get_local $13) - ) - (get_local $9) - ) - ) - (set_local $11 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - (get_local $10) - ) - (get_local $11) - ) - (get_local $7) - ) - ) - (set_local $10 - (get_local $15) - ) - (if - (i32.ge_u - (get_local $14) - (get_local $2) - ) - (br $break|2) - ) - (set_local $14 - (i32.add - (get_local $14) - (i32.const 1) - ) - ) - ) - (br $continue|2) - ) - ) - ) - ) - (block $break|3 - (set_local $16 - (i32.const 0) - ) - (loop $continue|3 - (if - (i32.lt_s - (get_local $16) - (i32.const 4) - ) - (block - (block - (set_local $15 - (f64.add - (f64.sub - (tee_local $12 - (f64.mul - (get_local $10) - (get_local $10) - ) - ) - (tee_local $13 - (f64.mul - (get_local $11) - (get_local $11) - ) - ) - ) - (get_local $9) - ) - ) - (set_local $11 - (f64.add - (f64.mul - (f64.mul - (f64.const 2) - (get_local $10) - ) - (get_local $11) - ) - (get_local $7) - ) - ) - (set_local $10 - (get_local $15) - ) - ) - (set_local $16 - (i32.add - (get_local $16) - (i32.const 1) - ) - ) - (br $continue|3) - ) - ) - ) - ) - (set_local $15 - (f64.div - (call $~lib/math/JSMath.log - (call $~lib/math/JSMath.log - (f64.sqrt - (f64.add - (f64.mul - (get_local $10) - (get_local $10) - ) - (f64.mul - (get_local $11) - (get_local $11) - ) - ) - ) - ) - ) - (get_global $~lib/math/JSMath.LN2) - ) - ) - (if - (f64.gt - (get_local $15) - (f64.const 0) - ) - (block - (set_local $17 - (f64.max - (f64.min - (f64.div - (f64.sub - (f64.convert_u/i32 - (i32.add - (get_local $14) - (i32.const 1) - ) - ) - (get_local $15) - ) - (f64.convert_u/i32 - (get_local $2) - ) - ) - (f64.const 1) - ) - (f64.const 0) - ) - ) - (i32.store16 - (i32.shl - (i32.add - (i32.mul - (get_local $6) - (get_local $0) - ) - (get_local $8) - ) - (i32.const 1) - ) - (i32.trunc_u/f64 - (f64.mul - (f64.convert_u/i32 - (i32.const 2047) - ) - (get_local $17) - ) - ) - ) - ) - (i32.store16 - (i32.shl - (i32.add - (i32.mul - (get_local $6) - (get_local $0) - ) - (get_local $8) - ) - (i32.const 1) - ) - (i32.div_u - (i32.mul - (get_local $14) - (i32.const 2047) - ) - (get_local $2) - ) + (tee_local $13 + (f64.mul + (get_local $11) + (get_local $11) ) ) ) - (set_local $8 - (i32.add - (get_local $8) - (i32.const 1) + (f64.const 4) + ) + (block + (block + (set_local $15 + (f64.add + (f64.sub + (get_local $12) + (get_local $13) + ) + (get_local $9) + ) + ) + (set_local $11 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + (get_local $10) + ) + (get_local $11) + ) + (get_local $7) + ) + ) + (set_local $10 + (get_local $15) + ) + (if + (i32.ge_u + (get_local $14) + (get_local $3) + ) + (br $break|1) + ) + (set_local $14 + (i32.add + (get_local $14) + (i32.const 1) + ) ) ) (br $continue|1) @@ -340,10 +192,160 @@ ) ) ) + (block $break|2 + (set_local $16 + (i32.const 0) + ) + (loop $continue|2 + (if + (i32.and + (if (result i32) + (tee_local $17 + (i32.lt_s + (get_local $16) + (i32.const 5) + ) + ) + (i32.lt_u + (get_local $14) + (get_local $3) + ) + (get_local $17) + ) + (i32.const 1) + ) + (block + (block + (set_local $15 + (f64.add + (f64.sub + (f64.mul + (get_local $10) + (get_local $10) + ) + (f64.mul + (get_local $11) + (get_local $11) + ) + ) + (get_local $9) + ) + ) + (set_local $11 + (f64.add + (f64.mul + (f64.mul + (f64.const 2) + (get_local $10) + ) + (get_local $11) + ) + (get_local $7) + ) + ) + (set_local $10 + (get_local $15) + ) + ) + (block + (set_local $16 + (i32.add + (get_local $16) + (i32.const 1) + ) + ) + (set_local $14 + (i32.add + (get_local $14) + (i32.const 1) + ) + ) + ) + (br $continue|2) + ) + ) + ) + ) + (set_local $15 + (f64.div + (call $~lib/math/JSMath.log + (call $~lib/math/JSMath.log + (f64.sqrt + (f64.add + (f64.mul + (get_local $10) + (get_local $10) + ) + (f64.mul + (get_local $11) + (get_local $11) + ) + ) + ) + ) + ) + (get_global $~lib/math/JSMath.LN2) + ) + ) + (i32.store16 + (i32.shl + (i32.add + (i32.mul + (get_local $0) + (get_local $1) + ) + (get_local $8) + ) + (i32.const 1) + ) + (if (result i32) + (call $isFinite + (get_local $15) + ) + (i32.trunc_u/f64 + (f64.mul + (f64.convert_s/i32 + (i32.sub + (i32.const 2048) + (i32.const 1) + ) + ) + (call $../../examples/mandelbrot/assembly/index/clamp + (f64.div + (f64.sub + (f64.convert_u/i32 + (i32.add + (get_local $14) + (i32.const 1) + ) + ) + (get_local $15) + ) + (f64.convert_u/i32 + (get_local $3) + ) + ) + (f64.const 0) + (f64.const 1) + ) + ) + ) + (i32.div_s + (i32.mul + (i32.sub + (i32.const 2048) + (i32.const 1) + ) + (get_local $14) + ) + (get_local $3) + ) + ) + ) ) - (set_local $6 + (set_local $8 (i32.add - (get_local $6) + (get_local $8) (i32.const 1) ) )