1/*
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
3 * Copyright (c) 2003, 2007-14 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
27/* machine-dependent cycle counters code. Needs to be inlined. */
28
29/***************************************************************************/
30/* To use the cycle counters in your code, simply #include "cycle.h" (this
31 file), and then use the functions/macros:
32
33 ticks getticks(void);
34
35 ticks is an opaque typedef defined below, representing the current time.
36 You extract the elapsed time between two calls to gettick() via:
37
38 double elapsed(ticks t1, ticks t0);
39
40 which returns a double-precision variable in arbitrary units. You
41 are not expected to convert this into human units like seconds; it
42 is intended only for *comparisons* of time intervals.
43
44 (In order to use some of the OS-dependent timer routines like
45 Solaris' gethrtime, you need to paste the autoconf snippet below
46 into your configure.ac file and #include "config.h" before cycle.h,
47 or define the relevant macros manually if you are not using autoconf.)
48*/
49
50/***************************************************************************/
51/* This file uses macros like HAVE_GETHRTIME that are assumed to be
52 defined according to whether the corresponding function/type/header
53 is available on your system. The necessary macros are most
54 conveniently defined if you are using GNU autoconf, via the tests:
55
56 dnl ---------------------------------------------------------------------
57
58 AC_C_INLINE
59 AC_HEADER_TIME
60 AC_CHECK_HEADERS([sys/time.h c_asm.h intrinsics.h mach/mach_time.h])
61
62 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
63#include <sys/time.h>
64#endif])
65
66 AC_CHECK_FUNCS([gethrtime read_real_time time_base_to_time clock_gettime mach_absolute_time])
67
68 dnl Cray UNICOS _rtc() (real-time clock) intrinsic
69 AC_MSG_CHECKING([for _rtc intrinsic])
70 rtc_ok=yes
71 AC_TRY_LINK([#ifdef HAVE_INTRINSICS_H
72#include <intrinsics.h>
73#endif], [_rtc()], [AC_DEFINE(HAVE__RTC,1,[Define if you have the UNICOS _rtc() intrinsic.])], [rtc_ok=no])
74 AC_MSG_RESULT($rtc_ok)
75
76 dnl ---------------------------------------------------------------------
77*/
78
79#ifndef QBENCHLIB_CYCLE_H
80#define QBENCHLIB_CYCLE_H
81#define ticks CycleCounterTicks
82
83#ifndef QBENCHLIB_INCLUDING_CYCLE_P
84#error Include cycle_include_p.h instead of this file
85#endif
86
87/***************************************************************************/
88
89#if TIME_WITH_SYS_TIME
90# include <sys/time.h>
91# include <time.h>
92#else
93# if HAVE_SYS_TIME_H
94# include <sys/time.h>
95# else
96# include <time.h>
97# endif
98#endif
99
100#define INLINE_ELAPSED(INL) static INL double elapsed(ticks t1, ticks t0) \
101{ \
102 return (double)t1 - (double)t0; \
103}
104
105/*----------------------------------------------------------------*/
106/* Solaris */
107#if defined(HAVE_GETHRTIME) && defined(HAVE_HRTIME_T) && !defined(HAVE_TICK_COUNTER)
108typedef hrtime_t ticks;
109
110#define getticks gethrtime
111
112INLINE_ELAPSED(inline)
113
114#define HAVE_TICK_COUNTER
115#endif
116
117/*----------------------------------------------------------------*/
118/* AIX v. 4+ routines to read the real-time clock or time-base register */
119#if defined(HAVE_READ_REAL_TIME) && defined(HAVE_TIME_BASE_TO_TIME) && !defined(HAVE_TICK_COUNTER)
120typedef timebasestruct_t ticks;
121
122static __inline ticks getticks(void)
123{
124 ticks t;
125 read_real_time(&t, TIMEBASE_SZ);
126 return t;
127}
128
129static __inline double elapsed(ticks t1, ticks t0) /* time in nanoseconds */
130{
131 time_base_to_time(&t1, TIMEBASE_SZ);
132 time_base_to_time(&t0, TIMEBASE_SZ);
133 return (((double)t1.tb_high - (double)t0.tb_high) * 1.0e9 +
134 ((double)t1.tb_low - (double)t0.tb_low));
135}
136
137#define HAVE_TICK_COUNTER
138#endif
139
140/*----------------------------------------------------------------*/
141/*
142 * PowerPC ``cycle'' counter using the time base register.
143 */
144#if ((((defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))) || (defined(__MWERKS__) && defined(macintosh)))) || (defined(__IBM_GCC_ASM) && (defined(__powerpc__) || defined(__ppc__)))) && !defined(HAVE_TICK_COUNTER)
145typedef unsigned long long ticks;
146
147static __inline__ ticks getticks(void)
148{
149 unsigned int tbl, tbu0, tbu1;
150
151 do {
152 __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
153 __asm__ __volatile__ ("mftb %0" : "=r"(tbl));
154 __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
155 } while (tbu0 != tbu1);
156
157 return (((unsigned long long)tbu0) << 32) | tbl;
158}
159
160INLINE_ELAPSED(__inline__)
161
162#define HAVE_TICK_COUNTER
163#endif
164
165/* MacOS/Mach (Darwin) time-base register interface (unlike UpTime,
166 from Carbon, requires no additional libraries to be linked). */
167#if defined(HAVE_MACH_ABSOLUTE_TIME) && defined(HAVE_MACH_MACH_TIME_H) && !defined(HAVE_TICK_COUNTER)
168#include <mach/mach_time.h>
169typedef uint64_t ticks;
170#define getticks mach_absolute_time
171INLINE_ELAPSED(__inline__)
172#define HAVE_TICK_COUNTER
173#endif
174
175/*----------------------------------------------------------------*/
176/*
177 * Pentium cycle counter
178 */
179#if (defined(__GNUC__) || defined(__ICC)) && defined(__i386__) && !defined(HAVE_TICK_COUNTER)
180typedef unsigned long long ticks;
181
182static __inline__ ticks getticks(void)
183{
184 ticks ret;
185
186 __asm__ __volatile__("rdtsc": "=A" (ret));
187 /* no input, nothing else clobbered */
188 return ret;
189}
190
191INLINE_ELAPSED(__inline__)
192
193#define HAVE_TICK_COUNTER
194#define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
195#endif
196
197/* Visual C++ -- thanks to Morten Nissov for his help with this */
198#ifdef _MSC_VER
199#if _MSC_VER >= 1200 && _M_IX86 >= 500 && !defined(HAVE_TICK_COUNTER)
200#include <windows.h>
201typedef LARGE_INTEGER ticks;
202#define RDTSC __asm __emit 0fh __asm __emit 031h /* hack for VC++ 5.0 */
203
204static __inline ticks getticks(void)
205{
206 ticks retval;
207
208 __asm {
209 RDTSC
210 mov retval.HighPart, edx
211 mov retval.LowPart, eax
212 }
213 return retval;
214}
215
216static __inline double elapsed(ticks t1, ticks t0)
217{
218 return (double)t1.QuadPart - (double)t0.QuadPart;
219}
220
221#define HAVE_TICK_COUNTER
222#define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
223#endif
224#endif // _MSC_VER
225
226/*----------------------------------------------------------------*/
227/*
228 * X86-64 cycle counter
229 */
230#if (defined(__GNUC__) || defined(__ICC) || defined(__SUNPRO_C)) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
231typedef unsigned long long ticks;
232
233static __inline__ ticks getticks(void)
234{
235 unsigned a, d;
236 __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
237 return ((ticks)a) | (((ticks)d) << 32);
238}
239
240INLINE_ELAPSED(__inline__)
241
242#define HAVE_TICK_COUNTER
243#define TIME_MIN 5000.0
244#endif
245
246/* PGI compiler, courtesy Cristiano Calonaci, Andrea Tarsi, & Roberto Gori.
247 NOTE: this code will fail to link unless you use the -Masmkeyword compiler
248 option (grrr). */
249#if defined(__PGI) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
250typedef unsigned long long ticks;
251static ticks getticks(void)
252{
253 asm(" rdtsc; shl $0x20,%rdx; mov %eax,%eax; or %rdx,%rax; ");
254}
255INLINE_ELAPSED(__inline__)
256#define HAVE_TICK_COUNTER
257#define TIME_MIN 5000.0
258#endif
259
260/* Visual C++, courtesy of Dirk Michaelis */
261#ifdef _MSC_VER
262#if _MSC_VER >= 1400 && (defined(_M_AMD64) || defined(_M_X64)) && !defined(HAVE_TICK_COUNTER)
263
264#include <intrin.h>
265#pragma intrinsic(__rdtsc)
266typedef unsigned __int64 ticks;
267#define getticks __rdtsc
268INLINE_ELAPSED(__inline)
269
270#define HAVE_TICK_COUNTER
271#define TIME_MIN 5000.0
272#endif
273#endif // _MSC_VER
274
275/*----------------------------------------------------------------*/
276/*
277 * IA64 cycle counter
278 */
279
280/* intel's icc/ecc compiler */
281#if (defined(__EDG_VERSION) || defined(__ECC)) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
282typedef unsigned long ticks;
283#include <ia64intrin.h>
284
285static __inline__ ticks getticks(void)
286{
287 return __getReg(_IA64_REG_AR_ITC);
288}
289
290INLINE_ELAPSED(__inline__)
291
292#define HAVE_TICK_COUNTER
293#endif
294
295/* gcc */
296#if defined(__GNUC__) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
297typedef unsigned long ticks;
298
299static __inline__ ticks getticks(void)
300{
301 ticks ret;
302
303 __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret));
304 return ret;
305}
306
307INLINE_ELAPSED(__inline__)
308
309#define HAVE_TICK_COUNTER
310#endif
311
312/* HP/UX IA64 compiler, courtesy Teresa L. Johnson: */
313#if defined(__hpux) && defined(__ia64) && !defined(HAVE_TICK_COUNTER)
314#include <machine/sys/inline.h>
315typedef unsigned long ticks;
316
317static inline ticks getticks(void)
318{
319 ticks ret;
320
321 ret = _Asm_mov_from_ar (_AREG_ITC);
322 return ret;
323}
324
325INLINE_ELAPSED(inline)
326
327#define HAVE_TICK_COUNTER
328#endif
329
330/* Microsoft Visual C++ */
331#if defined(_MSC_VER) && defined(_M_IA64) && !defined(HAVE_TICK_COUNTER)
332typedef unsigned __int64 ticks;
333
334# ifdef __cplusplus
335extern "C"
336# endif
337ticks __getReg(int whichReg);
338#pragma intrinsic(__getReg)
339
340static __inline ticks getticks(void)
341{
342 volatile ticks temp;
343 temp = __getReg(3116);
344 return temp;
345}
346
347INLINE_ELAPSED(inline)
348
349#define HAVE_TICK_COUNTER
350#endif
351
352/*----------------------------------------------------------------*/
353/*
354 * PA-RISC cycle counter
355 */
356#if (defined(__hppa__) || defined(__hppa)) && !defined(HAVE_TICK_COUNTER)
357typedef unsigned long ticks;
358
359# ifdef __GNUC__
360static __inline__ ticks getticks(void)
361{
362 ticks ret;
363
364 __asm__ __volatile__("mfctl 16, %0": "=r" (ret));
365 /* no input, nothing else clobbered */
366 return ret;
367}
368
369INLINE_ELAPSED(inline)
370
371#define HAVE_TICK_COUNTER
372# elif 0 // Doesn't compile
373# include <machine/inline.h>
374static inline unsigned long getticks(void)
375{
376 register ticks ret;
377 _MFCTL(16, ret);
378 return ret;
379}
380# endif
381#endif
382
383/*----------------------------------------------------------------*/
384/* S390, courtesy of James Treacy */
385#if defined(__GNUC__) && defined(__s390__) && !defined(HAVE_TICK_COUNTER)
386typedef unsigned long long ticks;
387
388static __inline__ ticks getticks(void)
389{
390 ticks cycles;
391 __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc");
392 return cycles;
393}
394
395INLINE_ELAPSED(__inline__)
396
397#define HAVE_TICK_COUNTER
398#endif
399/*----------------------------------------------------------------*/
400#if defined(__GNUC__) && defined(__alpha__) && !defined(HAVE_TICK_COUNTER)
401/*
402 * The 32-bit cycle counter on alpha overflows pretty quickly,
403 * unfortunately. A 1GHz machine overflows in 4 seconds.
404 */
405typedef unsigned int ticks;
406
407static __inline__ ticks getticks(void)
408{
409 unsigned long cc;
410 __asm__ __volatile__ ("rpcc %0" : "=r"(cc));
411 return (cc & 0xFFFFFFFF);
412}
413
414INLINE_ELAPSED(__inline__)
415
416#define HAVE_TICK_COUNTER
417#endif
418
419/*----------------------------------------------------------------*/
420#if defined(__GNUC__) && defined(__sparc_v9__) && !defined(HAVE_TICK_COUNTER)
421typedef unsigned long ticks;
422
423static __inline__ ticks getticks(void)
424{
425 ticks ret;
426 __asm__ __volatile__("rd %%tick, %0" : "=r" (ret));
427 return ret;
428}
429
430INLINE_ELAPSED(__inline__)
431
432#define HAVE_TICK_COUNTER
433#endif
434
435/*----------------------------------------------------------------*/
436#if (defined(__DECC) || defined(__DECCXX)) && defined(__alpha) && defined(HAVE_C_ASM_H) && !defined(HAVE_TICK_COUNTER)
437# include <c_asm.h>
438typedef unsigned int ticks;
439
440static __inline ticks getticks(void)
441{
442 unsigned long cc;
443 cc = asm("rpcc %v0");
444 return (cc & 0xFFFFFFFF);
445}
446
447INLINE_ELAPSED(__inline)
448
449#define HAVE_TICK_COUNTER
450#endif
451/*----------------------------------------------------------------*/
452/* SGI/Irix */
453#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_SGI_CYCLE) && !defined(HAVE_TICK_COUNTER) && !defined(__ANDROID__)
454typedef struct timespec ticks;
455
456static inline ticks getticks(void)
457{
458 struct timespec t;
459 clock_gettime(CLOCK_SGI_CYCLE, &t);
460 return t;
461}
462
463static inline double elapsed(ticks t1, ticks t0)
464{
465 return ((double)t1.tv_sec - (double)t0.tv_sec) * 1.0E9 +
466 ((double)t1.tv_nsec - (double)t0.tv_nsec);
467}
468#define HAVE_TICK_COUNTER
469#endif
470
471/*----------------------------------------------------------------*/
472/* Cray UNICOS _rtc() intrinsic function */
473#if defined(HAVE__RTC) && !defined(HAVE_TICK_COUNTER)
474#ifdef HAVE_INTRINSICS_H
475# include <intrinsics.h>
476#endif
477
478typedef long long ticks;
479
480#define getticks _rtc
481
482INLINE_ELAPSED(inline)
483
484#define HAVE_TICK_COUNTER
485#endif
486
487/*----------------------------------------------------------------*/
488/* MIPS ZBus */
489#if HAVE_MIPS_ZBUS_TIMER
490#if defined(__mips__) && !defined(HAVE_TICK_COUNTER)
491#include <sys/mman.h>
492#include <unistd.h>
493#include <fcntl.h>
494
495typedef uint64_t ticks;
496
497static inline ticks getticks(void)
498{
499 static uint64_t* addr = 0;
500
501 if (addr == 0)
502 {
503 uint32_t rq_addr = 0x10030000;
504 int fd;
505 int pgsize;
506
507 pgsize = getpagesize();
508 fd = open ("/dev/mem", O_RDONLY | O_SYNC, 0);
509 if (fd < 0) {
510 perror("open");
511 return NULL;
512 }
513 addr = mmap(0, pgsize, PROT_READ, MAP_SHARED, fd, rq_addr);
514 close(fd);
515 if (addr == (uint64_t *)-1) {
516 perror("mmap");
517 return NULL;
518 }
519 }
520
521 return *addr;
522}
523
524INLINE_ELAPSED(inline)
525
526#define HAVE_TICK_COUNTER
527#endif
528#endif /* HAVE_MIPS_ZBUS_TIMER */
529
530#if defined(HAVE_ARMV7A_CNTVCT)
531typedef uint64_t ticks;
532static inline ticks getticks(void)
533{
534 uint32_t Rt, Rt2 = 0;
535 asm volatile("mrrc p15, 1, %0, %1, c14" : "=r"(Rt), "=r"(Rt2));
536 return ((uint64_t)Rt) | (((uint64_t)Rt2) << 32);
537}
538INLINE_ELAPSED(inline)
539#define HAVE_TICK_COUNTER
540#endif
541
542#if defined(HAVE_ARMV7A_PMCCNTR)
543typedef uint64_t ticks;
544static inline ticks getticks(void)
545{
546 uint32_t r;
547 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r) );
548 return r;
549}
550INLINE_ELAPSED(inline)
551#define HAVE_TICK_COUNTER
552#endif
553
554#if defined(__aarch64__) && defined(HAVE_ARMV8_CNTVCT_EL0) && !defined(HAVE_ARMV8_PMCCNTR_EL0)
555typedef uint64_t ticks;
556static inline ticks getticks(void)
557{
558 uint64_t Rt;
559 asm volatile("mrs %0, CNTVCT_EL0" : "=r" (Rt));
560 return Rt;
561}
562INLINE_ELAPSED(inline)
563#define HAVE_TICK_COUNTER
564#endif
565
566#if defined(__aarch64__) && defined(HAVE_ARMV8_PMCCNTR_EL0)
567typedef uint64_t ticks;
568static inline ticks getticks(void)
569{
570 uint64_t cc = 0;
571 asm volatile("mrs %0, PMCCNTR_EL0" : "=r"(cc));
572 return cc;
573}
574INLINE_ELAPSED(inline)
575#define HAVE_TICK_COUNTER
576#endif
577
578#undef ticks
579#endif // QBENCHLIB_CYCLE_H
580

source code of qtbase/src/testlib/3rdparty/cycle/cycle_p.h