From 67cc65e6f3352d2b545ad08d92d4b16734ab358a Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Fri, 20 Feb 2026 19:39:20 -0500 Subject: [PATCH] Fix randomize of real (#7115). Fixes #7115. --- Changes | 1 + include/verilated.cpp | 4 +++ include/verilated_types.h | 1 + test_regress/t/t_randomize_real.py | 18 +++++++++++ test_regress/t/t_randomize_real.v | 50 ++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100755 test_regress/t/t_randomize_real.py create mode 100644 test_regress/t/t_randomize_real.v diff --git a/Changes b/Changes index 0026d599f..7cc651b45 100644 --- a/Changes +++ b/Changes @@ -121,6 +121,7 @@ Verilator 5.045 devel * Fix time to not advance after `$finish` (#7095). * Fix associative array size() constraint generating invalid resize() call (#7103) (#7112). [Yilou Wang] * Fix circular class reference %p-printing causing infinite recursion (#7106). +* Fix randomize of real (#7115). [Srinivasan Venkataramanan] Verilator 5.044 2026-01-01 diff --git a/include/verilated.cpp b/include/verilated.cpp index 35c965dff..fbda7116f 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -442,6 +442,10 @@ WDataOutP VL_RANDOM_W(int obits, WDataOutP outwp) VL_MT_SAFE { return outwp; } +double VL_RANDOM_RNG_D(VlRNG& rngr) VL_MT_UNSAFE { + return VL_CVT_D_Q(VL_RANDOM_RNG_Q(rngr)); +} + WDataOutP VL_RANDOM_RNG_W(VlRNG& rngr, int obits, WDataOutP outwp) VL_MT_UNSAFE { for (int i = 0; i < VL_WORDS_I(obits); ++i) outwp[i] = rngr.rand64(); // Last word is unclean diff --git a/include/verilated_types.h b/include/verilated_types.h index 09930306f..6554d67c9 100644 --- a/include/verilated_types.h +++ b/include/verilated_types.h @@ -320,6 +320,7 @@ public: // These require the class object to have the thread safety lock inline IData VL_RANDOM_RNG_I(VlRNG& rngr) VL_MT_UNSAFE { return rngr.rand64(); } inline QData VL_RANDOM_RNG_Q(VlRNG& rngr) VL_MT_UNSAFE { return rngr.rand64(); } +extern double VL_RANDOM_RNG_D(VlRNG& rngr) VL_MT_UNSAFE; extern WDataOutP VL_RANDOM_RNG_W(VlRNG& rngr, int obits, WDataOutP outwp) VL_MT_UNSAFE; //=================================================================== diff --git a/test_regress/t/t_randomize_real.py b/test_regress/t/t_randomize_real.py new file mode 100755 index 000000000..8a938befd --- /dev/null +++ b/test_regress/t/t_randomize_real.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of either the GNU Lesser General Public License Version 3 +# or the Perl Artistic License Version 2.0. +# SPDX-FileCopyrightText: 2026 Wilson Snyder +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('simulator') + +test.compile() + +test.execute() + +test.passes() diff --git a/test_regress/t/t_randomize_real.v b/test_regress/t/t_randomize_real.v new file mode 100644 index 000000000..bea1fdcc0 --- /dev/null +++ b/test_regress/t/t_randomize_real.v @@ -0,0 +1,50 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain. +// SPDX-FileCopyrightText: 2026 Wilson Snyder +// SPDX-License-Identifier: CC0-1.0 + +// verilog_format: off +`define stop $stop +`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0); +// verilog_format: on + +class Cls; + rand real m_real; +endclass + +module test; + localparam LOOPS = 1000; + + int negative; + int bitcounts[64]; + int i; + bit [63:0] rbits; + + initial begin + Cls c; + c = new; + + repeat (LOOPS) begin + i = c.randomize(); + `checkd(i, 1); + + rbits = $realtobits(c.m_real); +`ifdef TEST_VERBOSE + $display("%x %g", rbits, c.m_real); +`endif + + if (c.m_real < 0) negative++; + for (int b = 0; b < 64; ++b) begin + if (rbits[b]) bitcounts[b]++; + end + end + + if (negative < LOOPS * 0.4) $error("Too few negative %0d", negative); + for (int b = 0; b < 64; ++b) begin + if (bitcounts[b] < LOOPS * 0.4) $error("Too few 1 bits at [%0d]: %0d", b, bitcounts[b]); + end + $finish; + end + +endmodule