OSDN Git Service

2005-01-12 Toon Moene <toon@moene.indiv.nluug.nl>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / system_clock.c
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
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.
10
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.)
19
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.
24
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., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA.  */
29
30 #include "config.h"
31 #include <sys/types.h>
32 #include "libgfortran.h"
33
34 #include <limits.h>
35
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
45
46
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
52
53
54 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
55 export_proto(system_clock_4);
56
57 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
58 export_proto(system_clock_8);
59
60
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. */
65
66 void
67 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
68                GFC_INTEGER_4 *count_max)
69 {
70   GFC_INTEGER_4 cnt;
71   GFC_INTEGER_4 rate;
72   GFC_INTEGER_4 mx;
73
74 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
75   struct timeval tp1;
76   struct timezone tzp;
77   double t;
78
79   if (gettimeofday(&tp1, &tzp) == 0)
80     {
81       if (tp0.tv_sec < 0)
82         {
83           tp0 = tp1;
84           cnt = 0;
85         }
86       else
87         {
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;
92
93           if (t > (double) GFC_INTEGER_4_HUGE)
94             {
95               /* Time has wrapped. */
96               while (t > (double) GFC_INTEGER_4_HUGE)
97                 t -= (double) GFC_INTEGER_4_HUGE;
98               tp0 = tp1;
99             }
100           cnt = (GFC_INTEGER_4) t;
101         }
102       rate = TCK;
103       mx = GFC_INTEGER_4_HUGE;
104     }
105   else
106     {
107       if (count != NULL) *count = - GFC_INTEGER_4_HUGE;
108       if (count_rate != NULL) *count_rate = 0;
109       if (count_max != NULL) *count_max = 0;
110     }
111 #elif defined(HAVE_TIME_H)
112   time_t t, t1;
113
114   t1 = time(NULL);
115
116   if (t1 == (time_t) -1)
117     {
118       cnt = - GFC_INTEGER_4_HUGE;
119       mx = 0;
120     }
121   else if (t0 == (time_t) -2) 
122     t0 = t1;
123   else
124     {
125       /* The timer counts in seconts, so for simplicity assume it never wraps.
126          Even with 32-bit counters this only happens once every 68 years.  */
127       cnt = t1 - t0;
128       mx = GFC_INTEGER_4_HUGE;
129     }
130 #else
131   cnt = - GFC_INTEGER_4_HUGE;
132   mx = 0;
133 #endif
134   if (count != NULL) *count = cnt;
135   if (count_rate != NULL) *count_rate = TCK;
136   if (count_max != NULL) *count_max = mx;
137 }
138
139
140 /* INTEGER(8) version of the above routine.  */
141
142 void
143 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
144                 GFC_INTEGER_8 *count_max)
145 {
146   GFC_INTEGER_8 cnt;
147   GFC_INTEGER_8 rate;
148   GFC_INTEGER_8 mx;
149
150 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
151   struct timeval tp1;
152   struct timezone tzp;
153   double t;
154
155   if (gettimeofday(&tp1, &tzp) == 0)
156     {
157       if (tp0.tv_sec < 0)
158         {
159           tp0 = tp1;
160           cnt = 0;
161         }
162       else
163         {
164           /* TODO: Convert this to integer arithmetic.  */
165           t  = (double) (tp1.tv_sec  - tp0.tv_sec);
166           t += (double) (tp1.tv_usec - tp0.tv_usec) * 1.e-6;
167           t *= TCK;
168
169           if (t > (double) GFC_INTEGER_8_HUGE)
170             {
171               /* Time has wrapped. */
172               while (t > (double) GFC_INTEGER_8_HUGE)
173                 t -= (double) GFC_INTEGER_8_HUGE;
174               tp0 = tp1;
175             }
176           cnt = (GFC_INTEGER_8) t;
177         }
178       rate = TCK;
179       mx = GFC_INTEGER_8_HUGE;
180     }
181   else
182     {
183       if (count != NULL) *count = - GFC_INTEGER_8_HUGE;
184       if (count_rate != NULL) *count_rate = 0;
185       if (count_max != NULL) *count_max = 0;
186     }
187 #elif defined(HAVE_TIME_H)
188   time_t t, t1;
189
190   t1 = time(NULL);
191
192   if (t1 == (time_t) -1)
193     {
194       cnt = - GFC_INTEGER_8_HUGE;
195       mx = 0;
196     }
197   else if (t0 == (time_t) -2) 
198     t0 = t1;
199   else
200     {
201       /* The timer counts in seconts, so for simplicity assume it never wraps.
202          Even with 32-bit counters this only happens once every 68 years.  */
203       cnt = t1 - t0;
204       mx = GFC_INTEGER_8_HUGE;
205     }
206 #else
207   cnt = - GFC_INTEGER_8_HUGE;
208   mx = 0;
209 #endif
210   if (count != NULL)
211     *count = cnt;
212   if (count_rate != NULL)
213     *count_rate = TCK;
214   if (count_max != NULL)
215     *count_max = mx;
216 }