NFFT 3.5.3alpha
cycle.h
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/***************************************************************************/
80
81#if HAVE_SYS_TIME_H
82# include <sys/time.h>
83#endif
84#include <time.h>
85
86#define INLINE_ELAPSED(INL) static INL double elapsed(ticks t1, ticks t0) \
87{ \
88 return (double)t1 - (double)t0; \
89}
90
91/*----------------------------------------------------------------*/
92/* Solaris */
93#if defined(HAVE_GETHRTIME) && defined(HAVE_HRTIME_T) && !defined(HAVE_TICK_COUNTER)
94typedef hrtime_t ticks;
95
96#define getticks gethrtime
97
98INLINE_ELAPSED(inline)
99
100#define HAVE_TICK_COUNTER
101#endif
102
103/*----------------------------------------------------------------*/
104/* AIX v. 4+ routines to read the real-time clock or time-base register */
105#if defined(HAVE_READ_REAL_TIME) && defined(HAVE_TIME_BASE_TO_TIME) && !defined(HAVE_TICK_COUNTER)
106typedef timebasestruct_t ticks;
107
108static __inline ticks getticks(void)
109{
110 ticks t;
111 read_real_time(&t, TIMEBASE_SZ);
112 return t;
113}
114
115static __inline double elapsed(ticks t1, ticks t0) /* time in nanoseconds */
116{
117 time_base_to_time(&t1, TIMEBASE_SZ);
118 time_base_to_time(&t0, TIMEBASE_SZ);
119 return (((double)t1.tb_high - (double)t0.tb_high) * 1.0e9 +
120 ((double)t1.tb_low - (double)t0.tb_low));
121}
122
123#define HAVE_TICK_COUNTER
124#endif
125
126/*----------------------------------------------------------------*/
127/*
128 * PowerPC ``cycle'' counter using the time base register.
129 */
130#if ((((defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))) || (defined(__MWERKS__) && defined(macintosh)))) || (defined(__IBM_GCC_ASM) && (defined(__powerpc__) || defined(__ppc__)))) && !defined(HAVE_TICK_COUNTER)
131typedef unsigned long long ticks;
132
133static __inline__ ticks getticks(void)
134{
135 unsigned int tbl, tbu0, tbu1;
136
137 do {
138 __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
139 __asm__ __volatile__ ("mftb %0" : "=r"(tbl));
140 __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
141 } while (tbu0 != tbu1);
142
143 return (((unsigned long long)tbu0) << 32) | tbl;
144}
145
146INLINE_ELAPSED(__inline__)
147
148#define HAVE_TICK_COUNTER
149#endif
150
151/* MacOS/Mach (Darwin) time-base register interface (unlike UpTime,
152 from Carbon, requires no additional libraries to be linked). */
153#if defined(HAVE_MACH_ABSOLUTE_TIME) && defined(HAVE_MACH_MACH_TIME_H) && !defined(HAVE_TICK_COUNTER)
154#include <mach/mach_time.h>
155typedef uint64_t ticks;
156#define getticks mach_absolute_time
157INLINE_ELAPSED(__inline__)
158#define HAVE_TICK_COUNTER
159#endif
160
161/*----------------------------------------------------------------*/
162/*
163 * Pentium cycle counter
164 */
165#if (defined(__GNUC__) || defined(__ICC)) && defined(__i386__) && !defined(HAVE_TICK_COUNTER)
166typedef unsigned long long ticks;
167
168static __inline__ ticks getticks(void)
169{
170 ticks ret;
171
172 __asm__ __volatile__("rdtsc": "=A" (ret));
173 /* no input, nothing else clobbered */
174 return ret;
175}
176
177INLINE_ELAPSED(__inline__)
178
179#define HAVE_TICK_COUNTER
180#define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
181#endif
182
183/* Visual C++ -- thanks to Morten Nissov for his help with this */
184#if defined(_MSC_VER) && _MSC_VER >= 1200 && _M_IX86 >= 500 && !defined(HAVE_TICK_COUNTER)
185#include <windows.h>
186typedef LARGE_INTEGER ticks;
187#define RDTSC __asm __emit 0fh __asm __emit 031h /* hack for VC++ 5.0 */
188
189static __inline ticks getticks(void)
190{
191 ticks retval;
192
193 __asm {
194 RDTSC
195 mov retval.HighPart, edx
196 mov retval.LowPart, eax
197 }
198 return retval;
199}
200
201static __inline double elapsed(ticks t1, ticks t0)
202{
203 return (double)t1.QuadPart - (double)t0.QuadPart;
204}
205
206#define HAVE_TICK_COUNTER
207#define TIME_MIN 5000.0 /* unreliable pentium IV cycle counter */
208#endif
209
210/*----------------------------------------------------------------*/
211/*
212 * X86-64 cycle counter
213 */
214#if (defined(__GNUC__) || defined(__ICC) || defined(__SUNPRO_C)) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
215typedef unsigned long long ticks;
216
217static __inline__ ticks getticks(void)
218{
219 unsigned a, d;
220 asm volatile("rdtsc" : "=a" (a), "=d" (d));
221 return ((ticks)a) | (((ticks)d) << 32);
222}
223
224INLINE_ELAPSED(__inline__)
225
226#define HAVE_TICK_COUNTER
227#define TIME_MIN 5000.0
228#endif
229
230/* PGI compiler, courtesy Cristiano Calonaci, Andrea Tarsi, & Roberto Gori.
231 NOTE: this code will fail to link unless you use the -Masmkeyword compiler
232 option (grrr). */
233#if defined(__PGI) && defined(__x86_64__) && !defined(HAVE_TICK_COUNTER)
234typedef unsigned long long ticks;
235static ticks getticks(void)
236{
237 asm(" rdtsc; shl $0x20,%rdx; mov %eax,%eax; or %rdx,%rax; ");
238}
239INLINE_ELAPSED(__inline__)
240#define HAVE_TICK_COUNTER
241#define TIME_MIN 5000.0
242#endif
243
244/* Visual C++, courtesy of Dirk Michaelis */
245#if defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(_M_AMD64) || defined(_M_X64)) && !defined(HAVE_TICK_COUNTER)
246
247#include <intrin.h>
248#pragma intrinsic(__rdtsc)
249typedef unsigned __int64 ticks;
250#define getticks __rdtsc
251INLINE_ELAPSED(__inline)
252
253#define HAVE_TICK_COUNTER
254#define TIME_MIN 5000.0
255#endif
256
257/*----------------------------------------------------------------*/
258/*
259 * IA64 cycle counter
260 */
261
262/* intel's icc/ecc compiler */
263#if (defined(__EDG_VERSION) || defined(__ECC)) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
264typedef unsigned long ticks;
265#include <ia64intrin.h>
266
267static __inline__ ticks getticks(void)
268{
269 return __getReg(_IA64_REG_AR_ITC);
270}
271
272INLINE_ELAPSED(__inline__)
273
274#define HAVE_TICK_COUNTER
275#endif
276
277/* gcc */
278#if defined(__GNUC__) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
279typedef unsigned long ticks;
280
281static __inline__ ticks getticks(void)
282{
283 ticks ret;
284
285 __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret));
286 return ret;
287}
288
289INLINE_ELAPSED(__inline__)
290
291#define HAVE_TICK_COUNTER
292#endif
293
294/* HP/UX IA64 compiler, courtesy Teresa L. Johnson: */
295#if defined(__hpux) && defined(__ia64) && !defined(HAVE_TICK_COUNTER)
296#include <machine/sys/inline.h>
297typedef unsigned long ticks;
298
299static inline ticks getticks(void)
300{
301 ticks ret;
302
303 ret = _Asm_mov_from_ar (_AREG_ITC);
304 return ret;
305}
306
307INLINE_ELAPSED(inline)
308
309#define HAVE_TICK_COUNTER
310#endif
311
312/* Microsoft Visual C++ */
313#if defined(_MSC_VER) && defined(_M_IA64) && !defined(HAVE_TICK_COUNTER)
314typedef unsigned __int64 ticks;
315
316# ifdef __cplusplus
317extern "C"
318# endif
319ticks __getReg(int whichReg);
320#pragma intrinsic(__getReg)
321
322static __inline ticks getticks(void)
323{
324 volatile ticks temp;
325 temp = __getReg(3116);
326 return temp;
327}
328
329INLINE_ELAPSED(inline)
330
331#define HAVE_TICK_COUNTER
332#endif
333
334/*----------------------------------------------------------------*/
335/*
336 * PA-RISC cycle counter
337 */
338#if (defined(__hppa__) || defined(__hppa)) && !defined(HAVE_TICK_COUNTER)
339typedef unsigned long ticks;
340
341# ifdef __GNUC__
342static __inline__ ticks getticks(void)
343{
344 ticks ret;
345
346 __asm__ __volatile__("mfctl 16, %0": "=r" (ret));
347 /* no input, nothing else clobbered */
348 return ret;
349}
350# else
351# include <machine/inline.h>
352static inline unsigned long getticks(void)
353{
354 register ticks ret;
355 _MFCTL(16, ret);
356 return ret;
357}
358# endif
359
360INLINE_ELAPSED(inline)
361
362#define HAVE_TICK_COUNTER
363#endif
364
365/*----------------------------------------------------------------*/
366/* S390, courtesy of James Treacy */
367#if defined(__GNUC__) && defined(__s390__) && !defined(HAVE_TICK_COUNTER)
368typedef unsigned long long ticks;
369
370static __inline__ ticks getticks(void)
371{
372 ticks cycles;
373 __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc");
374 return cycles;
375}
376
377INLINE_ELAPSED(__inline__)
378
379#define HAVE_TICK_COUNTER
380#endif
381/*----------------------------------------------------------------*/
382#if defined(__GNUC__) && defined(__alpha__) && !defined(HAVE_TICK_COUNTER)
383/*
384 * The 32-bit cycle counter on alpha overflows pretty quickly,
385 * unfortunately. A 1GHz machine overflows in 4 seconds.
386 */
387typedef unsigned int ticks;
388
389static __inline__ ticks getticks(void)
390{
391 unsigned long cc;
392 __asm__ __volatile__ ("rpcc %0" : "=r"(cc));
393 return (cc & 0xFFFFFFFF);
394}
395
396INLINE_ELAPSED(__inline__)
397
398#define HAVE_TICK_COUNTER
399#endif
400
401/*----------------------------------------------------------------*/
402#if defined(__GNUC__) && defined(__sparc_v9__) && !defined(HAVE_TICK_COUNTER)
403typedef unsigned long ticks;
404
405static __inline__ ticks getticks(void)
406{
407 ticks ret;
408 __asm__ __volatile__("rd %%tick, %0" : "=r" (ret));
409 return ret;
410}
411
412INLINE_ELAPSED(__inline__)
413
414#define HAVE_TICK_COUNTER
415#endif
416
417/*----------------------------------------------------------------*/
418#if (defined(__DECC) || defined(__DECCXX)) && defined(__alpha) && defined(HAVE_C_ASM_H) && !defined(HAVE_TICK_COUNTER)
419# include <c_asm.h>
420typedef unsigned int ticks;
421
422static __inline ticks getticks(void)
423{
424 unsigned long cc;
425 cc = asm("rpcc %v0");
426 return (cc & 0xFFFFFFFF);
427}
428
429INLINE_ELAPSED(__inline)
430
431#define HAVE_TICK_COUNTER
432#endif
433/*----------------------------------------------------------------*/
434/* SGI/Irix */
435#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_SGI_CYCLE) && !defined(HAVE_TICK_COUNTER)
436typedef struct timespec ticks;
437
438static inline ticks getticks(void)
439{
440 struct timespec t;
441 clock_gettime(CLOCK_SGI_CYCLE, &t);
442 return t;
443}
444
445static inline double elapsed(ticks t1, ticks t0)
446{
447 return ((double)t1.tv_sec - (double)t0.tv_sec) * 1.0E9 +
448 ((double)t1.tv_nsec - (double)t0.tv_nsec);
449}
450#define HAVE_TICK_COUNTER
451#endif
452
453/*----------------------------------------------------------------*/
454/* Cray UNICOS _rtc() intrinsic function */
455#if defined(HAVE__RTC) && !defined(HAVE_TICK_COUNTER)
456#ifdef HAVE_INTRINSICS_H
457# include <intrinsics.h>
458#endif
459
460typedef long long ticks;
461
462#define getticks _rtc
463
464INLINE_ELAPSED(inline)
465
466#define HAVE_TICK_COUNTER
467#endif
468
469/*----------------------------------------------------------------*/
470/* MIPS ZBus */
471#if defined(HAVE_MIPS_ZBUS_TIMER) && HAVE_MIPS_ZBUS_TIMER
472#if defined(__mips__) && !defined(HAVE_TICK_COUNTER)
473#include <sys/mman.h>
474#include <unistd.h>
475#include <fcntl.h>
476
477typedef uint64_t ticks;
478
479static inline ticks getticks(void)
480{
481 static uint64_t* addr = 0;
482
483 if (addr == 0)
484 {
485 uint32_t rq_addr = 0x10030000;
486 int fd;
487 int pgsize;
488
489 pgsize = getpagesize();
490 fd = open ("/dev/mem", O_RDONLY | O_SYNC, 0);
491 if (fd < 0) {
492 perror("open");
493 return NULL;
494 }
495 addr = mmap(0, pgsize, PROT_READ, MAP_SHARED, fd, rq_addr);
496 close(fd);
497 if (addr == (uint64_t *)-1) {
498 perror("mmap");
499 return NULL;
500 }
501 }
502
503 return *addr;
504}
505
506INLINE_ELAPSED(inline)
507
508#define HAVE_TICK_COUNTER
509#endif
510#endif /* HAVE_MIPS_ZBUS_TIMER */
511