// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2015 Gael Guennebaud // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #ifdef EIGEN_TEST_PART_1 #define EIGEN_UNALIGNED_VECTORIZE 1 #endif #ifdef EIGEN_TEST_PART_2 #define EIGEN_UNALIGNED_VECTORIZE 0 #endif #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR #undef EIGEN_DEFAULT_TO_ROW_MAJOR #endif #define EIGEN_DEBUG_ASSIGN #include "main.h" #include // Disable "ignoring attributes on template argument" // for packet_traits // => The only workaround would be to wrap _m128 and the likes // within wrappers. #if EIGEN_GNUC_AT_LEAST(6,0) #pragma GCC diagnostic ignored "-Wignored-attributes" #endif using internal::demangle_flags; using internal::demangle_traversal; using internal::demangle_unrolling; template bool test_assign(const Dst&, const Src&, int traversal, int unrolling) { EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Dst,Src); typedef internal::copy_using_evaluator_traits,internal::evaluator, internal::assign_op > traits; bool res = traits::Traversal==traversal; if(unrolling==InnerUnrolling+CompleteUnrolling) res = res && (int(traits::Unrolling)==InnerUnrolling || int(traits::Unrolling)==CompleteUnrolling); else res = res && int(traits::Unrolling)==unrolling; if(!res) { std::cerr << "Src: " << demangle_flags(Src::Flags) << std::endl; std::cerr << " " << demangle_flags(internal::evaluator::Flags) << std::endl; std::cerr << "Dst: " << demangle_flags(Dst::Flags) << std::endl; std::cerr << " " << demangle_flags(internal::evaluator::Flags) << std::endl; traits::debug(); std::cerr << " Expected Traversal == " << demangle_traversal(traversal) << " got " << demangle_traversal(traits::Traversal) << "\n"; std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling) << " got " << demangle_unrolling(traits::Unrolling) << "\n"; } return res; } template bool test_assign(int traversal, int unrolling) { EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Dst,Src); typedef internal::copy_using_evaluator_traits,internal::evaluator, internal::assign_op > traits; bool res = traits::Traversal==traversal && traits::Unrolling==unrolling; if(!res) { std::cerr << "Src: " << demangle_flags(Src::Flags) << std::endl; std::cerr << " " << demangle_flags(internal::evaluator::Flags) << std::endl; std::cerr << "Dst: " << demangle_flags(Dst::Flags) << std::endl; std::cerr << " " << demangle_flags(internal::evaluator::Flags) << std::endl; traits::debug(); std::cerr << " Expected Traversal == " << demangle_traversal(traversal) << " got " << demangle_traversal(traits::Traversal) << "\n"; std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling) << " got " << demangle_unrolling(traits::Unrolling) << "\n"; } return res; } template bool test_redux(const Xpr&, int traversal, int unrolling) { typedef typename Xpr::Scalar Scalar; typedef internal::redux_traits,internal::redux_evaluator > traits; bool res = traits::Traversal==traversal && traits::Unrolling==unrolling; if(!res) { std::cerr << demangle_flags(Xpr::Flags) << std::endl; std::cerr << demangle_flags(internal::evaluator::Flags) << std::endl; traits::debug(); std::cerr << " Expected Traversal == " << demangle_traversal(traversal) << " got " << demangle_traversal(traits::Traversal) << "\n"; std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling) << " got " << demangle_unrolling(traits::Unrolling) << "\n"; } return res; } template::Vectorizable> struct vectorization_logic { typedef internal::packet_traits PacketTraits; typedef typename internal::packet_traits::type PacketType; typedef typename internal::unpacket_traits::half HalfPacketType; enum { PacketSize = internal::unpacket_traits::size, HalfPacketSize = internal::unpacket_traits::size }; static void run() { typedef Matrix Vector1; typedef Matrix VectorX; typedef Matrix MatrixXX; typedef Matrix Matrix11; typedef Matrix Matrix22; typedef Matrix Matrix44; typedef Matrix Matrix44u; typedef Matrix Matrix44c; typedef Matrix Matrix44r; typedef Matrix Matrix1; typedef Matrix Matrix1u; // this type is made such that it can only be vectorized when viewed as a linear 1D vector typedef Matrix Matrix3; #if !EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT VERIFY(test_assign(Vector1(),Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1()+Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1().cwiseProduct(Vector1()), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1().template cast(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix44(),Matrix44()+Matrix44(), InnerVectorizedTraversal,InnerUnrolling)); VERIFY(test_assign(Matrix44u(),Matrix44()+Matrix44(), EIGEN_UNALIGNED_VECTORIZE ? InnerVectorizedTraversal : LinearTraversal, EIGEN_UNALIGNED_VECTORIZE ? InnerUnrolling : NoUnrolling)); VERIFY(test_assign(Matrix1(),Matrix1()+Matrix1(), (int(Matrix1::InnerSizeAtCompileTime) % int(PacketSize))==0 ? InnerVectorizedTraversal : LinearVectorizedTraversal, CompleteUnrolling)); VERIFY(test_assign(Matrix1u(),Matrix1()+Matrix1(), EIGEN_UNALIGNED_VECTORIZE ? ((int(Matrix1::InnerSizeAtCompileTime) % int(PacketSize))==0 ? InnerVectorizedTraversal : LinearVectorizedTraversal) : LinearTraversal, CompleteUnrolling)); VERIFY(test_assign(Matrix44c().col(1),Matrix44c().col(2)+Matrix44c().col(3), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix44r().row(2),Matrix44r().row(1)+Matrix44r().row(1), InnerVectorizedTraversal,CompleteUnrolling)); if(PacketSize>1) { typedef Matrix Matrix33c; typedef Matrix Vector3; VERIFY(test_assign(Matrix33c().row(2),Matrix33c().row(1)+Matrix33c().row(1), LinearTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector3(),Vector3()+Vector3(), sizeof(Scalar)==16 ? InnerVectorizedTraversal : (EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : LinearTraversal), CompleteUnrolling)); VERIFY(test_assign(Matrix33c().col(0),Matrix33c().col(1)+Matrix33c().col(1), EIGEN_UNALIGNED_VECTORIZE ? (sizeof(Scalar)==16 ? InnerVectorizedTraversal : LinearVectorizedTraversal) : (sizeof(Scalar)==16 ? SliceVectorizedTraversal : LinearTraversal), ((!EIGEN_UNALIGNED_VECTORIZE) && (sizeof(Scalar)==16)) ? NoUnrolling : CompleteUnrolling)); VERIFY(test_assign(Matrix3(),Matrix3().cwiseProduct(Matrix3()), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix(),Matrix()+Matrix(), sizeof(Scalar)==16 ? InnerVectorizedTraversal : EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : LinearTraversal, NoUnrolling)); VERIFY(test_assign(Matrix11(), Matrix11()+Matrix11(),InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix11(),Matrix().template block(2,3)+Matrix().template block(3,2), (EIGEN_UNALIGNED_VECTORIZE) ? InnerVectorizedTraversal : DefaultTraversal, CompleteUnrolling|InnerUnrolling)); VERIFY(test_assign(Vector1(),Matrix11()*Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix11(),Matrix11().lazyProduct(Matrix11()), InnerVectorizedTraversal,InnerUnrolling+CompleteUnrolling)); } VERIFY(test_redux(Vector1(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Vector1().array()*Vector1().array(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux((Vector1().array()*Vector1().array()).col(0), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix3(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix44(), LinearVectorizedTraversal,NoUnrolling)); if(PacketSize>1) { VERIFY(test_redux(Matrix44().template block<(Matrix1::Flags&RowMajorBit)?4:PacketSize,(Matrix1::Flags&RowMajorBit)?PacketSize:4>(1,2), SliceVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix44().template block<(Matrix1::Flags&RowMajorBit)?2:PacketSize,(Matrix1::Flags&RowMajorBit)?PacketSize:2>(1,2), DefaultTraversal,CompleteUnrolling)); } VERIFY(test_redux(Matrix44c().template block<2*PacketSize,1>(1,2), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix44r().template block<1,2*PacketSize>(2,1), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY((test_assign< Map >, Matrix22 >(InnerVectorizedTraversal,CompleteUnrolling))); VERIFY((test_assign< Map, AlignedMax, InnerStride<3*PacketSize> >, Matrix >(DefaultTraversal,PacketSize>=8?InnerUnrolling:CompleteUnrolling))); VERIFY((test_assign(Matrix11(), Matrix()*Matrix(), InnerVectorizedTraversal, CompleteUnrolling))); #endif VERIFY(test_assign(MatrixXX(10,10),MatrixXX(20,20).block(10,10,2,3), SliceVectorizedTraversal,NoUnrolling)); VERIFY(test_redux(VectorX(10), LinearVectorizedTraversal,NoUnrolling)); } }; template struct vectorization_logic { static void run() {} }; template::type>::half, typename internal::packet_traits::type>::value > struct vectorization_logic_half { typedef internal::packet_traits PacketTraits; typedef typename internal::unpacket_traits::type>::half PacketType; enum { PacketSize = internal::unpacket_traits::size }; static void run() { typedef Matrix Vector1; typedef Matrix Matrix11; typedef Matrix Matrix57; typedef Matrix Matrix35; typedef Matrix Matrix57u; typedef Matrix Matrix1; typedef Matrix Matrix1u; // this type is made such that it can only be vectorized when viewed as a linear 1D vector typedef Matrix Matrix3; #if !EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT VERIFY(test_assign(Vector1(),Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1()+Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1().template segment(0).derived(), EIGEN_UNALIGNED_VECTORIZE ? InnerVectorizedTraversal : LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Scalar(2.1)*Vector1()-Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),(Scalar(2.1)*Vector1().template segment(0)-Vector1().template segment(0)).derived(), EIGEN_UNALIGNED_VECTORIZE ? InnerVectorizedTraversal : LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1().cwiseProduct(Vector1()), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Vector1(),Vector1().template cast(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix57(),Matrix57()+Matrix57(), InnerVectorizedTraversal,InnerUnrolling)); VERIFY(test_assign(Matrix57u(),Matrix57()+Matrix57(), EIGEN_UNALIGNED_VECTORIZE ? InnerVectorizedTraversal : LinearTraversal, EIGEN_UNALIGNED_VECTORIZE ? InnerUnrolling : NoUnrolling)); VERIFY(test_assign(Matrix1u(),Matrix1()+Matrix1(), EIGEN_UNALIGNED_VECTORIZE ? ((int(Matrix1::InnerSizeAtCompileTime) % int(PacketSize))==0 ? InnerVectorizedTraversal : LinearVectorizedTraversal) : LinearTraversal,CompleteUnrolling)); if(PacketSize>1) { typedef Matrix Matrix33c; VERIFY(test_assign(Matrix33c().row(2),Matrix33c().row(1)+Matrix33c().row(1), LinearTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix33c().col(0),Matrix33c().col(1)+Matrix33c().col(1), EIGEN_UNALIGNED_VECTORIZE ? (sizeof(Scalar)==16 ? InnerVectorizedTraversal : LinearVectorizedTraversal) : (sizeof(Scalar)==16 ? SliceVectorizedTraversal : LinearTraversal), ((!EIGEN_UNALIGNED_VECTORIZE) && (sizeof(Scalar)==16)) ? NoUnrolling : CompleteUnrolling)); VERIFY(test_assign(Matrix3(),Matrix3().cwiseQuotient(Matrix3()), PacketTraits::HasDiv ? LinearVectorizedTraversal : LinearTraversal, PacketTraits::HasDiv ? CompleteUnrolling : NoUnrolling)); VERIFY(test_assign(Matrix(),Matrix()+Matrix(), sizeof(Scalar)==16 ? InnerVectorizedTraversal : (EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : LinearTraversal), NoUnrolling)); VERIFY(test_assign(Matrix11(),Matrix().template block(2,3)+Matrix().template block(8,4), EIGEN_UNALIGNED_VECTORIZE ? InnerVectorizedTraversal : DefaultTraversal,InnerUnrolling+CompleteUnrolling)); VERIFY(test_assign(Vector1(),Matrix11()*Vector1(), InnerVectorizedTraversal,CompleteUnrolling)); VERIFY(test_assign(Matrix11(),Matrix11().lazyProduct(Matrix11()), InnerVectorizedTraversal,InnerUnrolling+CompleteUnrolling)); } VERIFY(test_redux(Vector1(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix3(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix35(), LinearVectorizedTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix57().template block(1,0), SliceVectorizedTraversal,CompleteUnrolling)); if(PacketSize>1) { VERIFY(test_redux(Matrix57().template block(1,0), DefaultTraversal,CompleteUnrolling)); } VERIFY((test_assign< Map, AlignedMax, InnerStride<3*PacketSize> >, Matrix >(DefaultTraversal,PacketSize>4?InnerUnrolling:CompleteUnrolling))); VERIFY((test_assign(Matrix57(), Matrix()*Matrix(), InnerVectorizedTraversal, InnerUnrolling+CompleteUnrolling))); #endif } }; template struct vectorization_logic_half { static void run() {} }; EIGEN_DECLARE_TEST(vectorization_logic) { #ifdef EIGEN_VECTORIZE CALL_SUBTEST( vectorization_logic::run() ); CALL_SUBTEST( vectorization_logic::run() ); CALL_SUBTEST( vectorization_logic::run() ); CALL_SUBTEST( vectorization_logic >::run() ); CALL_SUBTEST( vectorization_logic >::run() ); CALL_SUBTEST( vectorization_logic_half::run() ); CALL_SUBTEST( vectorization_logic_half::run() ); CALL_SUBTEST( vectorization_logic_half::run() ); CALL_SUBTEST( vectorization_logic_half >::run() ); CALL_SUBTEST( vectorization_logic_half >::run() ); if(internal::packet_traits::Vectorizable) { VERIFY(test_assign(Matrix(),Matrix()+Matrix(), EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : LinearTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix(), EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : DefaultTraversal,CompleteUnrolling)); } if(internal::packet_traits::Vectorizable) { VERIFY(test_assign(Matrix(),Matrix()+Matrix(), EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : LinearTraversal,CompleteUnrolling)); VERIFY(test_redux(Matrix(), EIGEN_UNALIGNED_VECTORIZE ? LinearVectorizedTraversal : DefaultTraversal,CompleteUnrolling)); } #endif // EIGEN_VECTORIZE }