-
-
Notifications
You must be signed in to change notification settings - Fork 672
Add an option to select how to seed random() #1080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7deb71d
8ae7a2b
3fbbb9e
328c952
7fb3809
1db98cf
5fe77c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
/// <reference path="./util/seedrandom.d.ts" /> | ||
|
||
import { ArrayBufferView } from "./arraybuffer"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By doing that, we can |
||
import * as JSMath from "./bindings/Math"; | ||
import * as JSDate from "./bindings/Date"; | ||
import * as wasi from "./bindings/wasi_snapshot"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's something wrong here in that |
||
|
||
export { JSMath }; | ||
|
||
import { | ||
|
@@ -135,7 +141,7 @@ function umuldi(u: u64, v: u64): u64 { | |
|
||
/** @internal */ | ||
function pio2_large_quot(x: f64, u: i64): i32 { // see: jdh8/metallic/blob/master/src/math/double/rem_pio2.c | ||
const bits = PIO2_TABLE.dataStart; | ||
const bits = changetype<ArrayBufferView>(PIO2_TABLE).dataStart; | ||
|
||
var magnitude = u & 0x7FFFFFFFFFFFFFFF; | ||
var offset = (magnitude >> 52) - 1045; | ||
|
@@ -1410,7 +1416,7 @@ export namespace NativeMath { | |
} | ||
|
||
export function random(): f64 { // see: v8/src/base/utils/random-number-generator.cc | ||
if (!random_seeded) throw new Error("PRNG must be seeded."); | ||
if (!random_seeded) seedRandom(seedRandomSelect()); | ||
var s1 = random_state0_64; | ||
var s0 = random_state1_64; | ||
random_state0_64 = s0; | ||
|
@@ -1782,7 +1788,7 @@ function expo2f(x: f32): f32 { // exp(x)/2 for x >= log(DBL_MAX) | |
@inline | ||
function pio2f_large_quot(x: f32, u: i32): i32 { // see: jdh8/metallic/blob/master/src/math/float/rem_pio2f.c | ||
const coeff = reinterpret<f64>(0x3BF921FB54442D18); // π * 0x1p-65 = 8.51530395021638647334e-20 | ||
const bits = PIO2F_TABLE.dataStart; | ||
const bits = changetype<ArrayBufferView>(PIO2F_TABLE).dataStart; | ||
|
||
var offset = (u >> 23) - 152; | ||
var shift = <u64>(offset & 63); | ||
|
@@ -2603,7 +2609,7 @@ export namespace NativeMathf { | |
|
||
// Using xoroshiro64starstar from http://xoshiro.di.unimi.it/xoroshiro64starstar.c | ||
export function random(): f32 { | ||
if (!random_seeded) throw new Error("PRNG must be seeded."); | ||
if (!random_seeded) NativeMath.seedRandom(seedRandomSelect()); | ||
|
||
var s0 = random_state0_32; | ||
var s1 = random_state1_32; | ||
|
@@ -3132,3 +3138,30 @@ export function ipow64f(x: f64, e: i32): f64 { | |
} | ||
return sign ? 1.0 / out : out; | ||
} | ||
|
||
// @ts-ignore: decorator | ||
@lazy | ||
const seedRandomSelect_wasiBuf: i64[] = [ 0 ]; | ||
|
||
function seedRandomSelect(): i64 { | ||
if (isDefined(ASC_SEEDRANDOM_FUNC)) return ASC_SEEDRANDOM_FUNC(); | ||
if (isDefined(ASC_SEEDRANDOM_MATH)) { | ||
let val: i64; | ||
do val = reinterpret<i64>(JSMath.random()); | ||
while (!val); | ||
return val; | ||
} | ||
if (isDefined(ASC_SEEDRANDOM_DATE)) return <i64>JSDate.now(); | ||
if (isDefined(ASC_SEEDRANDOM_WASI)) { | ||
let buf = changetype<ArrayBufferView>(seedRandomSelect_wasiBuf).dataStart; | ||
let val: i64; | ||
do assert(wasi.random_get(buf, 8) == wasi.errno.SUCCESS); | ||
while (!(val = load<i64>(buf))); | ||
return val; | ||
} | ||
if (!isDefined(ASC_SEEDRANDOM_CONST)) { | ||
WARNING("Falling back to a compile-time constant random seed. See --seedRandom to silence this warning."); | ||
} | ||
const value = ((<i64>ASC_SEEDRANDOM_HIGH) << 32) | <i64><u32>ASC_SEEDRANDOM_LOW; | ||
return value; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare const ASC_SEEDRANDOM_LOW: i32; | ||
declare const ASC_SEEDRANDOM_HIGH: i32; | ||
declare const ASC_SEEDRANDOM_CONST: i32; | ||
declare const ASC_SEEDRANDOM_DATE: i32; | ||
declare const ASC_SEEDRANDOM_MATH: i32; | ||
declare const ASC_SEEDRANDOM_WASI: i32; | ||
declare const ASC_SEEDRANDOM_FUNC: i32; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
"asc_flags": [ | ||
"--runtime half", | ||
"--explicitStart", | ||
"--seedRandom math", | ||
"--use ASC_RTRACE=1" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems use
Long.fromBits
here is unnecessary. What about: