compiler.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00040 #ifndef BASE_COMPILER_H
00041 #define BASE_COMPILER_H
00042
00048 #ifdef __cplusplus
00049 #define EXTERN_C_BEGIN extern "C" {
00050 #else
00051 #define EXTERN_C_BEGIN
00052 #endif
00053
00059 #ifdef __cplusplus
00060 #define EXTERN_C_END };
00061 #else
00062 #define EXTERN_C_END
00063 #endif
00064
00065
00066 #if !defined(__GNUC__) && !defined(__INTEL_COMPILER)
00067 #warning Unknown compiler; optimizations and directives disabled.
00068 #ifndef NO_COMPILER_DEFS
00069 #define NO_COMPILER_DEFS
00070 #endif
00071 #endif
00072
00073
00074
00084 #ifndef NO_COMPILER_DEFS
00085 #define expect(expr, value) (__builtin_expect((expr), (value)))
00086 #else
00087 #define expect(expr, value) (expr)
00088 #endif
00089
00108 #define likely(cond) expect(!!(cond), 1)
00109
00128 #define unlikely(cond) expect(!!(cond), 0)
00129
00131 #ifndef NO_COMPILER_DEFS
00132 #define IS_CONST_EXPR(expr) (__builtin_constant_p(expr))
00133 #else
00134 #define IS_CONST_EXPR(expr) 0
00135 #endif
00136
00137
00138
00151 #ifndef NO_COMPILER_DEFS
00152 #define COMPILER_NO_RETURN __attribute__((noreturn))
00153 #else
00154 #define COMPILER_NO_RETURN
00155 #endif
00156
00175 #ifndef NO_COMPILER_DEFS
00176 #define COMPILER_PRINTF(format_arg, dotdotdot_arg) \
00177 __attribute__((format(printf, format_arg, dotdotdot_arg)))
00178 #else
00179 #define COMPILER_PRINTF(format_arg, dotdotdot_arg)
00180 #endif
00181
00199 #ifndef NO_COMPILER_DEFS
00200 #define COMPILER_FUNCTIONAL __attribute__((const))
00201 #else
00202 #define COMPILER_FUNCTIONAL
00203 #endif
00204
00210 #ifndef NO_COMPILER_DEFS
00211 #define COMPILER_NO_INLINE __attribute__((noinline))
00212 #else
00213 #define COMPILER_NO_INLINE
00214 #endif
00215
00224 #if !defined(NO_COMPILER_DEFS) && !defined(NO_DEPRECATION_WARNINGS)
00225 #define COMPILER_DEPRECATED __attribute__((deprecated))
00226 #else
00227 #define COMPILER_DEPRECATED
00228 #endif
00229
00242 #if !defined(NO_COMPILER_DEFS) && !defined(NO_DEPRECATION_WARNINGS)
00243 #define COMPILER_DEPRECATED_MSG(msg) COMPILER_DEPRECATED
00244 #else
00245 #define COMPILER_DEPRECATED_MSG(msg)
00246 #endif
00247
00248
00249
00264 #ifdef __cplusplus
00265 #define strideof(T) (compiler_strideof<T>::STRIDE)
00266 template <typename T>
00267 struct compiler_strideof {
00268 struct S {T x; char c;};
00269
00270 static const int NATURAL_STRIDE =
00271 (sizeof(S) > sizeof(T)) ? (sizeof(S) - sizeof(T)) : sizeof(T);
00272
00273 static const int PREFERRED_STRIDE =
00274 sizeof(T) >= 8 ? 8 : sizeof(T) >= 4 ? 4 : 0;
00275
00276 enum { STRIDE = NATURAL_STRIDE > PREFERRED_STRIDE
00277 ? NATURAL_STRIDE : PREFERRED_STRIDE };
00278 };
00279 #else
00280 #define strideof(T) (sizeof(struct {T x; char c;}) - sizeof(T))
00281 #endif
00282
00292 #define stride_align(num, T) \
00293 (((size_t)(num) + strideof(T) - 1) / strideof(T) * strideof(T))
00294
00296 #define MAX_STRIDE 16
00297
00306 #define stride_align_max(num) \
00307 (((size_t)(num) + MAX_STRIDE - 1) & ~(size_t)(MAX_STRIDE - 1))
00308
00309
00310 #ifndef offsetof
00311 #define offsetof(S, member) ((size_t)(&((S const *)0)->member))
00312 #endif
00313
00314
00315
00317 #ifdef __cplusplus
00318 #define COMPILER_CAST(cast, T, val) (cast< T >(val))
00319 #else
00320 #define COMPILER_CAST(cast, T, val) ((T)(val))
00321 #endif
00322
00328 #define STATIC_CAST(T, val) COMPILER_CAST(static_cast, T, val)
00329
00335 #define REINTERPRET_CAST(T, val) COMPILER_CAST(reinterpret_cast, T, val)
00336
00337
00338
00340 #ifdef __cplusplus
00341 #define CONST_REF const &
00342 #else
00343 #define CONST_REF const *
00344 #endif
00345
00347 #ifdef __cplusplus
00348 #define REF &
00349 #else
00350 #define REF *
00351 #endif
00352
00353
00354
00355 #endif