Skip to content

Commit c65a056

Browse files
MaxGraeydcodeIO
authored andcommitted
Update Math.random according to v8 (AssemblyScript#826)
1 parent e7d4786 commit c65a056

File tree

5 files changed

+136
-67
lines changed

5 files changed

+136
-67
lines changed

std/assembly/math.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,18 @@ export namespace NativeMath {
10331033
}
10341034

10351035
export function seedRandom(value: i64): void {
1036-
assert(value);
10371036
random_seeded = true;
10381037
random_state0_64 = murmurHash3(value);
10391038
random_state1_64 = murmurHash3(~random_state0_64);
10401039
random_state0_32 = splitMix32(<u32>value);
10411040
random_state1_32 = splitMix32(random_state0_32);
1041+
assert(
1042+
random_state0_64 != 0 && random_state1_64 != 0 &&
1043+
random_state0_32 != 0 && random_state1_32 != 0
1044+
);
10421045
}
10431046

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

tests/compiler/std/array.optimized.wat

+28-16
Original file line numberDiff line numberDiff line change
@@ -4151,16 +4151,6 @@
41514151
i32.xor
41524152
)
41534153
(func $~lib/math/NativeMath.seedRandom (; 97 ;) (type $FUNCSIG$vj) (param $0 i64)
4154-
local.get $0
4155-
i64.eqz
4156-
if
4157-
i32.const 0
4158-
i32.const 3160
4159-
i32.const 1036
4160-
i32.const 4
4161-
call $~lib/builtins/abort
4162-
unreachable
4163-
end
41644154
i32.const 1
41654155
global.set $~lib/math/random_seeded
41664156
local.get $0
@@ -4178,6 +4168,31 @@
41784168
global.get $~lib/math/random_state0_32
41794169
call $~lib/math/splitMix32
41804170
global.set $~lib/math/random_state1_32
4171+
global.get $~lib/math/random_state1_32
4172+
i32.const 0
4173+
i32.ne
4174+
i32.const 0
4175+
global.get $~lib/math/random_state0_32
4176+
i32.const 0
4177+
global.get $~lib/math/random_state1_64
4178+
i64.const 0
4179+
i64.ne
4180+
i32.const 0
4181+
global.get $~lib/math/random_state0_64
4182+
i64.const 0
4183+
i64.ne
4184+
select
4185+
select
4186+
select
4187+
i32.eqz
4188+
if
4189+
i32.const 0
4190+
i32.const 3160
4191+
i32.const 1041
4192+
i32.const 4
4193+
call $~lib/builtins/abort
4194+
unreachable
4195+
end
41814196
)
41824197
(func $~lib/util/sort/insertionSort<f32> (; 98 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
41834198
(local $3 i32)
@@ -5709,7 +5724,7 @@
57095724
if
57105725
i32.const 3936
57115726
i32.const 3160
5712-
i32.const 1045
5727+
i32.const 1048
57135728
i32.const 24
57145729
call $~lib/builtins/abort
57155730
unreachable
@@ -5735,13 +5750,10 @@
57355750
i64.const 26
57365751
i64.shr_u
57375752
i64.xor
5738-
local.tee $0
57395753
global.set $~lib/math/random_state1_64
5740-
local.get $0
57415754
local.get $1
5742-
i64.add
5743-
i64.const 4503599627370495
5744-
i64.and
5755+
i64.const 12
5756+
i64.shr_u
57455757
i64.const 4607182418800017408
57465758
i64.or
57475759
f64.reinterpret_i64

tests/compiler/std/array.untouched.wat

+36-15
Original file line numberDiff line numberDiff line change
@@ -6529,16 +6529,6 @@
65296529
i32.xor
65306530
)
65316531
(func $~lib/math/NativeMath.seedRandom (; 129 ;) (type $FUNCSIG$vj) (param $0 i64)
6532-
local.get $0
6533-
i64.eqz
6534-
if
6535-
i32.const 0
6536-
i32.const 3160
6537-
i32.const 1036
6538-
i32.const 4
6539-
call $~lib/builtins/abort
6540-
unreachable
6541-
end
65426532
i32.const 1
65436533
global.set $~lib/math/random_seeded
65446534
local.get $0
@@ -6556,6 +6546,39 @@
65566546
global.get $~lib/math/random_state0_32
65576547
call $~lib/math/splitMix32
65586548
global.set $~lib/math/random_state1_32
6549+
global.get $~lib/math/random_state0_64
6550+
i64.const 0
6551+
i64.ne
6552+
if (result i32)
6553+
global.get $~lib/math/random_state1_64
6554+
i64.const 0
6555+
i64.ne
6556+
else
6557+
i32.const 0
6558+
end
6559+
if (result i32)
6560+
global.get $~lib/math/random_state0_32
6561+
i32.const 0
6562+
i32.ne
6563+
else
6564+
i32.const 0
6565+
end
6566+
if (result i32)
6567+
global.get $~lib/math/random_state1_32
6568+
i32.const 0
6569+
i32.ne
6570+
else
6571+
i32.const 0
6572+
end
6573+
i32.eqz
6574+
if
6575+
i32.const 0
6576+
i32.const 3160
6577+
i32.const 1041
6578+
i32.const 4
6579+
call $~lib/builtins/abort
6580+
unreachable
6581+
end
65596582
)
65606583
(func $~lib/util/sort/insertionSort<f32> (; 130 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
65616584
(local $3 i32)
@@ -8842,7 +8865,7 @@
88428865
if
88438866
i32.const 3936
88448867
i32.const 3160
8845-
i32.const 1045
8868+
i32.const 1048
88468869
i32.const 24
88478870
call $~lib/builtins/abort
88488871
unreachable
@@ -8878,10 +8901,8 @@
88788901
local.get $0
88798902
global.set $~lib/math/random_state1_64
88808903
local.get $1
8881-
local.get $0
8882-
i64.add
8883-
i64.const 4503599627370495
8884-
i64.and
8904+
i64.const 12
8905+
i64.shr_u
88858906
i64.const 4607182418800017408
88868907
i64.or
88878908
local.set $2

tests/compiler/std/math.optimized.wat

+29-17
Original file line numberDiff line numberDiff line change
@@ -8039,16 +8039,6 @@
80398039
i32.xor
80408040
)
80418041
(func $~lib/math/NativeMath.seedRandom (; 130 ;) (type $FUNCSIG$vj) (param $0 i64)
8042-
local.get $0
8043-
i64.eqz
8044-
if
8045-
i32.const 0
8046-
i32.const 144
8047-
i32.const 1036
8048-
i32.const 4
8049-
call $~lib/builtins/abort
8050-
unreachable
8051-
end
80528042
i32.const 1
80538043
global.set $~lib/math/random_seeded
80548044
local.get $0
@@ -8066,6 +8056,31 @@
80668056
global.get $~lib/math/random_state0_32
80678057
call $~lib/math/splitMix32
80688058
global.set $~lib/math/random_state1_32
8059+
global.get $~lib/math/random_state1_32
8060+
i32.const 0
8061+
i32.ne
8062+
i32.const 0
8063+
global.get $~lib/math/random_state0_32
8064+
i32.const 0
8065+
global.get $~lib/math/random_state1_64
8066+
i64.const 0
8067+
i64.ne
8068+
i32.const 0
8069+
global.get $~lib/math/random_state0_64
8070+
i64.const 0
8071+
i64.ne
8072+
select
8073+
select
8074+
select
8075+
i32.eqz
8076+
if
8077+
i32.const 0
8078+
i32.const 144
8079+
i32.const 1041
8080+
i32.const 4
8081+
call $~lib/builtins/abort
8082+
unreachable
8083+
end
80698084
)
80708085
(func $~lib/math/NativeMath.random (; 131 ;) (type $FUNCSIG$d) (result f64)
80718086
(local $0 i64)
@@ -8075,7 +8090,7 @@
80758090
if
80768091
i32.const 184
80778092
i32.const 144
8078-
i32.const 1045
8093+
i32.const 1048
80798094
i32.const 24
80808095
call $~lib/builtins/abort
80818096
unreachable
@@ -8101,13 +8116,10 @@
81018116
i64.const 26
81028117
i64.shr_u
81038118
i64.xor
8104-
local.tee $0
81058119
global.set $~lib/math/random_state1_64
8106-
local.get $0
81078120
local.get $1
8108-
i64.add
8109-
i64.const 4503599627370495
8110-
i64.and
8121+
i64.const 12
8122+
i64.shr_u
81118123
i64.const 4607182418800017408
81128124
i64.or
81138125
f64.reinterpret_i64
@@ -8122,7 +8134,7 @@
81228134
if
81238135
i32.const 184
81248136
i32.const 144
8125-
i32.const 2319
8137+
i32.const 2322
81268138
i32.const 24
81278139
call $~lib/builtins/abort
81288140
unreachable

tests/compiler/std/math.untouched.wat

+37-16
Original file line numberDiff line numberDiff line change
@@ -10130,16 +10130,6 @@
1013010130
i32.xor
1013110131
)
1013210132
(func $~lib/math/NativeMath.seedRandom (; 133 ;) (type $FUNCSIG$vj) (param $0 i64)
10133-
local.get $0
10134-
i64.eqz
10135-
if
10136-
i32.const 0
10137-
i32.const 144
10138-
i32.const 1036
10139-
i32.const 4
10140-
call $~lib/builtins/abort
10141-
unreachable
10142-
end
1014310133
i32.const 1
1014410134
global.set $~lib/math/random_seeded
1014510135
local.get $0
@@ -10157,6 +10147,39 @@
1015710147
global.get $~lib/math/random_state0_32
1015810148
call $~lib/math/splitMix32
1015910149
global.set $~lib/math/random_state1_32
10150+
global.get $~lib/math/random_state0_64
10151+
i64.const 0
10152+
i64.ne
10153+
if (result i32)
10154+
global.get $~lib/math/random_state1_64
10155+
i64.const 0
10156+
i64.ne
10157+
else
10158+
i32.const 0
10159+
end
10160+
if (result i32)
10161+
global.get $~lib/math/random_state0_32
10162+
i32.const 0
10163+
i32.ne
10164+
else
10165+
i32.const 0
10166+
end
10167+
if (result i32)
10168+
global.get $~lib/math/random_state1_32
10169+
i32.const 0
10170+
i32.ne
10171+
else
10172+
i32.const 0
10173+
end
10174+
i32.eqz
10175+
if
10176+
i32.const 0
10177+
i32.const 144
10178+
i32.const 1041
10179+
i32.const 4
10180+
call $~lib/builtins/abort
10181+
unreachable
10182+
end
1016010183
)
1016110184
(func $~lib/math/NativeMath.random (; 134 ;) (type $FUNCSIG$d) (result f64)
1016210185
(local $0 i64)
@@ -10167,7 +10190,7 @@
1016710190
if
1016810191
i32.const 184
1016910192
i32.const 144
10170-
i32.const 1045
10193+
i32.const 1048
1017110194
i32.const 24
1017210195
call $~lib/builtins/abort
1017310196
unreachable
@@ -10203,10 +10226,8 @@
1020310226
local.get $0
1020410227
global.set $~lib/math/random_state1_64
1020510228
local.get $1
10206-
local.get $0
10207-
i64.add
10208-
i64.const 4503599627370495
10209-
i64.and
10229+
i64.const 12
10230+
i64.shr_u
1021010231
i64.const 4607182418800017408
1021110232
i64.or
1021210233
local.set $2
@@ -10224,7 +10245,7 @@
1022410245
if
1022510246
i32.const 184
1022610247
i32.const 144
10227-
i32.const 2319
10248+
i32.const 2322
1022810249
i32.const 24
1022910250
call $~lib/builtins/abort
1023010251
unreachable

0 commit comments

Comments
 (0)