2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief OS abstraction from time measurement
23 * @author Sebastian Hack, Michael Beck, Matthias Braun
25 #ifndef FIRM_STAT_TIMING_H
26 #define FIRM_STAT_TIMING_H
28 #if defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
31 typedef unsigned long long timing_ticks_t;
32 static inline timing_ticks_t __timing_ticks(void)
36 __asm__ volatile("rdtsc" : "=a" (l), "=d" (h));
37 return (timing_ticks_t)h << 32 | l;
40 #elif defined(_MSC_VER)
43 typedef unsigned __int64 timing_ticks_t;
44 static __inline timing_ticks_t __timing_ticks(void) { return __rdtsc(); }
46 #error need a 64bit int type
49 #define timing_ticks(t) ((t) = __timing_ticks())
50 #define timing_ticks_init(t) ((t) = 0)
51 #define timing_ticks_cmp(a, b, cmp) ((a) cmp (b))
52 #define timing_ticks_sub(r, a) ((r) = (r) - (a))
53 #define timing_ticks_add(r, a) ((r) = (r) + (a))
54 #define timing_ticks_ulong(t) ((unsigned long) (t))
55 #define timing_ticks_dbl(t) ((double) (t))
57 #else /* !__i386__ && !__x86_64 */
61 typedef struct timeval timing_ticks_t;
62 #define timing_ticks(t) (gettimeofday(&(t), NULL))
63 #define timing_ticks_init(t) memset(&(t), 0, sizeof(t))
66 * This shamelessly stolen and modified from glibc's
67 * /usr/include/sys/time.h
69 #define timing_ticks_cmp(a, b, CMP) \
70 (((a).tv_sec == (b).tv_sec) ? \
71 ((a).tv_usec CMP (b).tv_usec) : \
72 ((a).tv_sec CMP (b).tv_sec))
74 #define timing_ticks_add(r, a) \
76 (r).tv_sec = (r).tv_sec + (a).tv_sec; \
77 (r).tv_usec = (r).tv_usec + (a).tv_usec; \
78 if ((r).tv_usec >= 1000000) { \
80 (r).tv_usec -= 1000000; \
84 #define timing_ticks_sub(r, a) \
86 (r).tv_sec = (r).tv_sec - (a).tv_sec; \
87 (r).tv_usec = (r).tv_usec - (a).tv_usec; \
88 if ((r).tv_usec < 0) { \
90 (r).tv_usec += 1000000; \
94 #define timing_ticks_ulong(t) ((unsigned long) ((t).tv_usec + 1000000 * (t).tv_sec))
95 #define timing_ticks_dbl(t) (((t).tv_usec + 1000000.0 * (t).tv_sec))
99 void timing_enter_max_prio(void);
100 void timing_leave_max_prio(void);