Skip to content

Commit a1e2071

Browse files
committed
Fix n-body visualization
1 parent 813d2e3 commit a1e2071

File tree

15 files changed

+134
-43
lines changed

15 files changed

+134
-43
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ Examples
7070
* **[WASM parser](./lib/parse)**<br />
7171
A WebAssembly binary parser in WebAssembly.
7272

73-
Benchmarks
74-
---------
75-
* **[n-body](./examples/n-body)**<br />
76-
Compare performance and produced binary size with n-body example from Computer Language Benchmarks Game.
73+
* **[N-body system](./examples/n-body)** [ [demo](https://rawgit.com/AssemblyScript/assemblyscript/master/examples/n-body/index.html) ]<br />
74+
An implementation of the N-body system from the [Computer Language Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/).
7775

7876
Building
7977
--------

dist/asc.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

dist/asc.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/assemblyscript.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

dist/assemblyscript.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/n-body/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
N-Body-System
1+
N-body system
22
=============
33

44
An [AssemblyScript](http://assemblyscript.org) example. This is actually a benchmark - visualizing it just so happened.

examples/n-body/assembly/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,8 @@ export function step(): float {
203203
export function bench(steps: u32): void {
204204
for (let i: u32 = 0; i < steps; ++i) system.advance(0.01);
205205
}
206+
207+
export function getBody(index: i32): Body | null {
208+
var bodies = system.bodies;
209+
return <u32>index < <u32>bodies.length ? bodies[index] : null;
210+
}

examples/n-body/build/index.asm.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ function asmFunc(global, env, buffer) {
477477
};
478478
}
479479

480+
function assembly_index_getBody($0) {
481+
$0 = $0 | 0;
482+
var $1 = 0, $22 = 0, $20 = 0;
483+
$1 = HEAPU32[assembly_index_system >> 2] | 0;
484+
if ($0 >>> 0 < (HEAP32[($1 + 4 | 0) >> 2] | 0) >>> 0) {
485+
$1 = HEAPU32[$1 >> 2] | 0;
486+
if ($0 >>> 0 < ((HEAP32[$1 >> 2] | 0) >>> 2 | 0) >>> 0) $20 = HEAPU32[(($1 + ($0 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
487+
$22 = $20;
488+
} else $22 = 0;
489+
$0 = $22;
490+
return $0 | 0;
491+
}
492+
480493
function start() {
481494
$lib_allocator_arena_startOffset = 40;
482495
$lib_allocator_arena_offset = $lib_allocator_arena_startOffset;
@@ -521,6 +534,7 @@ function asmFunc(global, env, buffer) {
521534
}),
522535
init: assembly_index_init,
523536
step: assembly_index_step,
524-
bench: assembly_index_bench
537+
bench: assembly_index_bench,
538+
getBody: assembly_index_getBody
525539
};
526540
}

examples/n-body/build/optimized.wasm

69 Bytes
Binary file not shown.

examples/n-body/build/optimized.wat

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
(export "init" (func $assembly/index/init))
2020
(export "step" (func $assembly/index/step))
2121
(export "bench" (func $assembly/index/bench))
22+
(export "getBody" (func $assembly/index/getBody))
2223
(start $start)
2324
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32)
2425
(local $1 i32)
@@ -1356,7 +1357,50 @@
13561357
)
13571358
)
13581359
)
1359-
(func $start (; 11 ;) (type $v)
1360+
(func $assembly/index/getBody (; 11 ;) (type $ii) (param $0 i32) (result i32)
1361+
(local $1 i32)
1362+
(tee_local $0
1363+
(if (result i32)
1364+
(i32.lt_u
1365+
(get_local $0)
1366+
(i32.load offset=4
1367+
(tee_local $1
1368+
(i32.load
1369+
(get_global $assembly/index/system)
1370+
)
1371+
)
1372+
)
1373+
)
1374+
(if (result i32)
1375+
(i32.lt_u
1376+
(get_local $0)
1377+
(i32.shr_u
1378+
(i32.load
1379+
(tee_local $1
1380+
(i32.load
1381+
(get_local $1)
1382+
)
1383+
)
1384+
)
1385+
(i32.const 2)
1386+
)
1387+
)
1388+
(i32.load offset=8
1389+
(i32.add
1390+
(get_local $1)
1391+
(i32.shl
1392+
(get_local $0)
1393+
(i32.const 2)
1394+
)
1395+
)
1396+
)
1397+
(unreachable)
1398+
)
1399+
(i32.const 0)
1400+
)
1401+
)
1402+
)
1403+
(func $start (; 12 ;) (type $v)
13601404
(set_global $~lib/allocator/arena/startOffset
13611405
(i32.const 40)
13621406
)

0 commit comments

Comments
 (0)