Skip to content
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

Rework Math.random #826

Merged
merged 3 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions std/assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,15 +1033,18 @@ export namespace NativeMath {
}

export function seedRandom(value: i64): void {
assert(value);
random_seeded = true;
random_state0_64 = murmurHash3(value);
random_state1_64 = murmurHash3(~random_state0_64);
random_state0_32 = splitMix32(<u32>value);
random_state1_32 = splitMix32(random_state0_32);
assert(
random_state0_64 != 0 && random_state1_64 != 0 &&
random_state0_32 != 0 && random_state1_32 != 0
);
}

export function random(): f64 { // see: v8/src/base/random-number-generator.cc
export function random(): f64 { // see: v8/src/base/utils/random-number-generator.cc
if (!random_seeded) throw new Error("PRNG must be seeded.");
var s1 = random_state0_64;
var s0 = random_state1_64;
Expand All @@ -1051,7 +1054,7 @@ export namespace NativeMath {
s1 ^= s0;
s1 ^= s0 >> 26;
random_state1_64 = s1;
var r = ((s0 + s1) & 0x000FFFFFFFFFFFFF) | 0x3FF0000000000000;
var r = (s0 >> 12) | 0x3FF0000000000000;
return reinterpret<f64>(r) - 1;
}

Expand Down
44 changes: 28 additions & 16 deletions tests/compiler/std/array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4151,16 +4151,6 @@
i32.xor
)
(func $~lib/math/NativeMath.seedRandom (; 97 ;) (type $FUNCSIG$vj) (param $0 i64)
local.get $0
i64.eqz
if
i32.const 0
i32.const 3160
i32.const 1036
i32.const 4
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~lib/math/random_seeded
local.get $0
Expand All @@ -4178,6 +4168,31 @@
global.get $~lib/math/random_state0_32
call $~lib/math/splitMix32
global.set $~lib/math/random_state1_32
global.get $~lib/math/random_state1_32
i32.const 0
i32.ne
i32.const 0
global.get $~lib/math/random_state0_32
i32.const 0
global.get $~lib/math/random_state1_64
i64.const 0
i64.ne
i32.const 0
global.get $~lib/math/random_state0_64
i64.const 0
i64.ne
select
select
select
i32.eqz
if
i32.const 0
i32.const 3160
i32.const 1041
i32.const 4
call $~lib/builtins/abort
unreachable
end
)
(func $~lib/util/sort/insertionSort<f32> (; 98 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i32)
Expand Down Expand Up @@ -5709,7 +5724,7 @@
if
i32.const 3936
i32.const 3160
i32.const 1045
i32.const 1048
i32.const 24
call $~lib/builtins/abort
unreachable
Expand All @@ -5735,13 +5750,10 @@
i64.const 26
i64.shr_u
i64.xor
local.tee $0
global.set $~lib/math/random_state1_64
local.get $0
local.get $1
i64.add
i64.const 4503599627370495
i64.and
i64.const 12
i64.shr_u
i64.const 4607182418800017408
i64.or
f64.reinterpret_i64
Expand Down
51 changes: 36 additions & 15 deletions tests/compiler/std/array.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6529,16 +6529,6 @@
i32.xor
)
(func $~lib/math/NativeMath.seedRandom (; 129 ;) (type $FUNCSIG$vj) (param $0 i64)
local.get $0
i64.eqz
if
i32.const 0
i32.const 3160
i32.const 1036
i32.const 4
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~lib/math/random_seeded
local.get $0
Expand All @@ -6556,6 +6546,39 @@
global.get $~lib/math/random_state0_32
call $~lib/math/splitMix32
global.set $~lib/math/random_state1_32
global.get $~lib/math/random_state0_64
i64.const 0
i64.ne
if (result i32)
global.get $~lib/math/random_state1_64
i64.const 0
i64.ne
else
i32.const 0
end
if (result i32)
global.get $~lib/math/random_state0_32
i32.const 0
i32.ne
else
i32.const 0
end
if (result i32)
global.get $~lib/math/random_state1_32
i32.const 0
i32.ne
else
i32.const 0
end
i32.eqz
if
i32.const 0
i32.const 3160
i32.const 1041
i32.const 4
call $~lib/builtins/abort
unreachable
end
)
(func $~lib/util/sort/insertionSort<f32> (; 130 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i32)
Expand Down Expand Up @@ -8842,7 +8865,7 @@
if
i32.const 3936
i32.const 3160
i32.const 1045
i32.const 1048
i32.const 24
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -8878,10 +8901,8 @@
local.get $0
global.set $~lib/math/random_state1_64
local.get $1
local.get $0
i64.add
i64.const 4503599627370495
i64.and
i64.const 12
i64.shr_u
i64.const 4607182418800017408
i64.or
local.set $2
Expand Down
46 changes: 29 additions & 17 deletions tests/compiler/std/math.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8039,16 +8039,6 @@
i32.xor
)
(func $~lib/math/NativeMath.seedRandom (; 130 ;) (type $FUNCSIG$vj) (param $0 i64)
local.get $0
i64.eqz
if
i32.const 0
i32.const 144
i32.const 1036
i32.const 4
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~lib/math/random_seeded
local.get $0
Expand All @@ -8066,6 +8056,31 @@
global.get $~lib/math/random_state0_32
call $~lib/math/splitMix32
global.set $~lib/math/random_state1_32
global.get $~lib/math/random_state1_32
i32.const 0
i32.ne
i32.const 0
global.get $~lib/math/random_state0_32
i32.const 0
global.get $~lib/math/random_state1_64
i64.const 0
i64.ne
i32.const 0
global.get $~lib/math/random_state0_64
i64.const 0
i64.ne
select
select
select
i32.eqz
if
i32.const 0
i32.const 144
i32.const 1041
i32.const 4
call $~lib/builtins/abort
unreachable
end
)
(func $~lib/math/NativeMath.random (; 131 ;) (type $FUNCSIG$d) (result f64)
(local $0 i64)
Expand All @@ -8075,7 +8090,7 @@
if
i32.const 184
i32.const 144
i32.const 1045
i32.const 1048
i32.const 24
call $~lib/builtins/abort
unreachable
Expand All @@ -8101,13 +8116,10 @@
i64.const 26
i64.shr_u
i64.xor
local.tee $0
global.set $~lib/math/random_state1_64
local.get $0
local.get $1
i64.add
i64.const 4503599627370495
i64.and
i64.const 12
i64.shr_u
i64.const 4607182418800017408
i64.or
f64.reinterpret_i64
Expand All @@ -8122,7 +8134,7 @@
if
i32.const 184
i32.const 144
i32.const 2319
i32.const 2322
i32.const 24
call $~lib/builtins/abort
unreachable
Expand Down
53 changes: 37 additions & 16 deletions tests/compiler/std/math.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10130,16 +10130,6 @@
i32.xor
)
(func $~lib/math/NativeMath.seedRandom (; 133 ;) (type $FUNCSIG$vj) (param $0 i64)
local.get $0
i64.eqz
if
i32.const 0
i32.const 144
i32.const 1036
i32.const 4
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~lib/math/random_seeded
local.get $0
Expand All @@ -10157,6 +10147,39 @@
global.get $~lib/math/random_state0_32
call $~lib/math/splitMix32
global.set $~lib/math/random_state1_32
global.get $~lib/math/random_state0_64
i64.const 0
i64.ne
if (result i32)
global.get $~lib/math/random_state1_64
i64.const 0
i64.ne
else
i32.const 0
end
if (result i32)
global.get $~lib/math/random_state0_32
i32.const 0
i32.ne
else
i32.const 0
end
if (result i32)
global.get $~lib/math/random_state1_32
i32.const 0
i32.ne
else
i32.const 0
end
i32.eqz
if
i32.const 0
i32.const 144
i32.const 1041
i32.const 4
call $~lib/builtins/abort
unreachable
end
)
(func $~lib/math/NativeMath.random (; 134 ;) (type $FUNCSIG$d) (result f64)
(local $0 i64)
Expand All @@ -10167,7 +10190,7 @@
if
i32.const 184
i32.const 144
i32.const 1045
i32.const 1048
i32.const 24
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -10203,10 +10226,8 @@
local.get $0
global.set $~lib/math/random_state1_64
local.get $1
local.get $0
i64.add
i64.const 4503599627370495
i64.and
i64.const 12
i64.shr_u
i64.const 4607182418800017408
i64.or
local.set $2
Expand All @@ -10224,7 +10245,7 @@
if
i32.const 184
i32.const 144
i32.const 2319
i32.const 2322
i32.const 24
call $~lib/builtins/abort
unreachable
Expand Down