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;
45 extern void system_clock_4 (GFC_INTEGER_4
*, GFC_INTEGER_4
*, GFC_INTEGER_4
*);
46 export_proto(system_clock_4
);
48 extern void system_clock_8 (GFC_INTEGER_8
*, GFC_INTEGER_8
*, GFC_INTEGER_8
*);
49 export_proto(system_clock_8
);
52 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
53 intrinsic subroutine. It returns the number of clock ticks for the current
54 system time, the number of ticks per second, and the maximum possible value
55 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
58 system_clock_4(GFC_INTEGER_4
*count
, GFC_INTEGER_4
*count_rate
,
59 GFC_INTEGER_4
*count_max
)
65 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
70 if (gettimeofday(&tp1
, &tzp
) == 0)
79 /* TODO: Convert this to integer arithmetic. */
80 t
= (double) (tp1
.tv_sec
- tp0
.tv_sec
);
81 t
+= (double) (tp1
.tv_usec
- tp0
.tv_usec
) * 1.e
-6;
84 if (t
> (double) GFC_INTEGER_4_HUGE
)
86 /* Time has wrapped. */
87 while (t
> (double) GFC_INTEGER_4_HUGE
)
88 t
-= (double) GFC_INTEGER_4_HUGE
;
91 cnt
= (GFC_INTEGER_4
) t
;
94 mx
= GFC_INTEGER_4_HUGE
;
98 if (count
!= NULL
) *count
= - GFC_INTEGER_4_HUGE
;
99 if (count_rate
!= NULL
) *count_rate
= 0;
100 if (count_max
!= NULL
) *count_max
= 0;
102 #elif defined(HAVE_TIME_H)
107 if (t1
== (time_t) -1)
109 cnt
= - GFC_INTEGER_4_HUGE
;
112 else if (t0
== (time_t) -2)
116 /* The timer counts in seconts, so for simplicity assume it never wraps.
117 Even with 32-bit counters this only happens once every 68 years. */
119 mx
= GFC_INTEGER_4_HUGE
;
122 cnt
= - GFC_INTEGER_4_HUGE
;
125 if (count
!= NULL
) *count
= cnt
;
126 if (count_rate
!= NULL
) *count_rate
= TCK
;
127 if (count_max
!= NULL
) *count_max
= mx
;
131 /* INTEGER(8) version of the above routine. */
134 system_clock_8 (GFC_INTEGER_8
*count
, GFC_INTEGER_8
*count_rate
,
135 GFC_INTEGER_8
*count_max
)
141 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
146 if (gettimeofday(&tp1
, &tzp
) == 0)
155 /* TODO: Convert this to integer arithmetic. */
156 t
= (double) (tp1
.tv_sec
- tp0
.tv_sec
);
157 t
+= (double) (tp1
.tv_usec
- tp0
.tv_usec
) * 1.e
-6;
160 if (t
> (double) GFC_INTEGER_8_HUGE
)
162 /* Time has wrapped. */
163 while (t
> (double) GFC_INTEGER_8_HUGE
)
164 t
-= (double) GFC_INTEGER_8_HUGE
;
167 cnt
= (GFC_INTEGER_8
) t
;
170 mx
= GFC_INTEGER_8_HUGE
;
174 if (count
!= NULL
) *count
= - GFC_INTEGER_8_HUGE
;
175 if (count_rate
!= NULL
) *count_rate
= 0;
176 if (count_max
!= NULL
) *count_max
= 0;
178 #elif defined(HAVE_TIME_H)
183 if (t1
== (time_t) -1)
185 cnt
= - GFC_INTEGER_8_HUGE
;
188 else if (t0
== (time_t) -2)
192 /* The timer counts in seconts, so for simplicity assume it never wraps.
193 Even with 32-bit counters this only happens once every 68 years. */
195 mx
= GFC_INTEGER_8_HUGE
;
198 cnt
= - GFC_INTEGER_8_HUGE
;
203 if (count_rate
!= NULL
)
205 if (count_max
!= NULL
)