sc_time.h revision 12027
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 24 Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21 25 26 CHANGE LOG AT THE END OF THE FILE 27 *****************************************************************************/ 28 29 30#ifndef SC_TIME_H 31#define SC_TIME_H 32 33 34#include "sysc/datatypes/int/sc_nbdefs.h" 35#include "sysc/datatypes/fx/scfx_ieee.h" 36#include "sysc/utils/sc_iostream.h" 37 38namespace sc_core { 39 40class sc_simcontext; 41 42// friend operator declarations 43 44 const sc_time operator + ( const sc_time&, const sc_time& ); 45 const sc_time operator - ( const sc_time&, const sc_time& ); 46 const sc_time operator * ( const sc_time&, double ); 47 const sc_time operator * ( double, const sc_time& ); 48 const sc_time operator / ( const sc_time&, double ); 49 double operator / ( const sc_time&, const sc_time& ); 50 51 52// ---------------------------------------------------------------------------- 53// ENUM : sc_time_unit 54// 55// Enumeration of time units. 56// ---------------------------------------------------------------------------- 57 58enum sc_time_unit 59{ 60 SC_FS = 0, 61 SC_PS, 62 SC_NS, 63 SC_US, 64 SC_MS, 65 SC_SEC 66}; 67 68 69// ---------------------------------------------------------------------------- 70// CLASS : sc_time 71// 72// The time class. 73// ---------------------------------------------------------------------------- 74 75class sc_time 76{ 77public: 78 79 typedef sc_dt::uint64 value_type; 80 81 // constructors 82 83 sc_time(); 84 sc_time( double, sc_time_unit ); 85 sc_time( double, sc_time_unit, sc_simcontext* ); 86 sc_time( const sc_time& ); 87 88 static sc_time from_value( value_type ); 89 90 // deprecated, use from_value(v) 91 sc_time( double, bool scale ); 92 sc_time( value_type, bool scale ); 93 94 // assignment operator 95 96 sc_time& operator = ( const sc_time& ); 97 98 99 // conversion functions 100 101 value_type value() const; // relative to the time resolution 102 double to_double() const; // relative to the time resolution 103 double to_default_time_units() const; 104 double to_seconds() const; 105 const std::string to_string() const; 106 107 108 // relational operators 109 110 bool operator == ( const sc_time& ) const; 111 bool operator != ( const sc_time& ) const; 112 bool operator < ( const sc_time& ) const; 113 bool operator <= ( const sc_time& ) const; 114 bool operator > ( const sc_time& ) const; 115 bool operator >= ( const sc_time& ) const; 116 117 118 // arithmetic operators 119 120 sc_time& operator += ( const sc_time& ); 121 sc_time& operator -= ( const sc_time& ); 122 123 friend const sc_time operator + ( const sc_time&, const sc_time& ); 124 friend const sc_time operator - ( const sc_time&, const sc_time& ); 125 126 sc_time& operator *= ( double ); 127 sc_time& operator /= ( double ); 128 sc_time& operator %= ( const sc_time& ); 129 130 friend const sc_time operator * ( const sc_time&, double ); 131 friend const sc_time operator * ( double, const sc_time& ); 132 friend const sc_time operator / ( const sc_time&, double ); 133 friend double operator / ( const sc_time&, const sc_time& ); 134 friend const sc_time operator % ( const sc_time&, const sc_time& ); 135 136 137 // print function 138 139 void print( ::std::ostream& os = std::cout ) const; 140 141private: 142 143 value_type m_value; 144}; 145 146 147// print operator 148 149inline ::std::ostream& operator << ( ::std::ostream&, const sc_time& ); 150 151 152// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 153 154// constructors 155 156inline 157sc_time::sc_time() 158: m_value( 0 ) 159{} 160 161inline 162sc_time::sc_time( const sc_time& t ) 163: m_value( t.m_value ) 164{} 165 166 167// assignment operator 168 169inline 170sc_time& 171sc_time::operator = ( const sc_time& t ) 172{ 173 m_value = t.m_value; 174 return *this; 175} 176 177 178// conversion functions 179 180inline 181sc_time::value_type 182sc_time::value() const // relative to the time resolution 183{ 184 return m_value; 185} 186 187 188inline 189double 190sc_time::to_double() const // relative to the time resolution 191{ 192 return sc_dt::uint64_to_double( m_value ); 193} 194 195 196// relational operators 197 198inline 199bool 200sc_time::operator == ( const sc_time& t ) const 201{ 202 return ( m_value == t.m_value ); 203} 204 205inline 206bool 207sc_time::operator != ( const sc_time& t ) const 208{ 209 return ( m_value != t.m_value ); 210} 211 212inline 213bool 214sc_time::operator < ( const sc_time& t ) const 215{ 216 return ( m_value < t.m_value ); 217} 218 219inline 220bool 221sc_time::operator <= ( const sc_time& t ) const 222{ 223 return ( m_value <= t.m_value ); 224} 225 226inline 227bool 228sc_time::operator > ( const sc_time& t ) const 229{ 230 return ( m_value > t.m_value ); 231} 232 233inline 234bool 235sc_time::operator >= ( const sc_time& t ) const 236{ 237 return ( m_value >= t.m_value ); 238} 239 240 241// arithmetic operators 242 243inline 244sc_time& 245sc_time::operator += ( const sc_time& t ) 246{ 247 m_value += t.m_value; 248 return *this; 249} 250 251inline 252sc_time& 253sc_time::operator -= ( const sc_time& t ) 254{ 255 m_value -= t.m_value; 256 return *this; 257} 258 259 260inline 261const sc_time 262operator + ( const sc_time& t1, const sc_time& t2 ) 263{ 264 return sc_time( t1 ) += t2; 265} 266 267inline 268const sc_time 269operator - ( const sc_time& t1, const sc_time& t2 ) 270{ 271 return sc_time( t1 ) -= t2; 272} 273 274 275inline 276sc_time& 277sc_time::operator *= ( double d ) 278{ 279 // linux bug workaround; don't change next two lines 280 volatile double tmp = sc_dt::uint64_to_double( m_value ) * d + 0.5; 281 m_value = SCAST<sc_dt::int64>( tmp ); 282 return *this; 283} 284 285inline 286sc_time& 287sc_time::operator /= ( double d ) 288{ 289 // linux bug workaround; don't change next two lines 290 volatile double tmp = sc_dt::uint64_to_double( m_value ) / d + 0.5; 291 m_value = SCAST<sc_dt::int64>( tmp ); 292 return *this; 293} 294 295inline 296sc_time& 297sc_time::operator %= ( const sc_time& t ) 298{ 299 m_value %= t.m_value; 300 return *this; 301} 302 303inline 304const sc_time 305operator * ( const sc_time& t, double d ) 306{ 307 sc_time tmp( t ); 308 return tmp *= d; 309} 310 311inline 312const sc_time 313operator * ( double d, const sc_time& t ) 314{ 315 sc_time tmp( t ); 316 return tmp *= d; 317} 318 319inline 320const sc_time 321operator / ( const sc_time& t, double d ) 322{ 323 sc_time tmp( t ); 324 return tmp /= d; 325} 326 327inline 328double 329operator / ( const sc_time& t1, const sc_time& t2 ) 330{ 331 return ( t1.to_double() / t2.to_double() ); 332} 333 334inline 335const sc_time 336operator % ( const sc_time& t1, const sc_time& t2 ) 337{ 338 sc_time tmp(t1); 339 return tmp %= t2; 340} 341 342// print operator 343 344inline 345::std::ostream& 346operator << ( ::std::ostream& os, const sc_time& t ) 347{ 348 t.print( os ); 349 return os; 350} 351 352 353// ---------------------------------------------------------------------------- 354// STRUCT : sc_time_params 355// 356// Struct that holds the time resolution and default time unit. 357// ---------------------------------------------------------------------------- 358 359struct sc_time_params 360{ 361 double time_resolution; // in femto seconds 362 bool time_resolution_specified; 363 bool time_resolution_fixed; 364 365 sc_time::value_type default_time_unit; // in time resolution 366 bool default_time_unit_specified; 367 368 sc_time_params(); 369 ~sc_time_params(); 370}; 371 372 373// ---------------------------------------------------------------------------- 374 375extern const sc_time SC_ZERO_TIME; 376 377 378// functions for accessing the time resolution and default time unit 379 380extern void sc_set_time_resolution( double, sc_time_unit ); 381extern sc_time sc_get_time_resolution(); 382 383extern void sc_set_default_time_unit( double, sc_time_unit ); 384extern sc_time sc_get_default_time_unit(); 385 386} // namespace sc_core 387 388#endif 389 390// $Log: sc_time.h,v $ 391// Revision 1.5 2011/08/26 20:46:11 acg 392// Andy Goodrich: moved the modification log to the end of the file to 393// eliminate source line number skew when check-ins are done. 394// 395// Revision 1.4 2011/02/18 20:27:14 acg 396// Andy Goodrich: Updated Copyrights. 397// 398// Revision 1.3 2011/02/13 21:47:38 acg 399// Andy Goodrich: update copyright notice. 400// 401// Revision 1.2 2008/05/22 17:06:27 acg 402// Andy Goodrich: updated copyright notice to include 2008. 403// 404// Revision 1.1.1.1 2006/12/15 20:20:05 acg 405// SystemC 2.3 406// 407// Revision 1.4 2006/05/08 18:02:06 acg 408// Andy Goodrich: added David Long's forward declarations for friend 409// functions, methods, and operators to keep the Microsoft compiler happy. 410// 411// Revision 1.3 2006/01/13 18:44:30 acg 412// Added $Log to record CVS changes into the source. 413 414// Taf! 415