sc_time.cc revision 12989
112837Sgabeblack@google.com/* 212837Sgabeblack@google.com * Copyright 2018 Google, Inc. 312837Sgabeblack@google.com * 412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312837Sgabeblack@google.com * this software without specific prior written permission. 1412837Sgabeblack@google.com * 1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612837Sgabeblack@google.com * 2712837Sgabeblack@google.com * Authors: Gabe Black 2812837Sgabeblack@google.com */ 2912837Sgabeblack@google.com 3012837Sgabeblack@google.com#include "base/logging.hh" 3112989Sgabeblack@google.com#include "base/types.hh" 3212986Sgabeblack@google.com#include "python/pybind11/pybind.hh" 3312837Sgabeblack@google.com#include "systemc/ext/core/sc_time.hh" 3412837Sgabeblack@google.com 3512837Sgabeblack@google.comnamespace sc_core 3612837Sgabeblack@google.com{ 3712837Sgabeblack@google.com 3812983Sgabeblack@google.comnamespace 3912837Sgabeblack@google.com{ 4012983Sgabeblack@google.com 4112983Sgabeblack@google.comconst char *TimeUnitNames[] = { 4212983Sgabeblack@google.com [SC_FS] = "fs", 4312983Sgabeblack@google.com [SC_PS] = "ps", 4412983Sgabeblack@google.com [SC_NS] = "ns", 4512983Sgabeblack@google.com [SC_US] = "us", 4612983Sgabeblack@google.com [SC_MS] = "ms", 4712983Sgabeblack@google.com [SC_SEC] = "s" 4812983Sgabeblack@google.com}; 4912983Sgabeblack@google.com 5012983Sgabeblack@google.comdouble TimeUnitScale[] = { 5112983Sgabeblack@google.com [SC_FS] = 1.0e-15, 5212983Sgabeblack@google.com [SC_PS] = 1.0e-12, 5312983Sgabeblack@google.com [SC_NS] = 1.0e-9, 5412983Sgabeblack@google.com [SC_US] = 1.0e-6, 5512983Sgabeblack@google.com [SC_MS] = 1.0e-3, 5612983Sgabeblack@google.com [SC_SEC] = 1.0 5712983Sgabeblack@google.com}; 5812983Sgabeblack@google.com 5912986Sgabeblack@google.comvoid 6012986Sgabeblack@google.comfixTimeResolution() 6112986Sgabeblack@google.com{ 6212986Sgabeblack@google.com static bool fixed = false; 6312986Sgabeblack@google.com if (fixed) 6412986Sgabeblack@google.com return; 6512986Sgabeblack@google.com 6612986Sgabeblack@google.com auto ticks = pybind11::module::import("m5.ticks"); 6712986Sgabeblack@google.com auto fix_global_frequency = ticks.attr("fixGlobalFrequency"); 6812986Sgabeblack@google.com fix_global_frequency(); 6912986Sgabeblack@google.com fixed = true; 7012986Sgabeblack@google.com} 7112986Sgabeblack@google.com 7212983Sgabeblack@google.com} // anonymous namespace 7312983Sgabeblack@google.com 7412983Sgabeblack@google.comsc_time::sc_time() : val(0) {} 7512983Sgabeblack@google.com 7612983Sgabeblack@google.comsc_time::sc_time(double d, sc_time_unit tu) 7712983Sgabeblack@google.com{ 7812983Sgabeblack@google.com val = 0; 7912983Sgabeblack@google.com if (d != 0) { 8012986Sgabeblack@google.com fixTimeResolution(); 8112983Sgabeblack@google.com //XXX Assuming the time resolution is 1ps. 8212983Sgabeblack@google.com double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS]; 8312983Sgabeblack@google.com // Accellera claims there is a linux bug, and that these next two 8412983Sgabeblack@google.com // lines work around them. 8512983Sgabeblack@google.com volatile double tmp = d * scale + 0.5; 8612983Sgabeblack@google.com val = static_cast<uint64_t>(tmp); 8712983Sgabeblack@google.com } 8812837Sgabeblack@google.com} 8912837Sgabeblack@google.com 9012983Sgabeblack@google.comsc_time::sc_time(const sc_time &t) 9112837Sgabeblack@google.com{ 9212983Sgabeblack@google.com val = t.val; 9312837Sgabeblack@google.com} 9412837Sgabeblack@google.com 9512925Sgabeblack@google.comsc_time::sc_time(double, bool) 9612925Sgabeblack@google.com{ 9712925Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 9812925Sgabeblack@google.com} 9912925Sgabeblack@google.com 10012925Sgabeblack@google.comsc_time::sc_time(sc_dt::uint64, bool) 10112925Sgabeblack@google.com{ 10212925Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 10312925Sgabeblack@google.com} 10412925Sgabeblack@google.com 10512837Sgabeblack@google.comsc_time & 10612983Sgabeblack@google.comsc_time::operator = (const sc_time &t) 10712837Sgabeblack@google.com{ 10812983Sgabeblack@google.com val = t.val; 10912837Sgabeblack@google.com return *this; 11012837Sgabeblack@google.com} 11112837Sgabeblack@google.com 11212837Sgabeblack@google.comsc_dt::uint64 11312837Sgabeblack@google.comsc_time::value() const 11412837Sgabeblack@google.com{ 11512983Sgabeblack@google.com return val; 11612837Sgabeblack@google.com} 11712837Sgabeblack@google.com 11812837Sgabeblack@google.comdouble 11912837Sgabeblack@google.comsc_time::to_double() const 12012837Sgabeblack@google.com{ 12112837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 12212837Sgabeblack@google.com return 0.0; 12312837Sgabeblack@google.com} 12412837Sgabeblack@google.comdouble 12512837Sgabeblack@google.comsc_time::to_seconds() const 12612837Sgabeblack@google.com{ 12712837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 12812837Sgabeblack@google.com return 0.0; 12912837Sgabeblack@google.com} 13012837Sgabeblack@google.com 13112837Sgabeblack@google.comconst std::string 13212837Sgabeblack@google.comsc_time::to_string() const 13312837Sgabeblack@google.com{ 13412837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 13512837Sgabeblack@google.com return ""; 13612837Sgabeblack@google.com} 13712837Sgabeblack@google.com 13812837Sgabeblack@google.combool 13912983Sgabeblack@google.comsc_time::operator == (const sc_time &t) const 14012837Sgabeblack@google.com{ 14112983Sgabeblack@google.com return val == t.val; 14212837Sgabeblack@google.com} 14312837Sgabeblack@google.com 14412837Sgabeblack@google.combool 14512983Sgabeblack@google.comsc_time::operator != (const sc_time &t) const 14612837Sgabeblack@google.com{ 14712983Sgabeblack@google.com return val != t.val; 14812837Sgabeblack@google.com} 14912837Sgabeblack@google.com 15012837Sgabeblack@google.combool 15112983Sgabeblack@google.comsc_time::operator < (const sc_time &t) const 15212837Sgabeblack@google.com{ 15312983Sgabeblack@google.com return val < t.val; 15412837Sgabeblack@google.com} 15512837Sgabeblack@google.com 15612837Sgabeblack@google.combool 15712983Sgabeblack@google.comsc_time::operator <= (const sc_time &t) const 15812837Sgabeblack@google.com{ 15912983Sgabeblack@google.com return val <= t.val; 16012837Sgabeblack@google.com} 16112837Sgabeblack@google.com 16212837Sgabeblack@google.combool 16312983Sgabeblack@google.comsc_time::operator > (const sc_time &t) const 16412837Sgabeblack@google.com{ 16512983Sgabeblack@google.com return val > t.val; 16612837Sgabeblack@google.com} 16712837Sgabeblack@google.com 16812837Sgabeblack@google.combool 16912983Sgabeblack@google.comsc_time::operator >= (const sc_time &t) const 17012837Sgabeblack@google.com{ 17112983Sgabeblack@google.com return val >= t.val; 17212837Sgabeblack@google.com} 17312837Sgabeblack@google.com 17412837Sgabeblack@google.comsc_time & 17512983Sgabeblack@google.comsc_time::operator += (const sc_time &t) 17612837Sgabeblack@google.com{ 17712983Sgabeblack@google.com val += t.val; 17812837Sgabeblack@google.com return *this; 17912837Sgabeblack@google.com} 18012837Sgabeblack@google.com 18112837Sgabeblack@google.comsc_time & 18212983Sgabeblack@google.comsc_time::operator -= (const sc_time &t) 18312837Sgabeblack@google.com{ 18412983Sgabeblack@google.com val -= t.val; 18512837Sgabeblack@google.com return *this; 18612837Sgabeblack@google.com} 18712837Sgabeblack@google.com 18812837Sgabeblack@google.comsc_time & 18912837Sgabeblack@google.comsc_time::operator *= (double) 19012837Sgabeblack@google.com{ 19112837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 19212837Sgabeblack@google.com return *this; 19312837Sgabeblack@google.com} 19412837Sgabeblack@google.com 19512837Sgabeblack@google.comsc_time & 19612837Sgabeblack@google.comsc_time::operator /= (double) 19712837Sgabeblack@google.com{ 19812837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 19912837Sgabeblack@google.com return *this; 20012837Sgabeblack@google.com} 20112837Sgabeblack@google.com 20212837Sgabeblack@google.comvoid 20312983Sgabeblack@google.comsc_time::print(std::ostream &os) const 20412837Sgabeblack@google.com{ 20512983Sgabeblack@google.com if (val == 0) { 20612983Sgabeblack@google.com os << "0 s"; 20712983Sgabeblack@google.com } else { 20812983Sgabeblack@google.com //XXX Assuming the time resolution is 1ps. 20912983Sgabeblack@google.com sc_time_unit tu = SC_PS; 21012983Sgabeblack@google.com uint64_t scaled = val; 21112983Sgabeblack@google.com while (tu < SC_SEC && (scaled % 1000) == 0) { 21212983Sgabeblack@google.com tu = (sc_time_unit)((int)tu + 1); 21312983Sgabeblack@google.com scaled /= 1000; 21412983Sgabeblack@google.com } 21512983Sgabeblack@google.com 21612983Sgabeblack@google.com os << scaled << ' ' << TimeUnitNames[tu]; 21712983Sgabeblack@google.com } 21812837Sgabeblack@google.com} 21912837Sgabeblack@google.com 22012923Sgabeblack@google.comsc_time 22112983Sgabeblack@google.comsc_time::from_value(sc_dt::uint64 u) 22212923Sgabeblack@google.com{ 22312983Sgabeblack@google.com sc_time t; 22412983Sgabeblack@google.com t.val = u; 22512983Sgabeblack@google.com return t; 22612923Sgabeblack@google.com} 22712923Sgabeblack@google.com 22812923Sgabeblack@google.comsc_time 22912923Sgabeblack@google.comsc_time::from_seconds(double) 23012923Sgabeblack@google.com{ 23112923Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 23212923Sgabeblack@google.com return sc_time(); 23312923Sgabeblack@google.com} 23412923Sgabeblack@google.com 23512923Sgabeblack@google.comsc_time 23612923Sgabeblack@google.comsc_time::from_string(const char *str) 23712923Sgabeblack@google.com{ 23812923Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 23912923Sgabeblack@google.com return sc_time(); 24012923Sgabeblack@google.com} 24112923Sgabeblack@google.com 24212837Sgabeblack@google.comconst sc_time 24312989Sgabeblack@google.comoperator + (const sc_time &a, const sc_time &b) 24412837Sgabeblack@google.com{ 24512989Sgabeblack@google.com return sc_time::from_value(a.value() + b.value()); 24612837Sgabeblack@google.com} 24712837Sgabeblack@google.com 24812837Sgabeblack@google.comconst sc_time 24912989Sgabeblack@google.comoperator - (const sc_time &a, const sc_time &b) 25012837Sgabeblack@google.com{ 25112989Sgabeblack@google.com return sc_time::from_value(a.value() - b.value()); 25212837Sgabeblack@google.com} 25312837Sgabeblack@google.com 25412837Sgabeblack@google.comconst sc_time 25512837Sgabeblack@google.comoperator * (const sc_time &, double) 25612837Sgabeblack@google.com{ 25712837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 25812837Sgabeblack@google.com return sc_time(); 25912837Sgabeblack@google.com} 26012837Sgabeblack@google.com 26112837Sgabeblack@google.comconst sc_time 26212837Sgabeblack@google.comoperator * (double, const sc_time &) 26312837Sgabeblack@google.com{ 26412837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 26512837Sgabeblack@google.com return sc_time(); 26612837Sgabeblack@google.com} 26712837Sgabeblack@google.com 26812837Sgabeblack@google.comconst sc_time 26912837Sgabeblack@google.comoperator / (const sc_time &, double) 27012837Sgabeblack@google.com{ 27112837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 27212837Sgabeblack@google.com return sc_time(); 27312837Sgabeblack@google.com} 27412837Sgabeblack@google.com 27512837Sgabeblack@google.comdouble 27612837Sgabeblack@google.comoperator / (const sc_time &, const sc_time &) 27712837Sgabeblack@google.com{ 27812837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 27912837Sgabeblack@google.com return 0.0; 28012837Sgabeblack@google.com} 28112837Sgabeblack@google.com 28212837Sgabeblack@google.comstd::ostream & 28312983Sgabeblack@google.comoperator << (std::ostream &os, const sc_time &t) 28412837Sgabeblack@google.com{ 28512983Sgabeblack@google.com t.print(os); 28612837Sgabeblack@google.com return os; 28712837Sgabeblack@google.com} 28812837Sgabeblack@google.com 28912837Sgabeblack@google.comconst sc_time SC_ZERO_TIME; 29012837Sgabeblack@google.com 29112837Sgabeblack@google.comvoid 29212837Sgabeblack@google.comsc_set_time_resolution(double, sc_time_unit) 29312837Sgabeblack@google.com{ 29412837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 29512837Sgabeblack@google.com} 29612837Sgabeblack@google.com 29712837Sgabeblack@google.comsc_time 29812837Sgabeblack@google.comsc_get_time_resolution() 29912837Sgabeblack@google.com{ 30012837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 30112837Sgabeblack@google.com return sc_time(); 30212837Sgabeblack@google.com} 30312837Sgabeblack@google.com 30412837Sgabeblack@google.comconst sc_time & 30512837Sgabeblack@google.comsc_max_time() 30612837Sgabeblack@google.com{ 30712989Sgabeblack@google.com static const sc_time MaxScTime = sc_time::from_value(MaxTick); 30812989Sgabeblack@google.com return MaxScTime; 30912837Sgabeblack@google.com} 31012837Sgabeblack@google.com 31112916Sgabeblack@google.comvoid 31212916Sgabeblack@google.comsc_set_default_time_unit(double, sc_time_unit) 31312916Sgabeblack@google.com{ 31412916Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 31512916Sgabeblack@google.com} 31612916Sgabeblack@google.com 31712916Sgabeblack@google.comsc_time 31812916Sgabeblack@google.comsc_get_default_time_unit() 31912916Sgabeblack@google.com{ 32012916Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 32112916Sgabeblack@google.com return *(sc_time *)nullptr; 32212916Sgabeblack@google.com} 32312916Sgabeblack@google.com 32412927Sgabeblack@google.comsc_time_tuple::sc_time_tuple(const sc_time &) 32512927Sgabeblack@google.com{ 32612927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 32712927Sgabeblack@google.com} 32812927Sgabeblack@google.com 32912927Sgabeblack@google.combool 33012927Sgabeblack@google.comsc_time_tuple::has_value() const 33112927Sgabeblack@google.com{ 33212927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 33312927Sgabeblack@google.com return false; 33412927Sgabeblack@google.com} 33512927Sgabeblack@google.com 33612927Sgabeblack@google.comsc_dt::uint64 33712927Sgabeblack@google.comsc_time_tuple::value() const 33812927Sgabeblack@google.com{ 33912927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 34012927Sgabeblack@google.com return 0; 34112927Sgabeblack@google.com} 34212927Sgabeblack@google.com 34312927Sgabeblack@google.comconst char * 34412927Sgabeblack@google.comsc_time_tuple::unit_symbol() const 34512927Sgabeblack@google.com{ 34612927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 34712927Sgabeblack@google.com return ""; 34812927Sgabeblack@google.com} 34912927Sgabeblack@google.com 35012927Sgabeblack@google.comdouble 35112927Sgabeblack@google.comsc_time_tuple::to_double() const 35212927Sgabeblack@google.com{ 35312927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 35412927Sgabeblack@google.com return 0.0; 35512927Sgabeblack@google.com} 35612927Sgabeblack@google.com 35712927Sgabeblack@google.comstd::string 35812927Sgabeblack@google.comsc_time_tuple::to_string() const 35912927Sgabeblack@google.com{ 36012927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 36112927Sgabeblack@google.com return ""; 36212927Sgabeblack@google.com} 36312927Sgabeblack@google.com 36412837Sgabeblack@google.com} // namespace sc_core 365