PR libfortran/18966
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blobb778b0373a3c60ca4d6df40244beda772da8660b
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. */
21 #include "config.h"
22 #include <sys/types.h>
23 #include "libgfortran.h"
25 #include <limits.h>
27 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
28 # include <sys/time.h>
29 # define TCK 1000
30 #elif defined(HAVE_TIME_H)
31 # include <time.h>
32 # define TCK 1
33 #else
34 #define TCK 0
35 #endif
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;
42 #endif
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. */
57 void
58 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
59 GFC_INTEGER_4 *count_max)
61 GFC_INTEGER_4 cnt;
62 GFC_INTEGER_4 rate;
63 GFC_INTEGER_4 mx;
65 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
66 struct timeval tp1;
67 struct timezone tzp;
68 double t;
70 if (gettimeofday(&tp1, &tzp) == 0)
72 if (tp0.tv_sec < 0)
74 tp0 = tp1;
75 cnt = 0;
77 else
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;
82 t *= TCK;
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;
89 tp0 = tp1;
91 cnt = (GFC_INTEGER_4) t;
93 rate = TCK;
94 mx = GFC_INTEGER_4_HUGE;
96 else
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)
103 time_t t, t1;
105 t1 = time(NULL);
107 if (t1 == (time_t) -1)
109 cnt = - GFC_INTEGER_4_HUGE;
110 mx = 0;
112 else if (t0 == (time_t) -2)
113 t0 = t1;
114 else
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. */
118 cnt = t1 - t0;
119 mx = GFC_INTEGER_4_HUGE;
121 #else
122 cnt = - GFC_INTEGER_4_HUGE;
123 mx = 0;
124 #endif
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. */
133 void
134 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
135 GFC_INTEGER_8 *count_max)
137 GFC_INTEGER_8 cnt;
138 GFC_INTEGER_8 rate;
139 GFC_INTEGER_8 mx;
141 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
142 struct timeval tp1;
143 struct timezone tzp;
144 double t;
146 if (gettimeofday(&tp1, &tzp) == 0)
148 if (tp0.tv_sec < 0)
150 tp0 = tp1;
151 cnt = 0;
153 else
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;
158 t *= TCK;
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;
165 tp0 = tp1;
167 cnt = (GFC_INTEGER_8) t;
169 rate = TCK;
170 mx = GFC_INTEGER_8_HUGE;
172 else
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)
179 time_t t, t1;
181 t1 = time(NULL);
183 if (t1 == (time_t) -1)
185 cnt = - GFC_INTEGER_8_HUGE;
186 mx = 0;
188 else if (t0 == (time_t) -2)
189 t0 = t1;
190 else
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. */
194 cnt = t1 - t0;
195 mx = GFC_INTEGER_8_HUGE;
197 #else
198 cnt = - GFC_INTEGER_8_HUGE;
199 mx = 0;
200 #endif
201 if (count != NULL)
202 *count = cnt;
203 if (count_rate != NULL)
204 *count_rate = TCK;
205 if (count_max != NULL)
206 *count_max = mx;