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