diff --git a/include/verilated.cpp b/include/verilated.cpp index 4c4836312..2f9d75a49 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -2412,6 +2412,22 @@ uint64_t vl_time_pow10(int n) { return pow10[n]; } +std::string vl_timescaled_double(double value, const char* format) VL_PURE { + const char* suffixp = "s"; + // clang-format off + if (value >= 1e0) { suffixp = "s"; value *= 1e0; } + else if (value >= 1e-3) { suffixp = "ms"; value *= 1e3; } + else if (value >= 1e-6) { suffixp = "us"; value *= 1e6; } + else if (value >= 1e-9) { suffixp = "ns"; value *= 1e9; } + else if (value >= 1e-12) { suffixp = "ps"; value *= 1e12; } + else if (value >= 1e-15) { suffixp = "fs"; value *= 1e15; } + else if (value >= 1e-18) { suffixp = "as"; value *= 1e18; } + // clang-format on + char valuestr[100]; + VL_SNPRINTF(valuestr, 100, format, value, suffixp); + return std::string{valuestr}; // Gets converted to string, so no ref to stack +} + void VL_PRINTTIMESCALE(const char* namep, const char* timeunitp, const VerilatedContext* contextp) VL_MT_SAFE { VL_PRINTF_MT("Time scale of %s is %s / %s\n", namep, timeunitp, diff --git a/include/verilated_funcs.h b/include/verilated_funcs.h index f03a59ea8..fe9151a75 100644 --- a/include/verilated_funcs.h +++ b/include/verilated_funcs.h @@ -263,7 +263,7 @@ static inline QData VL_EXTENDSIGN_Q(int lbits, QData lhs) VL_PURE { extern void _vl_debug_print_w(int lbits, WDataInP const iwp) VL_MT_SAFE; //========================================================================= -// Pli macros +// Time handling // clang-format off @@ -321,6 +321,8 @@ uint64_t VerilatedContext::time() const VL_MT_SAFE { double vl_time_multiplier(int scale) VL_PURE; // Return power of 10. e.g. returns 100 if n==2 uint64_t vl_time_pow10(int n) VL_PURE; +// Return time as string with timescale suffix +std::string vl_timescaled_double(double value, const char* format = "%0.0f%s") VL_PURE; //========================================================================= // Functional macros/routines diff --git a/include/verilated_trace_imp.h b/include/verilated_trace_imp.h index 0f9de329a..9529a2b66 100644 --- a/include/verilated_trace_imp.h +++ b/include/verilated_trace_imp.h @@ -60,22 +60,6 @@ static double timescaleToDouble(const char* unitp) VL_PURE { return value; } -static std::string doubleToTimescale(double value) VL_PURE { - const char* suffixp = "s"; - // clang-format off - if (value >= 1e0) { suffixp = "s"; value *= 1e0; } - else if (value >= 1e-3) { suffixp = "ms"; value *= 1e3; } - else if (value >= 1e-6) { suffixp = "us"; value *= 1e6; } - else if (value >= 1e-9) { suffixp = "ns"; value *= 1e9; } - else if (value >= 1e-12) { suffixp = "ps"; value *= 1e12; } - else if (value >= 1e-15) { suffixp = "fs"; value *= 1e15; } - else if (value >= 1e-18) { suffixp = "as"; value *= 1e18; } - // clang-format on - char valuestr[100]; - VL_SNPRINTF(valuestr, 100, "%0.0f%s", value, suffixp); - return valuestr; // Gets converted to string, so no ref to stack -} - //========================================================================= // Buffer management @@ -421,7 +405,7 @@ bool VerilatedTrace::declCode(uint32_t code, const std::stri template <> std::string VerilatedTrace::timeResStr() const { - return doubleToTimescale(m_timeRes); + return vl_timescaled_double(m_timeRes); } //========================================================================= diff --git a/src/V3Os.cpp b/src/V3Os.cpp index 9286a00d1..92413b62a 100644 --- a/src/V3Os.cpp +++ b/src/V3Os.cpp @@ -372,7 +372,9 @@ string V3Os::trueRandom(size_t size) VL_MT_SAFE { #if defined(_WIN32) || defined(__MINGW32__) const NTSTATUS hr = BCryptGenRandom(nullptr, reinterpret_cast(data), size, BCRYPT_USE_SYSTEM_PREFERRED_RNG); - if (!BCRYPT_SUCCESS(hr)) v3fatal("Could not acquire random data."); + if (VL_UNCOVERABLE(!BCRYPT_SUCCESS(hr))) { + v3fatal("Could not acquire random data. Try specifying a key instead."); // LCOV_EXCL_LINE + } #else std::ifstream is{"/dev/urandom", std::ios::in | std::ios::binary}; // This read uses the size of the buffer. @@ -418,7 +420,7 @@ uint64_t V3Os::memUsageBytes() { #else // Highly unportable. Sorry const char* const statmFilename = "/proc/self/statm"; - FILE* fp = fopen(statmFilename, "r"); + FILE* const fp = fopen(statmFilename, "r"); if (!fp) return 0; uint64_t size, resident, share, text, lib, data, dt; // All in pages const int items = fscanf(