#include "config.h"
#include <sys/types.h>
#include "libgfortran.h"
#include <limits.h>
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
# include <sys/time.h>
# define TCK 1000
#elif defined(HAVE_TIME_H)
# include <time.h>
# define TCK 1
#else
#define TCK 0
#endif
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
static struct timeval tp0 = {-1, 0};
#elif defined(HAVE_TIME_H)
static time_t t0 = (time_t) -2;
#endif
extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
export_proto(system_clock_4);
extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
export_proto(system_clock_8);
void
system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
GFC_INTEGER_4 *count_max)
{
GFC_INTEGER_4 cnt;
GFC_INTEGER_4 rate;
GFC_INTEGER_4 mx;
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
struct timeval tp1;
struct timezone tzp;
double t;
if (gettimeofday(&tp1, &tzp) == 0)
{
if (tp0.tv_sec < 0)
{
tp0 = tp1;
cnt = 0;
}
else
{
t = (double) (tp1.tv_sec - tp0.tv_sec);
t += (double) (tp1.tv_usec - tp0.tv_usec) * 1.e-6;
t *= TCK;
if (t > (double) GFC_INTEGER_4_HUGE)
{
while (t > (double) GFC_INTEGER_4_HUGE)
t -= (double) GFC_INTEGER_4_HUGE;
tp0 = tp1;
}
cnt = (GFC_INTEGER_4) t;
}
rate = TCK;
mx = GFC_INTEGER_4_HUGE;
}
else
{
if (count != NULL)
*count = - GFC_INTEGER_4_HUGE;
if (count_rate != NULL)
*count_rate = 0;
if (count_max != NULL)
*count_max = 0;
return;
}
#elif defined(HAVE_TIME_H)
time_t t, t1;
t1 = time(NULL);
if (t1 == (time_t) -1)
{
cnt = - GFC_INTEGER_4_HUGE;
mx = 0;
}
else if (t0 == (time_t) -2)
t0 = t1;
else
{
cnt = t1 - t0;
mx = GFC_INTEGER_4_HUGE;
}
#else
cnt = - GFC_INTEGER_4_HUGE;
mx = 0;
#endif
if (count != NULL)
*count = cnt;
if (count_rate != NULL)
*count_rate = TCK;
if (count_max != NULL)
*count_max = mx;
}
void
system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
GFC_INTEGER_8 *count_max)
{
GFC_INTEGER_8 cnt;
GFC_INTEGER_8 rate;
GFC_INTEGER_8 mx;
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
struct timeval tp1;
struct timezone tzp;
double t;
if (gettimeofday(&tp1, &tzp) == 0)
{
if (tp0.tv_sec < 0)
{
tp0 = tp1;
cnt = 0;
}
else
{
t = (double) (tp1.tv_sec - tp0.tv_sec);
t += (double) (tp1.tv_usec - tp0.tv_usec) * 1.e-6;
t *= TCK;
if (t > (double) GFC_INTEGER_8_HUGE)
{
while (t > (double) GFC_INTEGER_8_HUGE)
t -= (double) GFC_INTEGER_8_HUGE;
tp0 = tp1;
}
cnt = (GFC_INTEGER_8) t;
}
rate = TCK;
mx = GFC_INTEGER_8_HUGE;
}
else
{
if (count != NULL)
*count = - GFC_INTEGER_8_HUGE;
if (count_rate != NULL)
*count_rate = 0;
if (count_max != NULL)
*count_max = 0;
return;
}
#elif defined(HAVE_TIME_H)
time_t t, t1;
t1 = time(NULL);
if (t1 == (time_t) -1)
{
cnt = - GFC_INTEGER_8_HUGE;
mx = 0;
}
else if (t0 == (time_t) -2)
t0 = t1;
else
{
cnt = t1 - t0;
mx = GFC_INTEGER_8_HUGE;
}
#else
cnt = - GFC_INTEGER_8_HUGE;
mx = 0;
#endif
if (count != NULL)
*count = cnt;
if (count_rate != NULL)
*count_rate = TCK;
if (count_max != NULL)
*count_max = mx;
}