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