diff --git a/test/dense_storage.cpp b/test/dense_storage.cpp index 398cb3266..ec78f0163 100644 --- a/test/dense_storage.cpp +++ b/test/dense_storage.cpp @@ -100,11 +100,12 @@ void dense_storage_alignment() VERIFY_IS_EQUAL( (std::alignment_of >::value), Alignment); const std::size_t default_alignment = internal::compute_default_alignment::value; - - VERIFY_IS_EQUAL( (std::alignment_of >::value), default_alignment); - VERIFY_IS_EQUAL( (std::alignment_of >::value), default_alignment); - struct Nested2 { Matrix mat; }; - VERIFY_IS_EQUAL(std::alignment_of::value, default_alignment); + if (default_alignment > 0) { + VERIFY_IS_EQUAL( (std::alignment_of >::value), default_alignment); + VERIFY_IS_EQUAL( (std::alignment_of >::value), default_alignment); + struct Nested2 { Matrix mat; }; + VERIFY_IS_EQUAL(std::alignment_of::value, default_alignment); + } } template diff --git a/test/dynalloc.cpp b/test/dynalloc.cpp index 23c90a7b5..cdc10ee89 100644 --- a/test/dynalloc.cpp +++ b/test/dynalloc.cpp @@ -20,9 +20,12 @@ typedef Matrix Vector8f; void check_handmade_aligned_malloc() { + // Hand-make alignment needs at least sizeof(void*) to store the offset. + constexpr int alignment = (std::max)(EIGEN_DEFAULT_ALIGN_BYTES, sizeof(void*)); + for(int i = 1; i < 1000; i++) { - char *p = (char*)internal::handmade_aligned_malloc(i); + char *p = (char*)internal::handmade_aligned_malloc(i, alignment); VERIFY(internal::UIntPtr(p)%ALIGNMENT==0); // if the buffer is wrongly allocated this will give a bad write --> check with valgrind for(int j = 0; j < i; j++) p[j]=0; diff --git a/test/evaluators.cpp b/test/evaluators.cpp index 2810cd265..95bfb45d3 100644 --- a/test/evaluators.cpp +++ b/test/evaluators.cpp @@ -510,7 +510,9 @@ EIGEN_DECLARE_TEST(evaluators) const size_t K = 2; const size_t N = 5; float *destMem = new float[(M*N) + 1]; - float *dest = (internal::UIntPtr(destMem)%EIGEN_MAX_ALIGN_BYTES) == 0 ? destMem+1 : destMem; + // In case of no alignment, avoid division by zero. + constexpr int alignment = (std::max)(EIGEN_MAX_ALIGN_BYTES, 1); + float *dest = (internal::UIntPtr(destMem)%alignment) == 0 ? destMem+1 : destMem; const Matrix a = Matrix::Random(M, K); const Matrix b = Matrix::Random(K, N); diff --git a/test/mapped_matrix.cpp b/test/mapped_matrix.cpp index b5a13bff1..1dd695911 100644 --- a/test/mapped_matrix.cpp +++ b/test/mapped_matrix.cpp @@ -20,7 +20,9 @@ template void map_class_vector(const VectorType& m) Scalar* array1 = internal::aligned_new(size); Scalar* array2 = internal::aligned_new(size); Scalar* array3 = new Scalar[size+1]; - Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3; + // In case of no alignment, avoid division by zero. + constexpr int alignment = (std::max)(EIGEN_MAX_ALIGN_BYTES, 1); + Scalar* array3unaligned = (internal::UIntPtr(array3)%alignment) == 0 ? array3+1 : array3; Scalar array4[EIGEN_TESTMAP_MAX_SIZE]; Map(array1, size) = VectorType::Random(size); @@ -60,7 +62,9 @@ template void map_class_matrix(const MatrixType& m) Scalar* array3 = new Scalar[size+1]; Index sizep1 = size + 1; // <- without this temporary MSVC 2103 generates bad code for(Index i = 0; i < sizep1; i++) array3[i] = Scalar(1); - Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3; + // In case of no alignment, avoid division by zero. + constexpr int alignment = (std::max)(EIGEN_MAX_ALIGN_BYTES, 1); + Scalar* array3unaligned = (internal::UIntPtr(array3)%alignment) == 0 ? array3+1 : array3; Scalar array4[256]; if(size<=256) for(int i = 0; i < size; i++) array4[i] = Scalar(1); @@ -123,7 +127,9 @@ template void map_static_methods(const VectorType& m) Scalar* array1 = internal::aligned_new(size); Scalar* array2 = internal::aligned_new(size); Scalar* array3 = new Scalar[size+1]; - Scalar* array3unaligned = internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES == 0 ? array3+1 : array3; + // In case of no alignment, avoid division by zero. + constexpr int alignment = (std::max)(EIGEN_MAX_ALIGN_BYTES, 1); + Scalar* array3unaligned = (internal::UIntPtr(array3)%alignment) == 0 ? array3+1 : array3; VectorType::MapAligned(array1, size) = VectorType::Random(size); VectorType::Map(array2, size) = VectorType::Map(array1, size); diff --git a/test/mapstride.cpp b/test/mapstride.cpp index 93e880acc..42ceb0cf8 100644 --- a/test/mapstride.cpp +++ b/test/mapstride.cpp @@ -65,10 +65,13 @@ template void map_class_matrix(const MatrixTy Scalar a_array2[256]; Scalar* array2 = a_array2; - if(Alignment!=Aligned) + if(Alignment!=Aligned) { array2 = (Scalar*)(internal::IntPtr(a_array2) + (internal::packet_traits::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits::Real))); - else - array2 = (Scalar*)(((internal::UIntPtr(a_array2)+EIGEN_MAX_ALIGN_BYTES-1)/EIGEN_MAX_ALIGN_BYTES)*EIGEN_MAX_ALIGN_BYTES); + } else { + // In case there is no alignment, default to pointing to the start. + constexpr int alignment = (std::max)(EIGEN_MAX_ALIGN_BYTES, 1); + array2 = (Scalar*)(((internal::UIntPtr(a_array2)+alignment-1)/alignment)*alignment); + } Index maxsize2 = a_array2 - array2 + 256; // test no inner stride and some dynamic outer stride