005-04-17 Thomas Koenig <Thomas.Koenig@online.de>
[gcc.git] / libgfortran / intrinsics / etime.c
1 /* Implementation of the ETIME intrinsic.
2 Copyright (C) 2004 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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
30
31 #include "config.h"
32 #include <sys/types.h>
33 #include "libgfortran.h"
34
35 #include <stdio.h>
36
37 #if defined (HAVE_SYS_TIME_H) && defined (HAVE_SYS_RESOURCE_H)
38 #include <sys/time.h>
39 #include <sys/resource.h>
40 #endif
41
42 extern void etime_sub (gfc_array_r4 *t, GFC_REAL_4 *result);
43 iexport_proto(etime_sub);
44
45 void
46 etime_sub (gfc_array_r4 *t, GFC_REAL_4 *result)
47 {
48 GFC_REAL_4 tu, ts, tt, *tp;
49 index_type dim;
50
51 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
52 struct rusage rt;
53
54 if (getrusage(RUSAGE_SELF, &rt) == 0)
55 {
56 tu = (GFC_REAL_4)(rt.ru_utime.tv_sec + 1.e-6 * rt.ru_utime.tv_usec);
57 ts = (GFC_REAL_4)(rt.ru_stime.tv_sec + 1.e-6 * rt.ru_stime.tv_usec);
58 tt = tu + ts;
59 }
60 else
61 {
62 tu = -1.;
63 ts = -1.;
64 tt = -1.;
65 }
66 #else
67 tu = -1.;
68 ts = -1.;
69 tt = -1.;
70 #endif
71
72 if (((t->dim[0].ubound + 1 - t->dim[0].lbound)) < 2)
73 runtime_error ("Insufficient number of elements in TARRAY.");
74
75 if (t->dim[0].stride == 0)
76 t->dim[0].stride = 1;
77
78 tp = t->data;
79
80 *tp = tu;
81 tp += t->dim[0].stride;
82 *tp = ts;
83 *result = tt;
84 }
85 iexport(etime_sub);
86
87 extern GFC_REAL_4 etime (gfc_array_r4 *t);
88 export_proto(etime);
89
90 GFC_REAL_4
91 etime (gfc_array_r4 *t)
92 {
93 GFC_REAL_4 val;
94 etime_sub (t, &val);
95 return val;
96 }
97
98 /* LAPACK's test programs declares ETIME external, therefore we
99 need this. */
100
101 extern GFC_REAL_4 etime_ (GFC_REAL_4 *t);
102 export_proto_np(etime_);
103
104 GFC_REAL_4
105 etime_ (GFC_REAL_4 *t)
106 {
107 gfc_array_r4 desc;
108 GFC_REAL_4 val;
109
110 /* We only fill in the fields that are used in etime_sub. */
111 desc.dim[0].lbound = 0;
112 desc.dim[0].ubound = 1;
113 desc.dim[0].stride = 1;
114 desc.data = t;
115
116 etime_sub (&desc, &val);
117 return val;
118 }