OSDN Git Service

8929158b809d5aa27af0a621477df869d128288e
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / etime.c
1 /* Implementation of the ETIME intrinsic.
2    Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3    Contributed by Steven G. Kargl <kargls@comcast.net>.
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
30
31 #include "config.h"
32 #include "libgfortran.h"
33
34 #include <stdio.h>
35
36 #if defined (HAVE_SYS_TIME_H) && defined (HAVE_SYS_RESOURCE_H)
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #endif
40
41 extern void etime_sub (gfc_array_r4 *t, GFC_REAL_4 *result);
42 iexport_proto(etime_sub);
43
44 void
45 etime_sub (gfc_array_r4 *t, GFC_REAL_4 *result)
46 {
47   GFC_REAL_4 tu, ts, tt, *tp;
48
49 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
50   struct rusage rt;
51
52   if (getrusage(RUSAGE_SELF, &rt) == 0)
53     {
54       tu = (GFC_REAL_4)(rt.ru_utime.tv_sec + 1.e-6 * rt.ru_utime.tv_usec);
55       ts = (GFC_REAL_4)(rt.ru_stime.tv_sec + 1.e-6 * rt.ru_stime.tv_usec);
56       tt = tu + ts;
57     }
58   else
59     {
60       tu = -1.;
61       ts = -1.;
62       tt = -1.;
63     }
64 #else
65   tu = -1.;
66   ts = -1.;
67   tt = -1.;
68 #endif
69
70   if (((t->dim[0].ubound + 1 - t->dim[0].lbound)) < 2)
71     runtime_error ("Insufficient number of elements in TARRAY.");
72
73   tp = t->data;
74
75   *tp = tu;
76   tp += t->dim[0].stride;
77   *tp = ts;
78   *result = tt;
79 }
80 iexport(etime_sub);
81
82 extern GFC_REAL_4 etime (gfc_array_r4 *t);
83 export_proto(etime);
84
85 GFC_REAL_4
86 etime (gfc_array_r4 *t)
87 {
88   GFC_REAL_4 val;
89   etime_sub (t, &val);
90   return val;
91 }