1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004, 2005 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 General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with libgfortran; see the file COPYING. If not,
27 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
31 #include <sys/types.h>
32 #include "libgfortran.h"
36 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
37 # include <sys/time.h>
39 #elif defined(HAVE_TIME_H)
47 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
48 static struct timeval tp0
= {-1, 0};
49 #elif defined(HAVE_TIME_H)
50 static time_t t0
= (time_t) -2;
54 extern void system_clock_4 (GFC_INTEGER_4
*, GFC_INTEGER_4
*, GFC_INTEGER_4
*);
55 export_proto(system_clock_4
);
57 extern void system_clock_8 (GFC_INTEGER_8
*, GFC_INTEGER_8
*, GFC_INTEGER_8
*);
58 export_proto(system_clock_8
);
61 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
62 intrinsic subroutine. It returns the number of clock ticks for the current
63 system time, the number of ticks per second, and the maximum possible value
64 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
67 system_clock_4(GFC_INTEGER_4
*count
, GFC_INTEGER_4
*count_rate
,
68 GFC_INTEGER_4
*count_max
)
74 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
79 if (gettimeofday(&tp1
, &tzp
) == 0)
88 /* TODO: Convert this to integer arithmetic. */
89 t
= (double) (tp1
.tv_sec
- tp0
.tv_sec
);
90 t
+= (double) (tp1
.tv_usec
- tp0
.tv_usec
) * 1.e
-6;
93 if (t
> (double) GFC_INTEGER_4_HUGE
)
95 /* Time has wrapped. */
96 while (t
> (double) GFC_INTEGER_4_HUGE
)
97 t
-= (double) GFC_INTEGER_4_HUGE
;
100 cnt
= (GFC_INTEGER_4
) t
;
103 mx
= GFC_INTEGER_4_HUGE
;
108 *count
= - GFC_INTEGER_4_HUGE
;
109 if (count_rate
!= NULL
)
111 if (count_max
!= NULL
)
115 #elif defined(HAVE_TIME_H)
120 if (t1
== (time_t) -1)
122 cnt
= - GFC_INTEGER_4_HUGE
;
125 else if (t0
== (time_t) -2)
129 /* The timer counts in seconts, so for simplicity assume it never wraps.
130 Even with 32-bit counters this only happens once every 68 years. */
132 mx
= GFC_INTEGER_4_HUGE
;
135 cnt
= - GFC_INTEGER_4_HUGE
;
140 if (count_rate
!= NULL
)
142 if (count_max
!= NULL
)
147 /* INTEGER(8) version of the above routine. */
150 system_clock_8 (GFC_INTEGER_8
*count
, GFC_INTEGER_8
*count_rate
,
151 GFC_INTEGER_8
*count_max
)
157 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
162 if (gettimeofday(&tp1
, &tzp
) == 0)
171 /* TODO: Convert this to integer arithmetic. */
172 t
= (double) (tp1
.tv_sec
- tp0
.tv_sec
);
173 t
+= (double) (tp1
.tv_usec
- tp0
.tv_usec
) * 1.e
-6;
176 if (t
> (double) GFC_INTEGER_8_HUGE
)
178 /* Time has wrapped. */
179 while (t
> (double) GFC_INTEGER_8_HUGE
)
180 t
-= (double) GFC_INTEGER_8_HUGE
;
183 cnt
= (GFC_INTEGER_8
) t
;
186 mx
= GFC_INTEGER_8_HUGE
;
191 *count
= - GFC_INTEGER_8_HUGE
;
192 if (count_rate
!= NULL
)
194 if (count_max
!= NULL
)
199 #elif defined(HAVE_TIME_H)
204 if (t1
== (time_t) -1)
206 cnt
= - GFC_INTEGER_8_HUGE
;
209 else if (t0
== (time_t) -2)
213 /* The timer counts in seconts, so for simplicity assume it never wraps.
214 Even with 32-bit counters this only happens once every 68 years. */
216 mx
= GFC_INTEGER_8_HUGE
;
219 cnt
= - GFC_INTEGER_8_HUGE
;
224 if (count_rate
!= NULL
)
226 if (count_max
!= NULL
)