beifg: Simplify the quite complicated way to divide a number by 2 in be_ifg_stat().
[libfirm] / ir / adt / util.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @date   31.05.2005
9  * @author Sebastian Hack
10  * @brief  Miscellaneous utility macros.
11  */
12 #ifndef FIRM_ADT_UTIL_H
13 #define FIRM_ADT_UTIL_H
14
15 #include <stddef.h>
16
17 /**
18  * Make pointer to the struct from a pointer to a member of that struct.
19  * @param ptr     The pointer to the member.
20  * @param type    The type of the struct.
21  * @param member  The name of the member.
22  * @return        A pointer to the struct member is in.
23  */
24 #define firm_container_of(ptr, type, member) \
25         ((type *) ((char *) (ptr) - offsetof(type, member)))
26
27 /**
28  * Returns size of a static array. Warning: This returns invalid values for
29  * dynamically allocated arrays.
30  *
31  * @param a    static array
32  */
33 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
34
35 #undef MIN
36 #undef MAX
37 #define MAX(x, y) ((x) > (y) ? (x) : (y))
38 #define MIN(x, y) ((x) < (y) ? (x) : (y))
39
40 /**
41  * Three valued compare as demanded by e.g. qsort(3)
42  * @param c A number.
43  * @param d Another number.
44  * @return 0 if c == d, -1 if c < d, 1 if c > d.
45  */
46 #define QSORT_CMP(c, d) (((c) > (d)) - ((c) < (d)))
47
48 /**
49  * convert an integer into pointer
50  */
51 #define INT_TO_PTR(v)   ((void *)((char *)0 + (v)))
52
53 /**
54  * convert a pointer into an integer
55  */
56 #define PTR_TO_INT(v)   (((char *)(v) - (char *)0))
57
58 #endif