Internals: Move vl_timescaled_double to verilated_funcs.h. No functional change intended

This commit is contained in:
Wilson Snyder 2024-03-10 22:34:32 -04:00
parent 2c27f22900
commit ba47da6587
4 changed files with 24 additions and 20 deletions

View File

@ -2412,6 +2412,22 @@ uint64_t vl_time_pow10(int n) {
return pow10[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, void VL_PRINTTIMESCALE(const char* namep, const char* timeunitp,
const VerilatedContext* contextp) VL_MT_SAFE { const VerilatedContext* contextp) VL_MT_SAFE {
VL_PRINTF_MT("Time scale of %s is %s / %s\n", namep, timeunitp, VL_PRINTF_MT("Time scale of %s is %s / %s\n", namep, timeunitp,

View File

@ -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; extern void _vl_debug_print_w(int lbits, WDataInP const iwp) VL_MT_SAFE;
//========================================================================= //=========================================================================
// Pli macros // Time handling
// clang-format off // clang-format off
@ -321,6 +321,8 @@ uint64_t VerilatedContext::time() const VL_MT_SAFE {
double vl_time_multiplier(int scale) VL_PURE; double vl_time_multiplier(int scale) VL_PURE;
// Return power of 10. e.g. returns 100 if n==2 // Return power of 10. e.g. returns 100 if n==2
uint64_t vl_time_pow10(int n) VL_PURE; 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 // Functional macros/routines

View File

@ -60,22 +60,6 @@ static double timescaleToDouble(const char* unitp) VL_PURE {
return value; 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 // Buffer management
@ -421,7 +405,7 @@ bool VerilatedTrace<VL_SUB_T, VL_BUF_T>::declCode(uint32_t code, const std::stri
template <> template <>
std::string VerilatedTrace<VL_SUB_T, VL_BUF_T>::timeResStr() const { std::string VerilatedTrace<VL_SUB_T, VL_BUF_T>::timeResStr() const {
return doubleToTimescale(m_timeRes); return vl_timescaled_double(m_timeRes);
} }
//========================================================================= //=========================================================================

View File

@ -372,7 +372,9 @@ string V3Os::trueRandom(size_t size) VL_MT_SAFE {
#if defined(_WIN32) || defined(__MINGW32__) #if defined(_WIN32) || defined(__MINGW32__)
const NTSTATUS hr = BCryptGenRandom(nullptr, reinterpret_cast<BYTE*>(data), size, const NTSTATUS hr = BCryptGenRandom(nullptr, reinterpret_cast<BYTE*>(data), size,
BCRYPT_USE_SYSTEM_PREFERRED_RNG); 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 #else
std::ifstream is{"/dev/urandom", std::ios::in | std::ios::binary}; std::ifstream is{"/dev/urandom", std::ios::in | std::ios::binary};
// This read uses the size of the buffer. // This read uses the size of the buffer.
@ -418,7 +420,7 @@ uint64_t V3Os::memUsageBytes() {
#else #else
// Highly unportable. Sorry // Highly unportable. Sorry
const char* const statmFilename = "/proc/self/statm"; const char* const statmFilename = "/proc/self/statm";
FILE* fp = fopen(statmFilename, "r"); FILE* const fp = fopen(statmFilename, "r");
if (!fp) return 0; if (!fp) return 0;
uint64_t size, resident, share, text, lib, data, dt; // All in pages uint64_t size, resident, share, text, lib, data, dt; // All in pages
const int items = fscanf( const int items = fscanf(