Jump to content

Modeling packets age using sc_time_stamp()


MehdiT

Recommended Posts

The time at which a packet is generated is stored in the header of the packet. The header allocates only 32 bits for this timestamp.

For any variable of type sc_time T_start, T_start.to_default_time_unit() will return the number of clock cycles if default time unit is configured to (CLK_PERIOD, SC_NS).

 

The question here is how to convert sc_time values to 32 bits unsigned integer without large loss of precision.

 

At the producer side this piece of code runs each time is generated:

sc_time t_start = sc_time_stamp(); 
double magnitude = t_start.to_default_time_unit(); // number of clock cycles since the start of the simulation until this time (is this correct?)
unsigned int t_start_u_32 = (unsigned int) std::round(magnitude); // possible loss of precision if magnitude is too large to be presented in 32 bits 
// store t_start_u_32 in the correspondent packet header field. 

At the consumer side (an arbiter that selects packets depending on their age)

double t_start_ret = t_start_u_32 * CLK_PERIOD;
sc_time t_start(t_start_ret, SC_NS); 
sc_time packet_age = sc_time_stamp() - t_start; 

Is there any good way to manipulate sc_time variables, default time unit, round/cast operations without loosing precision under the constraint of 32 bits?

Link to comment
Share on other sites

The time at which a packet is generated is stored in the header of the packet. The header allocates only 32 bits for this timestamp.

For any variable of type sc_time T_start, T_start.to_default_time_unit() will return the number of clock cycles if default time unit is configured to (CLK_PERIOD, SC_NS).

 

The question here is how to convert sc_time values to 32 bits unsigned integer without large loss of precision.

 

At the producer side this piece of code runs each time is generated:

sc_time t_start = sc_time_stamp(); 
double magnitude = t_start.to_default_time_unit(); // number of clock cycles since the start of the simulation until this time (is this correct?)
unsigned int t_start_u_32 = (unsigned int) std::round(magnitude); // possible loss of precision if magnitude is too large to be presented in 32 bits 
// store t_start_u_32 in the correspondent packet header field. 

At the consumer side (an arbiter that selects packets depending on their age)

double t_start_ret = t_start_u_32 * CLK_PERIOD;
sc_time t_start(t_start_ret, SC_NS); 
sc_time packet_age = sc_time_stamp() - t_start; 

Is there any good way to manipulate sc_time variables, default time unit, round/cast operations without loosing precision under the constraint of 32 bits?

Hello Sir,

SystemC, being a C++ library, will always have inherent

loss of precision, when doing a type conversion that are

built into the core language -- cannot be avoided. Please

note that the double 'magnitude' value that you compute 

also involves a type conversion (time stamp object to

double) and will again involve loss of precision. Finally,

to compute the packet age, you re-convert the unsigned

int value to a time stamp object, which will again involve

loss of precision. Hope that helps.

Link to comment
Share on other sites

First of all, "default time units" and all related functions are not part of the IEEE 1666 SystemC standard and are sometimes provided as deprecated features in the SystemC implementations. You should not use them.

 

If you want to compute the number of cycles since the start of the simulation, you can store the clock period as sc_time value and divide the current simulation time by this period:
 

sc_time now = sc_time_stamp();
sc_dt::uint64 cycle = static_cast<sc_dt::uint64>( now / clk_period );

Assuming that your package won't get older than 2^32-1 cycles, you should be able to store the lowest 32-bit of this cycle number and check for overflows during the age computation (i.e. new time is smaller than start time).

 

hth,
  Philipp

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...