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