OSDN Git Service

2008-04-13 Thomas Koenig <tkoenig@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / cpu_time.c
1 /* Implementation of the CPU_TIME intrinsic.
2    Copyright (C) 2003, 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 #include "time_1.h"
32
33 /* The most accurate way to get the CPU time is getrusage ().
34    If we have times(), that's good enough, too.  */
35 #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
36 /* For times(), we _must_ know the number of clock ticks per second.  */
37 #  if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
38 #    ifdef HAVE_SYS_PARAM_H
39 #      include <sys/param.h>
40 #    endif
41 #    if defined (HAVE_SYS_TIMES_H)
42 #      include <sys/times.h>
43 #    endif
44 #    ifndef HZ
45 #      if defined _SC_CLK_TCK
46 #        define HZ  sysconf(_SC_CLK_TCK)
47 #      else
48 #        define HZ  CLK_TCK
49 #      endif
50 #    endif
51 #  endif  /* HAVE_TIMES etc.  */
52 #endif  /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H  */
53
54 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE;
55
56 static inline void
57 __cpu_time_1 (long *sec, long *usec)
58 {
59 #if defined(__MINGW32__) || defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
60   long user_sec, user_usec, system_sec, system_usec;
61   __time_1 (&user_sec, &user_usec, &system_sec, &system_usec);
62   *sec = user_sec + system_sec;
63   *usec = user_usec + system_usec;
64 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H  */
65 #ifdef HAVE_TIMES
66   struct tms buf;
67   times (&buf);
68   *sec = 0;
69   *usec = (buf.tms_utime + buf.tms_stime) * (1000000 / HZ);
70 #else /* ! HAVE_TIMES */
71   /* We have nothing to go on.  Return -1.  */
72   *sec = -1;
73   *usec = 0;
74 #endif  /* HAVE_TIMES */
75 #endif  /* __MINGW32__ || HAVE_GETRUSAGE */
76 }
77
78
79 extern void cpu_time_4 (GFC_REAL_4 *);
80 iexport_proto(cpu_time_4);
81
82 void cpu_time_4 (GFC_REAL_4 *time)
83 {
84   long sec, usec;
85   __cpu_time_1 (&sec, &usec);
86   *time = sec + usec * (GFC_REAL_4)1.e-6;
87 }
88 iexport(cpu_time_4);
89
90 extern void cpu_time_8 (GFC_REAL_8 *);
91 export_proto(cpu_time_8);
92
93 void cpu_time_8 (GFC_REAL_8 *time)
94 {
95   long sec, usec;
96   __cpu_time_1 (&sec, &usec);
97   *time = sec + usec * (GFC_REAL_8)1.e-6;
98 }
99
100 #ifdef HAVE_GFC_REAL_10
101 extern void cpu_time_10 (GFC_REAL_10 *);
102 export_proto(cpu_time_10);
103
104 void cpu_time_10 (GFC_REAL_10 *time)
105 {
106   long sec, usec;
107   __cpu_time_1 (&sec, &usec);
108   *time = sec + usec * (GFC_REAL_10)1.e-6;
109 }
110 #endif
111
112 #ifdef HAVE_GFC_REAL_16
113 extern void cpu_time_16 (GFC_REAL_16 *);
114 export_proto(cpu_time_16);
115
116 void cpu_time_16 (GFC_REAL_16 *time)
117 {
118   long sec, usec;
119   __cpu_time_1 (&sec, &usec);
120   *time = sec + usec * (GFC_REAL_16)1.e-6;
121 }
122 #endif
123
124 extern void second_sub (GFC_REAL_4 *);
125 export_proto(second_sub);
126
127 void
128 second_sub (GFC_REAL_4 *s)
129 {
130   cpu_time_4 (s);
131 }
132
133 extern GFC_REAL_4 second (void);
134 export_proto(second);
135
136 GFC_REAL_4
137 second (void)
138 {
139   GFC_REAL_4 s;
140   cpu_time_4 (&s);
141   return s;
142 }