Merged with mainline at revision 128810.
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blobdb87bd4d95b8e3cce20974c909f4038d4877d513
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004, 2005, 2007 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 "libgfortran.h"
32 #include <limits.h>
34 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
35 # include <sys/time.h>
36 # define TCK 1000
37 #elif defined(HAVE_TIME_H)
38 # include <time.h>
39 # define TCK 1
40 #else
41 #define TCK 0
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;
69 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
70 internal_error (NULL, "tv_sec too small");
72 if (gettimeofday(&tp1, &tzp) == 0)
74 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
75 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
76 if (ucnt > GFC_INTEGER_4_HUGE)
77 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
78 else
79 cnt = ucnt;
80 rate = TCK;
81 mx = GFC_INTEGER_4_HUGE;
83 else
85 if (count != NULL)
86 *count = - GFC_INTEGER_4_HUGE;
87 if (count_rate != NULL)
88 *count_rate = 0;
89 if (count_max != NULL)
90 *count_max = 0;
91 return;
93 #elif defined(HAVE_TIME_H)
94 GFC_UINTEGER_4 ucnt;
96 if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
97 internal_error (NULL, "time_t too small");
99 ucnt = time (NULL);
100 if (ucnt > GFC_INTEGER_4_HUGE)
101 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
102 else
103 cnt = ucnt;
104 mx = GFC_INTEGER_4_HUGE;
105 #else
106 cnt = - GFC_INTEGER_4_HUGE;
107 mx = 0;
108 #endif
109 if (count != NULL)
110 *count = cnt;
111 if (count_rate != NULL)
112 *count_rate = TCK;
113 if (count_max != NULL)
114 *count_max = mx;
118 /* INTEGER(8) version of the above routine. */
120 void
121 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
122 GFC_INTEGER_8 *count_max)
124 GFC_INTEGER_8 cnt;
125 GFC_INTEGER_8 rate;
126 GFC_INTEGER_8 mx;
128 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
129 struct timeval tp1;
130 struct timezone tzp;
132 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
133 internal_error (NULL, "tv_sec too small");
135 if (gettimeofday(&tp1, &tzp) == 0)
137 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
139 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
140 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
141 if (ucnt > GFC_INTEGER_4_HUGE)
142 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
143 else
144 cnt = ucnt;
145 mx = GFC_INTEGER_4_HUGE;
147 else
149 GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
150 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
151 if (ucnt > GFC_INTEGER_8_HUGE)
152 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
153 else
154 cnt = ucnt;
155 mx = GFC_INTEGER_8_HUGE;
157 rate = TCK;
159 else
161 if (count != NULL)
162 *count = - GFC_INTEGER_8_HUGE;
163 if (count_rate != NULL)
164 *count_rate = 0;
165 if (count_max != NULL)
166 *count_max = 0;
168 return;
170 #elif defined(HAVE_TIME_H)
171 if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
172 internal_error (NULL, "time_t too small");
173 else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
175 GFC_UINTEGER_4 ucnt = time (NULL);
176 if (ucnt > GFC_INTEGER_4_HUGE)
177 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
178 else
179 cnt = ucnt;
180 mx = GFC_INTEGER_4_HUGE;
182 else
184 GFC_UINTEGER_8 ucnt = time (NULL);
185 if (ucnt > GFC_INTEGER_8_HUGE)
186 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
187 else
188 cnt = ucnt;
189 mx = GFC_INTEGER_8_HUGE;
191 #else
192 cnt = - GFC_INTEGER_8_HUGE;
193 mx = 0;
194 #endif
195 if (count != NULL)
196 *count = cnt;
197 if (count_rate != NULL)
198 *count_rate = TCK;
199 if (count_max != NULL)
200 *count_max = mx;