blob: 109f70f3548c0d61c4ebc1369ea642518167aa89 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerff60ee62010-03-16 21:09:09 +000032#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
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
Paul Bakker34a90562009-04-19 21:17:09 +000057#if defined(POLARSSL_HAVE_ASM) && \
58 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60unsigned long hardclock( void )
61{
62 unsigned long tsc;
63 __asm rdtsc
64 __asm mov [tsc], eax
65 return( tsc );
66}
67
68#else
Paul Bakker34a90562009-04-19 21:17:09 +000069#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71unsigned long hardclock( void )
72{
73 unsigned long tsc;
74 asm( "rdtsc" : "=a" (tsc) );
75 return( tsc );
76}
77
78#else
Paul Bakker34a90562009-04-19 21:17:09 +000079#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
80 (defined(__amd64__) || defined(__x86_64__))
Paul Bakker5121ce52009-01-03 21:22:43 +000081
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
Paul Bakker34a90562009-04-19 21:17:09 +000090#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
91 (defined(__powerpc__) || defined(__ppc__))
Paul Bakker5121ce52009-01-03 21:22:43 +000092
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
Paul Bakker34a90562009-04-19 21:17:09 +0000109#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
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
Paul Bakker34a90562009-04-19 21:17:09 +0000120#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122unsigned long hardclock( void )
123{
124 unsigned long cc;
125 asm( "rpcc %0" : "=r" (cc) );
126 return( cc & 0xFFFFFFFF );
127}
128
129#else
Paul Bakker34a90562009-04-19 21:17:09 +0000130#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
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
141static int hardclock_init = 0;
142static struct timeval tv_init;
143
144unsigned long hardclock( void )
145{
146 struct timeval tv_cur;
147
148 if( hardclock_init == 0 )
149 {
150 gettimeofday( &tv_init, NULL );
151 hardclock_init = 1;
152 }
153
154 gettimeofday( &tv_cur, NULL );
155 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
156 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
157}
158
159#endif /* generic */
160#endif /* IA-64 */
161#endif /* Alpha */
162#endif /* SPARC8 */
163#endif /* PowerPC */
164#endif /* AMD64 */
165#endif /* i586+ */
166
167int alarmed = 0;
168
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171unsigned long get_timer( struct hr_time *val, int reset )
172{
173 unsigned long delta;
174 LARGE_INTEGER offset, hfreq;
175 struct _hr_time *t = (struct _hr_time *) val;
176
177 QueryPerformanceCounter( &offset );
178 QueryPerformanceFrequency( &hfreq );
179
180 delta = (unsigned long)( ( 1000 *
181 ( offset.QuadPart - t->start.QuadPart ) ) /
182 hfreq.QuadPart );
183
184 if( reset )
185 QueryPerformanceCounter( &t->start );
186
187 return( delta );
188}
189
190DWORD WINAPI TimerProc( LPVOID uElapse )
191{
192 Sleep( (DWORD) uElapse );
193 alarmed = 1;
194 return( TRUE );
195}
196
197void set_alarm( int seconds )
198{
199 DWORD ThreadId;
200
201 alarmed = 0;
202 CloseHandle( CreateThread( NULL, 0, TimerProc,
203 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
204}
205
206void m_sleep( int milliseconds )
207{
208 Sleep( milliseconds );
209}
210
211#else
212
213unsigned long get_timer( struct hr_time *val, int reset )
214{
215 unsigned long delta;
216 struct timeval offset;
217 struct _hr_time *t = (struct _hr_time *) val;
218
219 gettimeofday( &offset, NULL );
220
221 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
222 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
223
224 if( reset )
225 {
226 t->start.tv_sec = offset.tv_sec;
227 t->start.tv_usec = offset.tv_usec;
228 }
229
230 return( delta );
231}
232
233static void sighandler( int signum )
234{
235 alarmed = 1;
236 signal( signum, sighandler );
237}
238
239void set_alarm( int seconds )
240{
241 alarmed = 0;
242 signal( SIGALRM, sighandler );
243 alarm( seconds );
244}
245
246void m_sleep( int milliseconds )
247{
248 struct timeval tv;
249
250 tv.tv_sec = milliseconds / 1000;
251 tv.tv_usec = milliseconds * 1000;
252
253 select( 0, NULL, NULL, NULL, &tv );
254}
255
256#endif
257
258#endif