1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 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#include "main.h"
11
12template<int Alignment,typename VectorType> void map_class_vector(const VectorType& m)
13{
14  typedef typename VectorType::Index Index;
15  typedef typename VectorType::Scalar Scalar;
16
17  Index size = m.size();
18
19  VectorType v = VectorType::Random(size);
20
21  Index arraysize = 3*size;
22
23  Scalar* a_array = internal::aligned_new<Scalar>(arraysize+1);
24  Scalar* array = a_array;
25  if(Alignment!=Aligned)
26    array = (Scalar*)(ptrdiff_t(a_array) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
27
28  {
29    Map<VectorType, Alignment, InnerStride<3> > map(array, size);
30    map = v;
31    for(int i = 0; i < size; ++i)
32    {
33      VERIFY(array[3*i] == v[i]);
34      VERIFY(map[i] == v[i]);
35    }
36  }
37
38  {
39    Map<VectorType, Unaligned, InnerStride<Dynamic> > map(array, size, InnerStride<Dynamic>(2));
40    map = v;
41    for(int i = 0; i < size; ++i)
42    {
43      VERIFY(array[2*i] == v[i]);
44      VERIFY(map[i] == v[i]);
45    }
46  }
47
48  internal::aligned_delete(a_array, arraysize+1);
49}
50
51template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixType& _m)
52{
53  typedef typename MatrixType::Index Index;
54  typedef typename MatrixType::Scalar Scalar;
55
56  Index rows = _m.rows(), cols = _m.cols();
57
58  MatrixType m = MatrixType::Random(rows,cols);
59
60  Index arraysize = 2*(rows+4)*(cols+4);
61
62  Scalar* a_array = internal::aligned_new<Scalar>(arraysize+1);
63  Scalar* array = a_array;
64  if(Alignment!=Aligned)
65    array = (Scalar*)(ptrdiff_t(a_array) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
66
67  // test no inner stride and some dynamic outer stride
68  {
69    Map<MatrixType, Alignment, OuterStride<Dynamic> > map(array, rows, cols, OuterStride<Dynamic>(m.innerSize()+1));
70    map = m;
71    VERIFY(map.outerStride() == map.innerSize()+1);
72    for(int i = 0; i < m.outerSize(); ++i)
73      for(int j = 0; j < m.innerSize(); ++j)
74      {
75        VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
76        VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
77      }
78  }
79
80  // test no inner stride and an outer stride of +4. This is quite important as for fixed-size matrices,
81  // this allows to hit the special case where it's vectorizable.
82  {
83    enum {
84      InnerSize = MatrixType::InnerSizeAtCompileTime,
85      OuterStrideAtCompileTime = InnerSize==Dynamic ? Dynamic : InnerSize+4
86    };
87    Map<MatrixType, Alignment, OuterStride<OuterStrideAtCompileTime> >
88      map(array, rows, cols, OuterStride<OuterStrideAtCompileTime>(m.innerSize()+4));
89    map = m;
90    VERIFY(map.outerStride() == map.innerSize()+4);
91    for(int i = 0; i < m.outerSize(); ++i)
92      for(int j = 0; j < m.innerSize(); ++j)
93      {
94        VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
95        VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
96      }
97  }
98
99  // test both inner stride and outer stride
100  {
101    Map<MatrixType, Alignment, Stride<Dynamic,Dynamic> > map(array, rows, cols, Stride<Dynamic,Dynamic>(2*m.innerSize()+1, 2));
102    map = m;
103    VERIFY(map.outerStride() == 2*map.innerSize()+1);
104    VERIFY(map.innerStride() == 2);
105    for(int i = 0; i < m.outerSize(); ++i)
106      for(int j = 0; j < m.innerSize(); ++j)
107      {
108        VERIFY(array[map.outerStride()*i+map.innerStride()*j] == m.coeffByOuterInner(i,j));
109        VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
110      }
111  }
112
113  internal::aligned_delete(a_array, arraysize+1);
114}
115
116void test_mapstride()
117{
118  for(int i = 0; i < g_repeat; i++) {
119    int maxn = 30;
120    CALL_SUBTEST_1( map_class_vector<Aligned>(Matrix<float, 1, 1>()) );
121    CALL_SUBTEST_1( map_class_vector<Unaligned>(Matrix<float, 1, 1>()) );
122    CALL_SUBTEST_2( map_class_vector<Aligned>(Vector4d()) );
123    CALL_SUBTEST_2( map_class_vector<Unaligned>(Vector4d()) );
124    CALL_SUBTEST_3( map_class_vector<Aligned>(RowVector4f()) );
125    CALL_SUBTEST_3( map_class_vector<Unaligned>(RowVector4f()) );
126    CALL_SUBTEST_4( map_class_vector<Aligned>(VectorXcf(internal::random<int>(1,maxn))) );
127    CALL_SUBTEST_4( map_class_vector<Unaligned>(VectorXcf(internal::random<int>(1,maxn))) );
128    CALL_SUBTEST_5( map_class_vector<Aligned>(VectorXi(internal::random<int>(1,maxn))) );
129    CALL_SUBTEST_5( map_class_vector<Unaligned>(VectorXi(internal::random<int>(1,maxn))) );
130
131    CALL_SUBTEST_1( map_class_matrix<Aligned>(Matrix<float, 1, 1>()) );
132    CALL_SUBTEST_1( map_class_matrix<Unaligned>(Matrix<float, 1, 1>()) );
133    CALL_SUBTEST_2( map_class_matrix<Aligned>(Matrix4d()) );
134    CALL_SUBTEST_2( map_class_matrix<Unaligned>(Matrix4d()) );
135    CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,3,5>()) );
136    CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,3,5>()) );
137    CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,4,8>()) );
138    CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,4,8>()) );
139    CALL_SUBTEST_4( map_class_matrix<Aligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
140    CALL_SUBTEST_4( map_class_matrix<Unaligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
141    CALL_SUBTEST_5( map_class_matrix<Aligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
142    CALL_SUBTEST_5( map_class_matrix<Unaligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
143    CALL_SUBTEST_6( map_class_matrix<Aligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
144    CALL_SUBTEST_6( map_class_matrix<Unaligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
145
146    TEST_SET_BUT_UNUSED_VARIABLE(maxn);
147  }
148}
149