2 * Portable interface to the CPU cycle counter
4 * Copyright (C) 2006-2007 Christophe Devine
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.
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.
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.
21 #include "xyssl/config.h"
23 #if defined(XYSSL_TIMING_C)
25 #include "xyssl/timing.h"
40 #include <sys/types.h>
52 #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
54 unsigned long hardclock( void )
63 #if defined(__GNUC__) && defined(__i386__)
65 unsigned long hardclock( void )
68 asm( "rdtsc" : "=a" (tsc
) );
73 #if defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
75 unsigned long hardclock( void )
78 asm( "rdtsc" : "=a" (lo
), "=d" (hi
) );
79 return( lo
| (hi
<< 32) );
83 #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
85 unsigned long hardclock( void )
87 unsigned long tbl
, tbu0
, tbu1
;
91 asm( "mftbu %0" : "=r" (tbu0
) );
92 asm( "mftb %0" : "=r" (tbl
) );
93 asm( "mftbu %0" : "=r" (tbu1
) );
95 while( tbu0
!= tbu1
);
101 #if defined(__GNUC__) && defined(__sparc__)
103 unsigned long hardclock( void )
106 asm( ".byte 0x83, 0x41, 0x00, 0x00" );
107 asm( "mov %%g1, %0" : "=r" (tick
) );
112 #if defined(__GNUC__) && defined(__alpha__)
114 unsigned long hardclock( void )
117 asm( "rpcc %0" : "=r" (cc
) );
118 return( cc
& 0xFFFFFFFF );
122 #if defined(__GNUC__) && defined(__ia64__)
124 unsigned long hardclock( void )
127 asm( "mov %0 = ar.itc" : "=r" (itc
) );
133 static int hardclock_init
= 0;
134 static struct timeval tv_init
;
136 unsigned long hardclock( void )
138 struct timeval tv_cur
;
140 if( hardclock_init
== 0 )
142 gettimeofday( &tv_init
, NULL
);
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
) );
163 unsigned long get_timer( struct hr_time
*val
, int reset
)
166 LARGE_INTEGER offset
, hfreq
;
167 struct _hr_time
*t
= (struct _hr_time
*) val
;
169 QueryPerformanceCounter( &offset
);
170 QueryPerformanceFrequency( &hfreq
);
172 delta
= (unsigned long)( ( 1000 *
173 ( offset
.QuadPart
- t
->start
.QuadPart
) ) /
177 QueryPerformanceCounter( &t
->start
);
182 DWORD WINAPI
TimerProc( LPVOID uElapse
)
184 Sleep( (DWORD
) uElapse
);
189 void set_alarm( int seconds
)
194 CloseHandle( CreateThread( NULL
, 0, TimerProc
,
195 (LPVOID
) ( seconds
* 1000 ), 0, &ThreadId
) );
198 void m_sleep( int milliseconds
)
200 Sleep( milliseconds
);
205 unsigned long get_timer( struct hr_time
*val
, int reset
)
208 struct timeval offset
;
209 struct _hr_time
*t
= (struct _hr_time
*) val
;
211 gettimeofday( &offset
, NULL
);
213 delta
= ( offset
.tv_sec
- t
->start
.tv_sec
) * 1000
214 + ( offset
.tv_usec
- t
->start
.tv_usec
) / 1000;
218 t
->start
.tv_sec
= offset
.tv_sec
;
219 t
->start
.tv_usec
= offset
.tv_usec
;
225 static void sighandler( int signum
)
228 signal( signum
, sighandler
);
231 void set_alarm( int seconds
)
234 signal( SIGALRM
, sighandler
);
238 void m_sleep( int milliseconds
)
242 tv
.tv_sec
= milliseconds
/ 1000;
243 tv
.tv_usec
= milliseconds
* 1000;
245 select( 0, NULL
, NULL
, NULL
, &tv
);