2 Copyright (C) 2001 Paul Davis
3 Code derived from various headers from the Linux kernel
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifndef __ardour_cycles_h__
23 #define __ardour_cycles_h__
27 #if defined(__i386__) || defined(__x86_64__)
30 * Standard way to access the cycle counter on i586+ CPUs.
31 * Currently only used on SMP.
33 * If you really have a SMP machine with i486 chips or older,
34 * compile for that, and this will just always return zero.
35 * That's ok, it just means that the nicer scheduling heuristics
38 * We only use the low 32 bits, and we'd simply better make sure
39 * that we reschedule before that wraps. Scheduling at least every
40 * four billion cycles just basically sounds like a good idea,
41 * regardless of how fast the machine is.
43 typedef uint64_t cycles_t;
45 extern cycles_t cacheflush_time;
47 #define rdtscll(val) \
48 __asm__ __volatile__("rdtsc" : "=A" (val))
50 static inline cycles_t get_cycles (void)
58 #elif defined(__powerpc__)
60 #define CPU_FTR_601 0x00000100
62 typedef uint32_t cycles_t;
65 * For the "cycle" counter we use the timebase lower half.
66 * Currently only used on SMP.
69 extern cycles_t cacheflush_time;
71 static inline cycles_t get_cycles(void)
78 ".section __ftr_fixup,\"a\"\n"
84 : "=r" (ret) : "i" (CPU_FTR_601));
88 #elif defined(__ia64__)
91 typedef uint32_t cycles_t;
92 static inline cycles_t
96 __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret));
100 #elif defined(__alpha__)
104 * Standard way to access the cycle counter.
105 * Currently only used on SMP for scheduling.
107 * Only the low 32 bits are available as a continuously counting entity.
108 * But this only means we'll force a reschedule every 8 seconds or so,
109 * which isn't an evil thing.
112 typedef uint32_t cycles_t;
113 static inline cycles_t get_cycles (void)
116 __asm__ __volatile__ ("rpcc %0" : "=r"(ret));
120 #elif defined(__s390__)
123 typedef uint32_t long cycles_t;
124 static inline cycles_t get_cycles(void)
127 __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc");
131 #elif defined(__hppa__)
134 #define mfctl(reg) ({ \
136 __asm__ __volatile__( \
137 "mfctl " #reg ",%0" : \
143 typedef uint32_t cycles_t;
144 static inline cycles_t get_cycles (void)
149 #elif defined(__mips__)
153 * Standard way to access the cycle counter.
154 * Currently only used on SMP for scheduling.
156 * Only the low 32 bits are available as a continuously counting entity.
157 * But this only means we'll force a reschedule every 8 seconds or so,
158 * which isn't an evil thing.
160 * We know that all SMP capable CPUs have cycle counters.
163 #define __read_32bit_c0_register(source, sel) \
166 __asm__ __volatile__( \
167 "mfc0\t%0, " #source "\n\t" \
170 __asm__ __volatile__( \
172 "mfc0\t%0, " #source ", " #sel "\n\t" \
178 /* #define CP0_COUNT $9 */
179 #define read_c0_count() __read_32bit_c0_register($9, 0)
181 typedef uint32_t cycles_t;
182 static inline cycles_t get_cycles (void)
184 return read_c0_count();
188 #elif defined(__APPLE__)
190 #ifdef HAVE_COREAUDIO
191 #include <CoreAudio/HostTime.h>
192 #else // Due to MacTypes.h and libgnomecanvasmm Rect conflict
193 typedef unsigned long long UInt64;
196 AudioGetCurrentHostTime();
199 AudioConvertHostTimeToNanos(UInt64 inHostTime);
202 typedef UInt64 cycles_t;
203 static inline cycles_t get_cycles (void)
205 UInt64 time = AudioGetCurrentHostTime();
206 return AudioConvertHostTimeToNanos(time);
212 /* debian: sparc, arm, m68k */
214 #warning You are compiling libardour on a platform for which ardour/cycles.h needs work
216 #include <sys/time.h>
218 typedef long cycles_t;
220 extern cycles_t cacheflush_time;
222 static inline cycles_t get_cycles(void)
225 gettimeofday (&tv, NULL);
232 #endif /* __ardour_cycles_h__ */