sc_time.cc revision 12983:fb1f462ae89e
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "base/logging.hh"
31#include "systemc/ext/core/sc_time.hh"
32
33namespace sc_core
34{
35
36namespace
37{
38
39const char *TimeUnitNames[] = {
40    [SC_FS] = "fs",
41    [SC_PS] = "ps",
42    [SC_NS] = "ns",
43    [SC_US] = "us",
44    [SC_MS] = "ms",
45    [SC_SEC] = "s"
46};
47
48double TimeUnitScale[] = {
49    [SC_FS] = 1.0e-15,
50    [SC_PS] = 1.0e-12,
51    [SC_NS] = 1.0e-9,
52    [SC_US] = 1.0e-6,
53    [SC_MS] = 1.0e-3,
54    [SC_SEC] = 1.0
55};
56
57} // anonymous namespace
58
59sc_time::sc_time() : val(0) {}
60
61sc_time::sc_time(double d, sc_time_unit tu)
62{
63    val = 0;
64    if (d != 0) {
65        //XXX Assuming the time resolution is 1ps.
66        double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
67        // Accellera claims there is a linux bug, and that these next two
68        // lines work around them.
69        volatile double tmp = d * scale + 0.5;
70        val = static_cast<uint64_t>(tmp);
71    }
72}
73
74sc_time::sc_time(const sc_time &t)
75{
76    val = t.val;
77}
78
79sc_time::sc_time(double, bool)
80{
81    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
82}
83
84sc_time::sc_time(sc_dt::uint64, bool)
85{
86    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
87}
88
89sc_time &
90sc_time::operator = (const sc_time &t)
91{
92    val = t.val;
93    return *this;
94}
95
96sc_dt::uint64
97sc_time::value() const
98{
99    return val;
100}
101
102double
103sc_time::to_double() const
104{
105    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
106    return 0.0;
107}
108double
109sc_time::to_seconds() const
110{
111    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
112    return 0.0;
113}
114
115const std::string
116sc_time::to_string() const
117{
118    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
119    return "";
120}
121
122bool
123sc_time::operator == (const sc_time &t) const
124{
125    return val == t.val;
126}
127
128bool
129sc_time::operator != (const sc_time &t) const
130{
131    return val != t.val;
132}
133
134bool
135sc_time::operator < (const sc_time &t) const
136{
137    return val < t.val;
138}
139
140bool
141sc_time::operator <= (const sc_time &t) const
142{
143    return val <= t.val;
144}
145
146bool
147sc_time::operator > (const sc_time &t) const
148{
149    return val > t.val;
150}
151
152bool
153sc_time::operator >= (const sc_time &t) const
154{
155    return val >= t.val;
156}
157
158sc_time &
159sc_time::operator += (const sc_time &t)
160{
161    val += t.val;
162    return *this;
163}
164
165sc_time &
166sc_time::operator -= (const sc_time &t)
167{
168    val -= t.val;
169    return *this;
170}
171
172sc_time &
173sc_time::operator *= (double)
174{
175    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
176    return *this;
177}
178
179sc_time &
180sc_time::operator /= (double)
181{
182    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
183    return *this;
184}
185
186void
187sc_time::print(std::ostream &os) const
188{
189    if (val == 0) {
190        os << "0 s";
191    } else {
192        //XXX Assuming the time resolution is 1ps.
193        sc_time_unit tu = SC_PS;
194        uint64_t scaled = val;
195        while (tu < SC_SEC && (scaled % 1000) == 0) {
196            tu = (sc_time_unit)((int)tu + 1);
197            scaled /= 1000;
198        }
199
200        os << scaled << ' ' << TimeUnitNames[tu];
201    }
202}
203
204sc_time
205sc_time::from_value(sc_dt::uint64 u)
206{
207    sc_time t;
208    t.val = u;
209    return t;
210}
211
212sc_time
213sc_time::from_seconds(double)
214{
215    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
216    return sc_time();
217}
218
219sc_time
220sc_time::from_string(const char *str)
221{
222    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
223    return sc_time();
224}
225
226const sc_time
227operator + (const sc_time &, const sc_time &)
228{
229    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
230    return sc_time();
231}
232
233const sc_time
234operator - (const sc_time &, const sc_time &)
235{
236    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
237    return sc_time();
238}
239
240const sc_time
241operator * (const sc_time &, double)
242{
243    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
244    return sc_time();
245}
246
247const sc_time
248operator * (double, const sc_time &)
249{
250    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
251    return sc_time();
252}
253
254const sc_time
255operator / (const sc_time &, double)
256{
257    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
258    return sc_time();
259}
260
261double
262operator / (const sc_time &, const sc_time &)
263{
264    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
265    return 0.0;
266}
267
268std::ostream &
269operator << (std::ostream &os, const sc_time &t)
270{
271    t.print(os);
272    return os;
273}
274
275const sc_time SC_ZERO_TIME;
276
277void
278sc_set_time_resolution(double, sc_time_unit)
279{
280    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
281}
282
283sc_time
284sc_get_time_resolution()
285{
286    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
287    return sc_time();
288}
289
290const sc_time &
291sc_max_time()
292{
293    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
294    return *(const sc_time *)nullptr;
295}
296
297void
298sc_set_default_time_unit(double, sc_time_unit)
299{
300    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
301}
302
303sc_time
304sc_get_default_time_unit()
305{
306    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
307    return *(sc_time *)nullptr;
308}
309
310sc_time_tuple::sc_time_tuple(const sc_time &)
311{
312    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
313}
314
315bool
316sc_time_tuple::has_value() const
317{
318    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
319    return false;
320}
321
322sc_dt::uint64
323sc_time_tuple::value() const
324{
325    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
326    return 0;
327}
328
329const char *
330sc_time_tuple::unit_symbol() const
331{
332    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
333    return "";
334}
335
336double
337sc_time_tuple::to_double() const
338{
339    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
340    return 0.0;
341}
342
343std::string
344sc_time_tuple::to_string() const
345{
346    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
347    return "";
348}
349
350} // namespace sc_core
351