More constexpr helpers

This commit is contained in:
Erik Schultheis 2022-04-04 18:38:34 +00:00 committed by Rasmus Munk Larsen
parent 64909b82bd
commit e1df3636b2
9 changed files with 49 additions and 73 deletions

View File

@ -105,8 +105,7 @@ template<typename Derived> class DenseBase
* \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */ * \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */
SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime, SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::ColsAtCompileTime>::ret),
/**< This is equal to the number of coefficients, i.e. the number of /**< This is equal to the number of coefficients, i.e. the number of
* rows times the number of columns, or to \a Dynamic if this is not * rows times the number of columns, or to \a Dynamic if this is not
* known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */ * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */
@ -133,8 +132,7 @@ template<typename Derived> class DenseBase
* \sa ColsAtCompileTime, MaxRowsAtCompileTime, MaxSizeAtCompileTime * \sa ColsAtCompileTime, MaxRowsAtCompileTime, MaxSizeAtCompileTime
*/ */
MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime, MaxSizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::MaxColsAtCompileTime>::ret),
/**< This value is equal to the maximum possible number of coefficients that this expression /**< This value is equal to the maximum possible number of coefficients that this expression
* might have. If this expression might have an arbitrarily high number of coefficients, * might have. If this expression might have an arbitrarily high number of coefficients,
* this value is set to \a Dynamic. * this value is set to \a Dynamic.

View File

@ -20,7 +20,7 @@ template<typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int
struct traits<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_> > struct traits<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_> >
{ {
private: private:
enum { size = internal::size_at_compile_time<Rows_,Cols_>::ret }; constexpr static int size = internal::size_at_compile_time(Rows_,Cols_);
typedef typename find_best_packet<Scalar_,size>::type PacketScalar; typedef typename find_best_packet<Scalar_,size>::type PacketScalar;
enum { enum {
row_major_bit = Options_&RowMajor ? RowMajorBit : 0, row_major_bit = Options_&RowMajor ? RowMajorBit : 0,
@ -42,7 +42,7 @@ public:
ColsAtCompileTime = Cols_, ColsAtCompileTime = Cols_,
MaxRowsAtCompileTime = MaxRows_, MaxRowsAtCompileTime = MaxRows_,
MaxColsAtCompileTime = MaxCols_, MaxColsAtCompileTime = MaxCols_,
Flags = compute_matrix_flags<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>::ret, Flags = compute_matrix_flags(Options_),
Options = Options_, Options = Options_,
InnerStrideAtCompileTime = 1, InnerStrideAtCompileTime = 1,
OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime, OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime,

View File

@ -81,12 +81,10 @@ class SolverBase : public EigenBase<Derived>
enum { enum {
RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime, RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime, ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime, SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::ColsAtCompileTime>::ret),
MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime, MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime, MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime, MaxSizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::MaxColsAtCompileTime>::ret),
IsVectorAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime == 1 IsVectorAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime == 1
|| internal::traits<Derived>::MaxColsAtCompileTime == 1, || internal::traits<Derived>::MaxColsAtCompileTime == 1,
NumDimensions = int(MaxSizeAtCompileTime) == 1 ? 0 : bool(IsVectorAtCompileTime) ? 1 : 2 NumDimensions = int(MaxSizeAtCompileTime) == 1 ? 0 : bool(IsVectorAtCompileTime) ? 1 : 2

View File

@ -37,14 +37,12 @@ template<typename Derived> class TriangularBase : public EigenBase<Derived>
MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime, MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime, MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime, SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::ColsAtCompileTime>::ret),
/**< This is equal to the number of coefficients, i.e. the number of /**< This is equal to the number of coefficients, i.e. the number of
* rows times the number of columns, or to \a Dynamic if this is not * rows times the number of columns, or to \a Dynamic if this is not
* known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */ * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */
MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime, MaxSizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret)
internal::traits<Derived>::MaxColsAtCompileTime>::ret)
}; };
typedef typename internal::traits<Derived>::Scalar Scalar; typedef typename internal::traits<Derived>::Scalar Scalar;

View File

@ -913,7 +913,7 @@
// Suppresses 'unused variable' warnings. // Suppresses 'unused variable' warnings.
namespace Eigen { namespace Eigen {
namespace internal { namespace internal {
template<typename T> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void ignore_unused_variable(const T&) {} template<typename T> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void ignore_unused_variable(const T&) {}
} }
} }
#define EIGEN_UNUSED_VARIABLE(var) Eigen::internal::ignore_unused_variable(var); #define EIGEN_UNUSED_VARIABLE(var) Eigen::internal::ignore_unused_variable(var);

View File

@ -203,38 +203,27 @@ struct find_best_packet
}; };
#if EIGEN_MAX_STATIC_ALIGN_BYTES>0 #if EIGEN_MAX_STATIC_ALIGN_BYTES>0
template<int ArrayBytes, int AlignmentBytes, constexpr inline int compute_default_alignment_helper(int ArrayBytes, int AlignmentBytes) {
bool Match = bool((ArrayBytes%AlignmentBytes)==0), if((ArrayBytes % AlignmentBytes) == 0) {
bool TryHalf = bool(EIGEN_MIN_ALIGN_BYTES<AlignmentBytes) > return AlignmentBytes;
struct compute_default_alignment_helper } else if (EIGEN_MIN_ALIGN_BYTES<AlignmentBytes) {
{ return compute_default_alignment_helper(ArrayBytes, AlignmentBytes/2);
enum { value = 0 }; } else {
}; return 0;
}
template<int ArrayBytes, int AlignmentBytes, bool TryHalf> }
struct compute_default_alignment_helper<ArrayBytes, AlignmentBytes, true, TryHalf> // Match
{
enum { value = AlignmentBytes };
};
template<int ArrayBytes, int AlignmentBytes>
struct compute_default_alignment_helper<ArrayBytes, AlignmentBytes, false, true> // Try-half
{
// current packet too large, try with an half-packet
enum { value = compute_default_alignment_helper<ArrayBytes, AlignmentBytes/2>::value };
};
#else #else
// If static alignment is disabled, no need to bother. // If static alignment is disabled, no need to bother.
// This also avoids a division by zero in "bool Match = bool((ArrayBytes%AlignmentBytes)==0)" // This also avoids a division by zero
template<int ArrayBytes, int AlignmentBytes> constexpr inline int compute_default_alignment_helper(int ArrayBytes, int AlignmentBytes) {
struct compute_default_alignment_helper EIGEN_UNUSED_VARIABLE(ArrayBytes);
{ EIGEN_UNUSED_VARIABLE(AlignmentBytes);
enum { value = 0 }; return 0;
}; }
#endif #endif
template<typename T, int Size> struct compute_default_alignment { template<typename T, int Size> struct compute_default_alignment {
enum { value = compute_default_alignment_helper<Size*sizeof(T),EIGEN_MAX_STATIC_ALIGN_BYTES>::value }; enum { value = compute_default_alignment_helper(Size*sizeof(T), EIGEN_MAX_STATIC_ALIGN_BYTES) };
}; };
template<typename T> struct compute_default_alignment<T,Dynamic> { template<typename T> struct compute_default_alignment<T,Dynamic> {
@ -261,25 +250,21 @@ template<typename Scalar_, int Rows_, int Cols_,
typedef Matrix<Scalar_, Rows_, Cols_, Options, MaxRows_, MaxCols_> type; typedef Matrix<Scalar_, Rows_, Cols_, Options, MaxRows_, MaxCols_> type;
}; };
template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols> constexpr inline unsigned compute_matrix_flags(int Options) {
class compute_matrix_flags unsigned row_major_bit = Options&RowMajor ? RowMajorBit : 0;
{ // FIXME currently we still have to handle DirectAccessBit at the expression level to handle DenseCoeffsBase<>
enum { row_major_bit = Options&RowMajor ? RowMajorBit : 0 }; // and then propagate this information to the evaluator's flags.
public: // However, I (Gael) think that DirectAccessBit should only matter at the evaluation stage.
// FIXME currently we still have to handle DirectAccessBit at the expression level to handle DenseCoeffsBase<> return DirectAccessBit | LvalueBit | NestByRefBit | row_major_bit;
// and then propagate this information to the evaluator's flags. }
// However, I (Gael) think that DirectAccessBit should only matter at the evaluation stage.
enum { ret = DirectAccessBit | LvalueBit | NestByRefBit | row_major_bit };
};
template<int Rows_, int Cols_> struct size_at_compile_time constexpr inline int size_at_compile_time(int rows, int cols) {
{ return (rows==Dynamic || cols==Dynamic) ? Dynamic : rows * cols;
enum { ret = (Rows_==Dynamic || Cols_==Dynamic) ? Dynamic : Rows_ * Cols_ }; }
};
template<typename XprType> struct size_of_xpr_at_compile_time template<typename XprType> struct size_of_xpr_at_compile_time
{ {
enum { ret = size_at_compile_time<traits<XprType>::RowsAtCompileTime,traits<XprType>::ColsAtCompileTime>::ret }; enum { ret = size_at_compile_time(traits<XprType>::RowsAtCompileTime, traits<XprType>::ColsAtCompileTime) };
}; };
/* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type, /* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type,

View File

@ -71,8 +71,7 @@ template<typename Derived> class SparseMatrixBase
* \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */ * \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */
SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime, SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::ColsAtCompileTime>::ret),
/**< This is equal to the number of coefficients, i.e. the number of /**< This is equal to the number of coefficients, i.e. the number of
* rows times the number of columns, or to \a Dynamic if this is not * rows times the number of columns, or to \a Dynamic if this is not
* known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */ * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */
@ -80,8 +79,7 @@ template<typename Derived> class SparseMatrixBase
MaxRowsAtCompileTime = RowsAtCompileTime, MaxRowsAtCompileTime = RowsAtCompileTime,
MaxColsAtCompileTime = ColsAtCompileTime, MaxColsAtCompileTime = ColsAtCompileTime,
MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime, MaxSizeAtCompileTime = internal::size_at_compile_time(MaxRowsAtCompileTime, MaxColsAtCompileTime),
MaxColsAtCompileTime>::ret),
IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1, IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1,
/**< This is set to true if either the number of rows or the number of /**< This is set to true if either the number of rows or the number of

View File

@ -209,10 +209,10 @@ struct traits<KroneckerProduct<Lhs_,Rhs_> >
typedef typename promote_index_type<typename Lhs::StorageIndex, typename Rhs::StorageIndex>::type StorageIndex; typedef typename promote_index_type<typename Lhs::StorageIndex, typename Rhs::StorageIndex>::type StorageIndex;
enum { enum {
Rows = size_at_compile_time<traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime>::ret, Rows = size_at_compile_time(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime),
Cols = size_at_compile_time<traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime>::ret, Cols = size_at_compile_time(traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime),
MaxRows = size_at_compile_time<traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime>::ret, MaxRows = size_at_compile_time(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime),
MaxCols = size_at_compile_time<traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime>::ret MaxCols = size_at_compile_time(traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime)
}; };
typedef Matrix<Scalar,Rows,Cols> ReturnType; typedef Matrix<Scalar,Rows,Cols> ReturnType;
@ -232,10 +232,10 @@ struct traits<KroneckerProductSparse<Lhs_,Rhs_> >
LhsFlags = Lhs::Flags, LhsFlags = Lhs::Flags,
RhsFlags = Rhs::Flags, RhsFlags = Rhs::Flags,
RowsAtCompileTime = size_at_compile_time<traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime>::ret, RowsAtCompileTime = size_at_compile_time(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime),
ColsAtCompileTime = size_at_compile_time<traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime>::ret, ColsAtCompileTime = size_at_compile_time(traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime),
MaxRowsAtCompileTime = size_at_compile_time<traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime>::ret, MaxRowsAtCompileTime = size_at_compile_time(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime),
MaxColsAtCompileTime = size_at_compile_time<traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime>::ret, MaxColsAtCompileTime = size_at_compile_time(traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime),
EvalToRowMajor = (int(LhsFlags) & int(RhsFlags) & RowMajorBit), EvalToRowMajor = (int(LhsFlags) & int(RhsFlags) & RowMajorBit),
RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit), RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit),

View File

@ -46,8 +46,7 @@ public:
* \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */ * \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */
SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime, SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
internal::traits<Derived>::ColsAtCompileTime>::ret),
/**< This is equal to the number of coefficients, i.e. the number of /**< This is equal to the number of coefficients, i.e. the number of
* rows times the number of columns, or to \a Dynamic if this is not * rows times the number of columns, or to \a Dynamic if this is not
* known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */ * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */
@ -55,8 +54,8 @@ public:
MaxRowsAtCompileTime = RowsAtCompileTime, MaxRowsAtCompileTime = RowsAtCompileTime,
MaxColsAtCompileTime = ColsAtCompileTime, MaxColsAtCompileTime = ColsAtCompileTime,
MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime, MaxSizeAtCompileTime = (internal::size_at_compile_time(MaxRowsAtCompileTime,
MaxColsAtCompileTime>::ret), MaxColsAtCompileTime)),
IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1, IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1,
/**< This is set to true if either the number of rows or the number of /**< This is set to true if either the number of rows or the number of