re PR fortran/17283 (UNPACK issues)
[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 Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include <sys/types.h>
24 #include "libgfortran.h"
25
26 #include <stdio.h>
27
28 #if defined (HAVE_SYS_TIME_H) && defined (HAVE_SYS_RESOURCE_H)
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #endif
32
33 void
34 prefix(etime_sub) (gfc_array_r4 *t, GFC_REAL_4 *result)
35 {
36 GFC_REAL_4 tu, ts, tt, *tp;
37 index_type dim;
38
39 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
40 struct rusage rt;
41
42 if (getrusage(RUSAGE_SELF, &rt) == 0)
43 {
44 tu = (GFC_REAL_4)(rt.ru_utime.tv_sec + 1.e-6 * rt.ru_utime.tv_usec);
45 ts = (GFC_REAL_4)(rt.ru_stime.tv_sec + 1.e-6 * rt.ru_stime.tv_usec);
46 tt = tu + ts;
47 }
48 else
49 {
50 tu = -1.;
51 ts = -1.;
52 tt = -1.;
53 }
54 #else
55 tu = -1.;
56 ts = -1.;
57 tt = -1.;
58 #endif
59
60 if (((t->dim[0].ubound + 1 - t->dim[0].lbound)) < 2)
61 runtime_error ("Insufficient number of elements in TARRAY.");
62
63 if (t->dim[0].stride == 0)
64 t->dim[0].stride = 1;
65
66 tp = t->data;
67
68 *tp = tu;
69 tp += t->dim[0].stride;
70 *tp = ts;
71 *result = tt;
72 }
73
74 GFC_REAL_4
75 prefix(etime) (gfc_array_r4 *t)
76 {
77 GFC_REAL_4 val;
78 prefix(etime_sub) (t, &val);
79 return val;
80 }
81
82 /* LAPACK's test programs declares ETIME external, therefore we
83 need this. */
84
85 GFC_REAL_4
86 etime_ (GFC_REAL_4 *t)
87 {
88 gfc_array_r4 desc;
89 GFC_REAL_4 val;
90
91 /* We only fill in the fields that are used in etime_sub. */
92 desc.dim[0].lbound = 0;
93 desc.dim[0].ubound = 1;
94 desc.dim[0].stride = 1;
95 desc.data = t;
96
97 prefix(etime_sub) (&desc, &val);
98 return val;
99 }