compiler.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
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 /* Check for availability of compiler optimizations and directives. */
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   /* The compiler gives this stride in a struct. */
00270   static const int NATURAL_STRIDE =
00271       (sizeof(S) > sizeof(T)) ? (sizeof(S) - sizeof(T)) : sizeof(T);
00272   /* This power-of-two stride is likely to be faster. */
00273   static const int PREFERRED_STRIDE =
00274       sizeof(T) >= 8 ? 8 : sizeof(T) >= 4 ? 4 : 0;
00275   /* Use the larger of the two. */
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 /* TODO: confirm correct */
00297 
00306 #define stride_align_max(num) \
00307     (((size_t)(num) + MAX_STRIDE - 1) & ~(size_t)(MAX_STRIDE - 1))
00308 
00309 /* Fill possibly lacking definition for member memory offsets. */
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 /* BASE_COMPILER_H */
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3