Skip to content

Commit 78d40b0

Browse files
authored
(math) avoid zero seed inside seedRandom (#2053)
1 parent fe06a2f commit 78d40b0

File tree

7 files changed

+1259
-2317
lines changed

7 files changed

+1259
-2317
lines changed

std/assembly/math.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1413,15 +1413,15 @@ export namespace NativeMath {
14131413
}
14141414

14151415
export function seedRandom(value: i64): void {
1416-
random_seeded = true;
1416+
// Instead zero seed use golden ratio:
1417+
// phi = (1 + sqrt(5)) / 2
1418+
// trunc(2^64 / phi) = 0x9e3779b97f4a7c15
1419+
if (value == 0) value = 0x9e3779b97f4a7c15;
14171420
random_state0_64 = murmurHash3(value);
14181421
random_state1_64 = murmurHash3(~random_state0_64);
14191422
random_state0_32 = splitMix32(<u32>value);
14201423
random_state1_32 = splitMix32(random_state0_32);
1421-
assert(
1422-
random_state0_64 != 0 && random_state1_64 != 0 &&
1423-
random_state0_32 != 0 && random_state1_32 != 0
1424-
);
1424+
random_seeded = true;
14251425
}
14261426

14271427
export function random(): f64 { // see: v8/src/base/utils/random-number-generator.cc

tests/compiler/std/array.optimized.wat

+858-944
Large diffs are not rendered by default.

tests/compiler/std/array.untouched.wat

+334-361
Large diffs are not rendered by default.

tests/compiler/std/math.optimized.wat

+8-34
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@
6565
(global $~lib/math/res128_hi (mut i64) (i64.const 0))
6666
(global $~lib/math/rempio2f_y (mut f64) (f64.const 0))
6767
(global $~lib/util/math/log_tail (mut f64) (f64.const 0))
68-
(global $~lib/math/random_seeded (mut i32) (i32.const 0))
6968
(global $~lib/math/random_state0_64 (mut i64) (i64.const 0))
7069
(global $~lib/math/random_state1_64 (mut i64) (i64.const 0))
7170
(global $~lib/math/random_state0_32 (mut i32) (i32.const 0))
7271
(global $~lib/math/random_state1_32 (mut i32) (i32.const 0))
72+
(global $~lib/math/random_seeded (mut i32) (i32.const 0))
7373
(global $~lib/math/NativeMath.sincos_sin (mut f64) (f64.const 0))
7474
(global $~lib/math/NativeMath.sincos_cos (mut f64) (f64.const 0))
7575
(memory $0 1)
@@ -214,8 +214,6 @@
214214
(data (i32.const 14289) "\a08\07\ae\"\d5?Y\c7d\81p\be.=\00\00\00\00\00\e0\e6?")
215215
(data (i32.const 14321) "\d0\c8S\f7{\d5?\ef@]\ee\ed\ad\1f=\00\00\00\00\00\c0\e6?")
216216
(data (i32.const 14353) "`Y\df\bd\d5\d5?\dce\a4\08*\0b\n\bd")
217-
(data (i32.const 14380) ",")
218-
(data (i32.const 14392) "\01\00\00\00\18\00\00\00~\00l\00i\00b\00/\00m\00a\00t\00h\00.\00t\00s")
219217
(export "memory" (memory $0))
220218
(start $~start)
221219
(func $~lib/math/NativeMath.scalbn (param $0 f64) (param $1 i32) (result f64)
@@ -8527,11 +8525,12 @@
85278525
(func $~lib/math/NativeMath.seedRandom (param $0 i64)
85288526
(local $1 i32)
85298527
(local $2 i64)
8530-
(local $3 i64)
8531-
(local $4 i32)
8532-
i32.const 1
8533-
global.set $~lib/math/random_seeded
8528+
i64.const -7046029254386353131
8529+
local.get $0
85348530
local.get $0
8531+
i64.eqz
8532+
select
8533+
local.tee $0
85358534
i64.const 33
85368535
i64.shr_u
85378536
local.get $0
@@ -8552,7 +8551,6 @@
85528551
i64.xor
85538552
global.set $~lib/math/random_state0_64
85548553
global.get $~lib/math/random_state0_64
8555-
local.tee $3
85568554
i64.const -1
85578555
i64.xor
85588556
local.tee $2
@@ -8608,7 +8606,6 @@
86088606
i32.xor
86098607
global.set $~lib/math/random_state0_32
86108608
global.get $~lib/math/random_state0_32
8611-
local.tee $4
86128609
i32.const 1831565813
86138610
i32.add
86148611
local.tee $1
@@ -8639,31 +8636,8 @@
86398636
local.get $1
86408637
i32.xor
86418638
global.set $~lib/math/random_state1_32
8642-
global.get $~lib/math/random_state1_32
8643-
i32.const 0
8644-
i32.ne
8645-
i32.const 0
8646-
local.get $4
8647-
i32.const 0
8648-
global.get $~lib/math/random_state1_64
8649-
i64.const 0
8650-
i64.ne
8651-
i32.const 0
8652-
local.get $3
8653-
i64.const 0
8654-
i64.ne
8655-
select
8656-
select
8657-
select
8658-
i32.eqz
8659-
if
8660-
i32.const 0
8661-
i32.const 14400
8662-
i32.const 1421
8663-
i32.const 5
8664-
call $~lib/builtins/abort
8665-
unreachable
8666-
end
8639+
i32.const 1
8640+
global.set $~lib/math/random_seeded
86678641
)
86688642
(func $std/math/test_round (param $0 f64) (param $1 f64) (result i32)
86698643
(local $2 f64)

tests/compiler/std/math.untouched.wat

+13-40
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)