Use clock_gettime in libgfortran timing intrinsics, cleanup
[official-gcc.git] / libgfortran / intrinsics / system_clock.c
blob37155628d53076974060cb744e62962d1edb9951
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004, 2005, 2007, 2009, 2010, 2011 Free Software
3 Foundation, Inc.
5 This file is part of the GNU Fortran runtime library (libgfortran).
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 3 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 General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
28 #include <limits.h>
30 #include "time_1.h"
32 extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
33 export_proto(system_clock_4);
35 extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
36 export_proto(system_clock_8);
39 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
40 intrinsic subroutine. It returns the number of clock ticks for the current
41 system time, the number of ticks per second, and the maximum possible value
42 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
44 void
45 system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
46 GFC_INTEGER_4 *count_max)
48 #undef TCK
49 #define TCK 1000
50 GFC_INTEGER_4 cnt;
51 GFC_INTEGER_4 mx;
53 time_t secs;
54 long nanosecs;
56 if (sizeof (secs) < sizeof (GFC_INTEGER_4))
57 internal_error (NULL, "secs too small");
59 if (gf_gettime (GF_CLOCK_MONOTONIC, &secs, &nanosecs) == 0)
61 GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) secs * TCK;
62 ucnt += (nanosecs + 500000000 / TCK) / (1000000000 / TCK);
63 if (ucnt > GFC_INTEGER_4_HUGE)
64 cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
65 else
66 cnt = ucnt;
67 mx = GFC_INTEGER_4_HUGE;
69 else
71 if (count != NULL)
72 *count = - GFC_INTEGER_4_HUGE;
73 if (count_rate != NULL)
74 *count_rate = 0;
75 if (count_max != NULL)
76 *count_max = 0;
77 return;
80 if (count != NULL)
81 *count = cnt;
82 if (count_rate != NULL)
83 *count_rate = TCK;
84 if (count_max != NULL)
85 *count_max = mx;
89 /* INTEGER(8) version of the above routine. */
91 void
92 system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
93 GFC_INTEGER_8 *count_max)
95 #undef TCK
96 #define TCK 1000000000
97 GFC_INTEGER_8 cnt;
98 GFC_INTEGER_8 mx;
100 time_t secs;
101 long nanosecs;
103 if (sizeof (secs) < sizeof (GFC_INTEGER_4))
104 internal_error (NULL, "secs too small");
106 if (gf_gettime (GF_CLOCK_MONOTONIC, &secs, &nanosecs) == 0)
108 GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) secs * TCK;
109 ucnt += (nanosecs + 500000000 / TCK) / (1000000000 / TCK);
110 if (ucnt > GFC_INTEGER_8_HUGE)
111 cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
112 else
113 cnt = ucnt;
114 mx = GFC_INTEGER_8_HUGE;
116 else
118 if (count != NULL)
119 *count = - GFC_INTEGER_8_HUGE;
120 if (count_rate != NULL)
121 *count_rate = 0;
122 if (count_max != NULL)
123 *count_max = 0;
125 return;
128 if (count != NULL)
129 *count = cnt;
130 if (count_rate != NULL)
131 *count_rate = TCK;
132 if (count_max != NULL)
133 *count_max = mx;