Make the mandelbrot example a bit more fun as well

This commit is contained in:
dcodeIO 2018-04-20 15:36:01 +02:00
parent b53b3e08ec
commit 4eade0f319
9 changed files with 1348 additions and 1333 deletions

View File

@ -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<f64>(min<f64>((<f64>(iteration + 1) - frac) / limit, 1.0), 0.0);
store<u16>((y * width + x) << 1, <u32>(2047 * norm));
} else {
store<u16>((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<u16>((y * width + x) << 1,
isFinite(frac)
? <u32>((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<T>(value: T, minValue: T, maxValue: T): T {
return min(max(value, minValue), maxValue);
}

View File

@ -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)
)
)

View File

@ -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<f64> (; 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<f64> (; 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<f64>
;;@ 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<f64>
;;@ 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)
)
)

View File

@ -1,60 +1,99 @@
<!DOCTYPE html>
<html>
<head>
<title>Mandelbrot Set - AssemblyScript</title>
<title>Mandelbrot set - AssemblyScript</title>
<style>
html, body { height: 100%; margin: 0; overflow: hidden; color: #111; background: #fff; font-family: sans-serif; }
h1 { padding: 20px; font-size: 12pt; margin: 0; }
a { color: #111; text-decoration: none; }
a:hover { color: #0074C1; text-decoration: underline; }
canvas { position: absolute; top: 60px; left: 20px; width: calc(100% - 40px); height: calc(100% - 80px); background: #eee; }
canvas.gradient { left: 0; top: 0px; height: 2px; width: 100%; }
</style>
</head>
<body>
<h1>
<a href="https://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set</a> in
<a href="https://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot set</a> in
<a href="http://assemblyscript.org">AssemblyScript</a>
( <a href="https://github.com/AssemblyScript/assemblyscript/blob/master/examples/mandelbrot/assembly/index.ts">source</a> )
</h1>
<canvas></canvas>
<script>"use strict";
// Set up a 2D rendering context
// Set this to false if you prefer a plain image instead.
var animate = true;
// Set up the canvas with a 2D rendering context
var cnv = document.getElementsByTagName("canvas")[0];
var ctx = cnv.getContext("2d");
var bcr = cnv.getBoundingClientRect();
cnv.width = bcr.width | 0;
cnv.height = bcr.height | 0;
// Compute the size of the viewport
var width = bcr.width | 0;
var height = bcr.height | 0;
var size = width * height;
var byteSize = size << 1; // discrete color indices in range [0, 2047] (here: 2b per pixel)
cnv.width = width;
cnv.height = height;
// Compute the size of and instantiate the module's memory
var memory = new WebAssembly.Memory({ initial: ((byteSize + 0xffff) & ~0xffff) >>> 16 });
var mem = new Uint16Array(memory.buffer);
var imageData = ctx.createImageData(width, height);
var argb = new Uint32Array(imageData.data.buffer);
// Fetch and instantiate the module
fetch("build/optimized.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, { JSMath: Math }))
.then(buffer => WebAssembly.instantiate(buffer, {
env: { memory: memory },
JSMath: Math
}))
.then(module => {
var exports = module.instance.exports;
var memory = exports.memory;
var computeLine = exports.computeLine;
var updateLine = function(y) {
var yx = y * width;
for (let x = 0; x < width; ++x) argb[yx + x] = colors[mem[yx + x]];
};
// Determine required memory size and grow if necessary
var width = cnv.width | 0;
var height = cnv.height | 0;
var byteSize = 2 * width * height; // 2b per pixel (0..2041)
if (memory.buffer.byteLength < byteSize) {
let pages = ((byteSize - memory.buffer.byteLength + 0xffff) & ~0xffff) >>> 16;
memory.grow(pages);
}
// Compute and render to the canvas
exports.compute(width, height, 40);
var mem = new Uint16Array(memory.buffer);
var imageData = ctx.createImageData(width, height);
var argb = new Uint32Array(imageData.data.buffer);
// Compute an initial balanced version of the set.
const limit = 40;
for (let y = 0; y < height; ++y) {
let yx = y * width;
for (let x = 0; x < width; ++x) {
argb[yx + x] = colors[mem[yx + x]];
}
computeLine(y, width, height, limit);
updateLine(y);
}
// Keep rendering the image buffer.
(function render() {
if (animate) requestAnimationFrame(render);
ctx.putImageData(imageData, 0, 0);
})();
if (animate) {
// Let it glow a bit by occasionally shifting the limit...
var currentLimit = limit;
var shiftRange = 10;
(function updateShift() {
currentLimit = limit + (2 * Math.random() * shiftRange - shiftRange) | 0
setTimeout(updateShift, 1000 + (1500 * Math.random()) | 0);
})();
// ...while continously recomputing a subset of it.
var flickerRange = 3;
(function updateFlicker() {
for (let i = 0, k = (0.05 * height) | 0; i < k; ++i) {
let ry = (Math.random() * height) | 0;
let rl = (2 * Math.random() * flickerRange - flickerRange) | 0;
computeLine(ry, width, height, currentLimit + rl);
updateLine(ry);
}
setTimeout(updateFlicker, 1000 / 30);
})();
}
ctx.putImageData(imageData, 0, 0);
}).catch(err => {
alert("Failed to load WASM: " + err.message + " (ad blocker, maybe?)");
console.log(err.stack);
@ -67,13 +106,15 @@ var colors = (() => {
cnv.height = 1;
var ctx = cnv.getContext("2d");
var grd = ctx.createLinearGradient(0, 0, 2048, 0);
grd.addColorStop(0, "#000764");
grd.addColorStop(0.25, "#2068CB");
grd.addColorStop(0.50, "#EDFFFF");
grd.addColorStop(0.65, "#FFAA00");
grd.addColorStop(0.85, "#000200");
grd.addColorStop(0.00, "#000764");
grd.addColorStop(0.16, "#2068CB");
grd.addColorStop(0.42, "#EDFFFF");
grd.addColorStop(0.6425, "#FFAA00");
grd.addColorStop(0.8575, "#000200");
ctx.fillStyle = grd;
ctx.fillRect(0, 0, 2048, 1);
cnv.className = "gradient";
setTimeout(() => document.body.appendChild(cnv));
return new Uint32Array(ctx.getImageData(0, 0, 2048, 1).data.buffer);
})();
</script>

View File

@ -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"
},

View File

@ -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<f64> (; 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<f64> (; 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<f64>
(get_local $7)
)
(i32.trunc_u/f64
(f64.mul
(f64.const 2047)
(call $../../examples/mandelbrot/assembly/index/clamp<f64>
(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)
)
)

View File

@ -1 +1 @@
export { compute } from "../../examples/mandelbrot/assembly/index";
export { computeLine } from "../../examples/mandelbrot/assembly/index";

View File

@ -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<f64> (; 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<f64> (; 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<f64>
(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>
(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)
)
)