new, smaller, faster and untested version of KLISP
[syren.git] / src / xyssl / timing.c
blob49fe7f3c3457d5be6834196e608436824bac1077
1 /*
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"
27 #if defined(WIN32)
29 #include <windows.h>
30 #include <winbase.h>
32 struct _hr_time
34 LARGE_INTEGER start;
37 #else
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <time.h>
45 struct _hr_time
47 struct timeval start;
50 #endif
52 #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
54 unsigned long hardclock( void )
56 unsigned long tsc;
57 __asm rdtsc
58 __asm mov [tsc], eax
59 return( tsc );
62 #else
63 #if defined(__GNUC__) && defined(__i386__)
65 unsigned long hardclock( void )
67 unsigned long tsc;
68 asm( "rdtsc" : "=a" (tsc) );
69 return( tsc );
72 #else
73 #if defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
75 unsigned long hardclock( void )
77 unsigned long lo, hi;
78 asm( "rdtsc" : "=a" (lo), "=d" (hi) );
79 return( lo | (hi << 32) );
82 #else
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 );
97 return( tbl );
100 #else
101 #if defined(__GNUC__) && defined(__sparc__)
103 unsigned long hardclock( void )
105 unsigned long tick;
106 asm( ".byte 0x83, 0x41, 0x00, 0x00" );
107 asm( "mov %%g1, %0" : "=r" (tick) );
108 return( tick );
111 #else
112 #if defined(__GNUC__) && defined(__alpha__)
114 unsigned long hardclock( void )
116 unsigned long cc;
117 asm( "rpcc %0" : "=r" (cc) );
118 return( cc & 0xFFFFFFFF );
121 #else
122 #if defined(__GNUC__) && defined(__ia64__)
124 unsigned long hardclock( void )
126 unsigned long itc;
127 asm( "mov %0 = ar.itc" : "=r" (itc) );
128 return( itc );
131 #else
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 );
143 hardclock_init = 1;
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 ) );
151 #endif /* generic */
152 #endif /* IA-64 */
153 #endif /* Alpha */
154 #endif /* SPARC8 */
155 #endif /* PowerPC */
156 #endif /* AMD64 */
157 #endif /* i586+ */
159 int alarmed = 0;
161 #if defined(WIN32)
163 unsigned long get_timer( struct hr_time *val, int reset )
165 unsigned long delta;
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 ) ) /
174 hfreq.QuadPart );
176 if( reset )
177 QueryPerformanceCounter( &t->start );
179 return( delta );
182 DWORD WINAPI TimerProc( LPVOID uElapse )
184 Sleep( (DWORD) uElapse );
185 alarmed = 1;
186 return( TRUE );
189 void set_alarm( int seconds )
191 DWORD ThreadId;
193 alarmed = 0;
194 CloseHandle( CreateThread( NULL, 0, TimerProc,
195 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
198 void m_sleep( int milliseconds )
200 Sleep( milliseconds );
203 #else
205 unsigned long get_timer( struct hr_time *val, int reset )
207 unsigned long delta;
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;
216 if( reset )
218 t->start.tv_sec = offset.tv_sec;
219 t->start.tv_usec = offset.tv_usec;
222 return( delta );
225 static void sighandler( int signum )
227 alarmed = 1;
228 signal( signum, sighandler );
231 void set_alarm( int seconds )
233 alarmed = 0;
234 signal( SIGALRM, sighandler );
235 alarm( seconds );
238 void m_sleep( int milliseconds )
240 struct timeval tv;
242 tv.tv_sec = milliseconds / 1000;
243 tv.tv_usec = milliseconds * 1000;
245 select( 0, NULL, NULL, NULL, &tv );
248 #endif
250 #endif