util/macros: Move DIV_ROUND_UP to util/macros.h
[mesa.git] / src / util / macros.h
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef UTIL_MACROS_H
25 #define UTIL_MACROS_H
26
27 /* Compute the size of an array */
28 #ifndef ARRAY_SIZE
29 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
30 #endif
31
32 /* For compatibility with Clang's __has_builtin() */
33 #ifndef __has_builtin
34 # define __has_builtin(x) 0
35 #endif
36
37 /**
38 * __builtin_expect macros
39 */
40 #if !defined(HAVE___BUILTIN_EXPECT)
41 # define __builtin_expect(x, y) (x)
42 #endif
43
44 #ifndef likely
45 # ifdef HAVE___BUILTIN_EXPECT
46 # define likely(x) __builtin_expect(!!(x), 1)
47 # define unlikely(x) __builtin_expect(!!(x), 0)
48 # else
49 # define likely(x) (x)
50 # define unlikely(x) (x)
51 # endif
52 #endif
53
54
55 /**
56 * Static (compile-time) assertion.
57 * Basically, use COND to dimension an array. If COND is false/zero the
58 * array size will be -1 and we'll get a compilation error.
59 */
60 #define STATIC_ASSERT(COND) \
61 do { \
62 (void) sizeof(char [1 - 2*!(COND)]); \
63 } while (0)
64
65
66 /**
67 * Unreachable macro. Useful for suppressing "control reaches end of non-void
68 * function" warnings.
69 */
70 #ifdef HAVE___BUILTIN_UNREACHABLE
71 #define unreachable(str) \
72 do { \
73 assert(!str); \
74 __builtin_unreachable(); \
75 } while (0)
76 #elif defined (_MSC_VER)
77 #define unreachable(str) \
78 do { \
79 assert(!str); \
80 __assume(0); \
81 } while (0)
82 #else
83 #define unreachable(str) assert(!str)
84 #endif
85
86 /**
87 * Assume macro. Useful for expressing our assumptions to the compiler,
88 * typically for purposes of silencing warnings.
89 */
90 #if __has_builtin(__builtin_assume)
91 #define assume(expr) \
92 do { \
93 assert(expr); \
94 __builtin_assume(expr); \
95 } while (0)
96 #elif defined HAVE___BUILTIN_UNREACHABLE
97 #define assume(expr) ((expr) ? ((void) 0) \
98 : (assert(!"assumption failed"), \
99 __builtin_unreachable()))
100 #elif defined (_MSC_VER)
101 #define assume(expr) __assume(expr)
102 #else
103 #define assume(expr) assert(expr)
104 #endif
105
106 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
107 #define FLATTEN __attribute__((__flatten__))
108 #else
109 #define FLATTEN
110 #endif
111
112 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
113 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
114 #else
115 #define PRINTFLIKE(f, a)
116 #endif
117
118 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
119 #define MALLOCLIKE __attribute__((__malloc__))
120 #else
121 #define MALLOCLIKE
122 #endif
123
124 /* Used to optionally mark structures with misaligned elements or size as
125 * packed, to trade off performance for space.
126 */
127 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
128 #define PACKED __attribute__((__packed__))
129 #else
130 #define PACKED
131 #endif
132
133 #ifdef __cplusplus
134 /**
135 * Macro function that evaluates to true if T is a trivially
136 * destructible type -- that is, if its (non-virtual) destructor
137 * performs no action and all member variables and base classes are
138 * trivially destructible themselves.
139 */
140 # if defined(__GNUC__)
141 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
142 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
143 # endif
144 # elif (defined(__clang__) && defined(__has_feature))
145 # if __has_feature(has_trivial_destructor)
146 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
147 # endif
148 # endif
149 # ifndef HAS_TRIVIAL_DESTRUCTOR
150 /* It's always safe (if inefficient) to assume that a
151 * destructor is non-trivial.
152 */
153 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
154 # endif
155 #endif
156
157 /**
158 * PUBLIC/USED macros
159 *
160 * If we build the library with gcc's -fvisibility=hidden flag, we'll
161 * use the PUBLIC macro to mark functions that are to be exported.
162 *
163 * We also need to define a USED attribute, so the optimizer doesn't
164 * inline a static function that we later use in an alias. - ajax
165 */
166 #ifndef PUBLIC
167 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
168 # define PUBLIC __attribute__((visibility("default")))
169 # define USED __attribute__((used))
170 # elif defined(_MSC_VER)
171 # define PUBLIC __declspec(dllexport)
172 # define USED
173 # else
174 # define PUBLIC
175 # define USED
176 # endif
177 #endif
178
179 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
180 #define UNUSED __attribute__((unused))
181 #else
182 #define UNUSED
183 #endif
184
185 /** Compute ceiling of integer quotient of A divided by B. */
186 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
187
188 #endif /* UTIL_MACROS_H */