Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Last active November 30, 2015 05:26
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 yomotsu/4ff8a3f441d16fd81250 to your computer and use it in GitHub Desktop.
Save yomotsu/4ff8a3f441d16fd81250 to your computer and use it in GitHub Desktop.
handwritten ASM.js
// ( +n ) // double
// ( n|0 ) // signed int
function calc_module () {
'use asm';
function sumInt( n1, n2 ) {
n1 = n1|0;
n2 = n2|0;
return ( n1 + n2 )|0;
}
return { sumInt: sumInt };
}
console.log( calc_module().sumInt( 1, 2 ) );
// CONSOLE
// -----------------
// Successfully compiled asm.js code (total compilation time 0ms; not stored in cache) asm.html
// 3
// -----------------
function calc_module2 ( stdlib ) {
'use asm';
const PI = stdlib.Math.PI;
function sumInt( n1, n2 ) {
n1 = n1|0;
n2 = n2|0;
return ( n1 + n2 )|0;
}
function degToRad ( deg ) {
deg = +deg;
return +( deg * PI / 180.0 );
}
return {
sumInt: sumInt,
degToRad: degToRad
};
}
console.log( calc_module2( window ).degToRad( 180 ) );
// CONSOLE
// -----------------
// Successfully compiled asm.js code (total compilation time 0ms; not stored in cache) asm.html
// 3.141592653589793
// -----------------
// Double to Int with `~~`
// ---
// var a = 1|0;
// var b = 0;
//
// b = ~~a + 0.5;
// ---
@max-mapper
Copy link

very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment