Skip to content

Commit

Permalink
Minor improvments and fixes (#2)
Browse files Browse the repository at this point in the history
* updates

* more improvments

* remove comments

* revert mult ops eliminations
  • Loading branch information
MaxGraey authored and krisselden committed Jan 2, 2019
1 parent e5e290b commit bc56b66
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
27 changes: 11 additions & 16 deletions assembly/index.ts
Expand Up @@ -17,37 +17,32 @@ export function initState(seed: f64): void {
s1 = splitmix64(s0);
}

// Random between 0 and 9007199254740991 inclusive.
// 9007199254740991 == Number.MAX_SAFE_INTEGER
@inline
function nextUint53(): f64 {
return <f64>(nextUint64() >> 11);
}

// Random between 0 and 0.9999999999999999 inclusive.
// This matches Math.random() >= 0 and < 1.
// A uniform random between 0-9 would be `Math.floor(next() * 10)`
// To be inclusive of 10 you would do `Math.floor(next() * 11)`. Don't
// use `Math.round(next() * 10)` as it will split the probability between
// 0 and 10.
export function next(): f64 {
// MAX_SAFE_INTEGER == 9007199254740991
// 9007199254740992 would be 1.0
// 9007199254740991 / 9007199254740992 == 0.9999999999999999
// 2^-53 == 1 / 9007199254740992 == 1.1102230246251565e-16
return nextUint53() * 1.1102230246251565e-16;
return reinterpret<f64>(<u64>0x3FF << 52 | nextUint64() >> 12) - 1.0;
}

// Random u64 using xoroshiro128starstar
// 2018 by David Blackman and Sebastiano Vigna (public domain)
// http://xoshiro.di.unimi.it/xoroshiro128starstar.c
@inline
function nextUint64(): u64 {
let result: u64 = rotl<u64>(s0 * 5, 7) * 9;
let _s0 = s0;
let _s1 = s1;

let result = rotl(_s0 * 5, 7) * 9;

_s1 ^= _s0;
_s0 = rotl(_s0, 24) ^ _s1 ^ (_s1 << 16); // a, b
_s1 = rotl(_s1, 37); // c

s1 ^= s0;
s0 = rotl<u64>(s0, 24) ^ s1 ^ (s1 << 16); // a, b
s1 = rotl<u64>(s1, 37); // c
s0 = _s0;
s1 = _s1;

return result;
}
8 changes: 4 additions & 4 deletions bench.js
@@ -1,15 +1,15 @@
/* globals: mathrandom, wasmrandom, seedrandom, freq, gc */
mathrandom = Math.random;
wasmrandom = require("./index")(1234567);
seedrandom = require("seedrandom")("hello.");
global.mathrandom = Math.random;
global.wasmrandom = require("./index")(1234567);
global.seedrandom = require("seedrandom")("hello.");

// ensure we do the same test, and name === function
function bench(name) {
return {
name: `${name} (0 <= int < 10)`,
onStart: () => {
if (typeof gc === "function") gc();
freq = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
global.freq = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
},
onComplete: () => {
// check counts uniformly distributed
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -29,6 +29,7 @@ const { random: defaultSeed } = Math;
* used to initialize the 128 bit state.
*/
module.exports = function xoroshiro128starstar(seed) {
"use strict";
const instance = new WebAssembly.Instance(wasmModule);
const { initState, next } = instance.exports;
if (typeof seed !== "number" || seed === 0) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -18,11 +18,11 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"prepare": "asc --baseDir assembly -Oz -b index.wasm -t index.wat index.ts --sourceMap --noDebug"
"prepare": "asc --baseDir assembly -O3 -b index.wasm -t index.wat index.ts --validate --sourceMap"
},
"devDependencies": {
"@types/node": "^10.7.1",
"assemblyscript": "AssemblyScript/assemblyscript",
"assemblyscript": "github:AssemblyScript/assemblyscript",
"do-you-even-bench": "^1.0.5",
"seedrandom": "^2.4.4",
"typescript": "^3.0.1"
Expand Down
53 changes: 37 additions & 16 deletions yarn.lock
Expand Up @@ -5,59 +5,69 @@
"@protobufjs/utf8@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=

"@types/node@^10.7.1":
version "10.7.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.7.1.tgz#b704d7c259aa40ee052eec678758a68d07132a2e"
version "10.12.18"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.18.tgz#1d3ca764718915584fcd9f6344621b7672665c67"
integrity sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==

assemblyscript@AssemblyScript/assemblyscript:
"assemblyscript@github:AssemblyScript/assemblyscript":
version "0.5.0"
resolved "https://codeload.github.com/AssemblyScript/assemblyscript/tar.gz/c769f65bacd5392e5c9efab112b33244fc0a0c8f"
resolved "https://codeload.github.com/AssemblyScript/assemblyscript/tar.gz/d5f72e32d7e99cec3f8c37e6bb2916864706f4f0"
dependencies:
"@protobufjs/utf8" "^1.1.0"
binaryen "49.0.0-nightly.20180731"
glob "^7.1.2"
binaryen "55.0.0-nightly.20181130"
glob "^7.1.3"
long "^4.0.0"

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=

benchmark@^2.0.0:
version "2.1.4"
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
integrity sha1-CfPeMckWQl1JjMLuVloOvzwqVik=
dependencies:
lodash "^4.17.4"
platform "^1.3.3"

binaryen@49.0.0-nightly.20180731:
version "49.0.0-nightly.20180731"
resolved "https://registry.yarnpkg.com/binaryen/-/binaryen-49.0.0-nightly.20180731.tgz#d57a3ceeca56673377951aee1c229ad2c203023d"
binaryen@55.0.0-nightly.20181130:
version "55.0.0-nightly.20181130"
resolved "https://registry.yarnpkg.com/binaryen/-/binaryen-55.0.0-nightly.20181130.tgz#67159e12073d8195813057714754727320521b3d"
integrity sha512-RfMiI0vavw7Sy7KX8h1xOs4D3zp9nehmtE87DSfY6nXyd2EAdMwJ97tWdepuhOc6JWZsntOfzUA2fqu/sYTTLg==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==
dependencies:
balanced-match "^1.0.0"
concat-map "0.0.1"

concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=

do-you-even-bench@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/do-you-even-bench/-/do-you-even-bench-1.0.5.tgz#3df2f765100c88f72f444bb15503d670d262ae77"
integrity sha512-qkQrU+x4lsikgmLOaQYZegBsQTyRFkRpBMMAtTL/aVjHlaBEmmbg8GXHPB+dc+VcQd+WP2D9VwhUyZEcnDmVWg==
dependencies:
benchmark "^2.0.0"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=

glob@^7.1.2:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
glob@^7.1.3:
version "7.1.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1"
integrity sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
Expand All @@ -69,50 +79,61 @@ glob@^7.1.2:
inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
dependencies:
once "^1.3.0"
wrappy "1"

inherits@2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=

lodash@^4.17.4:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==

minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
dependencies:
brace-expansion "^1.1.7"

once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
dependencies:
wrappy "1"

path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=

platform@^1.3.3:
version "1.3.5"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"
integrity sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q==

seedrandom@^2.4.4:
version "2.4.4"
resolved "https://registry.yarnpkg.com/seedrandom/-/seedrandom-2.4.4.tgz#b25ea98632c73e45f58b77cfaa931678df01f9ba"
integrity sha512-9A+PDmgm+2du77B5i0Ip2cxOqqHjgNxnBgglxLcX78A2D6c2rTo61z4jnVABpF4cKeDMDG+cmXXvdnqse2VqMA==

typescript@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.0.1.tgz#43738f29585d3a87575520a4b93ab6026ef11fdb"
version "3.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.2.tgz#fe8101c46aa123f8353523ebdcf5730c2ae493e5"
integrity sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==

wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

0 comments on commit bc56b66

Please sign in to comment.