Skip to content

Instantly share code, notes, and snippets.

@oupo
Created October 13, 2014 13:07
Show Gist options
  • Save oupo/a250f4d1075133c737f2 to your computer and use it in GitHub Desktop.
Save oupo/a250f4d1075133c737f2 to your computer and use it in GitHub Desktop.
/* emcc u64mul.cpp -o u64mul.js -s EXPORTED_FUNCTIONS="['_u64mul']" -g0 */
#include <stdio.h>
#include <stdint.h>
#include <emscripten.h>
typedef uint32_t u32;
typedef uint64_t u64;
extern "C" {
void u64mul(u32 ahi, u32 alo, u32 bhi, u32 blo, u32 *pc) {
u64 a = (u64)ahi << 32 | alo;
u64 b = (u64)bhi << 32 | blo;
u64 c = a * b;
pc[0] = c >> 32;
pc[1] = (u32)c;
}
}
<!doctype html>
<meta charset="utf-8">
<title>u64mul.html</title>
<script type="text/javascript" src="u64mul.js"></script>
<script>
/*
A = 0x5d588b656c078965;
AA = A * A % 2**64
p [A >> 32, A % 2**32]
=> [1566083941, 1812433253]
p [AA >> 32, AA % 2**32]
=> [1915515864, 88293849]
*/
window.onload = function () {
var ahi, alo, bhi, blo;
var ahi = bhi = 0x5d588b65;
var alo = blo = 0x6c078965;
var ptr = Module._malloc(8);
Module.ccall("u64mul", null,
["number", "number", "number", "number", "number"],
[ahi, alo, bhi, blo, ptr]);
alert(Module.getValue(ptr, "i32")+", "+Module.getValue(ptr + 4, "i32"));
Module._free(ptr);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment