Skip to content

Instantly share code, notes, and snippets.

@imaya
Created June 6, 2017 11:58
Show Gist options
  • Save imaya/8c7339f63ac155c65eb189d12a4f9b26 to your computer and use it in GitHub Desktop.
Save imaya/8c7339f63ac155c65eb189d12a4f9b26 to your computer and use it in GitHub Desktop.
Adler32(asm.js)
function adler32_asm(array) {
function asm(stdlib, foreign, heap) {
"use asm";
var array = new stdlib.Uint8Array(heap);
function adler32(length) {
length = length | 0;
return update(1, length) | 0;
}
function update(adler, length) {
adler = adler | 0;
length = length | 0;
var s1 = 0;
var s2 = 0;
var len = 0;
var tlen = 0;
var index = 0;
s1 = adler & 0xffff;
s2 = (adler >>> 16) & 0xffff;
while (length | 0 > 0) {
tlen = (length | 0) > 5550 ? 5550 : length;
length = length - tlen | 0;
do {
s1 = s1 + (array[index] | 0) | 0;
index = index + 1 | 0;
s2 = s1 + s2 | 0;
} while (tlen = tlen - 1 | 0);
s1 = (s1 | 0) % 65521 | 0;
s2 = (s2 | 0) % 65521 | 0;
}
return ((s2 << 16) | s1) | 0;
}
return {adler32: adler32, update: update};
}
return asm(window, {}, array.buffer).adler32(array.length) >>> 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment