Last active
November 30, 2015 05:26
-
-
Save yomotsu/4ff8a3f441d16fd81250 to your computer and use it in GitHub Desktop.
handwritten ASM.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ( +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; | |
// --- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool!