Fix logic in compute_default_alignment, extend it to Dynamic size, and move it to XprHelper.h file.

This commit is contained in:
Gael Guennebaud 2015-08-06 14:07:59 +02:00
parent becd89df29
commit 65186ef18d
2 changed files with 42 additions and 24 deletions

View File

@ -34,34 +34,13 @@ void check_static_allocation_size()
#endif
}
template<int ArrayBytes, int AlignmentBytes,
bool Match = bool((ArrayBytes%AlignmentBytes)==0),
bool TryHalf = bool(AlignmentBytes>EIGEN_MIN_ALIGN_BYTES) >
struct compute_default_alignment
{
enum { value = 0 };
};
template<int ArrayBytes, int AlignmentBytes, bool TryHalf>
struct compute_default_alignment<ArrayBytes, AlignmentBytes, true, TryHalf> // Match
{
enum { value = AlignmentBytes };
};
template<int ArrayBytes, int AlignmentBytes>
struct compute_default_alignment<ArrayBytes, AlignmentBytes, false, true> // Try-half
{
// current packet too large, try with an half-packet
enum { value = compute_default_alignment<ArrayBytes, AlignmentBytes/2>::value };
};
/** \internal
* Static array. If the MatrixOrArrayOptions require auto-alignment, the array will be automatically aligned:
* to 16 bytes boundary if the total size is a multiple of 16 bytes.
*/
template <typename T, int Size, int MatrixOrArrayOptions,
int Alignment = (MatrixOrArrayOptions&DontAlign) ? 0
: compute_default_alignment<Size*sizeof(T), EIGEN_PLAIN_ENUM_MAX(packet_traits<T>::size*sizeof(T), EIGEN_MAX_STATIC_ALIGN_BYTES) >::value >
: compute_default_alignment<T,Size>::value >
struct plain_array
{
T array[Size];
@ -107,7 +86,7 @@ struct plain_array<T, Size, MatrixOrArrayOptions, 8>
EIGEN_DEVICE_FUNC
plain_array()
{
{
EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(7);
check_static_allocation_size<T,Size>();
}
@ -145,7 +124,7 @@ struct plain_array<T, Size, MatrixOrArrayOptions, 32>
EIGEN_DEVICE_FUNC
plain_array()
{
{
EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(31);
check_static_allocation_size<T,Size>();
}

View File

@ -122,6 +122,45 @@ template<typename T> struct unpacket_traits
enum {size=1};
};
#if EIGEN_MAX_STATIC_ALIGN_BYTES>0
template<int ArrayBytes, int AlignmentBytes,
bool Match = bool((ArrayBytes%AlignmentBytes)==0),
bool TryHalf = bool(AlignmentBytes>EIGEN_MIN_ALIGN_BYTES) >
struct compute_default_alignment_helper
{
enum { value = 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
// If static alignment is disabled, no need to bother.
// This also avoids a division by zero in "bool Match = bool((ArrayBytes%AlignmentBytes)==0)"
template<int ArrayBytes, int AlignmentBytes>
struct compute_default_alignment_helper
{
enum { value = 0 };
};
#endif
template<typename T, int Size> struct compute_default_alignment {
enum { value = compute_default_alignment_helper<Size*sizeof(T),EIGEN_MAX_STATIC_ALIGN_BYTES>::value };
};
template<typename T> struct compute_default_alignment<T,Dynamic> {
enum { value = EIGEN_MAX_ALIGN_BYTES };
};
template<typename _Scalar, int _Rows, int _Cols,
int _Options = AutoAlign |
( (_Rows==1 && _Cols!=1) ? RowMajor