Fix $urandom_range maximum value (#2723)

This commit is contained in:
Nandu Raj 2020-12-22 18:22:38 +05:30 committed by GitHub
parent 4096dee598
commit 1124829500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View File

@ -49,6 +49,7 @@ Marshal Qiao
Matthew Ballance
Michael Killough
Mike Popoloski
Nandu Raj
Nathan Kohagen
Nathan Myers
Patrick Stewart

View File

@ -661,9 +661,9 @@ inline IData VL_URANDOM_RANGE_I(IData hi, IData lo) {
vluint64_t rnd = vl_rand64();
if (VL_LIKELY(hi > lo)) {
// Modulus isn't very fast but it's common that hi-low is power-of-two
return (rnd % (hi - lo)) + lo;
return (rnd % (hi - lo + 1)) + lo;
} else {
return (rnd % (lo - hi)) + hi;
return (rnd % (lo - hi + 1)) + hi;
}
}

View File

@ -34,14 +34,17 @@ module t(/*AUTOARG*/);
if (v1 == v2 && v1 == v3) $stop; // Possible, but 2^-64
// Range
v2 = $urandom_range(v1, v1);
if (v1 != v2) $stop;
for (int test = 0; test < 20; ++test) begin
v1 = 2;
v1 = $urandom_range(0, v1);
if (v1 != 0 && v1 != 1) $stop;
v1 = $urandom_range(2, 0);
if (v1 != 0 && v1 != 1) $stop;
v1 = $urandom_range(3);
if (v1 != 0 && v1 != 1 && v1 != 2) $stop;
v1 = $urandom_range(2, 0);
if (v1 != 0 && v1 != 1 && v1 !=2) $stop;
v1 = $urandom_range(3);
if (v1 != 0 && v1 != 1 && v1 != 2 && v1 != 3) $stop;
end
// Seed stability