sc_time.cc revision 12983
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"
3112837Sgabeblack@google.com#include "systemc/ext/core/sc_time.hh"
3212837Sgabeblack@google.com
3312837Sgabeblack@google.comnamespace sc_core
3412837Sgabeblack@google.com{
3512837Sgabeblack@google.com
3612983Sgabeblack@google.comnamespace
3712837Sgabeblack@google.com{
3812983Sgabeblack@google.com
3912983Sgabeblack@google.comconst char *TimeUnitNames[] = {
4012983Sgabeblack@google.com    [SC_FS] = "fs",
4112983Sgabeblack@google.com    [SC_PS] = "ps",
4212983Sgabeblack@google.com    [SC_NS] = "ns",
4312983Sgabeblack@google.com    [SC_US] = "us",
4412983Sgabeblack@google.com    [SC_MS] = "ms",
4512983Sgabeblack@google.com    [SC_SEC] = "s"
4612983Sgabeblack@google.com};
4712983Sgabeblack@google.com
4812983Sgabeblack@google.comdouble TimeUnitScale[] = {
4912983Sgabeblack@google.com    [SC_FS] = 1.0e-15,
5012983Sgabeblack@google.com    [SC_PS] = 1.0e-12,
5112983Sgabeblack@google.com    [SC_NS] = 1.0e-9,
5212983Sgabeblack@google.com    [SC_US] = 1.0e-6,
5312983Sgabeblack@google.com    [SC_MS] = 1.0e-3,
5412983Sgabeblack@google.com    [SC_SEC] = 1.0
5512983Sgabeblack@google.com};
5612983Sgabeblack@google.com
5712983Sgabeblack@google.com} // anonymous namespace
5812983Sgabeblack@google.com
5912983Sgabeblack@google.comsc_time::sc_time() : val(0) {}
6012983Sgabeblack@google.com
6112983Sgabeblack@google.comsc_time::sc_time(double d, sc_time_unit tu)
6212983Sgabeblack@google.com{
6312983Sgabeblack@google.com    val = 0;
6412983Sgabeblack@google.com    if (d != 0) {
6512983Sgabeblack@google.com        //XXX Assuming the time resolution is 1ps.
6612983Sgabeblack@google.com        double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
6712983Sgabeblack@google.com        // Accellera claims there is a linux bug, and that these next two
6812983Sgabeblack@google.com        // lines work around them.
6912983Sgabeblack@google.com        volatile double tmp = d * scale + 0.5;
7012983Sgabeblack@google.com        val = static_cast<uint64_t>(tmp);
7112983Sgabeblack@google.com    }
7212837Sgabeblack@google.com}
7312837Sgabeblack@google.com
7412983Sgabeblack@google.comsc_time::sc_time(const sc_time &t)
7512837Sgabeblack@google.com{
7612983Sgabeblack@google.com    val = t.val;
7712837Sgabeblack@google.com}
7812837Sgabeblack@google.com
7912925Sgabeblack@google.comsc_time::sc_time(double, bool)
8012925Sgabeblack@google.com{
8112925Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
8212925Sgabeblack@google.com}
8312925Sgabeblack@google.com
8412925Sgabeblack@google.comsc_time::sc_time(sc_dt::uint64, bool)
8512925Sgabeblack@google.com{
8612925Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
8712925Sgabeblack@google.com}
8812925Sgabeblack@google.com
8912837Sgabeblack@google.comsc_time &
9012983Sgabeblack@google.comsc_time::operator = (const sc_time &t)
9112837Sgabeblack@google.com{
9212983Sgabeblack@google.com    val = t.val;
9312837Sgabeblack@google.com    return *this;
9412837Sgabeblack@google.com}
9512837Sgabeblack@google.com
9612837Sgabeblack@google.comsc_dt::uint64
9712837Sgabeblack@google.comsc_time::value() const
9812837Sgabeblack@google.com{
9912983Sgabeblack@google.com    return val;
10012837Sgabeblack@google.com}
10112837Sgabeblack@google.com
10212837Sgabeblack@google.comdouble
10312837Sgabeblack@google.comsc_time::to_double() const
10412837Sgabeblack@google.com{
10512837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
10612837Sgabeblack@google.com    return 0.0;
10712837Sgabeblack@google.com}
10812837Sgabeblack@google.comdouble
10912837Sgabeblack@google.comsc_time::to_seconds() const
11012837Sgabeblack@google.com{
11112837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
11212837Sgabeblack@google.com    return 0.0;
11312837Sgabeblack@google.com}
11412837Sgabeblack@google.com
11512837Sgabeblack@google.comconst std::string
11612837Sgabeblack@google.comsc_time::to_string() const
11712837Sgabeblack@google.com{
11812837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
11912837Sgabeblack@google.com    return "";
12012837Sgabeblack@google.com}
12112837Sgabeblack@google.com
12212837Sgabeblack@google.combool
12312983Sgabeblack@google.comsc_time::operator == (const sc_time &t) const
12412837Sgabeblack@google.com{
12512983Sgabeblack@google.com    return val == t.val;
12612837Sgabeblack@google.com}
12712837Sgabeblack@google.com
12812837Sgabeblack@google.combool
12912983Sgabeblack@google.comsc_time::operator != (const sc_time &t) const
13012837Sgabeblack@google.com{
13112983Sgabeblack@google.com    return val != t.val;
13212837Sgabeblack@google.com}
13312837Sgabeblack@google.com
13412837Sgabeblack@google.combool
13512983Sgabeblack@google.comsc_time::operator < (const sc_time &t) const
13612837Sgabeblack@google.com{
13712983Sgabeblack@google.com    return val < t.val;
13812837Sgabeblack@google.com}
13912837Sgabeblack@google.com
14012837Sgabeblack@google.combool
14112983Sgabeblack@google.comsc_time::operator <= (const sc_time &t) const
14212837Sgabeblack@google.com{
14312983Sgabeblack@google.com    return val <= t.val;
14412837Sgabeblack@google.com}
14512837Sgabeblack@google.com
14612837Sgabeblack@google.combool
14712983Sgabeblack@google.comsc_time::operator > (const sc_time &t) const
14812837Sgabeblack@google.com{
14912983Sgabeblack@google.com    return val > t.val;
15012837Sgabeblack@google.com}
15112837Sgabeblack@google.com
15212837Sgabeblack@google.combool
15312983Sgabeblack@google.comsc_time::operator >= (const sc_time &t) const
15412837Sgabeblack@google.com{
15512983Sgabeblack@google.com    return val >= t.val;
15612837Sgabeblack@google.com}
15712837Sgabeblack@google.com
15812837Sgabeblack@google.comsc_time &
15912983Sgabeblack@google.comsc_time::operator += (const sc_time &t)
16012837Sgabeblack@google.com{
16112983Sgabeblack@google.com    val += t.val;
16212837Sgabeblack@google.com    return *this;
16312837Sgabeblack@google.com}
16412837Sgabeblack@google.com
16512837Sgabeblack@google.comsc_time &
16612983Sgabeblack@google.comsc_time::operator -= (const sc_time &t)
16712837Sgabeblack@google.com{
16812983Sgabeblack@google.com    val -= t.val;
16912837Sgabeblack@google.com    return *this;
17012837Sgabeblack@google.com}
17112837Sgabeblack@google.com
17212837Sgabeblack@google.comsc_time &
17312837Sgabeblack@google.comsc_time::operator *= (double)
17412837Sgabeblack@google.com{
17512837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
17612837Sgabeblack@google.com    return *this;
17712837Sgabeblack@google.com}
17812837Sgabeblack@google.com
17912837Sgabeblack@google.comsc_time &
18012837Sgabeblack@google.comsc_time::operator /= (double)
18112837Sgabeblack@google.com{
18212837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
18312837Sgabeblack@google.com    return *this;
18412837Sgabeblack@google.com}
18512837Sgabeblack@google.com
18612837Sgabeblack@google.comvoid
18712983Sgabeblack@google.comsc_time::print(std::ostream &os) const
18812837Sgabeblack@google.com{
18912983Sgabeblack@google.com    if (val == 0) {
19012983Sgabeblack@google.com        os << "0 s";
19112983Sgabeblack@google.com    } else {
19212983Sgabeblack@google.com        //XXX Assuming the time resolution is 1ps.
19312983Sgabeblack@google.com        sc_time_unit tu = SC_PS;
19412983Sgabeblack@google.com        uint64_t scaled = val;
19512983Sgabeblack@google.com        while (tu < SC_SEC && (scaled % 1000) == 0) {
19612983Sgabeblack@google.com            tu = (sc_time_unit)((int)tu + 1);
19712983Sgabeblack@google.com            scaled /= 1000;
19812983Sgabeblack@google.com        }
19912983Sgabeblack@google.com
20012983Sgabeblack@google.com        os << scaled << ' ' << TimeUnitNames[tu];
20112983Sgabeblack@google.com    }
20212837Sgabeblack@google.com}
20312837Sgabeblack@google.com
20412923Sgabeblack@google.comsc_time
20512983Sgabeblack@google.comsc_time::from_value(sc_dt::uint64 u)
20612923Sgabeblack@google.com{
20712983Sgabeblack@google.com    sc_time t;
20812983Sgabeblack@google.com    t.val = u;
20912983Sgabeblack@google.com    return t;
21012923Sgabeblack@google.com}
21112923Sgabeblack@google.com
21212923Sgabeblack@google.comsc_time
21312923Sgabeblack@google.comsc_time::from_seconds(double)
21412923Sgabeblack@google.com{
21512923Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
21612923Sgabeblack@google.com    return sc_time();
21712923Sgabeblack@google.com}
21812923Sgabeblack@google.com
21912923Sgabeblack@google.comsc_time
22012923Sgabeblack@google.comsc_time::from_string(const char *str)
22112923Sgabeblack@google.com{
22212923Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
22312923Sgabeblack@google.com    return sc_time();
22412923Sgabeblack@google.com}
22512923Sgabeblack@google.com
22612837Sgabeblack@google.comconst sc_time
22712837Sgabeblack@google.comoperator + (const sc_time &, const sc_time &)
22812837Sgabeblack@google.com{
22912837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
23012837Sgabeblack@google.com    return sc_time();
23112837Sgabeblack@google.com}
23212837Sgabeblack@google.com
23312837Sgabeblack@google.comconst sc_time
23412837Sgabeblack@google.comoperator - (const sc_time &, const sc_time &)
23512837Sgabeblack@google.com{
23612837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
23712837Sgabeblack@google.com    return sc_time();
23812837Sgabeblack@google.com}
23912837Sgabeblack@google.com
24012837Sgabeblack@google.comconst sc_time
24112837Sgabeblack@google.comoperator * (const sc_time &, double)
24212837Sgabeblack@google.com{
24312837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
24412837Sgabeblack@google.com    return sc_time();
24512837Sgabeblack@google.com}
24612837Sgabeblack@google.com
24712837Sgabeblack@google.comconst sc_time
24812837Sgabeblack@google.comoperator * (double, const sc_time &)
24912837Sgabeblack@google.com{
25012837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
25112837Sgabeblack@google.com    return sc_time();
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.comdouble
26212837Sgabeblack@google.comoperator / (const sc_time &, const sc_time &)
26312837Sgabeblack@google.com{
26412837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
26512837Sgabeblack@google.com    return 0.0;
26612837Sgabeblack@google.com}
26712837Sgabeblack@google.com
26812837Sgabeblack@google.comstd::ostream &
26912983Sgabeblack@google.comoperator << (std::ostream &os, const sc_time &t)
27012837Sgabeblack@google.com{
27112983Sgabeblack@google.com    t.print(os);
27212837Sgabeblack@google.com    return os;
27312837Sgabeblack@google.com}
27412837Sgabeblack@google.com
27512837Sgabeblack@google.comconst sc_time SC_ZERO_TIME;
27612837Sgabeblack@google.com
27712837Sgabeblack@google.comvoid
27812837Sgabeblack@google.comsc_set_time_resolution(double, sc_time_unit)
27912837Sgabeblack@google.com{
28012837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
28112837Sgabeblack@google.com}
28212837Sgabeblack@google.com
28312837Sgabeblack@google.comsc_time
28412837Sgabeblack@google.comsc_get_time_resolution()
28512837Sgabeblack@google.com{
28612837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
28712837Sgabeblack@google.com    return sc_time();
28812837Sgabeblack@google.com}
28912837Sgabeblack@google.com
29012837Sgabeblack@google.comconst sc_time &
29112837Sgabeblack@google.comsc_max_time()
29212837Sgabeblack@google.com{
29312837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
29412837Sgabeblack@google.com    return *(const sc_time *)nullptr;
29512837Sgabeblack@google.com}
29612837Sgabeblack@google.com
29712916Sgabeblack@google.comvoid
29812916Sgabeblack@google.comsc_set_default_time_unit(double, sc_time_unit)
29912916Sgabeblack@google.com{
30012916Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
30112916Sgabeblack@google.com}
30212916Sgabeblack@google.com
30312916Sgabeblack@google.comsc_time
30412916Sgabeblack@google.comsc_get_default_time_unit()
30512916Sgabeblack@google.com{
30612916Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
30712916Sgabeblack@google.com    return *(sc_time *)nullptr;
30812916Sgabeblack@google.com}
30912916Sgabeblack@google.com
31012927Sgabeblack@google.comsc_time_tuple::sc_time_tuple(const sc_time &)
31112927Sgabeblack@google.com{
31212927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
31312927Sgabeblack@google.com}
31412927Sgabeblack@google.com
31512927Sgabeblack@google.combool
31612927Sgabeblack@google.comsc_time_tuple::has_value() const
31712927Sgabeblack@google.com{
31812927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
31912927Sgabeblack@google.com    return false;
32012927Sgabeblack@google.com}
32112927Sgabeblack@google.com
32212927Sgabeblack@google.comsc_dt::uint64
32312927Sgabeblack@google.comsc_time_tuple::value() const
32412927Sgabeblack@google.com{
32512927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
32612927Sgabeblack@google.com    return 0;
32712927Sgabeblack@google.com}
32812927Sgabeblack@google.com
32912927Sgabeblack@google.comconst char *
33012927Sgabeblack@google.comsc_time_tuple::unit_symbol() const
33112927Sgabeblack@google.com{
33212927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
33312927Sgabeblack@google.com    return "";
33412927Sgabeblack@google.com}
33512927Sgabeblack@google.com
33612927Sgabeblack@google.comdouble
33712927Sgabeblack@google.comsc_time_tuple::to_double() const
33812927Sgabeblack@google.com{
33912927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
34012927Sgabeblack@google.com    return 0.0;
34112927Sgabeblack@google.com}
34212927Sgabeblack@google.com
34312927Sgabeblack@google.comstd::string
34412927Sgabeblack@google.comsc_time_tuple::to_string() const
34512927Sgabeblack@google.com{
34612927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
34712927Sgabeblack@google.com    return "";
34812927Sgabeblack@google.com}
34912927Sgabeblack@google.com
35012837Sgabeblack@google.com} // namespace sc_core
351