Created
November 13, 2022 03:55
-
-
Save zakki/f3c80a8587c8f8132b2dda6ccf9e5128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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')}`); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ 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