blob: b6ecbf6c86c50229e3908668c8333bb8abebf527 [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 Bakker785a9ee2009-01-25 14:15:10 +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
Paul Bakker34a90562009-04-19 21:17:09 +000054#if defined(POLARSSL_HAVE_ASM) && \
55 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000056
57unsigned long hardclock( void )
58{
59 unsigned long tsc;
60 __asm rdtsc
61 __asm mov [tsc], eax
62 return( tsc );
63}
64
65#else
Paul Bakker34a90562009-04-19 21:17:09 +000066#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68unsigned long hardclock( void )
69{
70 unsigned long tsc;
71 asm( "rdtsc" : "=a" (tsc) );
72 return( tsc );
73}
74
75#else
Paul Bakker34a90562009-04-19 21:17:09 +000076#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
77 (defined(__amd64__) || defined(__x86_64__))
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79unsigned long hardclock( void )
80{
81 unsigned long lo, hi;
82 asm( "rdtsc" : "=a" (lo), "=d" (hi) );
83 return( lo | (hi << 32) );
84}
85
86#else
Paul Bakker34a90562009-04-19 21:17:09 +000087#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
88 (defined(__powerpc__) || defined(__ppc__))
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90unsigned long hardclock( void )
91{
92 unsigned long tbl, tbu0, tbu1;
93
94 do
95 {
96 asm( "mftbu %0" : "=r" (tbu0) );
97 asm( "mftb %0" : "=r" (tbl ) );
98 asm( "mftbu %0" : "=r" (tbu1) );
99 }
100 while( tbu0 != tbu1 );
101
102 return( tbl );
103}
104
105#else
Paul Bakker34a90562009-04-19 21:17:09 +0000106#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108unsigned long hardclock( void )
109{
110 unsigned long tick;
111 asm( ".byte 0x83, 0x41, 0x00, 0x00" );
112 asm( "mov %%g1, %0" : "=r" (tick) );
113 return( tick );
114}
115
116#else
Paul Bakker34a90562009-04-19 21:17:09 +0000117#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119unsigned long hardclock( void )
120{
121 unsigned long cc;
122 asm( "rpcc %0" : "=r" (cc) );
123 return( cc & 0xFFFFFFFF );
124}
125
126#else
Paul Bakker34a90562009-04-19 21:17:09 +0000127#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129unsigned long hardclock( void )
130{
131 unsigned long itc;
132 asm( "mov %0 = ar.itc" : "=r" (itc) );
133 return( itc );
134}
135
136#else
137
138static int hardclock_init = 0;
139static struct timeval tv_init;
140
141unsigned long hardclock( void )
142{
143 struct timeval tv_cur;
144
145 if( hardclock_init == 0 )
146 {
147 gettimeofday( &tv_init, NULL );
148 hardclock_init = 1;
149 }
150
151 gettimeofday( &tv_cur, NULL );
152 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
153 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
154}
155
156#endif /* generic */
157#endif /* IA-64 */
158#endif /* Alpha */
159#endif /* SPARC8 */
160#endif /* PowerPC */
161#endif /* AMD64 */
162#endif /* i586+ */
163
164int alarmed = 0;
165
166#if defined(WIN32)
167
168unsigned long get_timer( struct hr_time *val, int reset )
169{
170 unsigned long delta;
171 LARGE_INTEGER offset, hfreq;
172 struct _hr_time *t = (struct _hr_time *) val;
173
174 QueryPerformanceCounter( &offset );
175 QueryPerformanceFrequency( &hfreq );
176
177 delta = (unsigned long)( ( 1000 *
178 ( offset.QuadPart - t->start.QuadPart ) ) /
179 hfreq.QuadPart );
180
181 if( reset )
182 QueryPerformanceCounter( &t->start );
183
184 return( delta );
185}
186
187DWORD WINAPI TimerProc( LPVOID uElapse )
188{
189 Sleep( (DWORD) uElapse );
190 alarmed = 1;
191 return( TRUE );
192}
193
194void set_alarm( int seconds )
195{
196 DWORD ThreadId;
197
198 alarmed = 0;
199 CloseHandle( CreateThread( NULL, 0, TimerProc,
200 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
201}
202
203void m_sleep( int milliseconds )
204{
205 Sleep( milliseconds );
206}
207
208#else
209
210unsigned long get_timer( struct hr_time *val, int reset )
211{
212 unsigned long delta;
213 struct timeval offset;
214 struct _hr_time *t = (struct _hr_time *) val;
215
216 gettimeofday( &offset, NULL );
217
218 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
219 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
220
221 if( reset )
222 {
223 t->start.tv_sec = offset.tv_sec;
224 t->start.tv_usec = offset.tv_usec;
225 }
226
227 return( delta );
228}
229
230static void sighandler( int signum )
231{
232 alarmed = 1;
233 signal( signum, sighandler );
234}
235
236void set_alarm( int seconds )
237{
238 alarmed = 0;
239 signal( SIGALRM, sighandler );
240 alarm( seconds );
241}
242
243void m_sleep( int milliseconds )
244{
245 struct timeval tv;
246
247 tv.tv_sec = milliseconds / 1000;
248 tv.tv_usec = milliseconds * 1000;
249
250 select( 0, NULL, NULL, NULL, &tv );
251}
252
253#endif
254
255#endif