xref: /aosp_15_r20/external/eigen/test/mapped_matrix.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2006-2010 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #ifndef EIGEN_NO_STATIC_ASSERT
11*bf2c3715SXin Li #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them
12*bf2c3715SXin Li #endif
13*bf2c3715SXin Li 
14*bf2c3715SXin Li #include "main.h"
15*bf2c3715SXin Li 
16*bf2c3715SXin Li #define EIGEN_TESTMAP_MAX_SIZE 256
17*bf2c3715SXin Li 
map_class_vector(const VectorType & m)18*bf2c3715SXin Li template<typename VectorType> void map_class_vector(const VectorType& m)
19*bf2c3715SXin Li {
20*bf2c3715SXin Li   typedef typename VectorType::Scalar Scalar;
21*bf2c3715SXin Li 
22*bf2c3715SXin Li   Index size = m.size();
23*bf2c3715SXin Li 
24*bf2c3715SXin Li   Scalar* array1 = internal::aligned_new<Scalar>(size);
25*bf2c3715SXin Li   Scalar* array2 = internal::aligned_new<Scalar>(size);
26*bf2c3715SXin Li   Scalar* array3 = new Scalar[size+1];
27*bf2c3715SXin Li   Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
28*bf2c3715SXin Li   Scalar  array4[EIGEN_TESTMAP_MAX_SIZE];
29*bf2c3715SXin Li 
30*bf2c3715SXin Li   Map<VectorType, AlignedMax>(array1, size) = VectorType::Random(size);
31*bf2c3715SXin Li   Map<VectorType, AlignedMax>(array2, size) = Map<VectorType,AlignedMax>(array1, size);
32*bf2c3715SXin Li   Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size);
33*bf2c3715SXin Li   Map<VectorType>(array4, size)          = Map<VectorType,AlignedMax>(array1, size);
34*bf2c3715SXin Li   VectorType ma1 = Map<VectorType, AlignedMax>(array1, size);
35*bf2c3715SXin Li   VectorType ma2 = Map<VectorType, AlignedMax>(array2, size);
36*bf2c3715SXin Li   VectorType ma3 = Map<VectorType>(array3unaligned, size);
37*bf2c3715SXin Li   VectorType ma4 = Map<VectorType>(array4, size);
38*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma2);
39*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma3);
40*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma4);
41*bf2c3715SXin Li   #ifdef EIGEN_VECTORIZE
42*bf2c3715SXin Li   if(internal::packet_traits<Scalar>::Vectorizable && size>=AlignedMax)
43*bf2c3715SXin Li     VERIFY_RAISES_ASSERT((Map<VectorType,AlignedMax>(array3unaligned, size)))
44*bf2c3715SXin Li   #endif
45*bf2c3715SXin Li 
46*bf2c3715SXin Li   internal::aligned_delete(array1, size);
47*bf2c3715SXin Li   internal::aligned_delete(array2, size);
48*bf2c3715SXin Li   delete[] array3;
49*bf2c3715SXin Li }
50*bf2c3715SXin Li 
map_class_matrix(const MatrixType & m)51*bf2c3715SXin Li template<typename MatrixType> void map_class_matrix(const MatrixType& m)
52*bf2c3715SXin Li {
53*bf2c3715SXin Li   typedef typename MatrixType::Scalar Scalar;
54*bf2c3715SXin Li 
55*bf2c3715SXin Li   Index rows = m.rows(), cols = m.cols(), size = rows*cols;
56*bf2c3715SXin Li   Scalar s1 = internal::random<Scalar>();
57*bf2c3715SXin Li 
58*bf2c3715SXin Li   // array1 and array2 -> aligned heap allocation
59*bf2c3715SXin Li   Scalar* array1 = internal::aligned_new<Scalar>(size);
60*bf2c3715SXin Li   for(int i = 0; i < size; i++) array1[i] = Scalar(1);
61*bf2c3715SXin Li   Scalar* array2 = internal::aligned_new<Scalar>(size);
62*bf2c3715SXin Li   for(int i = 0; i < size; i++) array2[i] = Scalar(1);
63*bf2c3715SXin Li   // array3unaligned -> unaligned pointer to heap
64*bf2c3715SXin Li   Scalar* array3 = new Scalar[size+1];
65*bf2c3715SXin Li   Index sizep1 = size + 1; // <- without this temporary MSVC 2103 generates bad code
66*bf2c3715SXin Li   for(Index i = 0; i < sizep1; i++) array3[i] = Scalar(1);
67*bf2c3715SXin Li   Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
68*bf2c3715SXin Li   Scalar array4[256];
69*bf2c3715SXin Li   if(size<=256)
70*bf2c3715SXin Li     for(int i = 0; i < size; i++) array4[i] = Scalar(1);
71*bf2c3715SXin Li 
72*bf2c3715SXin Li   Map<MatrixType> map1(array1, rows, cols);
73*bf2c3715SXin Li   Map<MatrixType, AlignedMax> map2(array2, rows, cols);
74*bf2c3715SXin Li   Map<MatrixType> map3(array3unaligned, rows, cols);
75*bf2c3715SXin Li   Map<MatrixType> map4(array4, rows, cols);
76*bf2c3715SXin Li 
77*bf2c3715SXin Li   VERIFY_IS_EQUAL(map1, MatrixType::Ones(rows,cols));
78*bf2c3715SXin Li   VERIFY_IS_EQUAL(map2, MatrixType::Ones(rows,cols));
79*bf2c3715SXin Li   VERIFY_IS_EQUAL(map3, MatrixType::Ones(rows,cols));
80*bf2c3715SXin Li   map1 = MatrixType::Random(rows,cols);
81*bf2c3715SXin Li   map2 = map1;
82*bf2c3715SXin Li   map3 = map1;
83*bf2c3715SXin Li   MatrixType ma1 = map1;
84*bf2c3715SXin Li   MatrixType ma2 = map2;
85*bf2c3715SXin Li   MatrixType ma3 = map3;
86*bf2c3715SXin Li   VERIFY_IS_EQUAL(map1, map2);
87*bf2c3715SXin Li   VERIFY_IS_EQUAL(map1, map3);
88*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma2);
89*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma3);
90*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, map3);
91*bf2c3715SXin Li 
92*bf2c3715SXin Li   VERIFY_IS_APPROX(s1*map1, s1*map2);
93*bf2c3715SXin Li   VERIFY_IS_APPROX(s1*ma1, s1*ma2);
94*bf2c3715SXin Li   VERIFY_IS_EQUAL(s1*ma1, s1*ma3);
95*bf2c3715SXin Li   VERIFY_IS_APPROX(s1*map1, s1*map3);
96*bf2c3715SXin Li 
97*bf2c3715SXin Li   map2 *= s1;
98*bf2c3715SXin Li   map3 *= s1;
99*bf2c3715SXin Li   VERIFY_IS_APPROX(s1*map1, map2);
100*bf2c3715SXin Li   VERIFY_IS_APPROX(s1*map1, map3);
101*bf2c3715SXin Li 
102*bf2c3715SXin Li   if(size<=256)
103*bf2c3715SXin Li   {
104*bf2c3715SXin Li     VERIFY_IS_EQUAL(map4, MatrixType::Ones(rows,cols));
105*bf2c3715SXin Li     map4 = map1;
106*bf2c3715SXin Li     MatrixType ma4 = map4;
107*bf2c3715SXin Li     VERIFY_IS_EQUAL(map1, map4);
108*bf2c3715SXin Li     VERIFY_IS_EQUAL(ma1, map4);
109*bf2c3715SXin Li     VERIFY_IS_EQUAL(ma1, ma4);
110*bf2c3715SXin Li     VERIFY_IS_APPROX(s1*map1, s1*map4);
111*bf2c3715SXin Li 
112*bf2c3715SXin Li     map4 *= s1;
113*bf2c3715SXin Li     VERIFY_IS_APPROX(s1*map1, map4);
114*bf2c3715SXin Li   }
115*bf2c3715SXin Li 
116*bf2c3715SXin Li   internal::aligned_delete(array1, size);
117*bf2c3715SXin Li   internal::aligned_delete(array2, size);
118*bf2c3715SXin Li   delete[] array3;
119*bf2c3715SXin Li }
120*bf2c3715SXin Li 
map_static_methods(const VectorType & m)121*bf2c3715SXin Li template<typename VectorType> void map_static_methods(const VectorType& m)
122*bf2c3715SXin Li {
123*bf2c3715SXin Li   typedef typename VectorType::Scalar Scalar;
124*bf2c3715SXin Li 
125*bf2c3715SXin Li   Index size = m.size();
126*bf2c3715SXin Li 
127*bf2c3715SXin Li   Scalar* array1 = internal::aligned_new<Scalar>(size);
128*bf2c3715SXin Li   Scalar* array2 = internal::aligned_new<Scalar>(size);
129*bf2c3715SXin Li   Scalar* array3 = new Scalar[size+1];
130*bf2c3715SXin Li   Scalar* array3unaligned = internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES == 0 ? array3+1 : array3;
131*bf2c3715SXin Li 
132*bf2c3715SXin Li   VectorType::MapAligned(array1, size) = VectorType::Random(size);
133*bf2c3715SXin Li   VectorType::Map(array2, size) = VectorType::Map(array1, size);
134*bf2c3715SXin Li   VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size);
135*bf2c3715SXin Li   VectorType ma1 = VectorType::Map(array1, size);
136*bf2c3715SXin Li   VectorType ma2 = VectorType::MapAligned(array2, size);
137*bf2c3715SXin Li   VectorType ma3 = VectorType::Map(array3unaligned, size);
138*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma2);
139*bf2c3715SXin Li   VERIFY_IS_EQUAL(ma1, ma3);
140*bf2c3715SXin Li 
141*bf2c3715SXin Li   internal::aligned_delete(array1, size);
142*bf2c3715SXin Li   internal::aligned_delete(array2, size);
143*bf2c3715SXin Li   delete[] array3;
144*bf2c3715SXin Li }
145*bf2c3715SXin Li 
check_const_correctness(const PlainObjectType &)146*bf2c3715SXin Li template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
147*bf2c3715SXin Li {
148*bf2c3715SXin Li   // there's a lot that we can't test here while still having this test compile!
149*bf2c3715SXin Li   // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
150*bf2c3715SXin Li   // CMake can help with that.
151*bf2c3715SXin Li 
152*bf2c3715SXin Li   // verify that map-to-const don't have LvalueBit
153*bf2c3715SXin Li   typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
154*bf2c3715SXin Li   VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
155*bf2c3715SXin Li   VERIFY( !(internal::traits<Map<ConstPlainObjectType, AlignedMax> >::Flags & LvalueBit) );
156*bf2c3715SXin Li   VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
157*bf2c3715SXin Li   VERIFY( !(Map<ConstPlainObjectType, AlignedMax>::Flags & LvalueBit) );
158*bf2c3715SXin Li }
159*bf2c3715SXin Li 
160*bf2c3715SXin Li template<typename Scalar>
map_not_aligned_on_scalar()161*bf2c3715SXin Li void map_not_aligned_on_scalar()
162*bf2c3715SXin Li {
163*bf2c3715SXin Li   typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
164*bf2c3715SXin Li   Index size = 11;
165*bf2c3715SXin Li   Scalar* array1 = internal::aligned_new<Scalar>((size+1)*(size+1)+1);
166*bf2c3715SXin Li   Scalar* array2 = reinterpret_cast<Scalar*>(sizeof(Scalar)/2+std::size_t(array1));
167*bf2c3715SXin Li   Map<MatrixType,0,OuterStride<> > map2(array2, size, size, OuterStride<>(size+1));
168*bf2c3715SXin Li   MatrixType m2 = MatrixType::Random(size,size);
169*bf2c3715SXin Li   map2 = m2;
170*bf2c3715SXin Li   VERIFY_IS_EQUAL(m2, map2);
171*bf2c3715SXin Li 
172*bf2c3715SXin Li   typedef Matrix<Scalar,Dynamic,1> VectorType;
173*bf2c3715SXin Li   Map<VectorType> map3(array2, size);
174*bf2c3715SXin Li   MatrixType v3 = VectorType::Random(size);
175*bf2c3715SXin Li   map3 = v3;
176*bf2c3715SXin Li   VERIFY_IS_EQUAL(v3, map3);
177*bf2c3715SXin Li 
178*bf2c3715SXin Li   internal::aligned_delete(array1, (size+1)*(size+1)+1);
179*bf2c3715SXin Li }
180*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(mapped_matrix)181*bf2c3715SXin Li EIGEN_DECLARE_TEST(mapped_matrix)
182*bf2c3715SXin Li {
183*bf2c3715SXin Li   for(int i = 0; i < g_repeat; i++) {
184*bf2c3715SXin Li     CALL_SUBTEST_1( map_class_vector(Matrix<float, 1, 1>()) );
185*bf2c3715SXin Li     CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) );
186*bf2c3715SXin Li     CALL_SUBTEST_2( map_class_vector(Vector4d()) );
187*bf2c3715SXin Li     CALL_SUBTEST_2( map_class_vector(VectorXd(13)) );
188*bf2c3715SXin Li     CALL_SUBTEST_2( check_const_correctness(Matrix4d()) );
189*bf2c3715SXin Li     CALL_SUBTEST_3( map_class_vector(RowVector4f()) );
190*bf2c3715SXin Li     CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) );
191*bf2c3715SXin Li     CALL_SUBTEST_5( map_class_vector(VectorXi(12)) );
192*bf2c3715SXin Li     CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
193*bf2c3715SXin Li 
194*bf2c3715SXin Li     CALL_SUBTEST_1( map_class_matrix(Matrix<float, 1, 1>()) );
195*bf2c3715SXin Li     CALL_SUBTEST_2( map_class_matrix(Matrix4d()) );
196*bf2c3715SXin Li     CALL_SUBTEST_11( map_class_matrix(Matrix<float,3,5>()) );
197*bf2c3715SXin Li     CALL_SUBTEST_4( map_class_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
198*bf2c3715SXin Li     CALL_SUBTEST_5( map_class_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
199*bf2c3715SXin Li 
200*bf2c3715SXin Li     CALL_SUBTEST_6( map_static_methods(Matrix<double, 1, 1>()) );
201*bf2c3715SXin Li     CALL_SUBTEST_7( map_static_methods(Vector3f()) );
202*bf2c3715SXin Li     CALL_SUBTEST_8( map_static_methods(RowVector3d()) );
203*bf2c3715SXin Li     CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) );
204*bf2c3715SXin Li     CALL_SUBTEST_10( map_static_methods(VectorXf(12)) );
205*bf2c3715SXin Li     CALL_SUBTEST_11( map_not_aligned_on_scalar<double>() );
206*bf2c3715SXin Li   }
207*bf2c3715SXin Li }
208