SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_time.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements. See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License. You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied. See the License for the specific language governing
16  permissions and limitations under the License.
17 
18  *****************************************************************************/
19 
20 /*****************************************************************************
21 
22  sc_time.h -- The time class.
23 */
34 #ifndef SC_TIME_H
35 #define SC_TIME_H
36 
37 
40 
41 #include <iostream>
42 
43 
44 namespace sc_core {
45 
46 class sc_simcontext;
47 
48 // friend operator declarations
49 
50  const sc_time operator + ( const sc_time&, const sc_time& );
51  const sc_time operator - ( const sc_time&, const sc_time& );
52  const sc_time operator * ( const sc_time&, double );
53  const sc_time operator * ( double, const sc_time& );
54  const sc_time operator / ( const sc_time&, double );
55  double operator / ( const sc_time&, const sc_time& );
56 
57 
65 {
66  SC_FS = 0,
72 };
73 
75 
83 {
84 public:
85 
87 
88  // constructors
89 
90  sc_time();
91  sc_time( const sc_time& );
92 
93  sc_time( double, sc_time_unit );
94  sc_time( double, sc_time_unit, sc_simcontext* );
95 
96  // convert time unit from string
97  // "fs"/"SC_FS"->SC_FS, "ps"/"SC_PS"->SC_PS, "ns"/"SC_NS"->SC_NS, ...
98  sc_time( double, const char* unit );
99  sc_time( double, const char* unit, sc_simcontext* );
100 
101  static sc_time from_value( value_type );
102  static sc_time from_seconds( double );
103  static sc_time from_string( const char * str );
104 
105  // deprecated, use from_value(v)
106  sc_time( double, bool scale );
107  sc_time( value_type, bool scale );
108 
109  // assignment operator
110 
111  sc_time& operator = ( const sc_time& );
112 
113 
114  // conversion functions
115 
116  value_type value() const; // relative to the time resolution
117  double to_double() const; // relative to the time resolution
118  double to_default_time_units() const;
119  double to_seconds() const;
120  const std::string to_string() const;
121 
122 
123  // relational operators
124 
125  bool operator == ( const sc_time& ) const;
126  bool operator != ( const sc_time& ) const;
127  bool operator < ( const sc_time& ) const;
128  bool operator <= ( const sc_time& ) const;
129  bool operator > ( const sc_time& ) const;
130  bool operator >= ( const sc_time& ) const;
131 
132 
133  // arithmetic operators
134 
135  sc_time& operator += ( const sc_time& );
136  sc_time& operator -= ( const sc_time& );
137 
138  friend const sc_time operator + ( const sc_time&, const sc_time& );
139  friend const sc_time operator - ( const sc_time&, const sc_time& );
140 
141  sc_time& operator *= ( double );
142  sc_time& operator /= ( double );
143  sc_time& operator %= ( const sc_time& );
144 
145  friend const sc_time operator * ( const sc_time&, double );
146  friend const sc_time operator * ( double, const sc_time& );
147  friend const sc_time operator / ( const sc_time&, double );
148  friend double operator / ( const sc_time&, const sc_time& );
149  friend const sc_time operator % ( const sc_time&, const sc_time& );
150 
151 
152  // print function
153 
154  void print( ::std::ostream& os = std::cout ) const;
155 
156 private:
157 
158  value_type m_value;
159 };
160 
161 // ----------------------------------------------------------------------------
162 // CLASS : sc_time_tuple
163 //
164 // The time tuple helper class.
165 // ----------------------------------------------------------------------------
166 
168 {
169  typedef sc_time::value_type value_type;
170  friend class sc_time;
171 
172 private:
173  explicit sc_time_tuple( value_type v );
174  void init( value_type v );
175 
176 public:
178  : m_value(), m_unit( SC_SEC ), m_offset(1) {}
179 
180  sc_time_tuple( const sc_time & t );
181 
182  bool has_value() const;
183  value_type value() const;
184  sc_time_unit unit() const { return m_unit; } // normalized unit
185  const char * unit_symbol() const; // normalized unit symbol
186 
187  operator sc_time() const { return sc_time( to_double(), m_unit ); }
188 
189  double to_double() const; // relative to the normalized unit
190  std::string to_string() const;
191 
192 private:
193  value_type m_value;
194  sc_time_unit m_unit;
195  unsigned m_offset;
196 };
197 
198 // print operator
199 
200 inline ::std::ostream& operator << ( ::std::ostream&, const sc_time& );
201 
202 
203 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
204 
205 extern SC_API const sc_time SC_ZERO_TIME;
206 
207 // constructors
208 
209 inline
211 : m_value( 0 )
212 {}
213 
214 inline
216 : m_value( t.m_value )
217 {}
218 
219 inline
221  : m_value(), m_unit( SC_SEC ), m_offset(1)
222 {
223  if( v )
224  init( v );
225 }
226 
227 inline
229  : m_value(), m_unit( SC_SEC ), m_offset(1)
230 {
231  if( t != SC_ZERO_TIME )
232  init( t.value() );
233 }
234 
235 inline
236 sc_time
238 {
239  return sc_time( v, SC_SEC );
240 }
241 
242 
243 // assignment operator
244 
245 inline
246 sc_time&
248 {
249  m_value = t.m_value;
250  return *this;
251 }
252 
253 
254 // conversion functions
255 
256 inline
258 sc_time::value() const // relative to the time resolution
259 {
260  return m_value;
261 }
262 
263 
264 inline
265 double
266 sc_time::to_double() const // relative to the time resolution
267 {
268  return sc_dt::uint64_to_double( m_value );
269 }
270 
271 
272 inline
273 double
274 sc_time_tuple::to_double() const // relative to the normalized time unit
275 {
276  return sc_dt::uint64_to_double( m_value ) * m_offset;
277 }
278 
279 
280 inline
281 const std::string
283 {
284  return sc_time_tuple( *this ).to_string();
285 }
286 
287 
288 // relational operators
289 
290 inline
291 bool
293 {
294  return ( m_value == t.m_value );
295 }
296 
297 inline
298 bool
300 {
301  return ( m_value != t.m_value );
302 }
303 
304 inline
305 bool
306 sc_time::operator < ( const sc_time& t ) const
307 {
308  return ( m_value < t.m_value );
309 }
310 
311 inline
312 bool
314 {
315  return ( m_value <= t.m_value );
316 }
317 
318 inline
319 bool
320 sc_time::operator > ( const sc_time& t ) const
321 {
322  return ( m_value > t.m_value );
323 }
324 
325 inline
326 bool
328 {
329  return ( m_value >= t.m_value );
330 }
331 
332 
333 // arithmetic operators
334 
335 inline
336 sc_time&
338 {
339  m_value += t.m_value;
340  return *this;
341 }
342 
343 inline
344 sc_time&
346 {
347  m_value -= t.m_value;
348  return *this;
349 }
350 
351 
352 inline
353 const sc_time
354 operator + ( const sc_time& t1, const sc_time& t2 )
355 {
356  return sc_time( t1 ) += t2;
357 }
358 
359 inline
360 const sc_time
361 operator - ( const sc_time& t1, const sc_time& t2 )
362 {
363  return sc_time( t1 ) -= t2;
364 }
365 
366 
367 inline
368 sc_time&
370 {
371  // linux bug workaround; don't change next two lines
372  volatile double tmp = sc_dt::uint64_to_double( m_value ) * d + 0.5;
373  m_value = static_cast<sc_dt::int64>( tmp );
374  return *this;
375 }
376 
377 inline
378 sc_time&
380 {
381  // linux bug workaround; don't change next two lines
382  volatile double tmp = sc_dt::uint64_to_double( m_value ) / d + 0.5;
383  m_value = static_cast<sc_dt::int64>( tmp );
384  return *this;
385 }
386 
387 inline
388 sc_time&
390 {
391  m_value %= t.m_value;
392  return *this;
393 }
394 
395 inline
396 const sc_time
397 operator * ( const sc_time& t, double d )
398 {
399  sc_time tmp( t );
400  return tmp *= d;
401 }
402 
403 inline
404 const sc_time
405 operator * ( double d, const sc_time& t )
406 {
407  sc_time tmp( t );
408  return tmp *= d;
409 }
410 
411 inline
412 const sc_time
413 operator / ( const sc_time& t, double d )
414 {
415  sc_time tmp( t );
416  return tmp /= d;
417 }
418 
419 inline
420 double
421 operator / ( const sc_time& t1, const sc_time& t2 )
422 {
423  return ( t1.to_double() / t2.to_double() );
424 }
425 
426 inline
427 const sc_time
428 operator % ( const sc_time& t1, const sc_time& t2 )
429 {
430  sc_time tmp(t1);
431  return tmp %= t2;
432 }
433 
434 // print operator
435 
436 inline
437 ::std::ostream&
438 operator << ( ::std::ostream& os, const sc_time& t )
439 {
440  t.print( os );
441  return os;
442 }
443 
444 
452 {
453  double time_resolution; // in femto seconds
456 
457  sc_time::value_type default_time_unit; // in time resolution
459 
460  sc_time_params();
461  ~sc_time_params();
462 };
463 
464 
465 // ----------------------------------------------------------------------------
466 
467 // functions for accessing the time resolution and default time unit
468 
469 SC_API extern void sc_set_time_resolution( double, sc_time_unit );
471 
472 SC_API extern void sc_set_default_time_unit( double, sc_time_unit );
474 
475 } // namespace sc_core
476 
477 #endif
478 
479 // $Log: sc_time.h,v $
480 // Revision 1.5 2011/08/26 20:46:11 acg
481 // Andy Goodrich: moved the modification log to the end of the file to
482 // eliminate source line number skew when check-ins are done.
483 //
484 // Revision 1.4 2011/02/18 20:27:14 acg
485 // Andy Goodrich: Updated Copyrights.
486 //
487 // Revision 1.3 2011/02/13 21:47:38 acg
488 // Andy Goodrich: update copyright notice.
489 //
490 // Revision 1.2 2008/05/22 17:06:27 acg
491 // Andy Goodrich: updated copyright notice to include 2008.
492 //
493 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
494 // SystemC 2.3
495 //
496 // Revision 1.4 2006/05/08 18:02:06 acg
497 // Andy Goodrich: added David Long's forward declarations for friend
498 // functions, methods, and operators to keep the Microsoft compiler happy.
499 //
500 // Revision 1.3 2006/01/13 18:44:30 acg
501 // Added $Log to record CVS changes into the source.
502 
503 // Taf!
const sc_time operator/(const sc_time &, double)
Definition: sc_time.h:413
SC_API const sc_time SC_ZERO_TIME
value_type value() const
Definition: sc_time.h:258
inline ::std::ostream & operator<<(::std::ostream &os, const sc_fifo< T > &a)
Definition: sc_fifo.h:431
SC_API sc_time sc_get_default_time_unit()
bool operator!=(const sc_time &) const
Definition: sc_time.h:299
void print(::std::ostream &os=std::cout) const
sc_time_unit unit() const
Definition: sc_time.h:184
int64_t int64
Definition: sc_nbdefs.h:188
bool operator<=(const sc_time &) const
Definition: sc_time.h:313
Struct that holds the time resolution and default time unit.
Definition: sc_time.h:451
const sc_time operator*(const sc_time &, double)
Definition: sc_time.h:397
friend class sc_time
Definition: sc_time.h:170
sc_time & operator/=(double)
Definition: sc_time.h:379
const sc_time operator-(const sc_time &, const sc_time &)
Definition: sc_time.h:361
uint64_t uint64
Definition: sc_nbdefs.h:189
sc_dt::uint64 value_type
Definition: sc_time.h:86
bool operator>(const sc_time &) const
Definition: sc_time.h:320
sc_time & operator+=(const sc_time &)
Definition: sc_time.h:337
bool operator==(const sc_process_handle &left, const sc_process_handle &right)
sc_time & operator*=(double)
Definition: sc_time.h:369
bool time_resolution_specified
Definition: sc_time.h:454
double uint64_to_double(uint64 a)
Platform independent conversion from double uint64 to double.
Definition: scfx_ieee.h:685
bool operator>=(const sc_int_base &a, const sc_int_base &b)
Definition: sc_int_base.h:741
const sc_time operator%(const sc_time &t1, const sc_time &t2)
Definition: sc_time.h:428
sc_clock period is zero sc_clock low time is zero sc_fifo< T > cannot have more than one writer bind interface to port failed complete binding failed remove port failed insert primitive channel failed sc_signal< T > cannot have more than one driver resolved port not bound to resolved signal sc_semaphore requires an initial value
SC_API const std::string to_string(sc_enc)
bool operator>(const sc_int_base &a, const sc_int_base &b)
Definition: sc_int_base.h:738
SC_API void sc_set_time_resolution(double, sc_time_unit)
The simulation context.
sc_time & operator=(const sc_time &)
Definition: sc_time.h:247
sc_time & operator-=(const sc_time &)
Definition: sc_time.h:345
double to_double() const
Definition: sc_time.h:266
sc_time::value_type default_time_unit
Definition: sc_time.h:457
const sc_time operator+(const sc_time &, const sc_time &)
Definition: sc_time.h:354
The time class.
Definition: sc_time.h:82
sc_time & operator%=(const sc_time &)
Definition: sc_time.h:389
bool operator<(const sc_time &) const
Definition: sc_time.h:306
SC_API sc_time sc_get_time_resolution()
bool operator<(const sc_process_handle &left, const sc_process_handle &right)
bool operator==(const sc_time &) const
Definition: sc_time.h:292
const std::string to_string() const
Definition: sc_time.h:282
Top level header file for arbitrary precision signed/unsigned.
double to_double() const
Definition: sc_time.h:274
class SC_API sc_simcontext
Definition: sc_object.h:51
SC_API void sc_set_default_time_unit(double, sc_time_unit)
bool operator>=(const sc_time &) const
Definition: sc_time.h:327
bool operator!=(const sc_process_handle &left, const sc_process_handle &right)
bool default_time_unit_specified
Definition: sc_time.h:458
static sc_time from_seconds(double)
Definition: sc_time.h:237
class SC_API sc_time_tuple
Definition: sc_time.h:74
bool operator<=(const sc_int_base &a, const sc_int_base &b)
Definition: sc_int_base.h:735
#define SC_API
Definition: sc_cmnhdr.h:168
sc_time_unit
Enumeration of time units.
Definition: sc_time.h:64