Emscripten, asm.js, and billions of math ops
-
Upload
luka-zakrajsek -
Category
Technology
-
view
274 -
download
0
Transcript of Emscripten, asm.js, and billions of math ops
EMSCRIPTEN, ASM.JSAND BILLIONS OF MATH OPS
Luka Zakrajšek
@bancek
Webcamp Ljubljana 2016
March 12, 2016
PROBLEMmusic transcription - from audio samples to music notes
⬇
ASM.JSan extraordinarily optimizable, low-level subset of JavaScript
"Assembler for Web"
EMSCRIPTENLLVM-based project that compiles C and C++ into highly-
optimizable JavaScript in asm.js format
It lets you run C and C++ on the web at near-native speed,without plugins.
C CODE#include<stdio.h>
int main() { printf("hello, world!\n"); return 0; }
COMPILE$ emcc hello_world.c
And run:$ node a.out.js hello, world!
C LIBRARY EXAMPLE
var samplesData = Module._malloc(samples.length * 8); var outJson = Module._malloc(4); var outJsonSize = Module._malloc(4);
Module.HEAPF64.set(samples, samplesData / 8); Module.ccall('samplesToTtm', 'number', ['number', 'number', 'number', 'number', [samplesData, samplesCount, sampleRate, layer, outJson, outJsonSize]); Module._free(samplesData);
var jsonSize = Module.getValue(outJsonSize, 'i32'); var jsonPtr = Module.getValue(outJson, 'i32'); var jsonBytes = Module.HEAPU8.subarray(jsonPtr, jsonPtr + jsonSize); var json = new TextDecoder('utf-8').decode(jsonBytes);
JSON.parse(json);
SOME NUMBERS180s × 41 000 samples per second = 7 380 000 samples
3 600 hops × 345 buckets × 4 410 samples × 2 =
10 954 440 000 multiplications
RESULTS
DEMO