1
0
Fork 0
A test of the feasibility of hardware pipes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

91 lines
1.5 KiB

#include <stdio.h>
#include <time.h>
static void err(char* msg, int exit_code)
{
fprintf(stderr, "error: %s\n", msg);
exit(exit_code);
}
typedef struct timespec TimeSpec;
unsigned long pow10(unsigned long p)
{
unsigned ret = 1;
size_t i;
for (i = 0; i < p; ++i)
{
ret *= 10;
}
return ret;
}
TimeSpec ts_get(void)
{
TimeSpec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts;
}
TimeSpec ts_diff(TimeSpec end, TimeSpec start)
{
TimeSpec temp;
if ((end.tv_nsec - start.tv_nsec) < 0)
{
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
}
else
{
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return temp;
}
TimeSpec ts_add(TimeSpec ts1, TimeSpec ts2)
{
TimeSpec temp;
if ((ts1.tv_nsec + ts2.tv_nsec) > 1000000000)
{
temp.tv_sec = ts1.tv_sec + ts2.tv_sec + 1;
temp.tv_nsec = ts1.tv_nsec + ts2.tv_nsec - 1000000000;
}
else
{
temp.tv_sec = ts1.tv_sec + ts2.tv_sec;
temp.tv_nsec = ts1.tv_nsec + ts2.tv_nsec;
}
return temp;
}
unsigned long ts_div(TimeSpec *ts, unsigned long denom)
{
TimeSpec temp;
unsigned long ns;
temp.tv_sec = ts->tv_sec / denom;
ns = (ts->tv_sec % denom) * 1000000000;
ns = ns + ts->tv_nsec;
temp.tv_nsec = ns / denom;
ns %= denom;
*ts = temp;
return ns;
}
void ts_time(TimeSpec *ts) {
clock_gettime(CLOCK_MONOTONIC, ts);
}
void ts_print(TimeSpec ts, size_t iters) {
unsigned long mod = ts_div(&ts, iters);
printf("%lu.%09lu%09zu", (unsigned long) ts.tv_sec, ts.tv_nsec, mod);
}