Skip to content

Instantly share code, notes, and snippets.

@zerobias
Created June 29, 2017 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerobias/6114c9eeae7de17995bbba6b811cabd9 to your computer and use it in GitHub Desktop.
Save zerobias/6114c9eeae7de17995bbba6b811cabd9 to your computer and use it in GitHub Desktop.
asm.js full module with heap allocation
function fooASM(stdlib,foreign,heap) {
"use asm";
var arr = new stdlib.Int32Array( heap );
function foo(x,y) {
x = x | 0;
y = y | 0;
var i = 0;
var p = 0;
var sum = 0;
var count = ((y|0) - (x|0)) | 0;
// calculate all the inner adjacent multiplications
for (i = x | 0;
(i | 0) < (y | 0);
p = (p + 8) | 0, i = (i + 1) | 0
) {
// store result
arr[ p >> 3 ] = (i * (i + 1)) | 0;
}
// calculate average of all intermediate values
for (i = 0, p = 0;
(i | 0) < (count | 0);
p = (p + 8) | 0, i = (i + 1) | 0
) {
sum = (sum + arr[ p >> 3 ]) | 0;
}
return +(sum / count);
}
return {
foo: foo
};
}
var heap = new ArrayBuffer( 0x1000 );
var foo = fooASM( window, null, heap ).foo;
foo( 10, 20 ); // 233
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment