OSDN Git Service

2008-02-19 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / system_clock.c
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2    Copyright (C) 2004, 2005, 2007 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., 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA.  */
29
30 #include "libgfortran.h"
31
32 #include <limits.h>
33
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
43
44
45 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
46 export_proto(system_clock_4);
47
48 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
49 export_proto(system_clock_8);
50
51
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. */
56
57 void
58 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
59                GFC_INTEGER_4 *count_max)
60 {
61   GFC_INTEGER_4 cnt;
62   GFC_INTEGER_4 mx;
63
64 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
65   struct timeval tp1;
66   struct timezone tzp;
67
68   if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
69     internal_error (NULL, "tv_sec too small");
70
71   if (gettimeofday(&tp1, &tzp) == 0)
72     {
73       GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
74       ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
75       if (ucnt > GFC_INTEGER_4_HUGE)
76         cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
77       else
78         cnt = ucnt;
79       mx = GFC_INTEGER_4_HUGE;
80     }
81   else
82     {
83       if (count != NULL)
84         *count = - GFC_INTEGER_4_HUGE;
85       if (count_rate != NULL)
86         *count_rate = 0;
87       if (count_max != NULL)
88         *count_max = 0;
89       return;
90     }
91 #elif defined(HAVE_TIME_H)
92   GFC_UINTEGER_4 ucnt;
93
94   if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
95     internal_error (NULL, "time_t too small");
96
97   ucnt = time (NULL);
98   if (ucnt > GFC_INTEGER_4_HUGE)
99     cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
100   else
101     cnt = ucnt;
102   mx = GFC_INTEGER_4_HUGE;
103 #else
104   cnt = - GFC_INTEGER_4_HUGE;
105   mx = 0;
106 #endif
107   if (count != NULL)
108     *count = cnt;
109   if (count_rate != NULL)
110     *count_rate = TCK;
111   if (count_max != NULL)
112     *count_max = mx;
113 }
114
115
116 /* INTEGER(8) version of the above routine.  */
117
118 void
119 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
120                 GFC_INTEGER_8 *count_max)
121 {
122   GFC_INTEGER_8 cnt;
123   GFC_INTEGER_8 mx;
124
125 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
126   struct timeval tp1;
127   struct timezone tzp;
128
129   if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
130     internal_error (NULL, "tv_sec too small");
131
132   if (gettimeofday(&tp1, &tzp) == 0)
133     {
134       if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
135         {
136           GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
137           ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
138           if (ucnt > GFC_INTEGER_4_HUGE)
139             cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
140           else
141             cnt = ucnt;
142           mx = GFC_INTEGER_4_HUGE;
143         }
144       else
145         {
146           GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
147           ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
148           if (ucnt > GFC_INTEGER_8_HUGE)
149             cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
150           else
151             cnt = ucnt;
152           mx = GFC_INTEGER_8_HUGE;
153         }
154     }
155   else
156     {
157       if (count != NULL)
158         *count = - GFC_INTEGER_8_HUGE;
159       if (count_rate != NULL)
160         *count_rate = 0;
161       if (count_max != NULL)
162         *count_max = 0;
163
164       return;
165     }
166 #elif defined(HAVE_TIME_H)
167   if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
168     internal_error (NULL, "time_t too small");
169   else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
170     {
171       GFC_UINTEGER_4 ucnt = time (NULL);
172       if (ucnt > GFC_INTEGER_4_HUGE)
173         cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
174       else
175         cnt = ucnt;
176       mx = GFC_INTEGER_4_HUGE;
177     }
178   else
179     {
180       GFC_UINTEGER_8 ucnt = time (NULL);
181       if (ucnt > GFC_INTEGER_8_HUGE)
182         cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
183       else
184         cnt = ucnt;
185       mx = GFC_INTEGER_8_HUGE;
186     }
187 #else
188   cnt = - GFC_INTEGER_8_HUGE;
189   mx = 0;
190 #endif
191   if (count != NULL)
192     *count = cnt;
193   if (count_rate != NULL)
194     *count_rate = TCK;
195   if (count_max != NULL)
196     *count_max = mx;
197 }