2005-08-31 J"orn Rennecke <joern.rennecke@st.com>
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blob8a38f78480a110c4c41332781ab20f36f8693b6e
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
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., 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, 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)
108 *count = - GFC_INTEGER_4_HUGE;
109 if (count_rate != NULL)
110 *count_rate = 0;
111 if (count_max != NULL)
112 *count_max = 0;
113 return;
115 #elif defined(HAVE_TIME_H)
116 time_t t, t1;
118 t1 = time(NULL);
120 if (t1 == (time_t) -1)
122 cnt = - GFC_INTEGER_4_HUGE;
123 mx = 0;
125 else if (t0 == (time_t) -2)
126 t0 = t1;
127 else
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. */
131 cnt = t1 - t0;
132 mx = GFC_INTEGER_4_HUGE;
134 #else
135 cnt = - GFC_INTEGER_4_HUGE;
136 mx = 0;
137 #endif
138 if (count != NULL)
139 *count = cnt;
140 if (count_rate != NULL)
141 *count_rate = TCK;
142 if (count_max != NULL)
143 *count_max = mx;
147 /* INTEGER(8) version of the above routine. */
149 void
150 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
151 GFC_INTEGER_8 *count_max)
153 GFC_INTEGER_8 cnt;
154 GFC_INTEGER_8 rate;
155 GFC_INTEGER_8 mx;
157 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
158 struct timeval tp1;
159 struct timezone tzp;
160 double t;
162 if (gettimeofday(&tp1, &tzp) == 0)
164 if (tp0.tv_sec < 0)
166 tp0 = tp1;
167 cnt = 0;
169 else
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;
174 t *= TCK;
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;
181 tp0 = tp1;
183 cnt = (GFC_INTEGER_8) t;
185 rate = TCK;
186 mx = GFC_INTEGER_8_HUGE;
188 else
190 if (count != NULL)
191 *count = - GFC_INTEGER_8_HUGE;
192 if (count_rate != NULL)
193 *count_rate = 0;
194 if (count_max != NULL)
195 *count_max = 0;
197 return;
199 #elif defined(HAVE_TIME_H)
200 time_t t, t1;
202 t1 = time(NULL);
204 if (t1 == (time_t) -1)
206 cnt = - GFC_INTEGER_8_HUGE;
207 mx = 0;
209 else if (t0 == (time_t) -2)
210 t0 = t1;
211 else
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. */
215 cnt = t1 - t0;
216 mx = GFC_INTEGER_8_HUGE;
218 #else
219 cnt = - GFC_INTEGER_8_HUGE;
220 mx = 0;
221 #endif
222 if (count != NULL)
223 *count = cnt;
224 if (count_rate != NULL)
225 *count_rate = TCK;
226 if (count_max != NULL)
227 *count_max = mx;