OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgfortran/] [intrinsics/] [system_clock.c] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 733 jeremybenn
/* Implementation of the SYSTEM_CLOCK intrinsic.
2
   Copyright (C) 2004, 2005, 2007, 2009, 2010, 2011 Free Software
3
   Foundation, Inc.
4
 
5
This file is part of the GNU Fortran runtime library (libgfortran).
6
 
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.
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 General Public License for more details.
16
 
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.
20
 
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/>.  */
25
 
26
#include "libgfortran.h"
27
 
28
#include <limits.h>
29
 
30
#include "time_1.h"
31
 
32
 
33
/* POSIX states that CLOCK_REALTIME must be present if clock_gettime
34
   is available, others are optional.  */
35
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_LIBRT)
36
#ifdef CLOCK_MONOTONIC
37
#define GF_CLOCK_MONOTONIC CLOCK_MONOTONIC
38
#else
39
#define GF_CLOCK_MONOTONIC CLOCK_REALTIME
40
#endif
41
#endif
42
 
43
/* Weakref trickery for clock_gettime().  On Glibc, clock_gettime()
44
   requires us to link in librt, which also pulls in libpthread.  In
45
   order to avoid this by default, only call clock_gettime() through a
46
   weak reference.
47
 
48
   Some targets don't support weak undefined references; on these
49
   GTHREAD_USE_WEAK is 0. So we need to define it to 1 on other
50
   targets.  */
51
#ifndef GTHREAD_USE_WEAK
52
#define GTHREAD_USE_WEAK 1
53
#endif
54
 
55
#if SUPPORTS_WEAK && GTHREAD_USE_WEAK && defined(HAVE_CLOCK_GETTIME_LIBRT)
56
static int weak_gettime (clockid_t, struct timespec *)
57
  __attribute__((__weakref__("clock_gettime")));
58
#endif
59
 
60
 
61
/* High resolution monotonic clock, falling back to the realtime clock
62
   if the target does not support such a clock.
63
 
64
   Arguments:
65
   secs     - OUTPUT, seconds
66
   nanosecs - OUTPUT, nanoseconds
67
 
68
   If the target supports a monotonic clock, the OUTPUT arguments
69
   represent a monotonically incrementing clock starting from some
70
   unspecified time in the past.
71
 
72
   If a monotonic clock is not available, falls back to the realtime
73
   clock which is not monotonic.
74
 
75
   Return value: 0 for success, -1 for error. In case of error, errno
76
   is set.
77
*/
78
static int
79
gf_gettime_mono (time_t * secs, long * nanosecs)
80
{
81
  int err;
82
#ifdef HAVE_CLOCK_GETTIME
83
  struct timespec ts;
84
  err = clock_gettime (GF_CLOCK_MONOTONIC, &ts);
85
  *secs = ts.tv_sec;
86
  *nanosecs = ts.tv_nsec;
87
  return err;
88
#else
89
#if defined(HAVE_CLOCK_GETTIME_LIBRT) && SUPPORTS_WEAK && GTHREAD_USE_WEAK
90
  if (weak_gettime)
91
    {
92
      struct timespec ts;
93
      err = weak_gettime (GF_CLOCK_MONOTONIC, &ts);
94
      *secs = ts.tv_sec;
95
      *nanosecs = ts.tv_nsec;
96
      return err;
97
    }
98
#endif
99
  err = gf_gettime (secs, nanosecs);
100
  *nanosecs *= 1000;
101
  return err;
102
#endif
103
}
104
 
105
extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
106
export_proto(system_clock_4);
107
 
108
extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
109
export_proto(system_clock_8);
110
 
111
 
112
/* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
113
   intrinsic subroutine.  It returns the number of clock ticks for the current
114
   system time, the number of ticks per second, and the maximum possible value
115
   for COUNT.  On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
116
 
117
void
118
system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
119
               GFC_INTEGER_4 *count_max)
120
{
121
#undef TCK
122
#define TCK 1000
123
  GFC_INTEGER_4 cnt;
124
  GFC_INTEGER_4 mx;
125
 
126
  time_t secs;
127
  long nanosecs;
128
 
129
  if (sizeof (secs) < sizeof (GFC_INTEGER_4))
130
    internal_error (NULL, "secs too small");
131
 
132
  if (gf_gettime_mono (&secs, &nanosecs) == 0)
133
    {
134
      GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) secs * TCK;
135
      ucnt += (nanosecs + 500000000 / TCK) / (1000000000 / TCK);
136
      if (ucnt > GFC_INTEGER_4_HUGE)
137
        cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
138
      else
139
        cnt = ucnt;
140
      mx = GFC_INTEGER_4_HUGE;
141
    }
142
  else
143
    {
144
      if (count != NULL)
145
        *count = - GFC_INTEGER_4_HUGE;
146
      if (count_rate != NULL)
147
        *count_rate = 0;
148
      if (count_max != NULL)
149
        *count_max = 0;
150
      return;
151
    }
152
 
153
  if (count != NULL)
154
    *count = cnt;
155
  if (count_rate != NULL)
156
    *count_rate = TCK;
157
  if (count_max != NULL)
158
    *count_max = mx;
159
}
160
 
161
 
162
/* INTEGER(8) version of the above routine.  */
163
 
164
void
165
system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
166
                GFC_INTEGER_8 *count_max)
167
{
168
#undef TCK
169
#define TCK 1000000000
170
  GFC_INTEGER_8 cnt;
171
  GFC_INTEGER_8 mx;
172
 
173
  time_t secs;
174
  long nanosecs;
175
 
176
  if (sizeof (secs) < sizeof (GFC_INTEGER_4))
177
    internal_error (NULL, "secs too small");
178
 
179
  if (gf_gettime_mono (&secs, &nanosecs) == 0)
180
    {
181
      GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) secs * TCK;
182
      ucnt += (nanosecs + 500000000 / TCK) / (1000000000 / TCK);
183
      if (ucnt > GFC_INTEGER_8_HUGE)
184
        cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
185
      else
186
        cnt = ucnt;
187
      mx = GFC_INTEGER_8_HUGE;
188
    }
189
  else
190
    {
191
      if (count != NULL)
192
        *count = - GFC_INTEGER_8_HUGE;
193
      if (count_rate != NULL)
194
        *count_rate = 0;
195
      if (count_max != NULL)
196
        *count_max = 0;
197
 
198
      return;
199
    }
200
 
201
  if (count != NULL)
202
    *count = cnt;
203
  if (count_rate != NULL)
204
    *count_rate = TCK;
205
  if (count_max != NULL)
206
    *count_max = mx;
207
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.