gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sc_time.cc
Go to the documentation of this file.
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 <cmath>
31 #include <cstring>
32 #include <sstream>
33 #include <vector>
34 
35 #include "base/types.hh"
36 #include "sim/core.hh"
37 #include "systemc/core/time.hh"
42 
43 namespace sc_core
44 {
45 
46 namespace
47 {
48 
49 void
51 {
52  if (d != 0)
54 
56  // Accellera claims there is a linux bug, and that these next two
57  // lines work around them.
58  volatile double tmp = d * scale + 0.5;
59  *time = sc_time::from_value(static_cast<uint64_t>(tmp));
60 }
61 
62 double defaultUnit = 1.0e-9;
63 
64 } // anonymous namespace
65 
67 
69 {
70  val = 0;
71  set(this, d, tu);
72 }
73 
75 {
76  val = t.val;
77 }
78 
79 sc_time::sc_time(double d, const char *unit)
80 {
82  for (tu = SC_FS; tu <= SC_SEC; tu = (sc_time_unit)(tu + 1)) {
83  if (strcmp(unit, sc_gem5::TimeUnitNames[tu]) == 0 ||
84  strcmp(unit, sc_gem5::TimeUnitConstantNames[tu]) == 0) {
85  break;
86  }
87  }
88 
89  if (tu > SC_SEC) {
90  SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_,"invalid unit given");
91  val = 0;
92  return;
93  }
94  set(this, d, tu);
95 }
96 
97 sc_time::sc_time(double d, bool scale)
98 {
99  double scaler = scale ? defaultUnit : SimClock::Float::Hz;
100  set(this, d * scaler, SC_SEC);
101 }
102 
104 {
105  double scaler = scale ? defaultUnit : SimClock::Float::Hz;
106  set(this, static_cast<double>(v) * scaler, SC_SEC);
107 }
108 
109 sc_time &
111 {
112  val = t.val;
113  return *this;
114 }
115 
118 {
119  return val;
120 }
121 
122 double
124 {
125  return static_cast<double>(val);
126 }
127 double
129 {
130  return to_double() * SimClock::Float::Hz;
131 }
132 
133 const std::string
135 {
136  std::ostringstream ss;
137  print(ss);
138  return ss.str();
139 }
140 
141 bool
143 {
144  return val == t.val;
145 }
146 
147 bool
149 {
150  return val != t.val;
151 }
152 
153 bool
155 {
156  return val < t.val;
157 }
158 
159 bool
161 {
162  return val <= t.val;
163 }
164 
165 bool
167 {
168  return val > t.val;
169 }
170 
171 bool
173 {
174  return val >= t.val;
175 }
176 
177 sc_time &
179 {
180  val += t.val;
181  return *this;
182 }
183 
184 sc_time &
186 {
187  val -= t.val;
188  return *this;
189 }
190 
191 sc_time &
193 {
194  val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
195  return *this;
196 }
197 
198 sc_time &
200 {
201  val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
202  return *this;
203 }
204 
205 void
206 sc_time::print(std::ostream &os) const
207 {
208  os << sc_time_tuple(*this).to_string();
209 }
210 
211 sc_time
213 {
214  if (u)
216  sc_time t;
217  t.val = u;
218  return t;
219 }
220 
221 sc_time
223 {
224  sc_time t;
225  set(&t, d, SC_SEC);
226  return t;
227 }
228 
229 sc_time
230 sc_time::from_string(const char *str)
231 {
232  char *end = nullptr;
233 
234  double d = str ? std::strtod(str, &end) : 0.0;
235  if (str == end || d < 0.0) {
236  SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_, "invalid value given");
237  return SC_ZERO_TIME;
238  }
239 
240  while (*end && std::isspace(*end))
241  end++;
242 
243  return sc_time(d, end);
244 }
245 
246 const sc_time
247 operator + (const sc_time &a, const sc_time &b)
248 {
249  return sc_time::from_value(a.value() + b.value());
250 }
251 
252 const sc_time
253 operator - (const sc_time &a, const sc_time &b)
254 {
255  return sc_time::from_value(a.value() - b.value());
256 }
257 
258 const sc_time
259 operator * (const sc_time &t, double d)
260 {
261  volatile double tmp = static_cast<double>(t.value()) * d + 0.5;
262  return sc_time::from_value(static_cast<int64_t>(tmp));
263 }
264 
265 const sc_time
266 operator * (double d, const sc_time &t)
267 {
268  volatile double tmp = d * static_cast<double>(t.value()) + 0.5;
269  return sc_time::from_value(static_cast<int64_t>(tmp));
270 }
271 
272 const sc_time
273 operator / (const sc_time &t, double d)
274 {
275  volatile double tmp = static_cast<double>(t.value()) / d + 0.5;
276  return sc_time::from_value(static_cast<int64_t>(tmp));
277 }
278 
279 double
280 operator / (const sc_time &t1, const sc_time &t2)
281 {
282  return t1.to_double() / t2.to_double();
283 }
284 
285 std::ostream &
286 operator << (std::ostream &os, const sc_time &t)
287 {
288  t.print(os);
289  return os;
290 }
291 
293 
294 void
296 {
297  if (d <= 0.0)
298  SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value not positive");
299 
300  double dummy;
301  if (modf(log10(d), &dummy) != 0.0) {
303  "value not a power of ten");
304  }
305  if (sc_is_running())
306  SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "simulation running");
307 
308  static bool specified = false;
309  if (specified)
310  SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "already specified");
311 
312  // This won't detect the timescale being fixed outside of systemc, but
313  // it's at least some protection.
314  if (clockFrequencyFixed()) {
316  "sc_time object(s) constructed");
317  }
318 
319  double seconds = d * sc_gem5::TimeUnitScale[tu];
320  if (seconds < sc_gem5::TimeUnitScale[SC_FS])
321  SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value smaller than 1 fs");
322 
323  if (seconds > defaultUnit) {
325  defaultUnit = seconds;
326  }
327 
328  // Get rid of fractional parts of d.
329  while (d < 1.0 && tu > SC_FS) {
330  d *= 1000;
331  tu = (sc_time_unit)(tu - 1);
332  }
333 
334  Tick ticks_per_second =
335  sc_gem5::TimeUnitFrequency[tu] / static_cast<Tick>(d);
336  setClockFrequency(ticks_per_second);
337  specified = true;
338 }
339 
340 sc_time
342 {
343  return sc_time::from_value(1);
344 }
345 
346 const sc_time &
348 {
349  static const sc_time MaxScTime = sc_time::from_value(MaxTick);
350  return MaxScTime;
351 }
352 
353 void
355 {
356  if (d < 0.0)
357  SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "value not positive");
358 
359  double dummy;
360  if (modf(log10(d), &dummy) != 0.0) {
362  "value not a power of ten");
363  }
364  if (sc_is_running())
365  SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "simulation running");
366 
367  static bool specified = false;
368  if (specified) {
369  SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "already specified");
370  }
371  // This won't detect the timescale being fixed outside of systemc, but
372  // it's at least some protection.
373  if (clockFrequencyFixed()) {
375  "sc_time object(s) constructed");
376  }
377 
378  // Normalize d to seconds.
379  defaultUnit = d * sc_gem5::TimeUnitScale[tu];
380  specified = true;
381 
382  double resolution = SimClock::Float::Hz;
383  if (resolution == 0.0)
384  resolution = sc_gem5::TimeUnitScale[SC_PS];
385  if (defaultUnit < resolution) {
387  "value smaller than time resolution");
388  }
389 }
390 
391 sc_time
393 {
394  return sc_time(defaultUnit, SC_SEC);
395 }
396 
398  _value(), _unit(SC_SEC), _set(true)
399 {
400  if (!t.value())
401  return;
402 
403  Tick frequency = SimClock::Frequency;
404 
405  // Shrink the frequency by scaling down the time period, ie converting
406  // it from cycles per second to cycles per millisecond, etc.
407  while (_unit > 1 && (frequency % 1000 == 0)) {
408  _unit = (sc_time_unit)((int)_unit - 1);
409  frequency /= 1000;
410  }
411 
412  // Convert the frequency into a period.
413  Tick period;
414  if (frequency > 1) {
415  _unit = (sc_time_unit)((int)_unit - 1);
416  period = 1000 / frequency;
417  } else {
418  period = frequency;
419  }
420 
421  // Scale our integer value by the period.
422  _value = t.value() * period;
423 
424  // Shrink the scaled time value by increasing the size of the units
425  // it's measured by, avoiding fractional parts.
426  while (_unit < SC_SEC && (_value % 1000) == 0) {
427  _unit = (sc_time_unit)((int)_unit + 1);
428  _value /= 1000;
429  }
430 }
431 
432 bool
434 {
435  return _set;
436 }
437 
439 
440 const char *
442 {
444 }
445 
446 double sc_time_tuple::to_double() const { return static_cast<double>(_value); }
447 
448 std::string
450 {
451  std::ostringstream ss;
452  ss << _value << ' ' << unit_symbol();
453  return ss.str();
454 }
455 
456 } // namespace sc_core
bool operator<(const sc_time &) const
Definition: sc_time.cc:154
void print(std::ostream &=std::cout) const
Definition: sc_time.cc:206
sc_time_unit _unit
Definition: sc_time.hh:136
bool sc_is_running()
Definition: sc_main.cc:144
Bitfield< 28 > v
sc_time & operator+=(const sc_time &)
Definition: sc_time.cc:178
Bitfield< 30, 28 > tu
bool operator!=(const sc_time &) const
Definition: sc_time.cc:148
sc_time_unit
Definition: sc_time.hh:42
Bitfield< 2 > t2
const sc_time operator/(const sc_time &t, double d)
Definition: sc_time.cc:273
bool operator==(const sc_time &) const
Definition: sc_time.cc:142
Bitfield< 8 > a
sc_time & operator*=(double)
Definition: sc_time.cc:192
void setClockFrequency(Tick tps)
Definition: core.cc:115
double s
These variables equal the number of ticks in the unit of time they&#39;re named after in a double...
Definition: core.cc:52
sc_time & operator/=(double)
Definition: sc_time.cc:199
static sc_time from_string(const char *str)
Definition: sc_time.cc:230
std::ostream & operator<<(std::ostream &os, sc_status s)
Definition: sc_main.cc:181
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:49
#define SC_REPORT_WARNING(msg_type, msg)
Bitfield< 17 > os
Definition: misc.hh:805
Bitfield< 63 > val
Definition: misc.hh:771
static sc_time from_value(sc_dt::uint64)
Definition: sc_time.cc:212
Bitfield< 7 > b
sc_dt::uint64 _value
Definition: sc_time.hh:135
const Tick MaxTick
Definition: types.hh:65
sc_dt::uint64 value() const
Definition: sc_time.cc:117
Bitfield< 22 > u
const char SC_ID_TIME_CONVERSION_FAILED_[]
Definition: messages.cc:150
const char * unit_symbol() const
Definition: sc_time.cc:441
sc_time & operator=(const sc_time &)
Definition: sc_time.cc:110
uint64_t Tick
Tick count type.
Definition: types.hh:63
Tick TimeUnitFrequency[]
Definition: time.cc:64
const char SC_ID_SET_DEFAULT_TIME_UNIT_[]
Definition: messages.cc:59
Bitfield< 9 > d
sc_time & operator-=(const sc_time &)
Definition: sc_time.cc:185
bool clockFrequencyFixed()
Definition: core.cc:112
Bitfield< 21 > ss
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
double to_seconds() const
Definition: sc_time.cc:128
const char SC_ID_SET_TIME_RESOLUTION_[]
Definition: messages.cc:58
bool operator>(const sc_time &) const
Definition: sc_time.cc:166
double to_double() const
Definition: sc_time.cc:446
scale
Definition: types.hh:94
void fixClockFrequency()
Definition: core.cc:84
const sc_time SC_ZERO_TIME
Definition: sc_time.cc:292
const sc_time operator*(const sc_time &t, double d)
Definition: sc_time.cc:259
uint64_t uint64
Definition: sc_nbdefs.hh:172
Bitfield< 1 > t1
static sc_time from_seconds(double)
Definition: sc_time.cc:222
sc_dt::uint64 value() const
Definition: sc_time.cc:438
const char * TimeUnitConstantNames[]
Definition: time.cc:46
const char SC_ID_DEFAULT_TIME_UNIT_CHANGED_[]
Definition: messages.cc:60
const sc_time & sc_max_time()
Definition: sc_time.cc:347
sc_time sc_get_time_resolution()
Definition: sc_time.cc:341
#define SC_REPORT_ERROR(msg_type, msg)
const sc_time operator+(const sc_time &a, const sc_time &b)
Definition: sc_time.cc:247
const std::string to_string() const
Definition: sc_time.cc:134
double to_double() const
Definition: sc_time.cc:123
bool has_value() const
Definition: sc_time.cc:433
double Hz
These variables the inverse of above.
Definition: core.cc:58
void sc_set_time_resolution(double d, sc_time_unit tu)
Definition: sc_time.cc:295
bool operator>=(const sc_time &) const
Definition: sc_time.cc:172
Bitfield< 5 > t
bool operator<=(const sc_time &) const
Definition: sc_time.cc:160
sc_time sc_get_default_time_unit()
Definition: sc_time.cc:392
uint64_t val
Definition: sc_time.hh:92
const char * TimeUnitNames[]
Definition: time.cc:37
std::string to_string() const
Definition: sc_time.cc:449
double TimeUnitScale[]
Definition: time.cc:55
void sc_set_default_time_unit(double d, sc_time_unit tu)
Definition: sc_time.cc:354
const sc_time operator-(const sc_time &a, const sc_time &b)
Definition: sc_time.cc:253

Generated on Fri Feb 28 2020 16:27:03 for gem5 by doxygen 1.8.13