Macros.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_MACROS_H
00027 #define EIGEN_MACROS_H
00028 
00029 #define EIGEN_WORLD_VERSION 3
00030 #define EIGEN_MAJOR_VERSION 0
00031 #define EIGEN_MINOR_VERSION 93
00032 
00033 #define EIGEN_VERSION_AT_LEAST(x,y,z) (EIGEN_WORLD_VERSION>x || (EIGEN_WORLD_VERSION>=x && \
00034                                       (EIGEN_MAJOR_VERSION>y || (EIGEN_MAJOR_VERSION>=y && \
00035                                                                  EIGEN_MINOR_VERSION>=z))))
00036 #ifdef __GNUC__
00037   #define EIGEN_GNUC_AT_LEAST(x,y) ((__GNUC__==x && __GNUC_MINOR__>=y) || __GNUC__>x)
00038 #else
00039   #define EIGEN_GNUC_AT_LEAST(x,y) 0
00040 #endif
00041  
00042 #ifdef __GNUC__
00043   #define EIGEN_GNUC_AT_MOST(x,y) ((__GNUC__==x && __GNUC_MINOR__<=y) || __GNUC__<x)
00044 #else
00045   #define EIGEN_GNUC_AT_MOST(x,y) 0
00046 #endif
00047 
00048 #if EIGEN_GNUC_AT_MOST(4,3) && !defined(__clang__)
00049   // see bug 89
00050   #define EIGEN_SAFE_TO_USE_STANDARD_ASSERT_MACRO 0
00051 #else
00052   #define EIGEN_SAFE_TO_USE_STANDARD_ASSERT_MACRO 1
00053 #endif
00054 
00055 #if defined(__GNUC__) && (__GNUC__ <= 3)
00056 #define EIGEN_GCC3_OR_OLDER 1
00057 #else
00058 #define EIGEN_GCC3_OR_OLDER 0
00059 #endif
00060 
00061 // 16 byte alignment is only useful for vectorization. Since it affects the ABI, we need to enable
00062 // 16 byte alignment on all platforms where vectorization might be enabled. In theory we could always
00063 // enable alignment, but it can be a cause of problems on some platforms, so we just disable it in
00064 // certain common platform (compiler+architecture combinations) to avoid these problems.
00065 // Only static alignment is really problematic (relies on nonstandard compiler extensions that don't
00066 // work everywhere, for example don't work on GCC/ARM), try to keep heap alignment even
00067 // when we have to disable static alignment.
00068 #if defined(__GNUC__) && !(defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || defined(__ppc__) || defined(__ia64__))
00069 #define EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT 1
00070 #else
00071 #define EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT 0
00072 #endif
00073 
00074 // static alignment is completely disabled with GCC 3, Sun Studio, and QCC/QNX
00075 #if !EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT \
00076  && !EIGEN_GCC3_OR_OLDER \
00077  && !defined(__SUNPRO_CC) \
00078  && !defined(__QNXNTO__)
00079   #define EIGEN_ARCH_WANTS_STACK_ALIGNMENT 1
00080 #else
00081   #define EIGEN_ARCH_WANTS_STACK_ALIGNMENT 0
00082 #endif
00083 
00084 #ifdef EIGEN_DONT_ALIGN
00085   #ifndef EIGEN_DONT_ALIGN_STATICALLY
00086     #define EIGEN_DONT_ALIGN_STATICALLY
00087   #endif
00088   #define EIGEN_ALIGN 0
00089 #else
00090   #define EIGEN_ALIGN 1
00091 #endif
00092 
00093 // EIGEN_ALIGN_STATICALLY is the true test whether we want to align arrays on the stack or not. It takes into account both the user choice to explicitly disable
00094 // alignment (EIGEN_DONT_ALIGN_STATICALLY) and the architecture config (EIGEN_ARCH_WANTS_STACK_ALIGNMENT). Henceforth, only EIGEN_ALIGN_STATICALLY should be used.
00095 #if EIGEN_ARCH_WANTS_STACK_ALIGNMENT && !defined(EIGEN_DONT_ALIGN_STATICALLY)
00096   #define EIGEN_ALIGN_STATICALLY 1
00097 #else
00098   #define EIGEN_ALIGN_STATICALLY 0
00099   #ifndef EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
00100     #define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
00101   #endif
00102 #endif
00103 
00104 #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR
00105 #define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION RowMajor
00106 #else
00107 #define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ColMajor
00108 #endif
00109 
00110 #ifndef EIGEN_DEFAULT_DENSE_INDEX_TYPE
00111 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE std::ptrdiff_t
00112 #endif
00113 
00119 #ifndef EIGEN_FAST_MATH
00120 #define EIGEN_FAST_MATH 1
00121 #endif
00122 
00123 #define EIGEN_DEBUG_VAR(x) std::cerr << #x << " = " << x << std::endl;
00124 
00125 // concatenate two tokens
00126 #define EIGEN_CAT2(a,b) a ## b
00127 #define EIGEN_CAT(a,b) EIGEN_CAT2(a,b)
00128 
00129 // convert a token to a string
00130 #define EIGEN_MAKESTRING2(a) #a
00131 #define EIGEN_MAKESTRING(a) EIGEN_MAKESTRING2(a)
00132 
00133 #if EIGEN_GNUC_AT_LEAST(4,1) && !defined(__clang__) && !defined(__INTEL_COMPILER)
00134 #define EIGEN_FLATTEN_ATTRIB __attribute__((flatten))
00135 #else
00136 #define EIGEN_FLATTEN_ATTRIB
00137 #endif
00138 
00139 // EIGEN_STRONG_INLINE is a stronger version of the inline, using __forceinline on MSVC,
00140 // but it still doesn't use GCC's always_inline. This is useful in (common) situations where MSVC needs forceinline
00141 // but GCC is still doing fine with just inline.
00142 #if (defined _MSC_VER) || (defined __INTEL_COMPILER)
00143 #define EIGEN_STRONG_INLINE __forceinline
00144 #else
00145 #define EIGEN_STRONG_INLINE inline
00146 #endif
00147 
00148 // EIGEN_ALWAYS_INLINE is the stronget, it has the effect of making the function inline and adding every possible
00149 // attribute to maximize inlining. This should only be used when really necessary: in particular,
00150 // it uses __attribute__((always_inline)) on GCC, which most of the time is useless and can severely harm compile times.
00151 // FIXME with the always_inline attribute,
00152 // gcc 3.4.x reports the following compilation error:
00153 //   Eval.h:91: sorry, unimplemented: inlining failed in call to 'const Eigen::Eval<Derived> Eigen::MatrixBase<Scalar, Derived>::eval() const'
00154 //    : function body not available
00155 #if EIGEN_GNUC_AT_LEAST(4,0)
00156 #define EIGEN_ALWAYS_INLINE __attribute__((always_inline)) inline
00157 #else
00158 #define EIGEN_ALWAYS_INLINE EIGEN_STRONG_INLINE
00159 #endif
00160 
00161 #if (defined __GNUC__)
00162 #define EIGEN_DONT_INLINE __attribute__((noinline))
00163 #elif (defined _MSC_VER)
00164 #define EIGEN_DONT_INLINE __declspec(noinline)
00165 #else
00166 #define EIGEN_DONT_INLINE
00167 #endif
00168 
00169 // this macro allows to get rid of linking errors about multiply defined functions.
00170 //  - static is not very good because it prevents definitions from different object files to be merged.
00171 //           So static causes the resulting linked executable to be bloated with multiple copies of the same function.
00172 //  - inline is not perfect either as it unwantedly hints the compiler toward inlining the function.
00173 #define EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
00174 #define EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS inline
00175 
00176 #ifdef NDEBUG
00177 # ifndef EIGEN_NO_DEBUG
00178 #  define EIGEN_NO_DEBUG
00179 # endif
00180 #endif
00181 
00182 // eigen_plain_assert is where we implement the workaround for the assert() bug in GCC <= 4.3, see bug 89
00183 #ifdef EIGEN_NO_DEBUG
00184   #define eigen_plain_assert(x)
00185 #else
00186   #if EIGEN_SAFE_TO_USE_STANDARD_ASSERT_MACRO
00187     namespace Eigen {
00188     namespace internal {
00189     inline bool copy_bool(bool b) { return b; }
00190     }
00191     }
00192     #define eigen_plain_assert(x) assert(x)
00193   #else
00194     // work around bug 89
00195     #include <cstdlib>   // for abort
00196     #include <iostream>  // for std::cerr
00197 
00198     namespace Eigen {
00199     namespace internal {
00200     // trivial function copying a bool. Must be EIGEN_DONT_INLINE, so we implement it after including Eigen headers.
00201     // see bug 89.
00202     namespace {
00203     EIGEN_DONT_INLINE bool copy_bool(bool b) { return b; }
00204     }
00205     inline void assert_fail(const char *condition, const char *function, const char *file, int line)
00206     {
00207       std::cerr << "assertion failed: " << condition << " in function " << function << " at " << file << ":" << line << std::endl;
00208       abort();
00209     }
00210     }
00211     }
00212     #define eigen_plain_assert(x) \
00213       do { \
00214         if(!Eigen::internal::copy_bool(x)) \
00215           Eigen::internal::assert_fail(EIGEN_MAKESTRING(x), __PRETTY_FUNCTION__, __FILE__, __LINE__); \
00216       } while(false)
00217   #endif
00218 #endif
00219 
00220 // eigen_assert can be overridden
00221 #ifndef eigen_assert
00222 #define eigen_assert(x) eigen_plain_assert(x)
00223 #endif
00224 
00225 #ifdef EIGEN_INTERNAL_DEBUGGING
00226 #define eigen_internal_assert(x) eigen_assert(x)
00227 #else
00228 #define eigen_internal_assert(x)
00229 #endif
00230 
00231 #ifdef EIGEN_NO_DEBUG
00232 #define EIGEN_ONLY_USED_FOR_DEBUG(x) (void)x
00233 #else
00234 #define EIGEN_ONLY_USED_FOR_DEBUG(x)
00235 #endif
00236 
00237 #ifndef EIGEN_NO_DEPRECATED_WARNING
00238   #if (defined __GNUC__)
00239     #define EIGEN_DEPRECATED __attribute__((deprecated))
00240   #elif (defined _MSC_VER)
00241     #define EIGEN_DEPRECATED __declspec(deprecated)
00242   #else
00243     #define EIGEN_DEPRECATED
00244   #endif
00245 #else
00246   #define EIGEN_DEPRECATED
00247 #endif
00248 
00249 #if (defined __GNUC__)
00250 #define EIGEN_UNUSED __attribute__((unused))
00251 #else
00252 #define EIGEN_UNUSED
00253 #endif
00254 
00255 // Suppresses 'unused variable' warnings.
00256 #define EIGEN_UNUSED_VARIABLE(var) (void)var;
00257 
00258 #if !defined(EIGEN_ASM_COMMENT) && (defined __GNUC__)
00259 #define EIGEN_ASM_COMMENT(X)  asm("#" X)
00260 #else
00261 #define EIGEN_ASM_COMMENT(X)
00262 #endif
00263 
00264 /* EIGEN_ALIGN_TO_BOUNDARY(n) forces data to be n-byte aligned. This is used to satisfy SIMD requirements.
00265  * However, we do that EVEN if vectorization (EIGEN_VECTORIZE) is disabled,
00266  * so that vectorization doesn't affect binary compatibility.
00267  *
00268  * If we made alignment depend on whether or not EIGEN_VECTORIZE is defined, it would be impossible to link
00269  * vectorized and non-vectorized code.
00270  */
00271 #if (defined __GNUC__) || (defined __PGI) || (defined __IBMCPP__) || (defined __ARMCC_VERSION)
00272   #define EIGEN_ALIGN_TO_BOUNDARY(n) __attribute__((aligned(n)))
00273 #elif (defined _MSC_VER)
00274   #define EIGEN_ALIGN_TO_BOUNDARY(n) __declspec(align(n))
00275 #elif (defined __SUNPRO_CC)
00276   // FIXME not sure about this one:
00277   #define EIGEN_ALIGN_TO_BOUNDARY(n) __attribute__((aligned(n)))
00278 #else
00279   #error Please tell me what is the equivalent of __attribute__((aligned(n))) for your compiler
00280 #endif
00281 
00282 #define EIGEN_ALIGN16 EIGEN_ALIGN_TO_BOUNDARY(16)
00283 
00284 #if EIGEN_ALIGN_STATICALLY
00285 #define EIGEN_USER_ALIGN_TO_BOUNDARY(n) EIGEN_ALIGN_TO_BOUNDARY(n)
00286 #define EIGEN_USER_ALIGN16 EIGEN_ALIGN16
00287 #else
00288 #define EIGEN_USER_ALIGN_TO_BOUNDARY(n)
00289 #define EIGEN_USER_ALIGN16
00290 #endif
00291 
00292 #ifdef EIGEN_DONT_USE_RESTRICT_KEYWORD
00293   #define EIGEN_RESTRICT
00294 #endif
00295 #ifndef EIGEN_RESTRICT
00296   #define EIGEN_RESTRICT __restrict
00297 #endif
00298 
00299 #ifndef EIGEN_STACK_ALLOCATION_LIMIT
00300 #define EIGEN_STACK_ALLOCATION_LIMIT 20000
00301 #endif
00302 
00303 #ifndef EIGEN_DEFAULT_IO_FORMAT
00304 #ifdef EIGEN_MAKING_DOCS
00305 // format used in Eigen's documentation
00306 // needed to define it here as escaping characters in CMake add_definition's argument seems very problematic.
00307 #define EIGEN_DEFAULT_IO_FORMAT Eigen::IOFormat(3, 0, " ", "\n", "", "")
00308 #else
00309 #define EIGEN_DEFAULT_IO_FORMAT Eigen::IOFormat()
00310 #endif
00311 #endif
00312 
00313 // just an empty macro !
00314 #define EIGEN_EMPTY
00315 
00316 #if defined(_MSC_VER) && (!defined(__INTEL_COMPILER))
00317 #define EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) \
00318   using Base::operator =;
00319 #else
00320 #define EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) \
00321   using Base::operator =; \
00322   EIGEN_STRONG_INLINE Derived& operator=(const Derived& other) \
00323   { \
00324     Base::operator=(other); \
00325     return *this; \
00326   }
00327 #endif
00328 
00329 #define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
00330   EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived)
00331 
00340 #define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived) \
00341   typedef typename Eigen::internal::traits<Derived>::Scalar Scalar;  \
00342   typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;  \
00343   typedef typename Base::CoeffReturnType CoeffReturnType;  \
00344   typedef typename Eigen::internal::nested<Derived>::type Nested; \
00345   typedef typename Eigen::internal::traits<Derived>::StorageKind StorageKind; \
00346   typedef typename Eigen::internal::traits<Derived>::Index Index; \
00347   enum { RowsAtCompileTime = Eigen::internal::traits<Derived>::RowsAtCompileTime, \
00348         ColsAtCompileTime = Eigen::internal::traits<Derived>::ColsAtCompileTime, \
00349         Flags = Eigen::internal::traits<Derived>::Flags, \
00350         CoeffReadCost = Eigen::internal::traits<Derived>::CoeffReadCost, \
00351         SizeAtCompileTime = Base::SizeAtCompileTime, \
00352         MaxSizeAtCompileTime = Base::MaxSizeAtCompileTime, \
00353         IsVectorAtCompileTime = Base::IsVectorAtCompileTime };
00354 
00355 
00356 #define EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
00357   typedef typename Eigen::internal::traits<Derived>::Scalar Scalar;  \
00358   typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;  \
00359   typedef typename Base::PacketScalar PacketScalar; \
00360   typedef typename Base::CoeffReturnType CoeffReturnType;  \
00361   typedef typename Eigen::internal::nested<Derived>::type Nested; \
00362   typedef typename Eigen::internal::traits<Derived>::StorageKind StorageKind; \
00363   typedef typename Eigen::internal::traits<Derived>::Index Index; \
00364   enum { RowsAtCompileTime = Eigen::internal::traits<Derived>::RowsAtCompileTime, \
00365         ColsAtCompileTime = Eigen::internal::traits<Derived>::ColsAtCompileTime, \
00366         MaxRowsAtCompileTime = Eigen::internal::traits<Derived>::MaxRowsAtCompileTime, \
00367         MaxColsAtCompileTime = Eigen::internal::traits<Derived>::MaxColsAtCompileTime, \
00368         Flags = Eigen::internal::traits<Derived>::Flags, \
00369         CoeffReadCost = Eigen::internal::traits<Derived>::CoeffReadCost, \
00370         SizeAtCompileTime = Base::SizeAtCompileTime, \
00371         MaxSizeAtCompileTime = Base::MaxSizeAtCompileTime, \
00372         IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
00373   using Base::derived; \
00374   using Base::const_cast_derived;
00375 
00376 
00377 #define EIGEN_PLAIN_ENUM_MIN(a,b) (((int)a <= (int)b) ? (int)a : (int)b)
00378 #define EIGEN_PLAIN_ENUM_MAX(a,b) (((int)a >= (int)b) ? (int)a : (int)b)
00379 
00380 // EIGEN_SIZE_MIN_PREFER_DYNAMIC gives the min between compile-time sizes. 0 has absolute priority, followed by 1,
00381 // followed by Dynamic, followed by other finite values. The reason for giving Dynamic the priority over
00382 // finite values is that min(3, Dynamic) should be Dynamic, since that could be anything between 0 and 3.
00383 #define EIGEN_SIZE_MIN_PREFER_DYNAMIC(a,b) (((int)a == 0 || (int)b == 0) ? 0 \
00384                            : ((int)a == 1 || (int)b == 1) ? 1 \
00385                            : ((int)a == Dynamic || (int)b == Dynamic) ? Dynamic \
00386                            : ((int)a <= (int)b) ? (int)a : (int)b)
00387 
00388 // EIGEN_SIZE_MIN_PREFER_FIXED is a variant of EIGEN_SIZE_MIN_PREFER_DYNAMIC comparing MaxSizes. The difference is that finite values
00389 // now have priority over Dynamic, so that min(3, Dynamic) gives 3. Indeed, whatever the actual value is
00390 // (between 0 and 3), it is not more than 3.
00391 #define EIGEN_SIZE_MIN_PREFER_FIXED(a,b)  (((int)a == 0 || (int)b == 0) ? 0 \
00392                            : ((int)a == 1 || (int)b == 1) ? 1 \
00393                            : ((int)a == Dynamic && (int)b == Dynamic) ? Dynamic \
00394                            : ((int)a == Dynamic) ? (int)b \
00395                            : ((int)b == Dynamic) ? (int)a \
00396                            : ((int)a <= (int)b) ? (int)a : (int)b)
00397 
00398 // see EIGEN_SIZE_MIN_PREFER_DYNAMIC. No need for a separate variant for MaxSizes here.
00399 #define EIGEN_SIZE_MAX(a,b) (((int)a == Dynamic || (int)b == Dynamic) ? Dynamic \
00400                            : ((int)a >= (int)b) ? (int)a : (int)b)
00401 
00402 #define EIGEN_LOGICAL_XOR(a,b) (((a) || (b)) && !((a) && (b)))
00403 
00404 #define EIGEN_IMPLIES(a,b) (!(a) || (b))
00405 
00406 #define EIGEN_MAKE_CWISE_BINARY_OP(METHOD,FUNCTOR) \
00407   template<typename OtherDerived> \
00408   EIGEN_STRONG_INLINE const CwiseBinaryOp<FUNCTOR<Scalar>, const Derived, const OtherDerived> \
00409   (METHOD)(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const \
00410   { \
00411     return CwiseBinaryOp<FUNCTOR<Scalar>, const Derived, const OtherDerived>(derived(), other.derived()); \
00412   }
00413 
00414 // the expression type of a cwise product
00415 #define EIGEN_CWISE_PRODUCT_RETURN_TYPE(LHS,RHS) \
00416     CwiseBinaryOp< \
00417       internal::scalar_product_op< \
00418           typename internal::traits<LHS>::Scalar, \
00419           typename internal::traits<RHS>::Scalar \
00420       >, \
00421       const LHS, \
00422       const RHS \
00423     >
00424 
00425 #endif // EIGEN_MACROS_H