Macros.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_MACROS_H
27 #define EIGEN_MACROS_H
28 
29 #define EIGEN_WORLD_VERSION 3
30 #define EIGEN_MAJOR_VERSION 1
31 #define EIGEN_MINOR_VERSION 0
32 
33 #define EIGEN_VERSION_AT_LEAST(x,y,z) (EIGEN_WORLD_VERSION>x || (EIGEN_WORLD_VERSION>=x && \
34  (EIGEN_MAJOR_VERSION>y || (EIGEN_MAJOR_VERSION>=y && \
35  EIGEN_MINOR_VERSION>=z))))
36 #ifdef __GNUC__
37  #define EIGEN_GNUC_AT_LEAST(x,y) ((__GNUC__==x && __GNUC_MINOR__>=y) || __GNUC__>x)
38 #else
39  #define EIGEN_GNUC_AT_LEAST(x,y) 0
40 #endif
41 
42 #ifdef __GNUC__
43  #define EIGEN_GNUC_AT_MOST(x,y) ((__GNUC__==x && __GNUC_MINOR__<=y) || __GNUC__<x)
44 #else
45  #define EIGEN_GNUC_AT_MOST(x,y) 0
46 #endif
47 
48 #if EIGEN_GNUC_AT_MOST(4,3) && !defined(__clang__)
49  // see bug 89
50  #define EIGEN_SAFE_TO_USE_STANDARD_ASSERT_MACRO 0
51 #else
52  #define EIGEN_SAFE_TO_USE_STANDARD_ASSERT_MACRO 1
53 #endif
54 
55 #if defined(__GNUC__) && (__GNUC__ <= 3)
56 #define EIGEN_GCC3_OR_OLDER 1
57 #else
58 #define EIGEN_GCC3_OR_OLDER 0
59 #endif
60 
61 // 16 byte alignment is only useful for vectorization. Since it affects the ABI, we need to enable
62 // 16 byte alignment on all platforms where vectorization might be enabled. In theory we could always
63 // enable alignment, but it can be a cause of problems on some platforms, so we just disable it in
64 // certain common platform (compiler+architecture combinations) to avoid these problems.
65 // Only static alignment is really problematic (relies on nonstandard compiler extensions that don't
66 // work everywhere, for example don't work on GCC/ARM), try to keep heap alignment even
67 // when we have to disable static alignment.
68 #if defined(__GNUC__) && !(defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || defined(__ppc__) || defined(__ia64__))
69 #define EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT 1
70 #else
71 #define EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT 0
72 #endif
73 
74 // static alignment is completely disabled with GCC 3, Sun Studio, and QCC/QNX
75 #if !EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT \
76  && !EIGEN_GCC3_OR_OLDER \
77  && !defined(__SUNPRO_CC) \
78  && !defined(__QNXNTO__)
79  #define EIGEN_ARCH_WANTS_STACK_ALIGNMENT 1
80 #else
81  #define EIGEN_ARCH_WANTS_STACK_ALIGNMENT 0
82 #endif
83 
84 #ifdef EIGEN_DONT_ALIGN
85  #ifndef EIGEN_DONT_ALIGN_STATICALLY
86  #define EIGEN_DONT_ALIGN_STATICALLY
87  #endif
88  #define EIGEN_ALIGN 0
89 #else
90  #define EIGEN_ALIGN 1
91 #endif
92 
93 // 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
94 // alignment (EIGEN_DONT_ALIGN_STATICALLY) and the architecture config (EIGEN_ARCH_WANTS_STACK_ALIGNMENT). Henceforth, only EIGEN_ALIGN_STATICALLY should be used.
95 #if EIGEN_ARCH_WANTS_STACK_ALIGNMENT && !defined(EIGEN_DONT_ALIGN_STATICALLY)
96  #define EIGEN_ALIGN_STATICALLY 1
97 #else
98  #define EIGEN_ALIGN_STATICALLY 0
99  #ifndef EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
100  #define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
101  #endif
102 #endif
103 
104 #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR
105 #define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION RowMajor
106 #else
107 #define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ColMajor
108 #endif
109 
110 #ifndef EIGEN_DEFAULT_DENSE_INDEX_TYPE
111 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE std::ptrdiff_t
112 #endif
113 
119 #ifndef EIGEN_FAST_MATH
120 #define EIGEN_FAST_MATH 1
121 #endif
122 
123 #define EIGEN_DEBUG_VAR(x) std::cerr << #x << " = " << x << std::endl;
124 
125 // concatenate two tokens
126 #define EIGEN_CAT2(a,b) a ## b
127 #define EIGEN_CAT(a,b) EIGEN_CAT2(a,b)
128 
129 // convert a token to a string
130 #define EIGEN_MAKESTRING2(a) #a
131 #define EIGEN_MAKESTRING(a) EIGEN_MAKESTRING2(a)
132 
133 #if EIGEN_GNUC_AT_LEAST(4,1) && !defined(__clang__) && !defined(__INTEL_COMPILER)
134 #define EIGEN_FLATTEN_ATTRIB __attribute__((flatten))
135 #else
136 #define EIGEN_FLATTEN_ATTRIB
137 #endif
138 
139 // EIGEN_STRONG_INLINE is a stronger version of the inline, using __forceinline on MSVC,
140 // but it still doesn't use GCC's always_inline. This is useful in (common) situations where MSVC needs forceinline
141 // but GCC is still doing fine with just inline.
142 #if (defined _MSC_VER) || (defined __INTEL_COMPILER)
143 #define EIGEN_STRONG_INLINE __forceinline
144 #else
145 #define EIGEN_STRONG_INLINE inline
146 #endif
147 
148 // EIGEN_ALWAYS_INLINE is the stronget, it has the effect of making the function inline and adding every possible
149 // attribute to maximize inlining. This should only be used when really necessary: in particular,
150 // it uses __attribute__((always_inline)) on GCC, which most of the time is useless and can severely harm compile times.
151 // FIXME with the always_inline attribute,
152 // gcc 3.4.x reports the following compilation error:
153 // Eval.h:91: sorry, unimplemented: inlining failed in call to 'const Eigen::Eval<Derived> Eigen::MatrixBase<Scalar, Derived>::eval() const'
154 // : function body not available
155 #if EIGEN_GNUC_AT_LEAST(4,0)
156 #define EIGEN_ALWAYS_INLINE __attribute__((always_inline)) inline
157 #else
158 #define EIGEN_ALWAYS_INLINE EIGEN_STRONG_INLINE
159 #endif
160 
161 #if (defined __GNUC__)
162 #define EIGEN_DONT_INLINE __attribute__((noinline))
163 #elif (defined _MSC_VER)
164 #define EIGEN_DONT_INLINE __declspec(noinline)
165 #else
166 #define EIGEN_DONT_INLINE
167 #endif
168 
169 // this macro allows to get rid of linking errors about multiply defined functions.
170 // - static is not very good because it prevents definitions from different object files to be merged.
171 // So static causes the resulting linked executable to be bloated with multiple copies of the same function.
172 // - inline is not perfect either as it unwantedly hints the compiler toward inlining the function.
173 #define EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
174 #define EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS inline
175 
176 #ifdef NDEBUG
177 # ifndef EIGEN_NO_DEBUG
178 # define EIGEN_NO_DEBUG
179 # endif
180 #endif
181 
182 // eigen_plain_assert is where we implement the workaround for the assert() bug in GCC <= 4.3, see bug 89
183 #ifdef EIGEN_NO_DEBUG
184  #define eigen_plain_assert(x)
185 #else
186  #if EIGEN_SAFE_TO_USE_STANDARD_ASSERT_MACRO
187  namespace Eigen {
188  namespace internal {
189  inline bool copy_bool(bool b) { return b; }
190  }
191  }
192  #define eigen_plain_assert(x) assert(x)
193  #else
194  // work around bug 89
195  #include <cstdlib> // for abort
196  #include <iostream> // for std::cerr
197 
198  namespace Eigen {
199  namespace internal {
200  // trivial function copying a bool. Must be EIGEN_DONT_INLINE, so we implement it after including Eigen headers.
201  // see bug 89.
202  namespace {
203  EIGEN_DONT_INLINE bool copy_bool(bool b) { return b; }
204  }
205  inline void assert_fail(const char *condition, const char *function, const char *file, int line)
206  {
207  std::cerr << "assertion failed: " << condition << " in function " << function << " at " << file << ":" << line << std::endl;
208  abort();
209  }
210  }
211  }
212  #define eigen_plain_assert(x) \
213  do { \
214  if(!Eigen::internal::copy_bool(x)) \
215  Eigen::internal::assert_fail(EIGEN_MAKESTRING(x), __PRETTY_FUNCTION__, __FILE__, __LINE__); \
216  } while(false)
217  #endif
218 #endif
219 
220 // eigen_assert can be overridden
221 #ifndef eigen_assert
222 #define eigen_assert(x) eigen_plain_assert(x)
223 #endif
224 
225 #ifdef EIGEN_INTERNAL_DEBUGGING
226 #define eigen_internal_assert(x) eigen_assert(x)
227 #else
228 #define eigen_internal_assert(x)
229 #endif
230 
231 #ifdef EIGEN_NO_DEBUG
232 #define EIGEN_ONLY_USED_FOR_DEBUG(x) (void)x
233 #else
234 #define EIGEN_ONLY_USED_FOR_DEBUG(x)
235 #endif
236 
237 #ifndef EIGEN_NO_DEPRECATED_WARNING
238  #if (defined __GNUC__)
239  #define EIGEN_DEPRECATED __attribute__((deprecated))
240  #elif (defined _MSC_VER)
241  #define EIGEN_DEPRECATED __declspec(deprecated)
242  #else
243  #define EIGEN_DEPRECATED
244  #endif
245 #else
246  #define EIGEN_DEPRECATED
247 #endif
248 
249 #if (defined __GNUC__)
250 #define EIGEN_UNUSED __attribute__((unused))
251 #else
252 #define EIGEN_UNUSED
253 #endif
254 
255 // Suppresses 'unused variable' warnings.
256 #define EIGEN_UNUSED_VARIABLE(var) (void)var;
257 
258 #if !defined(EIGEN_ASM_COMMENT) && (defined __GNUC__)
259 #define EIGEN_ASM_COMMENT(X) asm("#" X)
260 #else
261 #define EIGEN_ASM_COMMENT(X)
262 #endif
263 
264 /* EIGEN_ALIGN_TO_BOUNDARY(n) forces data to be n-byte aligned. This is used to satisfy SIMD requirements.
265  * However, we do that EVEN if vectorization (EIGEN_VECTORIZE) is disabled,
266  * so that vectorization doesn't affect binary compatibility.
267  *
268  * If we made alignment depend on whether or not EIGEN_VECTORIZE is defined, it would be impossible to link
269  * vectorized and non-vectorized code.
270  */
271 #if (defined __GNUC__) || (defined __PGI) || (defined __IBMCPP__) || (defined __ARMCC_VERSION)
272  #define EIGEN_ALIGN_TO_BOUNDARY(n) __attribute__((aligned(n)))
273 #elif (defined _MSC_VER)
274  #define EIGEN_ALIGN_TO_BOUNDARY(n) __declspec(align(n))
275 #elif (defined __SUNPRO_CC)
276  // FIXME not sure about this one:
277  #define EIGEN_ALIGN_TO_BOUNDARY(n) __attribute__((aligned(n)))
278 #else
279  #error Please tell me what is the equivalent of __attribute__((aligned(n))) for your compiler
280 #endif
281 
282 #define EIGEN_ALIGN16 EIGEN_ALIGN_TO_BOUNDARY(16)
283 
284 #if EIGEN_ALIGN_STATICALLY
285 #define EIGEN_USER_ALIGN_TO_BOUNDARY(n) EIGEN_ALIGN_TO_BOUNDARY(n)
286 #define EIGEN_USER_ALIGN16 EIGEN_ALIGN16
287 #else
288 #define EIGEN_USER_ALIGN_TO_BOUNDARY(n)
289 #define EIGEN_USER_ALIGN16
290 #endif
291 
292 #ifdef EIGEN_DONT_USE_RESTRICT_KEYWORD
293  #define EIGEN_RESTRICT
294 #endif
295 #ifndef EIGEN_RESTRICT
296  #define EIGEN_RESTRICT __restrict
297 #endif
298 
299 #ifndef EIGEN_STACK_ALLOCATION_LIMIT
300 #define EIGEN_STACK_ALLOCATION_LIMIT 20000
301 #endif
302 
303 #ifndef EIGEN_DEFAULT_IO_FORMAT
304 #ifdef EIGEN_MAKING_DOCS
305 // format used in Eigen's documentation
306 // needed to define it here as escaping characters in CMake add_definition's argument seems very problematic.
307 #define EIGEN_DEFAULT_IO_FORMAT Eigen::IOFormat(3, 0, " ", "\n", "", "")
308 #else
309 #define EIGEN_DEFAULT_IO_FORMAT Eigen::IOFormat()
310 #endif
311 #endif
312 
313 // just an empty macro !
314 #define EIGEN_EMPTY
315 
316 #if defined(_MSC_VER) && (!defined(__INTEL_COMPILER))
317 #define EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) \
318  using Base::operator =;
319 #else
320 #define EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) \
321  using Base::operator =; \
322  EIGEN_STRONG_INLINE Derived& operator=(const Derived& other) \
323  { \
324  Base::operator=(other); \
325  return *this; \
326  }
327 #endif
328 
329 #define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
330  EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived)
331 
340 #define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived) \
341  typedef typename Eigen::internal::traits<Derived>::Scalar Scalar; \
342  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
343  typedef typename Base::CoeffReturnType CoeffReturnType; \
344  typedef typename Eigen::internal::nested<Derived>::type Nested; \
345  typedef typename Eigen::internal::traits<Derived>::StorageKind StorageKind; \
346  typedef typename Eigen::internal::traits<Derived>::Index Index; \
347  enum { RowsAtCompileTime = Eigen::internal::traits<Derived>::RowsAtCompileTime, \
348  ColsAtCompileTime = Eigen::internal::traits<Derived>::ColsAtCompileTime, \
349  Flags = Eigen::internal::traits<Derived>::Flags, \
350  CoeffReadCost = Eigen::internal::traits<Derived>::CoeffReadCost, \
351  SizeAtCompileTime = Base::SizeAtCompileTime, \
352  MaxSizeAtCompileTime = Base::MaxSizeAtCompileTime, \
353  IsVectorAtCompileTime = Base::IsVectorAtCompileTime };
354 
355 
356 #define EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
357  typedef typename Eigen::internal::traits<Derived>::Scalar Scalar; \
358  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
359  typedef typename Base::PacketScalar PacketScalar; \
360  typedef typename Base::CoeffReturnType CoeffReturnType; \
361  typedef typename Eigen::internal::nested<Derived>::type Nested; \
362  typedef typename Eigen::internal::traits<Derived>::StorageKind StorageKind; \
363  typedef typename Eigen::internal::traits<Derived>::Index Index; \
364  enum { RowsAtCompileTime = Eigen::internal::traits<Derived>::RowsAtCompileTime, \
365  ColsAtCompileTime = Eigen::internal::traits<Derived>::ColsAtCompileTime, \
366  MaxRowsAtCompileTime = Eigen::internal::traits<Derived>::MaxRowsAtCompileTime, \
367  MaxColsAtCompileTime = Eigen::internal::traits<Derived>::MaxColsAtCompileTime, \
368  Flags = Eigen::internal::traits<Derived>::Flags, \
369  CoeffReadCost = Eigen::internal::traits<Derived>::CoeffReadCost, \
370  SizeAtCompileTime = Base::SizeAtCompileTime, \
371  MaxSizeAtCompileTime = Base::MaxSizeAtCompileTime, \
372  IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
373  using Base::derived; \
374  using Base::const_cast_derived;
375 
376 
377 #define EIGEN_PLAIN_ENUM_MIN(a,b) (((int)a <= (int)b) ? (int)a : (int)b)
378 #define EIGEN_PLAIN_ENUM_MAX(a,b) (((int)a >= (int)b) ? (int)a : (int)b)
379 
380 // EIGEN_SIZE_MIN_PREFER_DYNAMIC gives the min between compile-time sizes. 0 has absolute priority, followed by 1,
381 // followed by Dynamic, followed by other finite values. The reason for giving Dynamic the priority over
382 // finite values is that min(3, Dynamic) should be Dynamic, since that could be anything between 0 and 3.
383 #define EIGEN_SIZE_MIN_PREFER_DYNAMIC(a,b) (((int)a == 0 || (int)b == 0) ? 0 \
384  : ((int)a == 1 || (int)b == 1) ? 1 \
385  : ((int)a == Dynamic || (int)b == Dynamic) ? Dynamic \
386  : ((int)a <= (int)b) ? (int)a : (int)b)
387 
388 // EIGEN_SIZE_MIN_PREFER_FIXED is a variant of EIGEN_SIZE_MIN_PREFER_DYNAMIC comparing MaxSizes. The difference is that finite values
389 // now have priority over Dynamic, so that min(3, Dynamic) gives 3. Indeed, whatever the actual value is
390 // (between 0 and 3), it is not more than 3.
391 #define EIGEN_SIZE_MIN_PREFER_FIXED(a,b) (((int)a == 0 || (int)b == 0) ? 0 \
392  : ((int)a == 1 || (int)b == 1) ? 1 \
393  : ((int)a == Dynamic && (int)b == Dynamic) ? Dynamic \
394  : ((int)a == Dynamic) ? (int)b \
395  : ((int)b == Dynamic) ? (int)a \
396  : ((int)a <= (int)b) ? (int)a : (int)b)
397 
398 // see EIGEN_SIZE_MIN_PREFER_DYNAMIC. No need for a separate variant for MaxSizes here.
399 #define EIGEN_SIZE_MAX(a,b) (((int)a == Dynamic || (int)b == Dynamic) ? Dynamic \
400  : ((int)a >= (int)b) ? (int)a : (int)b)
401 
402 #define EIGEN_LOGICAL_XOR(a,b) (((a) || (b)) && !((a) && (b)))
403 
404 #define EIGEN_IMPLIES(a,b) (!(a) || (b))
405 
406 #define EIGEN_MAKE_CWISE_BINARY_OP(METHOD,FUNCTOR) \
407  template<typename OtherDerived> \
408  EIGEN_STRONG_INLINE const CwiseBinaryOp<FUNCTOR<Scalar>, const Derived, const OtherDerived> \
409  (METHOD)(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const \
410  { \
411  return CwiseBinaryOp<FUNCTOR<Scalar>, const Derived, const OtherDerived>(derived(), other.derived()); \
412  }
413 
414 // the expression type of a cwise product
415 #define EIGEN_CWISE_PRODUCT_RETURN_TYPE(LHS,RHS) \
416  CwiseBinaryOp< \
417  internal::scalar_product_op< \
418  typename internal::traits<LHS>::Scalar, \
419  typename internal::traits<RHS>::Scalar \
420  >, \
421  const LHS, \
422  const RHS \
423  >
424 
425 #endif // EIGEN_MACROS_H