2 * Portable interface to the CPU cycle counter
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "polarssl/config.h"
38 #if defined(POLARSSL_TIMING_C)
40 #include "polarssl/timing.h"
55 #include <sys/types.h>
67 #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
69 unsigned long hardclock( void )
78 #if defined(__GNUC__) && defined(__i386__)
80 unsigned long hardclock( void )
83 asm( "rdtsc" : "=a" (tsc
) );
88 #if defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
90 unsigned long hardclock( void )
93 asm( "rdtsc" : "=a" (lo
), "=d" (hi
) );
94 return( lo
| (hi
<< 32) );
98 #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
100 unsigned long hardclock( void )
102 unsigned long tbl
, tbu0
, tbu1
;
106 asm( "mftbu %0" : "=r" (tbu0
) );
107 asm( "mftb %0" : "=r" (tbl
) );
108 asm( "mftbu %0" : "=r" (tbu1
) );
110 while( tbu0
!= tbu1
);
116 #if defined(__GNUC__) && defined(__sparc__)
118 unsigned long hardclock( void )
121 asm( ".byte 0x83, 0x41, 0x00, 0x00" );
122 asm( "mov %%g1, %0" : "=r" (tick
) );
127 #if defined(__GNUC__) && defined(__alpha__)
129 unsigned long hardclock( void )
132 asm( "rpcc %0" : "=r" (cc
) );
133 return( cc
& 0xFFFFFFFF );
137 #if defined(__GNUC__) && defined(__ia64__)
139 unsigned long hardclock( void )
142 asm( "mov %0 = ar.itc" : "=r" (itc
) );
148 static int hardclock_init
= 0;
149 static struct timeval tv_init
;
151 unsigned long hardclock( void )
153 struct timeval tv_cur
;
155 if( hardclock_init
== 0 )
157 gettimeofday( &tv_init
, NULL
);
161 gettimeofday( &tv_cur
, NULL
);
162 return( ( tv_cur
.tv_sec
- tv_init
.tv_sec
) * 1000000
163 + ( tv_cur
.tv_usec
- tv_init
.tv_usec
) );
178 unsigned long get_timer( struct hr_time
*val
, int reset
)
181 LARGE_INTEGER offset
, hfreq
;
182 struct _hr_time
*t
= (struct _hr_time
*) val
;
184 QueryPerformanceCounter( &offset
);
185 QueryPerformanceFrequency( &hfreq
);
187 delta
= (unsigned long)( ( 1000 *
188 ( offset
.QuadPart
- t
->start
.QuadPart
) ) /
192 QueryPerformanceCounter( &t
->start
);
197 DWORD WINAPI
TimerProc( LPVOID uElapse
)
199 Sleep( (DWORD
) uElapse
);
204 void set_alarm( int seconds
)
209 CloseHandle( CreateThread( NULL
, 0, TimerProc
,
210 (LPVOID
) ( seconds
* 1000 ), 0, &ThreadId
) );
213 void m_sleep( int milliseconds
)
215 Sleep( milliseconds
);
220 unsigned long get_timer( struct hr_time
*val
, int reset
)
223 struct timeval offset
;
224 struct _hr_time
*t
= (struct _hr_time
*) val
;
226 gettimeofday( &offset
, NULL
);
228 delta
= ( offset
.tv_sec
- t
->start
.tv_sec
) * 1000
229 + ( offset
.tv_usec
- t
->start
.tv_usec
) / 1000;
233 t
->start
.tv_sec
= offset
.tv_sec
;
234 t
->start
.tv_usec
= offset
.tv_usec
;
240 static void sighandler( int signum
)
243 signal( signum
, sighandler
);
246 void set_alarm( int seconds
)
249 signal( SIGALRM
, sighandler
);
253 void m_sleep( int milliseconds
)
257 tv
.tv_sec
= milliseconds
/ 1000;
258 tv
.tv_usec
= milliseconds
* 1000;
260 select( 0, NULL
, NULL
, NULL
, &tv
);
This page took 0.084901 seconds and 5 git commands to generate.