Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blob1d6455831cf03c33ca1d88f3fdda3d2b3d5b42e0
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 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
18 executable.)
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., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
30 #include "config.h"
31 #include <sys/types.h>
32 #include "libgfortran.h"
34 #include <limits.h>
36 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
37 # include <sys/time.h>
38 # define TCK 1000
39 #elif defined(HAVE_TIME_H)
40 # include <time.h>
41 # define TCK 1
42 #else
43 #define TCK 0
44 #endif
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;
51 #endif
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. */
66 void
67 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
68 GFC_INTEGER_4 *count_max)
70 GFC_INTEGER_4 cnt;
71 GFC_INTEGER_4 rate;
72 GFC_INTEGER_4 mx;
74 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
75 struct timeval tp1;
76 struct timezone tzp;
77 double t;
79 if (gettimeofday(&tp1, &tzp) == 0)
81 if (tp0.tv_sec < 0)
83 tp0 = tp1;
84 cnt = 0;
86 else
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;
91 t *= TCK;
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;
98 tp0 = tp1;
100 cnt = (GFC_INTEGER_4) t;
102 rate = TCK;
103 mx = GFC_INTEGER_4_HUGE;
105 else
107 if (count != NULL) *count = - GFC_INTEGER_4_HUGE;
108 if (count_rate != NULL) *count_rate = 0;
109 if (count_max != NULL) *count_max = 0;
111 #elif defined(HAVE_TIME_H)
112 time_t t, t1;
114 t1 = time(NULL);
116 if (t1 == (time_t) -1)
118 cnt = - GFC_INTEGER_4_HUGE;
119 mx = 0;
121 else if (t0 == (time_t) -2)
122 t0 = t1;
123 else
125 /* The timer counts in seconts, so for simplicity assume it never wraps.
126 Even with 32-bit counters this only happens once every 68 years. */
127 cnt = t1 - t0;
128 mx = GFC_INTEGER_4_HUGE;
130 #else
131 cnt = - GFC_INTEGER_4_HUGE;
132 mx = 0;
133 #endif
134 if (count != NULL) *count = cnt;
135 if (count_rate != NULL) *count_rate = TCK;
136 if (count_max != NULL) *count_max = mx;
140 /* INTEGER(8) version of the above routine. */
142 void
143 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
144 GFC_INTEGER_8 *count_max)
146 GFC_INTEGER_8 cnt;
147 GFC_INTEGER_8 rate;
148 GFC_INTEGER_8 mx;
150 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
151 struct timeval tp1;
152 struct timezone tzp;
153 double t;
155 if (gettimeofday(&tp1, &tzp) == 0)
157 if (tp0.tv_sec < 0)
159 tp0 = tp1;
160 cnt = 0;
162 else
164 /* TODO: Convert this to integer arithmetic. */
165 t = (double) (tp1.tv_sec - tp0.tv_sec);
166 t += (double) (tp1.tv_usec - tp0.tv_usec) * 1.e-6;
167 t *= TCK;
169 if (t > (double) GFC_INTEGER_8_HUGE)
171 /* Time has wrapped. */
172 while (t > (double) GFC_INTEGER_8_HUGE)
173 t -= (double) GFC_INTEGER_8_HUGE;
174 tp0 = tp1;
176 cnt = (GFC_INTEGER_8) t;
178 rate = TCK;
179 mx = GFC_INTEGER_8_HUGE;
181 else
183 if (count != NULL) *count = - GFC_INTEGER_8_HUGE;
184 if (count_rate != NULL) *count_rate = 0;
185 if (count_max != NULL) *count_max = 0;
187 #elif defined(HAVE_TIME_H)
188 time_t t, t1;
190 t1 = time(NULL);
192 if (t1 == (time_t) -1)
194 cnt = - GFC_INTEGER_8_HUGE;
195 mx = 0;
197 else if (t0 == (time_t) -2)
198 t0 = t1;
199 else
201 /* The timer counts in seconts, so for simplicity assume it never wraps.
202 Even with 32-bit counters this only happens once every 68 years. */
203 cnt = t1 - t0;
204 mx = GFC_INTEGER_8_HUGE;
206 #else
207 cnt = - GFC_INTEGER_8_HUGE;
208 mx = 0;
209 #endif
210 if (count != NULL)
211 *count = cnt;
212 if (count_rate != NULL)
213 *count_rate = TCK;
214 if (count_max != NULL)
215 *count_max = mx;