* global.c (global_alloc): Make it static.
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blob63c7045a9bd710baf8f17bcacf36d147c3f5ddc8
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 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
48 export_proto(system_clock_4);
50 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
51 export_proto(system_clock_8);
54 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
55 intrinsic subroutine. It returns the number of clock ticks for the current
56 system time, the number of ticks per second, and the maximum possible value
57 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
59 void
60 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
61 GFC_INTEGER_4 *count_max)
63 GFC_INTEGER_4 cnt;
64 GFC_INTEGER_4 rate;
65 GFC_INTEGER_4 mx;
67 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
68 struct timeval tp1;
69 struct timezone tzp;
71 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
72 internal_error (NULL, "tv_sec too small");
74 if (gettimeofday(&tp1, &tzp) == 0)
76 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
77 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
78 if (ucnt > GFC_INTEGER_4_HUGE)
79 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
80 else
81 cnt = ucnt;
82 rate = TCK;
83 mx = GFC_INTEGER_4_HUGE;
85 else
87 if (count != NULL)
88 *count = - GFC_INTEGER_4_HUGE;
89 if (count_rate != NULL)
90 *count_rate = 0;
91 if (count_max != NULL)
92 *count_max = 0;
93 return;
95 #elif defined(HAVE_TIME_H)
96 GFC_UINTEGER_4 ucnt;
98 if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
99 internal_error (NULL, "time_t too small");
101 ucnt = time (NULL);
102 if (ucnt > GFC_INTEGER_4_HUGE)
103 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
104 else
105 cnt = ucnt;
106 mx = GFC_INTEGER_4_HUGE;
107 #else
108 cnt = - GFC_INTEGER_4_HUGE;
109 mx = 0;
110 #endif
111 if (count != NULL)
112 *count = cnt;
113 if (count_rate != NULL)
114 *count_rate = TCK;
115 if (count_max != NULL)
116 *count_max = mx;
120 /* INTEGER(8) version of the above routine. */
122 void
123 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
124 GFC_INTEGER_8 *count_max)
126 GFC_INTEGER_8 cnt;
127 GFC_INTEGER_8 rate;
128 GFC_INTEGER_8 mx;
130 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
131 struct timeval tp1;
132 struct timezone tzp;
134 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
135 internal_error (NULL, "tv_sec too small");
137 if (gettimeofday(&tp1, &tzp) == 0)
139 if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
141 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
142 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
143 if (ucnt > GFC_INTEGER_4_HUGE)
144 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
145 else
146 cnt = ucnt;
147 mx = GFC_INTEGER_4_HUGE;
149 else
151 GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
152 ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
153 if (ucnt > GFC_INTEGER_8_HUGE)
154 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
155 else
156 cnt = ucnt;
157 mx = GFC_INTEGER_8_HUGE;
159 rate = TCK;
161 else
163 if (count != NULL)
164 *count = - GFC_INTEGER_8_HUGE;
165 if (count_rate != NULL)
166 *count_rate = 0;
167 if (count_max != NULL)
168 *count_max = 0;
170 return;
172 #elif defined(HAVE_TIME_H)
173 if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
174 internal_error (NULL, "time_t too small");
175 else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
177 GFC_UINTEGER_4 ucnt = time (NULL);
178 if (ucnt > GFC_INTEGER_4_HUGE)
179 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
180 else
181 cnt = ucnt;
182 mx = GFC_INTEGER_4_HUGE;
184 else
186 GFC_UINTEGER_8 ucnt = time (NULL);
187 if (ucnt > GFC_INTEGER_8_HUGE)
188 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
189 else
190 cnt = ucnt;
191 mx = GFC_INTEGER_8_HUGE;
193 #else
194 cnt = - GFC_INTEGER_8_HUGE;
195 mx = 0;
196 #endif
197 if (count != NULL)
198 *count = cnt;
199 if (count_rate != NULL)
200 *count_rate = TCK;
201 if (count_max != NULL)
202 *count_max = mx;