Skip to content

Instantly share code, notes, and snippets.

@zakki
Created November 13, 2022 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakki/f3c80a8587c8f8132b2dda6ccf9e5128 to your computer and use it in GitHub Desktop.
Save zakki/f3c80a8587c8f8132b2dda6ccf9e5128 to your computer and use it in GitHub Desktop.
#include <emscripten.h>
#include <stdio.h>
#include <stdlib.h>
static uint64_t x;
EMSCRIPTEN_KEEPALIVE uint64_t xorShiftSeed(int initialState) {
x = initialState;// & 0xFFFFFFFFFFFFFFFFL;
return x;
}
EMSCRIPTEN_KEEPALIVE uint64_t xorShift1() {
x ^= (x << 13);
x ^= x >> 7;
x ^= (x << 17);
return x;
}
EMSCRIPTEN_KEEPALIVE uint64_t* xorShift2(int n) {
uint64_t* a = (uint64_t*)malloc(sizeof(uint64_t) * n);
for (int i = 0; i < n; i++) {
a[i] = xorShift1();
}
return a;
}
int main() {
EM_ASM(
_xorShiftSeed(1);
const a1 = [];
console.time("xorShift1");
for (let i = 0; i < 10000000; i += 1) {
a1.push(_xorShift1());
}
console.timeEnd("xorShift1");
_xorShiftSeed(1);
console.time("xorShift2");
var a2 = _xorShift2(10000000);
console.timeEnd("xorShift2");
console.log(typeof(a2));
console.log(a2);
for (let i = 0; i < 10; i++) {
console.log(`${Module.getValue(a2+i*4, 'i32')}`);
}
);
}
@zakki
Copy link
Author

zakki commented Nov 13, 2022

$ emcc rand.c -o rand.js -sEXPORTED_FUNCTIONS=["_xorShiftSeed","_xorShift1","_xorShift2","getValue","_main"] -sINITIAL_MEMORY=100mb -O2 -sWASM_BIGINT

xorShift1: 821.662ms
xorShift2: 25.896ms
number
5244824
1082269761
0
201397313
268452102
1854285353
-1692498897
1432191013
-178981629
-1873178011
-2046025808

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment