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