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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libgfortran/] [intrinsics/] [system_clock.c] - Blame information for rev 20

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

Line No. Rev Author Line
1 14 jlechner
/* Implementation of the SYSTEM_CLOCK intrinsic.
2
   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
 
4
This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
 
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 2 of the License, or (at your option) any later version.
10
 
11
In addition to the permissions in the GNU General Public License, the
12
Free Software Foundation gives you unlimited permission to link the
13
compiled version of this file into combinations with other programs,
14
and to distribute those combinations without any restriction coming
15
from the use of this file.  (The General Public License restrictions
16
do apply in other respects; for example, they cover modification of
17
the file, and distribution when not linked into a combine
18
executable.)
19
 
20
Libgfortran is distributed in the hope that it will be useful,
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
GNU General Public License for more details.
24
 
25
You should have received a copy of the GNU General Public
26
License along with libgfortran; see the file COPYING.  If not,
27
write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28
Boston, MA 02110-1301, USA.  */
29
 
30
#include "config.h"
31
#include <sys/types.h>
32
#include "libgfortran.h"
33
 
34
#include <limits.h>
35
 
36
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
37
#  include <sys/time.h>
38
#  define TCK 1000
39
#elif defined(HAVE_TIME_H)
40
#  include <time.h>
41
#  define TCK 1
42
#else
43
#define TCK 0
44
#endif
45
 
46
 
47
extern void system_clock_4 (GFC_INTEGER_4 *, GFC_INTEGER_4 *, GFC_INTEGER_4 *);
48
export_proto(system_clock_4);
49
 
50
extern void system_clock_8 (GFC_INTEGER_8 *, GFC_INTEGER_8 *, GFC_INTEGER_8 *);
51
export_proto(system_clock_8);
52
 
53
 
54
/* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
55
   intrinsic subroutine.  It returns the number of clock ticks for the current
56
   system time, the number of ticks per second, and the maximum possible value
57
   for COUNT.  On the first call to SYSTEM_CLOCK, COUNT is set to zero. */
58
 
59
void
60
system_clock_4(GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
61
               GFC_INTEGER_4 *count_max)
62
{
63
  GFC_INTEGER_4 cnt;
64
  GFC_INTEGER_4 rate;
65
  GFC_INTEGER_4 mx;
66
 
67
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
68
  struct timeval tp1;
69
  struct timezone tzp;
70
 
71
  if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
72
    internal_error (NULL, "tv_sec too small");
73
 
74
  if (gettimeofday(&tp1, &tzp) == 0)
75
    {
76
      GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
77
      ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
78
      if (ucnt > GFC_INTEGER_4_HUGE)
79
        cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
80
      else
81
        cnt = ucnt;
82
      rate = TCK;
83
      mx = GFC_INTEGER_4_HUGE;
84
    }
85
  else
86
    {
87
      if (count != NULL)
88
        *count = - GFC_INTEGER_4_HUGE;
89
      if (count_rate != NULL)
90
        *count_rate = 0;
91
      if (count_max != NULL)
92
        *count_max = 0;
93
      return;
94
    }
95
#elif defined(HAVE_TIME_H)
96
  GFC_UINTEGER_4 ucnt;
97
 
98
  if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
99
    internal_error (NULL, "time_t too small");
100
 
101
  ucnt = time (NULL);
102
  if (ucnt > GFC_INTEGER_4_HUGE)
103
    cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
104
  else
105
    cnt = ucnt;
106
  mx = GFC_INTEGER_4_HUGE;
107
#else
108
  cnt = - GFC_INTEGER_4_HUGE;
109
  mx = 0;
110
#endif
111
  if (count != NULL)
112
    *count = cnt;
113
  if (count_rate != NULL)
114
    *count_rate = TCK;
115
  if (count_max != NULL)
116
    *count_max = mx;
117
}
118
 
119
 
120
/* INTEGER(8) version of the above routine.  */
121
 
122
void
123
system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
124
                GFC_INTEGER_8 *count_max)
125
{
126
  GFC_INTEGER_8 cnt;
127
  GFC_INTEGER_8 rate;
128
  GFC_INTEGER_8 mx;
129
 
130
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
131
  struct timeval tp1;
132
  struct timezone tzp;
133
 
134
  if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_4))
135
    internal_error (NULL, "tv_sec too small");
136
 
137
  if (gettimeofday(&tp1, &tzp) == 0)
138
    {
139
      if (sizeof (tp1.tv_sec) < sizeof (GFC_INTEGER_8))
140
        {
141
          GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) tp1.tv_sec * TCK;
142
          ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
143
          if (ucnt > GFC_INTEGER_4_HUGE)
144
            cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
145
          else
146
            cnt = ucnt;
147
          mx = GFC_INTEGER_4_HUGE;
148
        }
149
      else
150
        {
151
          GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) tp1.tv_sec * TCK;
152
          ucnt += (tp1.tv_usec + 500000 / TCK) / (1000000 / TCK);
153
          if (ucnt > GFC_INTEGER_8_HUGE)
154
            cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
155
          else
156
            cnt = ucnt;
157
          mx = GFC_INTEGER_8_HUGE;
158
        }
159
      rate = TCK;
160
    }
161
  else
162
    {
163
      if (count != NULL)
164
        *count = - GFC_INTEGER_8_HUGE;
165
      if (count_rate != NULL)
166
        *count_rate = 0;
167
      if (count_max != NULL)
168
        *count_max = 0;
169
 
170
      return;
171
    }
172
#elif defined(HAVE_TIME_H)
173
  if (sizeof (time_t) < sizeof (GFC_INTEGER_4))
174
    internal_error (NULL, "time_t too small");
175
  else if (sizeof (time_t) == sizeof (GFC_INTEGER_4))
176
    {
177
      GFC_UINTEGER_4 ucnt = time (NULL);
178
      if (ucnt > GFC_INTEGER_4_HUGE)
179
        cnt = ucnt - GFC_INTEGER_4_HUGE - 1;
180
      else
181
        cnt = ucnt;
182
      mx = GFC_INTEGER_4_HUGE;
183
    }
184
  else
185
    {
186
      GFC_UINTEGER_8 ucnt = time (NULL);
187
      if (ucnt > GFC_INTEGER_8_HUGE)
188
        cnt = ucnt - GFC_INTEGER_8_HUGE - 1;
189
      else
190
        cnt = ucnt;
191
      mx = GFC_INTEGER_8_HUGE;
192
    }
193
#else
194
  cnt = - GFC_INTEGER_8_HUGE;
195
  mx = 0;
196
#endif
197
  if (count != NULL)
198
    *count = cnt;
199
  if (count_rate != NULL)
200
    *count_rate = TCK;
201
  if (count_max != NULL)
202
    *count_max = mx;
203
}

powered by: WebSVN 2.1.0

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