emscripten, asm.js, and billions of math ops
TRANSCRIPT
![Page 1: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/1.jpg)
EMSCRIPTEN, ASM.JSAND BILLIONS OF MATH OPS
Luka Zakrajšek
@bancek
Webcamp Ljubljana 2016
March 12, 2016
![Page 2: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/2.jpg)
PROBLEMmusic transcription - from audio samples to music notes
⬇
![Page 3: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/3.jpg)
ASM.JSan extraordinarily optimizable, low-level subset of JavaScript
"Assembler for Web"
![Page 4: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/4.jpg)
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.
![Page 5: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/5.jpg)
C CODE#include<stdio.h>
int main() { printf("hello, world!\n"); return 0; }
![Page 6: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/6.jpg)
COMPILE$ emcc hello_world.c
And run:$ node a.out.js hello, world!
![Page 7: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/7.jpg)
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);
![Page 8: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/8.jpg)
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
![Page 9: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/9.jpg)
RESULTS
![Page 10: Emscripten, asm.js, and billions of math ops](https://reader037.vdocuments.us/reader037/viewer/2022100204/58f060a11a28abb87d8b4605/html5/thumbnails/10.jpg)
DEMO