source: trunk/library/timing.c @ 1107

Revision 1107, 5.4 KB checked in by paul, 7 months ago (diff)
  • Expanded clobber list on i386 RDTSC call
Line 
1/*
2 *  Portable interface to the CPU cycle counter
3 *
4 *  Copyright (C) 2006-2010, Brainspark B.V.
5 *
6 *  This file is part of PolarSSL (http://www.polarssl.org)
7 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 *  All rights reserved.
10 *
11 *  This program is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  This program is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License along
22 *  with this program; if not, write to the Free Software Foundation, Inc.,
23 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_TIMING_C)
29
30#include "polarssl/timing.h"
31
32#if defined(_WIN32)
33
34#include <windows.h>
35#include <winbase.h>
36
37struct _hr_time
38{
39    LARGE_INTEGER start;
40};
41
42#else
43
44#include <unistd.h>
45#include <sys/types.h>
46#include <sys/time.h>
47#include <signal.h>
48#include <time.h>
49
50struct _hr_time
51{
52    struct timeval start;
53};
54
55#endif
56
57#if defined(POLARSSL_HAVE_ASM) &&                                       \
58        (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
59
60unsigned long hardclock( void )
61{
62    unsigned long tsc;
63    __asm   rdtsc
64    __asm   mov  [tsc], eax
65    return( tsc );
66}
67
68#else
69#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
70
71unsigned long hardclock( void )
72{
73    unsigned long lo, hi;
74    asm( "rdtsc" : "=a" (lo), "=d" (hi) );
75    return( lo );
76}
77
78#else
79#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) &&                  \
80        (defined(__amd64__) || defined(__x86_64__))
81
82unsigned long hardclock( void )
83{
84    unsigned long lo, hi;
85    asm( "rdtsc" : "=a" (lo), "=d" (hi) ); 
86    return( lo | (hi << 32) );
87}
88
89#else
90#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) &&                  \
91        (defined(__powerpc__) || defined(__ppc__))
92
93unsigned long hardclock( void )
94{
95    unsigned long tbl, tbu0, tbu1;
96
97    do
98    {
99        asm( "mftbu %0" : "=r" (tbu0) );
100        asm( "mftb  %0" : "=r" (tbl ) );
101        asm( "mftbu %0" : "=r" (tbu1) );
102    }
103    while( tbu0 != tbu1 );
104
105    return( tbl );
106}
107
108#else
109#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__)
110
111unsigned long hardclock( void )
112{
113    unsigned long tick;
114    asm( ".byte 0x83, 0x41, 0x00, 0x00" );
115    asm( "mov   %%g1, %0" : "=r" (tick) );
116    return( tick );
117}
118
119#else
120#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__)
121
122unsigned long hardclock( void )
123{
124    unsigned long cc;
125    asm( "rpcc %0" : "=r" (cc) );
126    return( cc & 0xFFFFFFFF );
127}
128
129#else
130#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__)
131
132unsigned long hardclock( void )
133{
134    unsigned long itc;
135    asm( "mov %0 = ar.itc" : "=r" (itc) );
136    return( itc );
137}
138
139#else
140#if defined(_MSC_VER)
141
142unsigned long hardclock( void )
143{
144    LARGE_INTEGER offset;
145   
146        QueryPerformanceCounter( &offset );
147
148        return (unsigned long)( offset.QuadPart );
149}
150
151#else
152
153static int hardclock_init = 0;
154static struct timeval tv_init;
155
156unsigned long hardclock( void )
157{
158    struct timeval tv_cur;
159
160    if( hardclock_init == 0 )
161    {
162        gettimeofday( &tv_init, NULL );
163        hardclock_init = 1;
164    }
165
166    gettimeofday( &tv_cur, NULL );
167    return( ( tv_cur.tv_sec  - tv_init.tv_sec  ) * 1000000
168          + ( tv_cur.tv_usec - tv_init.tv_usec ) );
169}
170
171#endif /* generic */
172#endif /* WIN32   */
173#endif /* IA-64   */
174#endif /* Alpha   */
175#endif /* SPARC8  */
176#endif /* PowerPC */
177#endif /* AMD64   */
178#endif /* i586+   */
179
180volatile int alarmed = 0;
181
182#if defined(_WIN32)
183
184unsigned long get_timer( struct hr_time *val, int reset )
185{
186    unsigned long delta;
187    LARGE_INTEGER offset, hfreq;
188    struct _hr_time *t = (struct _hr_time *) val;
189
190    QueryPerformanceCounter(  &offset );
191    QueryPerformanceFrequency( &hfreq );
192
193    delta = (unsigned long)( ( 1000 *
194        ( offset.QuadPart - t->start.QuadPart ) ) /
195           hfreq.QuadPart );
196
197    if( reset )
198        QueryPerformanceCounter( &t->start );
199
200    return( delta );
201}
202
203DWORD WINAPI TimerProc( LPVOID uElapse )
204{   
205    Sleep( (DWORD) uElapse );
206    alarmed = 1; 
207    return( TRUE );
208}
209
210void set_alarm( int seconds )
211{   
212    DWORD ThreadId;
213
214    alarmed = 0; 
215    CloseHandle( CreateThread( NULL, 0, TimerProc,
216        (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
217}
218
219void m_sleep( int milliseconds )
220{
221    Sleep( milliseconds );
222}
223
224#else
225
226unsigned long get_timer( struct hr_time *val, int reset )
227{
228    unsigned long delta;
229    struct timeval offset;
230    struct _hr_time *t = (struct _hr_time *) val;
231
232    gettimeofday( &offset, NULL );
233
234    delta = ( offset.tv_sec  - t->start.tv_sec  ) * 1000
235          + ( offset.tv_usec - t->start.tv_usec ) / 1000;
236
237    if( reset )
238    {
239        t->start.tv_sec  = offset.tv_sec;
240        t->start.tv_usec = offset.tv_usec;
241    }
242
243    return( delta );
244}
245
246static void sighandler( int signum )
247{   
248    alarmed = 1;
249    signal( signum, sighandler );
250}
251
252void set_alarm( int seconds )
253{
254    alarmed = 0;
255    signal( SIGALRM, sighandler );
256    alarm( seconds );
257}
258
259void m_sleep( int milliseconds )
260{
261    struct timeval tv;
262
263    tv.tv_sec  = milliseconds / 1000;
264    tv.tv_usec = milliseconds * 1000;
265
266    select( 0, NULL, NULL, NULL, &tv );
267}
268
269#endif
270
271#endif
Note: See TracBrowser for help on using the repository browser.

What are you looking for?