OSDN Git Service

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