1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgfortran; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
23 #include "libgfortran.h"
27 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
28 # include <sys/time.h>
30 #elif defined(HAVE_TIME_H)
38 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
39 static struct timeval tp0
= {-1, 0};
40 #elif defined(HAVE_TIME_H)
41 static time_t t0
= (time_t) -2;
44 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
45 intrinsic subroutine. It returns the number of clock ticks for the current
46 system time, the number of ticks per second, and the maximum possible value
47 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
50 prefix(system_clock_4
)(GFC_INTEGER_4
*count
, GFC_INTEGER_4
*count_rate
,
51 GFC_INTEGER_4
*count_max
)
57 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
62 if (gettimeofday(&tp1
, &tzp
) == 0)
71 /* TODO: Convert this to integer arithmetic. */
72 t
= (double) (tp1
.tv_sec
- tp0
.tv_sec
);
73 t
+= (double) (tp1
.tv_usec
- tp0
.tv_usec
) * 1.e
-6;
76 if (t
> (double) GFC_INTEGER_4_HUGE
)
78 /* Time has wrapped. */
79 while (t
> (double) GFC_INTEGER_4_HUGE
)
80 t
-= (double) GFC_INTEGER_4_HUGE
;
83 cnt
= (GFC_INTEGER_4
) t
;
86 mx
= GFC_INTEGER_4_HUGE
;
90 if (count
!= NULL
) *count
= - GFC_INTEGER_4_HUGE
;
91 if (count_rate
!= NULL
) *count_rate
= 0;
92 if (count_max
!= NULL
) *count_max
= 0;
94 #elif defined(HAVE_TIME_H)
99 if (t1
== (time_t) -1)
101 cnt
= - GFC_INTEGER_4_HUGE
;
104 else if (t0
== (time_t) -2)
108 /* The timer counts in seconts, so for simplicity assume it never wraps.
109 Even with 32-bit counters this only happens once every 68 years. */
111 mx
= GFC_INTEGER_4_HUGE
;
114 cnt
= - GFC_INTEGER_4_HUGE
;
117 if (count
!= NULL
) *count
= cnt
;
118 if (count_rate
!= NULL
) *count_rate
= TCK
;
119 if (count_max
!= NULL
) *count_max
= mx
;
123 /* INTEGER(8) version of the above routine. */
126 prefix(system_clock_8
)(GFC_INTEGER_8
*count
, GFC_INTEGER_8
*count_rate
,
127 GFC_INTEGER_8
*count_max
)
133 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
138 if (gettimeofday(&tp1
, &tzp
) == 0)
147 /* TODO: Convert this to integer arithmetic. */
148 t
= (double) (tp1
.tv_sec
- tp0
.tv_sec
);
149 t
+= (double) (tp1
.tv_usec
- tp0
.tv_usec
) * 1.e
-6;
152 if (t
> (double) GFC_INTEGER_8_HUGE
)
154 /* Time has wrapped. */
155 while (t
> (double) GFC_INTEGER_8_HUGE
)
156 t
-= (double) GFC_INTEGER_8_HUGE
;
159 cnt
= (GFC_INTEGER_8
) t
;
162 mx
= GFC_INTEGER_8_HUGE
;
166 if (count
!= NULL
) *count
= - GFC_INTEGER_8_HUGE
;
167 if (count_rate
!= NULL
) *count_rate
= 0;
168 if (count_max
!= NULL
) *count_max
= 0;
170 #elif defined(HAVE_TIME_H)
175 if (t1
== (time_t) -1)
177 cnt
= - GFC_INTEGER_8_HUGE
;
180 else if (t0
== (time_t) -2)
184 /* The timer counts in seconts, so for simplicity assume it never wraps.
185 Even with 32-bit counters this only happens once every 68 years. */
187 mx
= GFC_INTEGER_8_HUGE
;
190 cnt
= - GFC_INTEGER_8_HUGE
;
195 if (count_rate
!= NULL
)
197 if (count_max
!= NULL
)