Always try to eliminate branches if tree-shaking is enabled

This commit is contained in:
dcodeIO
2018-04-24 23:11:11 +02:00
parent 97e7158fff
commit 391db28fe2
38 changed files with 782 additions and 789 deletions

View File

@ -1,6 +1,7 @@
(module
(type $iiv (func (param i32 i32)))
(type $F (func (result f64)))
(type $i (func (result i32)))
(type $v (func))
(type $iiFv (func (param i32 i32 f64)))
(import "env" "RGB_ALIVE" (global $assembly/index/RGB_ALIVE i32))

View File

@ -1,5 +1,6 @@
(module
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $i (func (result i32)))
(type $FF (func (param f64) (result f64)))
(type $Fi (func (param f64) (result i32)))
(type $FFFF (func (param f64 f64 f64) (result f64)))

View File

@ -1,5 +1,8 @@
import "allocator/arena";
// From The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org
const SOLAR_MASS = 4.0 * Math.PI * Math.PI;
const DAYS_PER_YEAR = 365.24;
@ -79,9 +82,9 @@ function Neptune(): Body {
class NBodySystem {
bodies: Body[];
constructor(bodies: Body[]) {
constructor(
public bodies: Body[]
) {
var px = 0.0;
var py = 0.0;
var pz = 0.0;
@ -93,16 +96,17 @@ class NBodySystem {
py += b.vy * m;
pz += b.vz * m;
}
this.bodies = bodies;
this.bodies[0].offsetMomentum(px, py, pz);
bodies[0].offsetMomentum(px, py, pz);
}
advance(dt: f64): void {
var bodies = this.bodies;
var size = bodies.length;
var size: u32 = bodies.length;
// var buffer = changetype<usize>(bodies.buffer_);
for (let i = 0; i < size; ++i) {
for (let i: u32 = 0; i < size; ++i) {
let bodyi = bodies[i];
// let bodyi = load<Body>(buffer + i * sizeof<Body>(), 8);
let ix = bodyi.x;
let iy = bodyi.y;
@ -113,8 +117,10 @@ class NBodySystem {
let bivz = bodyi.vz;
let bodyim = bodyi.mass;
for (let j = i + 1; j < size; ++j) {
for (let j: u32 = i + 1; j < size; ++j) {
let bodyj = bodies[j];
// let bodyj = load<Body>(buffer + j * sizeof<Body>(), 8);
let dx = ix - bodyj.x;
let dy = iy - bodyj.y;
let dz = iz - bodyj.z;
@ -148,9 +154,8 @@ class NBodySystem {
energy(): f64 {
var e = 0.0;
var bodies = this.bodies;
var size = bodies.length;
for (let i = 0; i < size; ++i) {
for (let i: u32 = 0, size: u32 = bodies.length; i < size; ++i) {
let bodyi = bodies[i];
let ix = bodyi.x;
@ -165,7 +170,7 @@ class NBodySystem {
e += 0.5 * bim * (vx * vx + vy * vy + vz * vz);
for (let j = i + 1; j < size; ++j) {
for (let j: u32 = i + 1; j < size; ++j) {
let bodyj = bodies[j];
let dx = ix - bodyj.x;
let dy = iy - bodyj.y;

View File

@ -679,40 +679,38 @@ function asmFunc(global, env, buffer) {
function assembly_index_NBodySystem_constructor($0, $1) {
$0 = $0 | 0;
$1 = $1 | 0;
var $2 = 0, $3 = 0, $5 = 0, $4 = 0.0, $6 = 0.0, $7 = 0.0, $8 = 0.0, $9 = 0, $9 = 0, $11 = 0, $12 = 0;
$2 = $1;
$9 = HEAP32[($2 + 4 | 0) >> 2] | 0;
var $2 = 0, $3 = 0, $4 = 0.0, $9 = 0, $5 = 0.0, $6 = 0.0, $7 = 0.0, $9 = 0, $8 = 0, $11 = 0, $12 = 0;
$3 = $1;
$8 = HEAP32[($3 + 4 | 0) >> 2] | 0;
continue_0 : do {
if (($3 | 0) < ($9 | 0)) {
$2 = $3;
$5 = $1;
$5 = HEAPU32[$5 >> 2] | 0;
if ($2 >>> 0 < ((HEAP32[$5 >> 2] | 0) >>> 2 | 0) >>> 0) $11 = HEAPU32[(($5 + ($2 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
$2 = $11;
$4 = +HEAPF64[($2 + 48 | 0) >> 3];
$6 = $6 + +HEAPF64[($2 + 24 | 0) >> 3] * $4;
$7 = $7 + +HEAPF64[($2 + 32 | 0) >> 3] * $4;
$8 = $8 + +HEAPF64[($2 + 40 | 0) >> 3] * $4;
$3 = $3 + 1 | 0;
if (($2 | 0) < ($8 | 0)) {
$9 = $2;
$3 = $1;
$3 = HEAPU32[$3 >> 2] | 0;
if ($9 >>> 0 < ((HEAP32[$3 >> 2] | 0) >>> 2 | 0) >>> 0) $11 = HEAPU32[(($3 + ($9 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
$3 = $11;
$4 = +HEAPF64[($3 + 48 | 0) >> 3];
$5 = $5 + +HEAPF64[($3 + 24 | 0) >> 3] * $4;
$6 = $6 + +HEAPF64[($3 + 32 | 0) >> 3] * $4;
$7 = $7 + +HEAPF64[($3 + 40 | 0) >> 3] * $4;
$2 = $2 + 1 | 0;
continue continue_0;
}
break continue_0;
} while (1);
$2 = $1;
$2 = HEAPU32[$2 >> 2] | 0;
if (0 >>> 0 < ((HEAP32[$2 >> 2] | 0) >>> 2 | 0) >>> 0) $12 = HEAPU32[(($2 + 0 | 0) + 8 | 0) >> 2] | 0; else abort();
$2 = $12;
HEAPF64[($2 + 24 | 0) >> 3] = -$5 / 39.47841760435743;
HEAPF64[($2 + 32 | 0) >> 3] = -$6 / 39.47841760435743;
HEAPF64[($2 + 40 | 0) >> 3] = -$7 / 39.47841760435743;
if ($0) $9 = $0; else {
$3 = $lib_allocator_arena_allocate_memory(4 | 0) | 0;
HEAP32[$3 >> 2] = 0;
$0 = $3;
$9 = $0;
$2 = $lib_allocator_arena_allocate_memory(4 | 0) | 0;
HEAP32[$2 >> 2] = $1;
$9 = $2;
}
HEAP32[$9 >> 2] = $1;
$1 = HEAPU32[$0 >> 2] | 0;
$1 = HEAPU32[$1 >> 2] | 0;
if (0 >>> 0 < ((HEAP32[$1 >> 2] | 0) >>> 2 | 0) >>> 0) $12 = HEAPU32[(($1 + 0 | 0) + 8 | 0) >> 2] | 0; else abort();
$1 = $12;
HEAPF64[($1 + 24 | 0) >> 3] = -$6 / 39.47841760435743;
HEAPF64[($1 + 32 | 0) >> 3] = -$7 / 39.47841760435743;
HEAPF64[($1 + 40 | 0) >> 3] = -$8 / 39.47841760435743;
return $0 | 0;
return $9 | 0;
}
function assembly_index_init() {
@ -746,7 +744,7 @@ function asmFunc(global, env, buffer) {
$0 = $13;
$14 = HEAP32[($0 + 4 | 0) >> 2] | 0;
continue_0 : do {
if (($6 | 0) < ($14 | 0)) {
if ($6 >>> 0 < $14 >>> 0) {
$3 = $6;
$0 = $13;
$0 = HEAPU32[$0 >> 2] | 0;
@ -761,7 +759,7 @@ function asmFunc(global, env, buffer) {
$18 = +HEAPF64[($0 + 48 | 0) >> 3];
$3 = $6 + 1 | 0;
continue_1 : do {
if (($3 | 0) < ($14 | 0)) {
if ($3 >>> 0 < $14 >>> 0) {
$21 = $15;
$19 = $3;
$2 = $13;
@ -807,7 +805,7 @@ function asmFunc(global, env, buffer) {
$0 = $4;
$5 = HEAP32[($0 + 4 | 0) >> 2] | 0;
continue_0 : do {
if (($3 | 0) < ($5 | 0)) {
if ($3 >>> 0 < $5 >>> 0) {
$2 = $3;
$0 = $4;
$0 = HEAPU32[$0 >> 2] | 0;
@ -826,7 +824,7 @@ function asmFunc(global, env, buffer) {
$1 = $13 + .5 * $10 * ($15 + $1 * $1);
$0 = $3 + 1 | 0;
continue_1 : do {
if (($0 | 0) < ($5 | 0)) {
if ($0 >>> 0 < $5 >>> 0) {
$16 = $7;
$11 = $0;
$2 = $4;

View File

@ -116,13 +116,13 @@ class NBodySystem {
}
var system;
function init() {
var bodies = new Array();
bodies.push(Sun());
bodies.push(Jupiter());
bodies.push(Saturn());
bodies.push(Uranus());
bodies.push(Neptune());
system = new NBodySystem(bodies);
system = new NBodySystem([
Sun(),
Jupiter(),
Saturn(),
Uranus(),
Neptune()
]);
}
exports.init = init;
function getBody(index) {

Binary file not shown.

View File

@ -2601,14 +2601,14 @@
(local $2 i32)
(local $3 i32)
(local $4 f64)
(local $5 i32)
(local $5 f64)
(local $6 f64)
(local $7 f64)
(local $8 f64)
(local $8 i32)
(local $9 i32)
(set_local $9
(set_local $8
(i32.load offset=4
(tee_local $2
(tee_local $3
(get_local $1)
)
)
@ -2616,23 +2616,23 @@
(loop $continue|0
(if
(i32.lt_s
(get_local $3)
(get_local $9)
(get_local $2)
(get_local $8)
)
(block
(set_local $4
(f64.load offset=48
(tee_local $2
(tee_local $3
(if (result i32)
(i32.lt_u
(tee_local $2
(get_local $3)
(tee_local $9
(get_local $2)
)
(i32.shr_u
(i32.load
(tee_local $5
(tee_local $3
(i32.load
(tee_local $5
(tee_local $3
(get_local $1)
)
)
@ -2643,9 +2643,9 @@
)
(i32.load offset=8
(i32.add
(get_local $5)
(get_local $3)
(i32.shl
(get_local $2)
(get_local $9)
(i32.const 2)
)
)
@ -2655,12 +2655,23 @@
)
)
)
(set_local $5
(f64.add
(get_local $5)
(f64.mul
(f64.load offset=24
(get_local $3)
)
(get_local $4)
)
)
)
(set_local $6
(f64.add
(get_local $6)
(f64.mul
(f64.load offset=24
(get_local $2)
(f64.load offset=32
(get_local $3)
)
(get_local $4)
)
@ -2669,28 +2680,17 @@
(set_local $7
(f64.add
(get_local $7)
(f64.mul
(f64.load offset=32
(get_local $2)
)
(get_local $4)
)
)
)
(set_local $8
(f64.add
(get_local $8)
(f64.mul
(f64.load offset=40
(get_local $2)
(get_local $3)
)
(get_local $4)
)
)
)
(set_local $3
(set_local $2
(i32.add
(get_local $3)
(get_local $2)
(i32.const 1)
)
)
@ -2698,39 +2698,17 @@
)
)
)
(i32.store
(if (result i32)
(get_local $0)
(get_local $0)
(block (result i32)
(i32.store
(tee_local $3
(call $~lib/allocator/arena/allocate_memory
(i32.const 4)
)
)
(i32.const 0)
)
(tee_local $0
(get_local $3)
)
)
)
(get_local $1)
)
(f64.store offset=24
(tee_local $1
(tee_local $2
(if (result i32)
(i32.lt_u
(i32.const 0)
(i32.shr_u
(i32.load
(tee_local $1
(tee_local $2
(i32.load
(tee_local $1
(i32.load
(get_local $0)
)
(tee_local $2
(get_local $1)
)
)
)
@ -2740,7 +2718,7 @@
)
(i32.load offset=8
(i32.add
(get_local $1)
(get_local $2)
(i32.const 0)
)
)
@ -2749,13 +2727,22 @@
)
(f64.div
(f64.neg
(get_local $6)
(get_local $5)
)
(f64.const 39.47841760435743)
)
)
(f64.store offset=32
(get_local $1)
(get_local $2)
(f64.div
(f64.neg
(get_local $6)
)
(f64.const 39.47841760435743)
)
)
(f64.store offset=40
(get_local $2)
(f64.div
(f64.neg
(get_local $7)
@ -2763,16 +2750,21 @@
(f64.const 39.47841760435743)
)
)
(f64.store offset=40
(get_local $1)
(f64.div
(f64.neg
(get_local $8)
(if (result i32)
(get_local $0)
(get_local $0)
(block (result i32)
(i32.store
(tee_local $2
(call $~lib/allocator/arena/allocate_memory
(i32.const 4)
)
)
(get_local $1)
)
(f64.const 39.47841760435743)
(get_local $2)
)
)
(get_local $0)
)
(func $assembly/index/init (; 11 ;) (type $v)
(local $0 i32)
@ -2933,7 +2925,7 @@
)
(loop $continue|0
(if
(i32.lt_s
(i32.lt_u
(get_local $6)
(get_local $14)
)
@ -3011,7 +3003,7 @@
)
(loop $continue|1
(if
(i32.lt_s
(i32.lt_u
(get_local $3)
(get_local $14)
)
@ -3267,7 +3259,7 @@
)
(loop $continue|0
(if
(i32.lt_s
(i32.lt_u
(get_local $3)
(get_local $5)
)
@ -3368,7 +3360,7 @@
)
(loop $continue|1
(if
(i32.lt_s
(i32.lt_u
(get_local $0)
(get_local $5)
)

File diff suppressed because it is too large Load Diff