PR libfortran/18966
[official-gcc.git] / libgfortran / intrinsics / etime.c
blob9a8f976a55d8ada6e4e33d2c611fc06267acdaa1
1 /* Implementation of the ETIME intrinsic.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Contributed by Steven G. Kargl <kargls@comcast.net>.
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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.
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. */
22 #include "config.h"
23 #include <sys/types.h>
24 #include "libgfortran.h"
26 #include <stdio.h>
28 #if defined (HAVE_SYS_TIME_H) && defined (HAVE_SYS_RESOURCE_H)
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #endif
33 extern void etime_sub (gfc_array_r4 *t, GFC_REAL_4 *result);
34 iexport_proto(etime_sub);
36 void
37 etime_sub (gfc_array_r4 *t, GFC_REAL_4 *result)
39 GFC_REAL_4 tu, ts, tt, *tp;
40 index_type dim;
42 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
43 struct rusage rt;
45 if (getrusage(RUSAGE_SELF, &rt) == 0)
47 tu = (GFC_REAL_4)(rt.ru_utime.tv_sec + 1.e-6 * rt.ru_utime.tv_usec);
48 ts = (GFC_REAL_4)(rt.ru_stime.tv_sec + 1.e-6 * rt.ru_stime.tv_usec);
49 tt = tu + ts;
51 else
53 tu = -1.;
54 ts = -1.;
55 tt = -1.;
57 #else
58 tu = -1.;
59 ts = -1.;
60 tt = -1.;
61 #endif
63 if (((t->dim[0].ubound + 1 - t->dim[0].lbound)) < 2)
64 runtime_error ("Insufficient number of elements in TARRAY.");
66 if (t->dim[0].stride == 0)
67 t->dim[0].stride = 1;
69 tp = t->data;
71 *tp = tu;
72 tp += t->dim[0].stride;
73 *tp = ts;
74 *result = tt;
76 iexport(etime_sub);
78 extern GFC_REAL_4 etime (gfc_array_r4 *t);
79 export_proto(etime);
81 GFC_REAL_4
82 etime (gfc_array_r4 *t)
84 GFC_REAL_4 val;
85 etime_sub (t, &val);
86 return val;
89 /* LAPACK's test programs declares ETIME external, therefore we
90 need this. */
92 extern GFC_REAL_4 etime_ (GFC_REAL_4 *t);
93 export_proto_np(etime_);
95 GFC_REAL_4
96 etime_ (GFC_REAL_4 *t)
98 gfc_array_r4 desc;
99 GFC_REAL_4 val;
101 /* We only fill in the fields that are used in etime_sub. */
102 desc.dim[0].lbound = 0;
103 desc.dim[0].ubound = 1;
104 desc.dim[0].stride = 1;
105 desc.data = t;
107 etime_sub (&desc, &val);
108 return val;