e93d5e66aa6913c92fd4cd3824626d044866ef66
[libfirm] / ir / stat / stat_timing.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   OS abstraction from time measurement
23  * @author  Sebastian Hack, Michael Beck, Matthias Braun
24  * @version $Id$
25  */
26 #ifndef FIRM_STAT_TIMING_H
27 #define FIRM_STAT_TIMING_H
28
29 #ifdef __i386__
30
31 #ifdef __GNUC__
32
33 typedef unsigned long long timing_ticks_t;
34 static inline timing_ticks_t __timing_ticks(void) { timing_ticks_t result; __asm__ __volatile__ ("rdtsc" : "=A" (result)); return result; }
35 #else
36 #if define _MSC_VER
37 /* win32 implementation using rdtsc */
38 typedef unsigned __int64 timing_ticks_t;
39 static __inline timing_ticks_t __timing_ticks(void) { __asm { rdtsc } }
40 #else
41 #error need a 64bit int type
42 #endif
43 #endif
44
45 #define timing_ticks(t)              ((t) = __timing_ticks())
46 #define timing_ticks_init(t)         ((t) = 0)
47 #define timing_ticks_cmp(a, b, cmp)  ((a) cmp (b))
48 #define timing_ticks_sub(r, a)       ((r) = (r) - (a))
49 #define timing_ticks_add(r, a)       ((r) = (r) + (a))
50 #define timing_ticks_ulong(t)        ((unsigned long) (t))
51 #define timing_ticks_dbl(t)          ((double) (t))
52
53 #else /* !__i386__ */
54
55 #include <sys/time.h>
56
57 typedef struct timeval timing_ticks_t;
58 #define timing_ticks(t)              (gettimeofday(&(t), NULL))
59 #define timing_ticks_init(t)         ((t).tv_sec = 0, (t).tv_usec = 0)
60
61 /*
62  * This shamelessly stolen and modified from glibc's
63  * /usr/include/sys/time.h
64  */
65 #define timing_ticks_cmp(a, b, CMP)   \
66   (((a).tv_sec == (b).tv_sec) ?           \
67    ((a).tv_usec CMP (b).tv_usec) :    \
68    ((a).tv_sec CMP (b).tv_sec))
69
70 #define timing_ticks_add(r, a)                       \
71         do {                                                                     \
72                 (r).tv_sec = (r).tv_sec + (a).tv_sec;        \
73                 (r).tv_usec = (r).tv_usec + (a).tv_usec;     \
74                 if ((r).tv_usec >= 1000000) {                        \
75                         ++(r).tv_sec;                            \
76                         (r).tv_usec -= 1000000;                  \
77                 }                                                                                \
78         } while (0)
79
80 #define timing_ticks_sub(r, a)                        \
81         do {                                                                              \
82                 (r).tv_sec = (r).tv_sec - (a).tv_sec;         \
83                 (r).tv_usec = (r).tv_usec - (a).tv_usec;      \
84                 if ((r).tv_usec < 0) {                                        \
85                         --(r).tv_sec;                                                 \
86                         (r).tv_usec += 1000000;                                   \
87                 }                                                                                 \
88         } while (0)
89
90 #define timing_ticks_ulong(t)        ((unsigned long) ((t).tv_usec + 1000000 * (t).tv_sec))
91 #define timing_ticks_dbl(t)          (((t).tv_usec + 1000000.0 * (t).tv_sec))
92
93 #endif
94
95 void timing_enter_max_prio(void);
96 void timing_leave_max_prio(void);
97
98 #endif