74bd8bfad88fd0d299c39514b4c5f4eea04d33fd
2 * Copyright © 2014 Intel Corporation
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:
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
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
29 /* Compute the size of an array */
31 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
34 /* For compatibility with Clang's __has_builtin() */
36 # define __has_builtin(x) 0
40 * __builtin_expect macros
42 #if !defined(HAVE___BUILTIN_EXPECT)
43 # define __builtin_expect(x, y) (x)
47 # ifdef HAVE___BUILTIN_EXPECT
48 # define likely(x) __builtin_expect(!!(x), 1)
49 # define unlikely(x) __builtin_expect(!!(x), 0)
51 # define likely(x) (x)
52 # define unlikely(x) (x)
58 * Static (compile-time) assertion.
59 * Basically, use COND to dimension an array. If COND is false/zero the
60 * array size will be -1 and we'll get a compilation error.
62 #define STATIC_ASSERT(COND) \
64 (void) sizeof(char [1 - 2*!(COND)]); \
69 * Unreachable macro. Useful for suppressing "control reaches end of non-void
72 #ifdef HAVE___BUILTIN_UNREACHABLE
73 #define unreachable(str) \
76 __builtin_unreachable(); \
78 #elif _MSC_VER >= 1200
79 #define unreachable(str) \
87 #define unreachable(str) assert(!str)
91 * Assume macro. Useful for expressing our assumptions to the compiler,
92 * typically for purposes of silencing warnings.
94 #if __has_builtin(__builtin_assume)
95 #define assume(expr) \
98 __builtin_assume(expr); \
100 #elif defined HAVE___BUILTIN_UNREACHABLE
101 #define assume(expr) ((expr) ? ((void) 0) \
102 : (assert(!"assumption failed"), \
103 __builtin_unreachable()))
104 #elif _MSC_VER >= 1200
105 #define assume(expr) __assume(expr)
107 #define assume(expr) assert(expr)
110 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
111 #define FLATTEN __attribute__((__flatten__))
116 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
117 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
119 #define PRINTFLIKE(f, a)
122 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
123 #define MALLOCLIKE __attribute__((__malloc__))
128 /* Used to optionally mark structures with misaligned elements or size as
129 * packed, to trade off performance for space.
131 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
132 #define PACKED __attribute__((__packed__))
139 * Macro function that evaluates to true if T is a trivially
140 * destructible type -- that is, if its (non-virtual) destructor
141 * performs no action and all member variables and base classes are
142 * trivially destructible themselves.
144 # if defined(__GNUC__)
145 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
146 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
148 # elif (defined(__clang__) && defined(__has_feature))
149 # if __has_feature(has_trivial_destructor)
150 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
153 # ifndef HAS_TRIVIAL_DESTRUCTOR
154 /* It's always safe (if inefficient) to assume that a
155 * destructor is non-trivial.
157 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
161 /* The fallbacks below don't work correctly in C++ and properly detecting
162 * FP_NORMAL in C++ is hard. Since we don't use fpclassify in any C++ code
163 * at the moment, we can just predicate this whole thing by not being in
164 * C++ and we shoudld be ok. If we ever want to use fpclassify in a C++
165 * file, we will have to revisit this.
170 /* ISO C99 says that fpclassify is a macro. Assume that any implementation
171 * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
173 #elif defined(_MSC_VER)
174 /* Not required on VS2013 and above. */
175 /* Oddly, the fpclassify() function doesn't exist in such a form
176 * on MSVC. This is an implementation using slightly different
177 * lower-level Windows functions.
181 static inline enum {FP_NAN
, FP_INFINITE
, FP_ZERO
, FP_SUBNORMAL
, FP_NORMAL
}
184 switch(_fpclass(x
)) {
185 case _FPCLASS_SNAN
: /* signaling NaN */
186 case _FPCLASS_QNAN
: /* quiet NaN */
188 case _FPCLASS_NINF
: /* negative infinity */
189 case _FPCLASS_PINF
: /* positive infinity */
191 case _FPCLASS_NN
: /* negative normal */
192 case _FPCLASS_PN
: /* positive normal */
194 case _FPCLASS_ND
: /* negative denormalized */
195 case _FPCLASS_PD
: /* positive denormalized */
197 case _FPCLASS_NZ
: /* negative zero */
198 case _FPCLASS_PZ
: /* positive zero */
201 /* Should never get here; but if we do, this will guarantee
202 * that the pattern is not treated like a number.
210 static inline enum {FP_NAN
, FP_INFINITE
, FP_ZERO
, FP_SUBNORMAL
, FP_NORMAL
}
213 /* XXX do something better someday */
219 #endif /* __cplusplus */
221 #endif /* UTIL_MACROS_H */