From 11248295008f1199a3cdd8b8a0180e3cf8f2adcf Mon Sep 17 00:00:00 2001 From: Nandu Raj Date: Tue, 22 Dec 2020 18:22:38 +0530 Subject: [PATCH] Fix $urandom_range maximum value (#2723) --- docs/CONTRIBUTORS | 1 + include/verilated.h | 4 ++-- test_regress/t/t_urandom.v | 11 +++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/CONTRIBUTORS b/docs/CONTRIBUTORS index a64dce238..363676c91 100644 --- a/docs/CONTRIBUTORS +++ b/docs/CONTRIBUTORS @@ -49,6 +49,7 @@ Marshal Qiao Matthew Ballance Michael Killough Mike Popoloski +Nandu Raj Nathan Kohagen Nathan Myers Patrick Stewart diff --git a/include/verilated.h b/include/verilated.h index e30cdad53..e8872c333 100644 --- a/include/verilated.h +++ b/include/verilated.h @@ -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; } } diff --git a/test_regress/t/t_urandom.v b/test_regress/t/t_urandom.v index 885118ab7..fcb6b7b9d 100644 --- a/test_regress/t/t_urandom.v +++ b/test_regress/t/t_urandom.v @@ -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