mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-16 14:49:39 +08:00
Fix unaligned scalar alignment UB.
This commit is contained in:
parent
66e8f38891
commit
0ae7d7a361
@ -193,6 +193,8 @@ template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
|
||||
EIGEN_DEVICE_FUNC
|
||||
void checkSanity(std::enable_if_t<(internal::traits<T>::Alignment>0),void*> = 0) const
|
||||
{
|
||||
// Pointer must be aligned to the Scalar type, otherwise we get UB.
|
||||
eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned");
|
||||
#if EIGEN_MAX_ALIGN_BYTES>0
|
||||
// innerStride() is not set yet when this function is called, so we optimistically assume the lowest plausible value:
|
||||
const Index minInnerStride = InnerStrideAtCompileTime == Dynamic ? 1 : Index(InnerStrideAtCompileTime);
|
||||
@ -205,7 +207,7 @@ template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
|
||||
template<typename T>
|
||||
EIGEN_DEVICE_FUNC
|
||||
void checkSanity(std::enable_if_t<internal::traits<T>::Alignment==0,void*> = 0) const
|
||||
{}
|
||||
{ eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned"); }
|
||||
|
||||
PointerType m_data;
|
||||
const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
|
||||
|
@ -68,12 +68,12 @@ template<typename MatrixType> void map_class_matrix(const MatrixType& m)
|
||||
Scalar array4[256];
|
||||
if(size<=256)
|
||||
for(int i = 0; i < size; i++) array4[i] = Scalar(1);
|
||||
|
||||
|
||||
Map<MatrixType> map1(array1, rows, cols);
|
||||
Map<MatrixType, AlignedMax> map2(array2, rows, cols);
|
||||
Map<MatrixType> map3(array3unaligned, rows, cols);
|
||||
Map<MatrixType> map4(array4, rows, cols);
|
||||
|
||||
|
||||
VERIFY_IS_EQUAL(map1, MatrixType::Ones(rows,cols));
|
||||
VERIFY_IS_EQUAL(map2, MatrixType::Ones(rows,cols));
|
||||
VERIFY_IS_EQUAL(map3, MatrixType::Ones(rows,cols));
|
||||
@ -88,17 +88,17 @@ template<typename MatrixType> void map_class_matrix(const MatrixType& m)
|
||||
VERIFY_IS_EQUAL(ma1, ma2);
|
||||
VERIFY_IS_EQUAL(ma1, ma3);
|
||||
VERIFY_IS_EQUAL(ma1, map3);
|
||||
|
||||
|
||||
VERIFY_IS_APPROX(s1*map1, s1*map2);
|
||||
VERIFY_IS_APPROX(s1*ma1, s1*ma2);
|
||||
VERIFY_IS_EQUAL(s1*ma1, s1*ma3);
|
||||
VERIFY_IS_APPROX(s1*map1, s1*map3);
|
||||
|
||||
|
||||
map2 *= s1;
|
||||
map3 *= s1;
|
||||
VERIFY_IS_APPROX(s1*map1, map2);
|
||||
VERIFY_IS_APPROX(s1*map1, map3);
|
||||
|
||||
|
||||
if(size<=256)
|
||||
{
|
||||
VERIFY_IS_EQUAL(map4, MatrixType::Ones(rows,cols));
|
||||
@ -108,7 +108,7 @@ template<typename MatrixType> void map_class_matrix(const MatrixType& m)
|
||||
VERIFY_IS_EQUAL(ma1, map4);
|
||||
VERIFY_IS_EQUAL(ma1, ma4);
|
||||
VERIFY_IS_APPROX(s1*map1, s1*map4);
|
||||
|
||||
|
||||
map4 *= s1;
|
||||
VERIFY_IS_APPROX(s1*map1, map4);
|
||||
}
|
||||
@ -159,27 +159,6 @@ template<typename PlainObjectType> void check_const_correctness(const PlainObjec
|
||||
VERIFY( !(Map<ConstPlainObjectType, AlignedMax>::Flags & LvalueBit) );
|
||||
}
|
||||
|
||||
template<typename Scalar>
|
||||
void map_not_aligned_on_scalar()
|
||||
{
|
||||
typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
|
||||
Index size = 11;
|
||||
Scalar* array1 = internal::aligned_new<Scalar>((size+1)*(size+1)+1);
|
||||
Scalar* array2 = reinterpret_cast<Scalar*>(sizeof(Scalar)/2+std::size_t(array1));
|
||||
Map<MatrixType,0,OuterStride<> > map2(array2, size, size, OuterStride<>(size+1));
|
||||
MatrixType m2 = MatrixType::Random(size,size);
|
||||
map2 = m2;
|
||||
VERIFY_IS_EQUAL(m2, map2);
|
||||
|
||||
typedef Matrix<Scalar,Dynamic,1> VectorType;
|
||||
Map<VectorType> map3(array2, size);
|
||||
MatrixType v3 = VectorType::Random(size);
|
||||
map3 = v3;
|
||||
VERIFY_IS_EQUAL(v3, map3);
|
||||
|
||||
internal::aligned_delete(array1, (size+1)*(size+1)+1);
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(mapped_matrix)
|
||||
{
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
@ -204,6 +183,5 @@ EIGEN_DECLARE_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<double>() );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user