Asm.js: Difference between revisions
Jump to navigation
Jump to search
Adamcolton (talk | contribs) Created page with "Uses a prologue directive function MyAsmModule() { "use asm"; // module body } Example function GeometricMean(stdlib, foreign, buffer) { "use asm"; var..." |
Adamcolton (talk | contribs) No edit summary |
||
| Line 8: | Line 8: | ||
function GeometricMean(stdlib, foreign, buffer) { | function GeometricMean(stdlib, foreign, buffer) { | ||
"use asm"; | "use asm"; | ||
var exp = stdlib.Math.exp; | var exp = stdlib.Math.exp; | ||
var log = stdlib.Math.log; | var log = stdlib.Math.log; | ||
var values = new stdlib.Float64Array(buffer); | var values = new stdlib.Float64Array(buffer); | ||
function logSum(start, end) { | function logSum(start, end) { | ||
start = start|0; | start = start|0; | ||
end = end|0; | end = end|0; | ||
var sum = 0.0, p = 0, q = 0; | var sum = 0.0, p = 0, q = 0; | ||
// asm.js forces byte addressing of the heap by requiring shifting by 3 | // asm.js forces byte addressing of the heap by requiring shifting by 3 | ||
for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) { | for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) { | ||
sum = sum + +log(values[p>>3]); | sum = sum + +log(values[p>>3]); | ||
} | } | ||
return +sum; | return +sum; | ||
} | } | ||
function geometricMean(start, end) { | function geometricMean(start, end) { | ||
start = start|0; | start = start|0; | ||
| Line 33: | Line 33: | ||
return +exp(+logSum(start, end) / +((end - start)|0)); | return +exp(+logSum(start, end) / +((end - start)|0)); | ||
} | } | ||
return { geometricMean: geometricMean }; | return { geometricMean: geometricMean }; | ||
} | } | ||
Revision as of 17:55, 8 August 2015
Uses a prologue directive
function MyAsmModule() {
"use asm";
// module body
}
Example
function GeometricMean(stdlib, foreign, buffer) {
"use asm";
var exp = stdlib.Math.exp;
var log = stdlib.Math.log;
var values = new stdlib.Float64Array(buffer);
function logSum(start, end) {
start = start|0;
end = end|0;
var sum = 0.0, p = 0, q = 0;
// asm.js forces byte addressing of the heap by requiring shifting by 3
for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {
sum = sum + +log(values[p>>3]);
}
return +sum;
}
function geometricMean(start, end) {
start = start|0;
end = end|0;
return +exp(+logSum(start, end) / +((end - start)|0));
}
return { geometricMean: geometricMean };
}