Merged trunk at revision 161680 into branch.
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blob7cc82d0c8cd77a89b29e4542676c34826cb0276f
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 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 3 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 General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
27 #include <limits.h>
29 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
30 # include <sys/time.h>
31 #elif defined(HAVE_TIME_H)
32 # include <time.h>
33 # define TCK 1
34 #else
35 #define TCK 0
36 #endif
39 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
40 export_proto(system_clock_4);
42 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
43 export_proto(system_clock_8);
46 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
47 intrinsic subroutine. It returns the number of clock ticks for the current
48 system time, the number of ticks per second, and the maximum possible value
49 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
51 void
52 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
53 GFC_INTEGER_4 *count_max)
55 GFC_INTEGER_4 cnt;
56 GFC_INTEGER_4 mx;
58 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
59 #undef TCK
60 #define TCK 1000
61 struct timeval tp1;
63 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
64 internal_error (NULL, "tv_sec too small");
66 if (gettimeofday(&tp1, NULL) == 0)
68 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
69 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
70 if (ucnt > GFC_INTEGER_4_HUGE)
71 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
72 else
73 cnt = ucnt;
74 mx = GFC_INTEGER_4_HUGE;
76 else
78 if (count != NULL)
79 *count = - GFC_INTEGER_4_HUGE;
80 if (count_rate != NULL)
81 *count_rate = 0;
82 if (count_max != NULL)
83 *count_max = 0;
84 return;
86 #elif defined(HAVE_TIME_H)
87 GFC_UINTEGER_4 ucnt;
89 if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
90 internal_error (NULL, "time_t too small");
92 ucnt = time (NULL);
93 if (ucnt > GFC_INTEGER_4_HUGE)
94 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
95 else
96 cnt = ucnt;
97 mx = GFC_INTEGER_4_HUGE;
98 #else
99 cnt = - GFC_INTEGER_4_HUGE;
100 mx = 0;
101 #endif
102 if (count != NULL)
103 *count = cnt;
104 if (count_rate != NULL)
105 *count_rate = TCK;
106 if (count_max != NULL)
107 *count_max = mx;
111 /* INTEGER(8) version of the above routine. */
113 void
114 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
115 GFC_INTEGER_8 *count_max)
117 GFC_INTEGER_8 cnt;
118 GFC_INTEGER_8 mx;
120 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
121 #undef TCK
122 #define TCK 1000000
123 struct timeval tp1;
125 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
126 internal_error (NULL, "tv_sec too small");
128 if (gettimeofday(&tp1, NULL) == 0)
130 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
132 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
133 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
134 if (ucnt > GFC_INTEGER_4_HUGE)
135 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
136 else
137 cnt = ucnt;
138 mx = GFC_INTEGER_4_HUGE;
140 else
142 GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
143 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
144 if (ucnt > GFC_INTEGER_8_HUGE)
145 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
146 else
147 cnt = ucnt;
148 mx = GFC_INTEGER_8_HUGE;
151 else
153 if (count != NULL)
154 *count = - GFC_INTEGER_8_HUGE;
155 if (count_rate != NULL)
156 *count_rate = 0;
157 if (count_max != NULL)
158 *count_max = 0;
160 return;
162 #elif defined(HAVE_TIME_H)
163 if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
164 internal_error (NULL, "time_t too small");
165 else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
167 GFC_UINTEGER_4 ucnt = time (NULL);
168 if (ucnt > GFC_INTEGER_4_HUGE)
169 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
170 else
171 cnt = ucnt;
172 mx = GFC_INTEGER_4_HUGE;
174 else
176 GFC_UINTEGER_8 ucnt = time (NULL);
177 if (ucnt > GFC_INTEGER_8_HUGE)
178 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
179 else
180 cnt = ucnt;
181 mx = GFC_INTEGER_8_HUGE;
183 #else
184 cnt = - GFC_INTEGER_8_HUGE;
185 mx = 0;
186 #endif
187 if (count != NULL)
188 *count = cnt;
189 if (count_rate != NULL)
190 *count_rate = TCK;
191 if (count_max != NULL)
192 *count_max = mx;