xref: /netbsd-src/external/gpl3/gcc/dist/libgfortran/intrinsics/cpu_time.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1181254a7Smrg /* Implementation of the CPU_TIME intrinsic.
2*b1e83836Smrg    Copyright (C) 2003-2022 Free Software Foundation, Inc.
3181254a7Smrg 
4181254a7Smrg This file is part of the GNU Fortran runtime library (libgfortran).
5181254a7Smrg 
6181254a7Smrg Libgfortran is free software; you can redistribute it and/or
7181254a7Smrg modify it under the terms of the GNU General Public
8181254a7Smrg License as published by the Free Software Foundation; either
9181254a7Smrg version 3 of the License, or (at your option) any later version.
10181254a7Smrg 
11181254a7Smrg Libgfortran is distributed in the hope that it will be useful,
12181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14181254a7Smrg GNU General Public License for more details.
15181254a7Smrg 
16181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
17181254a7Smrg permissions described in the GCC Runtime Library Exception, version
18181254a7Smrg 3.1, as published by the Free Software Foundation.
19181254a7Smrg 
20181254a7Smrg You should have received a copy of the GNU General Public License and
21181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
22181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23181254a7Smrg <http://www.gnu.org/licenses/>.  */
24181254a7Smrg 
25181254a7Smrg #include "libgfortran.h"
26181254a7Smrg #include "time_1.h"
27181254a7Smrg 
28181254a7Smrg 
29181254a7Smrg static void
__cpu_time_1(long * sec,long * usec)30181254a7Smrg __cpu_time_1 (long *sec, long *usec)
31181254a7Smrg {
32181254a7Smrg   long user_sec, user_usec, system_sec, system_usec;
33181254a7Smrg   if (gf_cputime (&user_sec, &user_usec, &system_sec, &system_usec) == 0)
34181254a7Smrg     {
35181254a7Smrg       *sec = user_sec + system_sec;
36181254a7Smrg       *usec = user_usec + system_usec;
37181254a7Smrg     }
38181254a7Smrg   else
39181254a7Smrg     {
40181254a7Smrg       *sec = -1;
41181254a7Smrg       *usec = 0;
42181254a7Smrg     }
43181254a7Smrg }
44181254a7Smrg 
45181254a7Smrg 
46181254a7Smrg extern void cpu_time_4 (GFC_REAL_4 *);
47181254a7Smrg iexport_proto(cpu_time_4);
48181254a7Smrg 
cpu_time_4(GFC_REAL_4 * time)49181254a7Smrg void cpu_time_4 (GFC_REAL_4 *time)
50181254a7Smrg {
51181254a7Smrg   long sec, usec;
52181254a7Smrg   __cpu_time_1 (&sec, &usec);
53181254a7Smrg   *time = sec + usec * GFC_REAL_4_LITERAL(1.e-6);
54181254a7Smrg }
55181254a7Smrg iexport(cpu_time_4);
56181254a7Smrg 
57181254a7Smrg extern void cpu_time_8 (GFC_REAL_8 *);
58181254a7Smrg export_proto(cpu_time_8);
59181254a7Smrg 
cpu_time_8(GFC_REAL_8 * time)60181254a7Smrg void cpu_time_8 (GFC_REAL_8 *time)
61181254a7Smrg {
62181254a7Smrg   long sec, usec;
63181254a7Smrg   __cpu_time_1 (&sec, &usec);
64181254a7Smrg   *time = sec + usec * GFC_REAL_8_LITERAL(1.e-6);
65181254a7Smrg }
66181254a7Smrg 
67181254a7Smrg #ifdef HAVE_GFC_REAL_10
68181254a7Smrg extern void cpu_time_10 (GFC_REAL_10 *);
69181254a7Smrg export_proto(cpu_time_10);
70181254a7Smrg 
cpu_time_10(GFC_REAL_10 * time)71181254a7Smrg void cpu_time_10 (GFC_REAL_10 *time)
72181254a7Smrg {
73181254a7Smrg   long sec, usec;
74181254a7Smrg   __cpu_time_1 (&sec, &usec);
75181254a7Smrg   *time = sec + usec * GFC_REAL_10_LITERAL(1.e-6);
76181254a7Smrg }
77181254a7Smrg #endif
78181254a7Smrg 
79181254a7Smrg #ifdef HAVE_GFC_REAL_16
80181254a7Smrg extern void cpu_time_16 (GFC_REAL_16 *);
81181254a7Smrg export_proto(cpu_time_16);
82181254a7Smrg 
cpu_time_16(GFC_REAL_16 * time)83181254a7Smrg void cpu_time_16 (GFC_REAL_16 *time)
84181254a7Smrg {
85181254a7Smrg   long sec, usec;
86181254a7Smrg   __cpu_time_1 (&sec, &usec);
87181254a7Smrg   *time = sec + usec * GFC_REAL_16_LITERAL(1.e-6);
88181254a7Smrg }
89181254a7Smrg #endif
90181254a7Smrg 
91181254a7Smrg extern void second_sub (GFC_REAL_4 *);
92181254a7Smrg export_proto(second_sub);
93181254a7Smrg 
94181254a7Smrg void
second_sub(GFC_REAL_4 * s)95181254a7Smrg second_sub (GFC_REAL_4 *s)
96181254a7Smrg {
97181254a7Smrg   cpu_time_4 (s);
98181254a7Smrg }
99181254a7Smrg 
100181254a7Smrg extern GFC_REAL_4 second (void);
101181254a7Smrg export_proto(second);
102181254a7Smrg 
103181254a7Smrg GFC_REAL_4
second(void)104181254a7Smrg second (void)
105181254a7Smrg {
106181254a7Smrg   GFC_REAL_4 s;
107181254a7Smrg   cpu_time_4 (&s);
108181254a7Smrg   return s;
109181254a7Smrg }
110