blob: 523dbe11ebf18052ff9979563dfd7668241baa65 [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{
Paul Bakkerca410102011-10-19 14:27:36 +000073 unsigned long lo, hi;
74 asm( "rdtsc" : "=a" (lo), "=d" (hi) );
75 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000076}
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
Paul Bakker2eee9022011-04-24 15:28:55 +0000140#if defined(_MSC_VER)
141
142unsigned long hardclock( void )
143{
144 LARGE_INTEGER offset;
145
146 QueryPerformanceCounter( &offset );
147
148 return (unsigned long)( offset.QuadPart );
149}
150
151#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153static int hardclock_init = 0;
154static struct timeval tv_init;
155
156unsigned long hardclock( void )
157{
158 struct timeval tv_cur;
159
160 if( hardclock_init == 0 )
161 {
162 gettimeofday( &tv_init, NULL );
163 hardclock_init = 1;
164 }
165
166 gettimeofday( &tv_cur, NULL );
167 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
168 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
169}
170
171#endif /* generic */
Paul Bakker2eee9022011-04-24 15:28:55 +0000172#endif /* WIN32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000173#endif /* IA-64 */
174#endif /* Alpha */
175#endif /* SPARC8 */
176#endif /* PowerPC */
177#endif /* AMD64 */
178#endif /* i586+ */
179
Paul Bakker2eee9022011-04-24 15:28:55 +0000180volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakkerff60ee62010-03-16 21:09:09 +0000182#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184unsigned long get_timer( struct hr_time *val, int reset )
185{
186 unsigned long delta;
187 LARGE_INTEGER offset, hfreq;
188 struct _hr_time *t = (struct _hr_time *) val;
189
190 QueryPerformanceCounter( &offset );
191 QueryPerformanceFrequency( &hfreq );
192
193 delta = (unsigned long)( ( 1000 *
194 ( offset.QuadPart - t->start.QuadPart ) ) /
195 hfreq.QuadPart );
196
197 if( reset )
198 QueryPerformanceCounter( &t->start );
199
200 return( delta );
201}
202
203DWORD WINAPI TimerProc( LPVOID uElapse )
204{
205 Sleep( (DWORD) uElapse );
206 alarmed = 1;
207 return( TRUE );
208}
209
210void set_alarm( int seconds )
211{
212 DWORD ThreadId;
213
214 alarmed = 0;
215 CloseHandle( CreateThread( NULL, 0, TimerProc,
216 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
217}
218
219void m_sleep( int milliseconds )
220{
221 Sleep( milliseconds );
222}
223
224#else
225
226unsigned long get_timer( struct hr_time *val, int reset )
227{
228 unsigned long delta;
229 struct timeval offset;
230 struct _hr_time *t = (struct _hr_time *) val;
231
232 gettimeofday( &offset, NULL );
233
234 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
235 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
236
237 if( reset )
238 {
239 t->start.tv_sec = offset.tv_sec;
240 t->start.tv_usec = offset.tv_usec;
241 }
242
243 return( delta );
244}
245
246static void sighandler( int signum )
247{
248 alarmed = 1;
249 signal( signum, sighandler );
250}
251
252void set_alarm( int seconds )
253{
254 alarmed = 0;
255 signal( SIGALRM, sighandler );
256 alarm( seconds );
257}
258
259void m_sleep( int milliseconds )
260{
261 struct timeval tv;
262
263 tv.tv_sec = milliseconds / 1000;
264 tv.tv_usec = milliseconds * 1000;
265
266 select( 0, NULL, NULL, NULL, &tv );
267}
268
269#endif
270
271#endif