IVOPT performance tuning patch. The main problem is a variant of maximal weight
[official-gcc.git] / libgfortran / intrinsics / cpu_time.c
blob3580bba0928160495167d58d6fcfc23c61e6228b
1 /* Implementation of the CPU_TIME intrinsic.
2 Copyright (C) 2003, 2007, 2009 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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.
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.
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/>. */
25 #include "libgfortran.h"
26 #include "time_1.h"
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 */
49 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE;
51 static inline void
52 __cpu_time_1 (long *sec, long *usec)
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 */
74 extern void cpu_time_4 (GFC_REAL_4 *);
75 iexport_proto(cpu_time_4);
77 void cpu_time_4 (GFC_REAL_4 *time)
79 long sec, usec;
80 __cpu_time_1 (&sec, &usec);
81 *time = sec + usec * GFC_REAL_4_LITERAL(1.e-6);
83 iexport(cpu_time_4);
85 extern void cpu_time_8 (GFC_REAL_8 *);
86 export_proto(cpu_time_8);
88 void cpu_time_8 (GFC_REAL_8 *time)
90 long sec, usec;
91 __cpu_time_1 (&sec, &usec);
92 *time = sec + usec * GFC_REAL_8_LITERAL(1.e-6);
95 #ifdef HAVE_GFC_REAL_10
96 extern void cpu_time_10 (GFC_REAL_10 *);
97 export_proto(cpu_time_10);
99 void cpu_time_10 (GFC_REAL_10 *time)
101 long sec, usec;
102 __cpu_time_1 (&sec, &usec);
103 *time = sec + usec * GFC_REAL_10_LITERAL(1.e-6);
105 #endif
107 #ifdef HAVE_GFC_REAL_16
108 extern void cpu_time_16 (GFC_REAL_16 *);
109 export_proto(cpu_time_16);
111 void cpu_time_16 (GFC_REAL_16 *time)
113 long sec, usec;
114 __cpu_time_1 (&sec, &usec);
115 *time = sec + usec * GFC_REAL_16_LITERAL(1.e-6);
117 #endif
119 extern void second_sub (GFC_REAL_4 *);
120 export_proto(second_sub);
122 void
123 second_sub (GFC_REAL_4 *s)
125 cpu_time_4 (s);
128 extern GFC_REAL_4 second (void);
129 export_proto(second);
131 GFC_REAL_4
132 second (void)
134 GFC_REAL_4 s;
135 cpu_time_4 (&s);
136 return s;