From 972a535288c5aaa62b66d43e1825e3b4eed7d234 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Sun, 14 Jun 2015 15:04:07 +0200 Subject: [PATCH] Remove aligned-on-scalar assert and fallback to non vectorized path at runtime (first_aligned already had this runtime guard) --- Eigen/src/Core/AssignEvaluator.h | 15 +++++++++++---- Eigen/src/Core/Map.h | 15 --------------- test/mapped_matrix.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Eigen/src/Core/AssignEvaluator.h b/Eigen/src/Core/AssignEvaluator.h index 506bace69..4622e2759 100644 --- a/Eigen/src/Core/AssignEvaluator.h +++ b/Eigen/src/Core/AssignEvaluator.h @@ -471,18 +471,25 @@ struct dense_assignment_loop { EIGEN_DEVICE_FUNC static inline void run(Kernel &kernel) { - typedef packet_traits PacketTraits; + typedef typename Kernel::Scalar Scalar; + typedef packet_traits PacketTraits; enum { packetSize = PacketTraits::size, alignable = PacketTraits::AlignedOnScalar, - dstAlignment = alignable ? Aligned : int(Kernel::AssignmentTraits::DstIsAligned) + dstIsAligned = Kernel::AssignmentTraits::DstIsAligned, + dstAlignment = alignable ? Aligned : int(dstIsAligned) }; + const Scalar *dst_ptr = &kernel.dstEvaluator().coeffRef(0,0); + if((!bool(dstIsAligned)) && (Index(dst_ptr) % sizeof(Scalar))>0) + { + // the pointer is not aligend-on scalar, so alignment is not possible + return dense_assignment_loop::run(kernel); + } const Index packetAlignedMask = packetSize - 1; const Index innerSize = kernel.innerSize(); const Index outerSize = kernel.outerSize(); const Index alignedStep = alignable ? (packetSize - kernel.outerStride() % packetSize) & packetAlignedMask : 0; - Index alignedStart = ((!alignable) || Kernel::AssignmentTraits::DstIsAligned) ? 0 - : internal::first_aligned(&kernel.dstEvaluator().coeffRef(0,0), innerSize); + Index alignedStart = ((!alignable) || bool(dstIsAligned)) ? 0 : internal::first_aligned(dst_ptr, innerSize); for(Index outer = 0; outer < outerSize; ++outer) { diff --git a/Eigen/src/Core/Map.h b/Eigen/src/Core/Map.h index ce431edc8..6b2b3ade4 100644 --- a/Eigen/src/Core/Map.h +++ b/Eigen/src/Core/Map.h @@ -124,7 +124,6 @@ template class Ma : Base(cast_to_pointer_type(dataPtr)), m_stride(stride) { PlainObjectType::Base::_check_template_params(); - checkPointer(dataPtr); } /** Constructor in the dynamic-size vector case. @@ -138,7 +137,6 @@ template class Ma : Base(cast_to_pointer_type(dataPtr), size), m_stride(stride) { PlainObjectType::Base::_check_template_params(); - checkPointer(dataPtr); } /** Constructor in the dynamic-size matrix case. @@ -153,24 +151,11 @@ template class Ma : Base(cast_to_pointer_type(dataPtr), rows, cols), m_stride(stride) { PlainObjectType::Base::_check_template_params(); - checkPointer(dataPtr); } EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map) protected: - - static void checkPointer(const Scalar* dataPtr) - { - enum { - MightTryToAlignOnScalar = internal::packet_traits::AlignedOnScalar - && bool(internal::traits::Flags&PacketAccessBit) - && internal::is_lvalue::value - }; - eigen_assert(EIGEN_IMPLIES(bool(MightTryToAlignOnScalar), (size_t(dataPtr) % sizeof(Scalar)) == 0) - && "input pointer is not aligned on scalar boundary, e.g., use \"EIGEN_ALIGN8 T ptr[N];\" for double or complex"); - } - StrideType m_stride; }; diff --git a/test/mapped_matrix.cpp b/test/mapped_matrix.cpp index 05a86e70b..1f9da31ac 100644 --- a/test/mapped_matrix.cpp +++ b/test/mapped_matrix.cpp @@ -159,6 +159,28 @@ template void check_const_correctness(const PlainObjec VERIFY( !(Map::Flags & LvalueBit) ); } +template +void map_not_aligned_on_scalar() +{ + typedef Matrix MatrixType; + typedef typename MatrixType::Index Index; + Index size = 11; + Scalar* array1 = internal::aligned_new((size+1)*(size+1)+1); + Scalar* array2 = reinterpret_cast(sizeof(Scalar)/2+std::size_t(array1)); + Map > map2(array2, size, size, OuterStride<>(size+1)); + MatrixType m2 = MatrixType::Random(size,size); + map2 = m2; + VERIFY_IS_EQUAL(m2, map2); + + typedef Matrix VectorType; + Map map3(array2, size); + MatrixType v3 = VectorType::Random(size); + map3 = v3; + VERIFY_IS_EQUAL(v3, map3); + + internal::aligned_delete(array1, (size+1)*(size+1)+1); +} + void test_mapped_matrix() { for(int i = 0; i < g_repeat; i++) { @@ -183,5 +205,7 @@ void test_mapped_matrix() CALL_SUBTEST_8( map_static_methods(RowVector3d()) ); CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) ); CALL_SUBTEST_10( map_static_methods(VectorXf(12)) ); + + CALL_SUBTEST_11( map_not_aligned_on_scalar() ); } }