diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h index f9bf737ca..58783850f 100644 --- a/Eigen/src/Core/PlainObjectBase.h +++ b/Eigen/src/Core/PlainObjectBase.h @@ -31,6 +31,7 @@ namespace Eigen { namespace internal { +#ifndef EIGEN_NO_DEBUG template struct check_rows_cols_for_overflow { EIGEN_STATIC_ASSERT(MaxRowsAtCompileTime* MaxColsAtCompileTime == MaxSizeAtCompileTime, @@ -44,7 +45,7 @@ struct check_rows_cols_for_overflow { template EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE constexpr void run(Index, Index cols) { constexpr Index MaxIndex = NumTraits::highest(); - bool error = cols > MaxIndex / MaxRowsAtCompileTime; + bool error = cols > (MaxIndex / MaxRowsAtCompileTime); if (error) throw_std_bad_alloc(); } }; @@ -54,7 +55,7 @@ struct check_rows_cols_for_overflow { template EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE constexpr void run(Index rows, Index) { constexpr Index MaxIndex = NumTraits::highest(); - bool error = rows > MaxIndex / MaxColsAtCompileTime; + bool error = rows > (MaxIndex / MaxColsAtCompileTime); if (error) throw_std_bad_alloc(); } }; @@ -64,10 +65,11 @@ struct check_rows_cols_for_overflow { template EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE constexpr void run(Index rows, Index cols) { constexpr Index MaxIndex = NumTraits::highest(); - bool error = cols == 0 ? false : (rows > MaxIndex / cols); + bool error = cols == 0 ? false : (rows > (MaxIndex / cols)); if (error) throw_std_bad_alloc(); } }; +#endif template @@ -297,8 +299,10 @@ class PlainObjectBase : public internal::dense_xpr_base::type internal::check_implication(ColsAtCompileTime == Dynamic && MaxColsAtCompileTime != Dynamic, cols <= MaxColsAtCompileTime) && rows >= 0 && cols >= 0 && "Invalid sizes when resizing a matrix or array."); +#ifndef EIGEN_NO_DEBUG internal::check_rows_cols_for_overflow::run(rows, cols); +#endif #ifdef EIGEN_INITIALIZE_COEFFS Index size = rows * cols; bool size_changed = size != this->size(); @@ -367,8 +371,10 @@ class PlainObjectBase : public internal::dense_xpr_base::type template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resizeLike(const EigenBase& _other) { const OtherDerived& other = _other.derived(); +#ifndef EIGEN_NO_DEBUG internal::check_rows_cols_for_overflow::run( other.rows(), other.cols()); +#endif const Index othersize = other.rows() * other.cols(); if (RowsAtCompileTime == 1) { eigen_assert(other.rows() == 1 || other.cols() == 1); @@ -941,8 +947,10 @@ struct conservative_resize_like_impl { ((Derived::IsRowMajor && _this.cols() == cols) || // row-major and we change only the number of rows (!Derived::IsRowMajor && _this.rows() == rows))) // column-major and we change only the number of columns { +#ifndef EIGEN_NO_DEBUG internal::check_rows_cols_for_overflow::run(rows, cols); +#endif _this.derived().m_storage.conservativeResize(rows * cols, rows, cols); } else { // The storage order does not allow us to use reallocation. diff --git a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h index 3b9eff7f0..9417469d2 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h +++ b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h @@ -303,12 +303,16 @@ class Tensor : public TensorBase& dimensions) { - int i; +#ifndef EIGEN_NO_DEBUG Index size = Index(1); - for (i = 0; i < NumIndices; i++) { + for (int i = 0; i < NumIndices; i++) { internal::check_rows_cols_for_overflow::run(size, dimensions[i]); size *= dimensions[i]; } +#else + Index size = internal::array_prod(dimensions); +#endif + #ifdef EIGEN_INITIALIZE_COEFFS bool size_changed = size != this->size(); m_storage.resize(size, dimensions); @@ -318,15 +322,6 @@ class Tensor : public TensorBase& dimensions) { - array dims; - for (int i = 0; i < NumIndices; ++i) { - dims[i] = dimensions[i]; - } - resize(dims); - } - EIGEN_DEVICE_FUNC void resize() { EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE); // Nothing to do: rank 0 tensors have fixed size @@ -347,7 +342,6 @@ class Tensor : public TensorBase(dimensions)); } -#ifndef EIGEN_EMULATE_CXX11_META_H template EIGEN_DEVICE_FUNC void resize(const Sizes& dimensions) { array dims; @@ -356,16 +350,6 @@ class Tensor : public TensorBase - EIGEN_DEVICE_FUNC void resize(const Sizes& dimensions) { - array dims; - for (int i = 0; i < NumIndices; ++i) { - dims[i] = static_cast(dimensions[i]); - } - resize(dims); - } -#endif #ifdef EIGEN_TENSOR_PLUGIN #include EIGEN_TENSOR_PLUGIN diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorBroadcasting.h b/unsupported/Eigen/CXX11/src/Tensor/TensorBroadcasting.h index 6c91d938f..9ef4bbce0 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorBroadcasting.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorBroadcasting.h @@ -55,12 +55,10 @@ template <> struct is_input_scalar> { static const bool value = true; }; -#ifndef EIGEN_EMULATE_CXX11_META_H template struct is_input_scalar> { - static const bool value = (Sizes::total_size == 1); + static constexpr bool value = (Sizes::total_size == 1); }; -#endif } // end namespace internal diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h index 4043e5eba..ae8f25f87 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h @@ -81,7 +81,6 @@ struct fixed_size_tensor_index_extraction_helper { } // end namespace internal // Fixed size -#ifndef EIGEN_EMULATE_CXX11_META_H template struct Sizes { typedef internal::numeric_list Base; @@ -133,87 +132,6 @@ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes -struct non_zero_size { - typedef internal::type2val type; -}; -template <> -struct non_zero_size<0> { - typedef internal::null_type type; -}; - -template -struct Sizes { - typedef typename internal::make_type_list::type, typename non_zero_size::type, - typename non_zero_size::type, typename non_zero_size::type, - typename non_zero_size::type>::type Base; - static const std::ptrdiff_t count = Base::count; - static const std::ptrdiff_t total_size = internal::arg_prod::value; - - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t rank() const { return count; } - - static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t TotalSize() { return internal::arg_prod::value; } - - Sizes() {} - template - explicit Sizes(const array& /*indices*/) { - // todo: add assertion - } - - template - Sizes& operator=(const T& /*other*/) { - // add assertion failure if the size of other is different - return *this; - } - - template - Sizes(DenseIndex... /*indices*/) {} - explicit Sizes(std::initializer_list) { - // todo: add assertion - } - - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index operator[](const Index index) const { - switch (index) { - case 0: - return internal::get<0, Base>::value; - case 1: - return internal::get<1, Base>::value; - case 2: - return internal::get<2, Base>::value; - case 3: - return internal::get<3, Base>::value; - case 4: - return internal::get<4, Base>::value; - default: - eigen_assert(false && "index overflow"); - return static_cast(-1); - } - } - - template - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t IndexOfColMajor(const array& indices) const { - return internal::fixed_size_tensor_index_linearization_helper::run( - indices, *reinterpret_cast(this)); - } - template - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t IndexOfRowMajor(const array& indices) const { - return internal::fixed_size_tensor_index_linearization_helper::run( - indices, *reinterpret_cast(this)); - } -}; - -namespace internal { -template -EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes&) { - return Sizes::total_size; -} -} // namespace internal - -#endif - // Boilerplate namespace internal { template @@ -289,21 +207,12 @@ struct DSizes : array { } } -#ifndef EIGEN_EMULATE_CXX11_META_H template EIGEN_DEVICE_FUNC DSizes(const Sizes& a) { for (int i = 0; i < NumDims; ++i) { (*this)[i] = a[i]; } } -#else - template - EIGEN_DEVICE_FUNC DSizes(const Sizes& a) { - for (int i = 0; i < NumDims; ++i) { - (*this)[i] = a[i]; - } - } -#endif template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit DSizes(DenseIndex firstDimension, DenseIndex secondDimension, @@ -374,7 +283,6 @@ template struct array_size > { static const ptrdiff_t value = NumDims; }; -#ifndef EIGEN_EMULATE_CXX11_META_H template struct array_size > { static const std::ptrdiff_t value = Sizes::count; @@ -392,22 +300,6 @@ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(const Sizes<>&) { eigen_assert(false && "should never be called"); return -1; } -#else -template -struct array_size > { - static const ptrdiff_t value = Sizes::count; -}; -template -struct array_size > { - static const ptrdiff_t value = Sizes::count; -}; -template -EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(const Sizes&) { - return get::Base>::value; -} - -#endif template struct sizes_match_below_dim { diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h b/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h index fe83b268d..69ab6840c 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h @@ -37,7 +37,7 @@ namespace Eigen { template struct type2index { - static const Index value = n; + static constexpr Index value = n; EIGEN_DEVICE_FUNC constexpr operator Index() const { return n; } EIGEN_DEVICE_FUNC void set(Index val) { eigen_assert(val == n); } }; @@ -46,8 +46,8 @@ struct type2index { // such as IndexPairList, type2indexpair<3,4>>(). template struct type2indexpair { - static const Index first = f; - static const Index second = s; + static constexpr Index first = f; + static constexpr Index second = s; constexpr EIGEN_DEVICE_FUNC operator IndexPair() const { return IndexPair(f, s); } @@ -134,7 +134,7 @@ struct IndexTuple { EIGEN_DEVICE_FUNC constexpr IndexTuple() : head(), others() {} EIGEN_DEVICE_FUNC constexpr IndexTuple(const T& v, const O... o) : head(v), others(o...) {} - constexpr static int count = 1 + sizeof...(O); + static constexpr int count = 1 + sizeof...(O); T head; IndexTuple others; typedef T Head; @@ -194,11 +194,11 @@ EIGEN_DEVICE_FUNC constexpr const typename IndexTupleExtractor::ValT } template struct array_size> { - static const size_t value = IndexTuple::count; + static constexpr size_t value = IndexTuple::count; }; template struct array_size> { - static const size_t value = IndexTuple::count; + static constexpr size_t value = IndexTuple::count; }; template diff --git a/unsupported/test/cxx11_tensor_concatenation.cpp b/unsupported/test/cxx11_tensor_concatenation.cpp index 68455b34d..562ac779b 100644 --- a/unsupported/test/cxx11_tensor_concatenation.cpp +++ b/unsupported/test/cxx11_tensor_concatenation.cpp @@ -47,13 +47,7 @@ static void test_static_dimension_failure() { // This can be worked around in this case. Tensor concatenation = left.reshape(Tensor::Dimensions(2, 3, 1)).concatenate(right, 0); - Tensor alternative = left - // Clang compiler break with {{{}}} with an ambiguous error on copy - // constructor the variadic DSize constructor added for #ifndef - // EIGEN_EMULATE_CXX11_META_H. Solution: either the code should change to - // Tensor::Dimensions{{2, 3}} - // or Tensor::Dimensions{Tensor::Dimensions{{2, 3}}} - .concatenate(right.reshape(Tensor::Dimensions(2, 3)), 0); + Tensor alternative = left.concatenate(right.reshape(Tensor::Dimensions(2, 3)), 0); } template