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