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