OSDN Git Service

Increase clock resolution for system_clock_8.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / system_clock.c
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2    Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran 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 3 of the License, or (at your option) any later version.
10
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.
15
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.
19
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/>.  */
24
25 #include "libgfortran.h"
26
27 #include <limits.h>
28
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
37
38
39 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
40 export_proto(system_clock_4);
41
42 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
43 export_proto(system_clock_8);
44
45
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. */
50
51 void
52 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
53                GFC_INTEGER_4 *count_max)
54 {
55   GFC_INTEGER_4 cnt;
56   GFC_INTEGER_4 mx;
57
58 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
59 #define TCK 1000
60   struct timeval tp1;
61
62   if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
63     internal_error (NULL, "tv_sec too small");
64
65   if (gettimeofday(&tp1, NULL) == 0)
66     {
67       GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
68       ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
69       if (ucnt > GFC_INTEGER_4_HUGE)
70         cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
71       else
72         cnt = ucnt;
73       mx = GFC_INTEGER_4_HUGE;
74     }
75   else
76     {
77       if (count != NULL)
78         *count = - GFC_INTEGER_4_HUGE;
79       if (count_rate != NULL)
80         *count_rate = 0;
81       if (count_max != NULL)
82         *count_max = 0;
83       return;
84     }
85 #elif defined(HAVE_TIME_H)
86   GFC_UINTEGER_4 ucnt;
87
88   if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
89     internal_error (NULL, "time_t too small");
90
91   ucnt = time (NULL);
92   if (ucnt > GFC_INTEGER_4_HUGE)
93     cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
94   else
95     cnt = ucnt;
96   mx = GFC_INTEGER_4_HUGE;
97 #else
98   cnt = - GFC_INTEGER_4_HUGE;
99   mx = 0;
100 #endif
101   if (count != NULL)
102     *count = cnt;
103   if (count_rate != NULL)
104     *count_rate = TCK;
105   if (count_max != NULL)
106     *count_max = mx;
107 }
108
109
110 /* INTEGER(8) version of the above routine.  */
111
112 void
113 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
114                 GFC_INTEGER_8 *count_max)
115 {
116   GFC_INTEGER_8 cnt;
117   GFC_INTEGER_8 mx;
118
119 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
120 #define TCK 1000000
121   struct timeval tp1;
122
123   if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
124     internal_error (NULL, "tv_sec too small");
125
126   if (gettimeofday(&tp1, NULL) == 0)
127     {
128       if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
129         {
130           GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
131           ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
132           if (ucnt > GFC_INTEGER_4_HUGE)
133             cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
134           else
135             cnt = ucnt;
136           mx = GFC_INTEGER_4_HUGE;
137         }
138       else
139         {
140           GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
141           ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
142           if (ucnt > GFC_INTEGER_8_HUGE)
143             cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
144           else
145             cnt = ucnt;
146           mx = GFC_INTEGER_8_HUGE;
147         }
148     }
149   else
150     {
151       if (count != NULL)
152         *count = - GFC_INTEGER_8_HUGE;
153       if (count_rate != NULL)
154         *count_rate = 0;
155       if (count_max != NULL)
156         *count_max = 0;
157
158       return;
159     }
160 #elif defined(HAVE_TIME_H)
161   if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
162     internal_error (NULL, "time_t too small");
163   else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
164     {
165       GFC_UINTEGER_4 ucnt = time (NULL);
166       if (ucnt > GFC_INTEGER_4_HUGE)
167         cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
168       else
169         cnt = ucnt;
170       mx = GFC_INTEGER_4_HUGE;
171     }
172   else
173     {
174       GFC_UINTEGER_8 ucnt = time (NULL);
175       if (ucnt > GFC_INTEGER_8_HUGE)
176         cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
177       else
178         cnt = ucnt;
179       mx = GFC_INTEGER_8_HUGE;
180     }
181 #else
182   cnt = - GFC_INTEGER_8_HUGE;
183   mx = 0;
184 #endif
185   if (count != NULL)
186     *count = cnt;
187   if (count_rate != NULL)
188     *count_rate = TCK;
189   if (count_max != NULL)
190     *count_max = mx;
191 }