OSDN Git Service

* intrinsics/cpu_time.c: Don't include headers already included
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / system_clock.c
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2    Copyright (C) 2004, 2005 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 "config.h"
31 #include "libgfortran.h"
32
33 #include <limits.h>
34
35 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
36 #  include <sys/time.h>
37 #  define TCK 1000
38 #elif defined(HAVE_TIME_H)
39 #  include <time.h>
40 #  define TCK 1
41 #else
42 #define TCK 0
43 #endif
44
45
46 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
47 export_proto(system_clock_4);
48
49 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
50 export_proto(system_clock_8);
51
52
53 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
54    intrinsic subroutine.  It returns the number of clock ticks for the current
55    system time, the number of ticks per second, and the maximum possible value
56    for COUNT.  On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
57
58 void
59 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
60                GFC_INTEGER_4 *count_max)
61 {
62   GFC_INTEGER_4 cnt;
63   GFC_INTEGER_4 rate;
64   GFC_INTEGER_4 mx;
65
66 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
67   struct timeval tp1;
68   struct timezone tzp;
69
70   if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
71     internal_error (NULL, "tv_sec too small");
72
73   if (gettimeofday(&tp1, &tzp) == 0)
74     {
75       GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
76       ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
77       if (ucnt > GFC_INTEGER_4_HUGE)
78         cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
79       else
80         cnt = ucnt;
81       rate = TCK;
82       mx = GFC_INTEGER_4_HUGE;
83     }
84   else
85     {
86       if (count != NULL)
87         *count = - GFC_INTEGER_4_HUGE;
88       if (count_rate != NULL)
89         *count_rate = 0;
90       if (count_max != NULL)
91         *count_max = 0;
92       return;
93     }
94 #elif defined(HAVE_TIME_H)
95   GFC_UINTEGER_4 ucnt;
96
97   if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
98     internal_error (NULL, "time_t too small");
99
100   ucnt = time (NULL);
101   if (ucnt > GFC_INTEGER_4_HUGE)
102     cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
103   else
104     cnt = ucnt;
105   mx = GFC_INTEGER_4_HUGE;
106 #else
107   cnt = - GFC_INTEGER_4_HUGE;
108   mx = 0;
109 #endif
110   if (count != NULL)
111     *count = cnt;
112   if (count_rate != NULL)
113     *count_rate = TCK;
114   if (count_max != NULL)
115     *count_max = mx;
116 }
117
118
119 /* INTEGER(8) version of the above routine.  */
120
121 void
122 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
123                 GFC_INTEGER_8 *count_max)
124 {
125   GFC_INTEGER_8 cnt;
126   GFC_INTEGER_8 rate;
127   GFC_INTEGER_8 mx;
128
129 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
130   struct timeval tp1;
131   struct timezone tzp;
132
133   if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
134     internal_error (NULL, "tv_sec too small");
135
136   if (gettimeofday(&tp1, &tzp) == 0)
137     {
138       if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
139         {
140           GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
141           ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
142           if (ucnt > GFC_INTEGER_4_HUGE)
143             cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
144           else
145             cnt = ucnt;
146           mx = GFC_INTEGER_4_HUGE;
147         }
148       else
149         {
150           GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
151           ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
152           if (ucnt > GFC_INTEGER_8_HUGE)
153             cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
154           else
155             cnt = ucnt;
156           mx = GFC_INTEGER_8_HUGE;
157         }
158       rate = TCK;
159     }
160   else
161     {
162       if (count != NULL)
163         *count = - GFC_INTEGER_8_HUGE;
164       if (count_rate != NULL)
165         *count_rate = 0;
166       if (count_max != NULL)
167         *count_max = 0;
168
169       return;
170     }
171 #elif defined(HAVE_TIME_H)
172   if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
173     internal_error (NULL, "time_t too small");
174   else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
175     {
176       GFC_UINTEGER_4 ucnt = time (NULL);
177       if (ucnt > GFC_INTEGER_4_HUGE)
178         cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
179       else
180         cnt = ucnt;
181       mx = GFC_INTEGER_4_HUGE;
182     }
183   else
184     {
185       GFC_UINTEGER_8 ucnt = time (NULL);
186       if (ucnt > GFC_INTEGER_8_HUGE)
187         cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
188       else
189         cnt = ucnt;
190       mx = GFC_INTEGER_8_HUGE;
191     }
192 #else
193   cnt = - GFC_INTEGER_8_HUGE;
194   mx = 0;
195 #endif
196   if (count != NULL)
197     *count = cnt;
198   if (count_rate != NULL)
199     *count_rate = TCK;
200   if (count_max != NULL)
201     *count_max = mx;
202 }