source: trunk/src/testlib/3rdparty/cycle_p.h@ 104

Last change on this file since 104 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 13.7 KB
Line 
1/*
2 * Copyright (c) 2003, 2006 Matteo Frigo
3 * Copyright (c) 2003, 2006 Massachusetts Institute of Technology
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26/* $Id: cycle.h,v 1.52 2006-02-08 02:36:47 athena Exp $ */
27
28/* machine-dependent cycle counters code. Needs to be inlined. */
29
30/***************************************************************************/
31/* To use the cycle counters in your code, simply #include "cycle.h" (this
32 file), and then use the functions/macros:
33
34 CycleCounterTicks getticks(void);
35
36 CycleCounterTicks is an opaque typedef defined below, representing the current time.
37 You extract the elapsed time between two calls to gettick() via:
38
39 double elapsed(CycleCounterTicks t1, CycleCounterTicks t0);
40
41 which returns a double-precision variable in arbitrary units. You
42 are not expected to convert this into human units like seconds; it
43 is intended only for *comparisons* of time intervals.
44
45 (In order to use some of the OS-dependent timer routines like
46 Solaris' gethrtime, you need to paste the autoconf snippet below
47 into your configure.ac file and #include "config.h" before cycle.h,
48 or define the relevant macros manually if you are not using autoconf.)
49*/
50
51/***************************************************************************/
52/* This file uses macros like HAVE_GETHRTIME that are assumed to be
53 defined according to whether the corresponding function/type/header
54 is available on your system. The necessary macros are most
55 conveniently defined if you are using GNU autoconf, via the tests:
56
57 dnl ---------------------------------------------------------------------
58
59 AC_C_INLINE
60 AC_HEADER_TIME
61 AC_CHECK_HEADERS([sys/time.h c_asm.h intrinsics.h mach/mach_time.h])
62
63 AC_CHECK_TYPE([hrtime_t],[AC_DEFINE(HAVE_HRTIME_T, 1, [Define to 1 if hrtime_t is defined in <sys/time.h>])],,[#if HAVE_SYS_TIME_H
64#include <sys/time.h>
65#endif])
66
67 AC_CHECK_FUNCS([gethrtime read_real_time time_base_to_time clock_gettime mach_absolute_time])
68
69 dnl Cray UNICOS _rtc() (real-time clock) intrinsic
70 AC_MSG_CHECKING([for _rtc intrinsic])
71 rtc_ok=yes
72 AC_TRY_LINK([#ifdef HAVE_INTRINSICS_H
73#include <intrinsics.h>
74#endif], [_rtc()], [AC_DEFINE(HAVE__RTC,1,[Define if you have the UNICOS _rtc() intrinsic.])], [rtc_ok=no])
75 AC_MSG_RESULT($rtc_ok)
76
77 dnl ---------------------------------------------------------------------
78*/
79
80/***************************************************************************/
81
82#ifndef QBENCHLIB_CYCLE_H
83#define QBENCHLIB_CYCLE_H
84
85#if TIME_WITH_SYS_TIME
86# include <sys/time.h>
87# include <time.h>
88#else
89# if HAVE_SYS_TIME_H
90# include <sys/time.h>
91# else
92# include <time.h>
93# endif
94#endif
95
96#define INLINE_ELAPSED(INL) static INL double elapsed(CycleCounterTicks t1, CycleCounterTicks t0) \
97{ \
98 return (double)(t1 - t0); \
99}
100
101/*----------------------------------------------------------------*/
102/* Solaris */
103#if defined(HAVE_GETHRTIME) && defined(HAVE_HRTIME_T) && !defined(HAVE_TICK_COUNTER)
104typedef hrtime_t CycleCounterTicks;
105
106#define getticks gethrtime
107
108INLINE_ELAPSED(inline)
109
110#define HAVE_TICK_COUNTER
111#endif
112
113/*----------------------------------------------------------------*/
114/* AIX v. 4+ routines to read the real-time clock or time-base register */
115#if defined(HAVE_READ_REAL_TIME) && defined(HAVE_TIME_BASE_TO_TIME) && !defined(HAVE_TICK_COUNTER)
116typedef timebasestruct_t CycleCounterTicks;
117
118static inline CycleCounterTicks getticks(void)
119{
120 CycleCounterTicks t;
121 read_real_time(&t, TIMEBASE_SZ);
122 return t;
123}
124
125static inline double elapsed(CycleCounterTicks t1, CycleCounterTicks t0) /* time in nanoseconds */
126{
127 time_base_to_time(&t1, TIMEBASE_SZ);
128 time_base_to_time(&t0, TIMEBASE_SZ);
129 return ((t1.tb_high - t0.tb_high) * 1e9 + (t1.tb_low - t0.tb_low));
130}
131
132#define HAVE_TICK_COUNTER
133#endif
134
135/*----------------------------------------------------------------*/
136/*
137 * PowerPC ``cycle'' counter using the time base register.
138 */
139#if ((defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))) || (defined(__MWERKS__) && defined(macintosh))) && !defined(HAVE_TICK_COUNTER)
140typedef unsigned long long CycleCounterTicks;
141
142static __inline__ CycleCounterTicks getticks(void)
143{
144 unsigned int tbl, tbu0, tbu1;
145
146 do {
147 __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
148 __asm__ __volatile__ ("mftb %0" : "=r"(tbl));
149 __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
150 } while (tbu0 != tbu1);
151
152 return (((unsigned long long)tbu0) << 32) | tbl;
153}
154
155INLINE_ELAPSED(__inline__)
156
157#define HAVE_TICK_COUNTER
158#endif
159
160/* MacOS/Mach (Darwin) time-base register interface (unlike UpTime,
161 from Carbon, requires no additional libraries to be linked). */
162#if defined(HAVE_MACH_ABSOLUTE_TIME) && defined(HAVE_MACH_MACH_TIME_H) && !defined(HAVE_TICK_COUNTER)
163#include <mach/mach_time.h>
164typedef uint64_t CycleCounterTicks;
165#define getticks mach_absolute_time
166INLINE_ELAPSED(__inline__)
167#define HAVE_TICK_COUNTER
168#endif
169
170/*----------------------------------------------------------------*/
171/*
172 * Pentium cycle counter
173 */
174#if (defined(__GNUC__) || defined(__ICC)) && defined(__i386__) && !defined(HAVE_TICK_COUNTER)
175typedef unsigned long long CycleCounterTicks;
176
177static __inline__ CycleCounterTicks getticks(void)
178{
179 CycleCounterTicks ret;
180
181 __asm__ __volatile__("rdtsc": "=A" (ret));
182 /* no input, nothing else clobbered */
183 return ret;
184}
185
186INLINE_ELAPSED(__inline__)
187
188#define HAVE_TICK_COUNTER
189#define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
190#endif
191
192/* Visual C++ -- thanks to Morten Nissov for his help with this */
193#if _MSC_VER >= 1200 && (_M_IX86 >= 500 || (defined(_WIN32_WCE) && defined(_X86_))) && !defined(HAVE_TICK_COUNTER)
194#include <windows.h>
195typedef LARGE_INTEGER CycleCounterTicks;
196#define RDTSC __asm __emit 0fh __asm __emit 031h /* hack for VC++ 5.0 */
197
198static __inline CycleCounterTicks getticks(void)
199{
200 CycleCounterTicks retval;
201
202 __asm {
203 RDTSC
204 mov retval.HighPart, edx
205 mov retval.LowPart, eax
206 }
207 return retval;
208}
209
210static __inline double elapsed(CycleCounterTicks t1, CycleCounterTicks t0)
211{
212 return (double)(t1.QuadPart - t0.QuadPart);
213}
214
215#define HAVE_TICK_COUNTER
216#define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
217#endif
218
219#if _MSC_VER >= 1400 && defined(_WIN32_WCE) && !defined(HAVE_TICK_COUNTER)
220#include <windows.h>
221typedef DWORD CycleCounterTicks;
222
223static __inline CycleCounterTicks getticks(void)
224{
225 return GetTickCount();
226}
227
228static __inline double elapsed(CycleCounterTicks t1, CycleCounterTicks t0)
229{
230 return (double)(t1 - t0);
231}
232
233#define HAVE_TICK_COUNTER
234#define TIME_MIN 5000.0
235#endif
236
237/*----------------------------------------------------------------*/
238/*
239 * X86-64 cycle counter
240 */
241#if (defined(__GNUC__) || defined(__ICC)) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
242typedef unsigned long long CycleCounterTicks;
243
244static __inline__ CycleCounterTicks getticks(void)
245{
246 unsigned a, d;
247 asm volatile("rdtsc" : "=a" (a), "=d" (d));
248 return ((CycleCounterTicks)a) | (((CycleCounterTicks)d) << 32);
249}
250
251INLINE_ELAPSED(__inline__)
252
253#define HAVE_TICK_COUNTER
254#endif
255
256/* PGI compiler, courtesy Cristiano Calonaci, Andrea Tarsi, & Roberto Gori.
257 NOTE: this code will fail to link unless you use the -Masmkeyword compiler
258 option (grrr). */
259#if defined(__PGI) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
260typedef unsigned long long CycleCounterTicks;
261static CycleCounterTicks getticks(void)
262{
263 asm(" rdtsc; shl $0x20,%rdx; mov %eax,%eax; or %rdx,%rax; ");
264}
265INLINE_ELAPSED(__inline__)
266#define HAVE_TICK_COUNTER
267#endif
268
269/* Visual C++ */
270#if _MSC_VER >= 1400 && (defined(_M_AMD64) || defined(_M_X64)) && !defined(HAVE_TICK_COUNTER)
271#include <intrin.h>
272
273typedef unsigned __int64 CycleCounterTicks;
274
275#define getticks __rdtsc
276
277INLINE_ELAPSED(__inline)
278
279#define HAVE_TICK_COUNTER
280#endif
281
282/*----------------------------------------------------------------*/
283/*
284 * IA64 cycle counter
285 */
286
287/* intel's icc/ecc compiler */
288#if (defined(__EDG_VERSION) || defined(__ECC)) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
289typedef unsigned long CycleCounterTicks;
290#include <ia64intrin.h>
291
292static __inline__ CycleCounterTicks getticks(void)
293{
294 return __getReg(_IA64_REG_AR_ITC);
295}
296
297INLINE_ELAPSED(__inline__)
298
299#define HAVE_TICK_COUNTER
300#endif
301
302/* gcc */
303#if defined(__GNUC__) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
304typedef unsigned long CycleCounterTicks;
305
306static __inline__ CycleCounterTicks getticks(void)
307{
308 CycleCounterTicks ret;
309
310 __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret));
311 return ret;
312}
313
314INLINE_ELAPSED(__inline__)
315
316#define HAVE_TICK_COUNTER
317#endif
318
319/* HP/UX IA64 compiler, courtesy Teresa L. Johnson: */
320#if defined(__hpux) && defined(__ia64) && !defined(HAVE_TICK_COUNTER)
321#include <machine/sys/inline.h>
322typedef unsigned long CycleCounterTicks;
323
324static inline CycleCounterTicks getticks(void)
325{
326 CycleCounterTicks ret;
327
328 ret = _Asm_mov_from_ar (_AREG_ITC);
329 return ret;
330}
331
332INLINE_ELAPSED(inline)
333
334#define HAVE_TICK_COUNTER
335#endif
336
337/* Microsoft Visual C++ */
338#if defined(_MSC_VER) && defined(_M_IA64) && !defined(HAVE_TICK_COUNTER)
339typedef unsigned __int64 CycleCounterTicks;
340
341# ifdef __cplusplus
342extern "C"
343# endif
344ticks __getReg(int whichReg);
345#pragma intrinsic(__getReg)
346
347static __inline CycleCounterTicks getticks(void)
348{
349 volatile CycleCounterTicks temp;
350 temp = __getReg(3116);
351 return temp;
352}
353
354#define HAVE_TICK_COUNTER
355#endif
356
357/*----------------------------------------------------------------*/
358/*
359 * PA-RISC cycle counter
360 */
361#if (defined(__hppa__) || defined(__hppa)) && !defined(HAVE_TICK_COUNTER)
362typedef unsigned long CycleCounterTicks;
363
364# ifdef __GNUC__
365static __inline__ CycleCounterTicks getticks(void)
366{
367 CycleCounterTicks ret;
368
369 __asm__ __volatile__("mfctl 16, %0": "=r" (ret));
370 /* no input, nothing else clobbered */
371 return ret;
372}
373
374INLINE_ELAPSED(inline)
375
376#define HAVE_TICK_COUNTER
377
378# elif 0 // Doesn't compile
379# include <machine/inline.h>
380static inline unsigned long getticks(void)
381{
382 register CycleCounterTicks ret;
383 _MFCTL(16, ret);
384 return ret;
385}
386# endif
387
388#endif
389
390/*----------------------------------------------------------------*/
391/* S390, courtesy of James Treacy */
392#if defined(__GNUC__) && defined(__s390__) && !defined(HAVE_TICK_COUNTER)
393typedef unsigned long long CycleCounterTicks;
394
395static __inline__ CycleCounterTicks getticks(void)
396{
397 CycleCounterTicks cycles;
398 __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc");
399 return cycles;
400}
401
402INLINE_ELAPSED(__inline__)
403
404#define HAVE_TICK_COUNTER
405#endif
406/*----------------------------------------------------------------*/
407#if defined(__GNUC__) && defined(__alpha__) && !defined(HAVE_TICK_COUNTER)
408/*
409 * The 32-bit cycle counter on alpha overflows pretty quickly,
410 * unfortunately. A 1GHz machine overflows in 4 seconds.
411 */
412typedef unsigned int CycleCounterTicks;
413
414static __inline__ CycleCounterTicks getticks(void)
415{
416 unsigned long cc;
417 __asm__ __volatile__ ("rpcc %0" : "=r"(cc));
418 return (cc & 0xFFFFFFFF);
419}
420
421INLINE_ELAPSED(__inline__)
422
423#define HAVE_TICK_COUNTER
424#endif
425
426/*----------------------------------------------------------------*/
427#if defined(__GNUC__) && defined(__sparc_v9__) && !defined(HAVE_TICK_COUNTER)
428typedef unsigned long CycleCounterTicks;
429
430static __inline__ CycleCounterTicks getticks(void)
431{
432 CycleCounterTicks ret;
433 __asm__ __volatile__("rd %%tick, %0" : "=r" (ret));
434 return ret;
435}
436
437INLINE_ELAPSED(__inline__)
438
439#define HAVE_TICK_COUNTER
440#endif
441
442/*----------------------------------------------------------------*/
443#if (defined(__DECC) || defined(__DECCXX)) && defined(__alpha) && defined(HAVE_C_ASM_H) && !defined(HAVE_TICK_COUNTER)
444# include <c_asm.h>
445typedef unsigned int CycleCounterTicks;
446
447static __inline CycleCounterTicks getticks(void)
448{
449 unsigned long cc;
450 cc = asm("rpcc %v0");
451 return (cc & 0xFFFFFFFF);
452}
453
454INLINE_ELAPSED(__inline)
455
456#define HAVE_TICK_COUNTER
457#endif
458/*----------------------------------------------------------------*/
459/* SGI/Irix */
460#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_SGI_CYCLE) && !defined(HAVE_TICK_COUNTER)
461typedef struct timespec CycleCounterTicks;
462
463static inline CycleCounterTicks getticks(void)
464{
465 struct timespec t;
466 clock_gettime(CLOCK_SGI_CYCLE, &t);
467 return t;
468}
469
470static inline double elapsed(CycleCounterTicks t1, CycleCounterTicks t0)
471{
472 return (double)(t1.tv_sec - t0.tv_sec) * 1.0E9 +
473 (double)(t1.tv_nsec - t0.tv_nsec);
474}
475#define HAVE_TICK_COUNTER
476#endif
477
478/*----------------------------------------------------------------*/
479/* Cray UNICOS _rtc() intrinsic function */
480#if defined(HAVE__RTC) && !defined(HAVE_TICK_COUNTER)
481#ifdef HAVE_INTRINSICS_H
482# include <intrinsics.h>
483#endif
484
485typedef long long CycleCounterTicks;
486
487#define getticks _rtc
488
489INLINE_ELAPSED(inline)
490
491#define HAVE_TICK_COUNTER
492#endif
493
494#endif // QBENCHLIB_CYCLE_H
Note: See TracBrowser for help on using the repository browser.