]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/system_clock.c
1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
29 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
30 # include <sys/time.h>
32 #elif defined(HAVE_TIME_H)
40 extern void system_clock_4 (GFC_INTEGER_4
*, GFC_INTEGER_4
*, GFC_INTEGER_4
*);
41 export_proto(system_clock_4
);
43 extern void system_clock_8 (GFC_INTEGER_8
*, GFC_INTEGER_8
*, GFC_INTEGER_8
*);
44 export_proto(system_clock_8
);
47 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
48 intrinsic subroutine. It returns the number of clock ticks for the current
49 system time, the number of ticks per second, and the maximum possible value
50 for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
53 system_clock_4(GFC_INTEGER_4
*count
, GFC_INTEGER_4
*count_rate
,
54 GFC_INTEGER_4
*count_max
)
59 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
63 if (sizeof (tp1
.tv_sec
) < sizeof (GFC_INTEGER_4
))
64 internal_error (NULL
, "tv_sec too small");
66 if (gettimeofday(&tp1
, &tzp
) == 0)
68 GFC_UINTEGER_4 ucnt
= (GFC_UINTEGER_4
) tp1
.tv_sec
* TCK
;
69 ucnt
+= (tp1
.tv_usec
+ 500000 / TCK
) / (1000000 / TCK
);
70 if (ucnt
> GFC_INTEGER_4_HUGE
)
71 cnt
= ucnt
- GFC_INTEGER_4_HUGE
- 1;
74 mx
= GFC_INTEGER_4_HUGE
;
79 *count
= - GFC_INTEGER_4_HUGE
;
80 if (count_rate
!= NULL
)
82 if (count_max
!= NULL
)
86 #elif defined(HAVE_TIME_H)
89 if (sizeof (time_t) < sizeof (GFC_INTEGER_4
))
90 internal_error (NULL
, "time_t too small");
93 if (ucnt
> GFC_INTEGER_4_HUGE
)
94 cnt
= ucnt
- GFC_INTEGER_4_HUGE
- 1;
97 mx
= GFC_INTEGER_4_HUGE
;
99 cnt
= - GFC_INTEGER_4_HUGE
;
104 if (count_rate
!= NULL
)
106 if (count_max
!= NULL
)
111 /* INTEGER(8) version of the above routine. */
114 system_clock_8 (GFC_INTEGER_8
*count
, GFC_INTEGER_8
*count_rate
,
115 GFC_INTEGER_8
*count_max
)
120 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
124 if (sizeof (tp1
.tv_sec
) < sizeof (GFC_INTEGER_4
))
125 internal_error (NULL
, "tv_sec too small");
127 if (gettimeofday(&tp1
, &tzp
) == 0)
129 if (sizeof (tp1
.tv_sec
) < sizeof (GFC_INTEGER_8
))
131 GFC_UINTEGER_4 ucnt
= (GFC_UINTEGER_4
) tp1
.tv_sec
* TCK
;
132 ucnt
+= (tp1
.tv_usec
+ 500000 / TCK
) / (1000000 / TCK
);
133 if (ucnt
> GFC_INTEGER_4_HUGE
)
134 cnt
= ucnt
- GFC_INTEGER_4_HUGE
- 1;
137 mx
= GFC_INTEGER_4_HUGE
;
141 GFC_UINTEGER_8 ucnt
= (GFC_UINTEGER_8
) tp1
.tv_sec
* TCK
;
142 ucnt
+= (tp1
.tv_usec
+ 500000 / TCK
) / (1000000 / TCK
);
143 if (ucnt
> GFC_INTEGER_8_HUGE
)
144 cnt
= ucnt
- GFC_INTEGER_8_HUGE
- 1;
147 mx
= GFC_INTEGER_8_HUGE
;
153 *count
= - GFC_INTEGER_8_HUGE
;
154 if (count_rate
!= NULL
)
156 if (count_max
!= NULL
)
161 #elif defined(HAVE_TIME_H)
162 if (sizeof (time_t) < sizeof (GFC_INTEGER_4
))
163 internal_error (NULL
, "time_t too small");
164 else if (sizeof (time_t) == sizeof (GFC_INTEGER_4
))
166 GFC_UINTEGER_4 ucnt
= time (NULL
);
167 if (ucnt
> GFC_INTEGER_4_HUGE
)
168 cnt
= ucnt
- GFC_INTEGER_4_HUGE
- 1;
171 mx
= GFC_INTEGER_4_HUGE
;
175 GFC_UINTEGER_8 ucnt
= time (NULL
);
176 if (ucnt
> GFC_INTEGER_8_HUGE
)
177 cnt
= ucnt
- GFC_INTEGER_8_HUGE
- 1;
180 mx
= GFC_INTEGER_8_HUGE
;
183 cnt
= - GFC_INTEGER_8_HUGE
;
188 if (count_rate
!= NULL
)
190 if (count_max
!= NULL
)