Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 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 | |
| 12 | template<int Alignment,typename VectorType> void map_class_vector(const VectorType& m) |
| 13 | { |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 14 | typedef typename VectorType::Scalar Scalar; |
| 15 | |
| 16 | Index size = m.size(); |
| 17 | |
| 18 | VectorType v = VectorType::Random(size); |
| 19 | |
| 20 | Index arraysize = 3*size; |
| 21 | |
| 22 | Scalar* a_array = internal::aligned_new<Scalar>(arraysize+1); |
| 23 | Scalar* array = a_array; |
| 24 | if(Alignment!=Aligned) |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 25 | array = (Scalar*)(internal::IntPtr(a_array) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real))); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 26 | |
| 27 | { |
| 28 | Map<VectorType, Alignment, InnerStride<3> > map(array, size); |
| 29 | map = v; |
| 30 | for(int i = 0; i < size; ++i) |
| 31 | { |
| 32 | VERIFY(array[3*i] == v[i]); |
| 33 | VERIFY(map[i] == v[i]); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | { |
| 38 | Map<VectorType, Unaligned, InnerStride<Dynamic> > map(array, size, InnerStride<Dynamic>(2)); |
| 39 | map = v; |
| 40 | for(int i = 0; i < size; ++i) |
| 41 | { |
| 42 | VERIFY(array[2*i] == v[i]); |
| 43 | VERIFY(map[i] == v[i]); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | internal::aligned_delete(a_array, arraysize+1); |
| 48 | } |
| 49 | |
| 50 | template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixType& _m) |
| 51 | { |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 52 | typedef typename MatrixType::Scalar Scalar; |
| 53 | |
| 54 | Index rows = _m.rows(), cols = _m.cols(); |
| 55 | |
| 56 | MatrixType m = MatrixType::Random(rows,cols); |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 57 | Scalar s1 = internal::random<Scalar>(); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 58 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 59 | Index arraysize = 4*(rows+4)*(cols+4); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 60 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 61 | Scalar* a_array1 = internal::aligned_new<Scalar>(arraysize+1); |
| 62 | Scalar* array1 = a_array1; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 63 | if(Alignment!=Aligned) |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 64 | array1 = (Scalar*)(internal::IntPtr(a_array1) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real))); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 65 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 66 | Scalar a_array2[256]; |
| 67 | Scalar* array2 = a_array2; |
| 68 | if(Alignment!=Aligned) |
| 69 | array2 = (Scalar*)(internal::IntPtr(a_array2) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real))); |
| 70 | else |
| 71 | array2 = (Scalar*)(((internal::UIntPtr(a_array2)+EIGEN_MAX_ALIGN_BYTES-1)/EIGEN_MAX_ALIGN_BYTES)*EIGEN_MAX_ALIGN_BYTES); |
| 72 | Index maxsize2 = a_array2 - array2 + 256; |
| 73 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 74 | // test no inner stride and some dynamic outer stride |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 75 | for(int k=0; k<2; ++k) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 76 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 77 | if(k==1 && (m.innerSize()+1)*m.outerSize() > maxsize2) |
| 78 | break; |
| 79 | Scalar* array = (k==0 ? array1 : array2); |
| 80 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 81 | Map<MatrixType, Alignment, OuterStride<Dynamic> > map(array, rows, cols, OuterStride<Dynamic>(m.innerSize()+1)); |
| 82 | map = m; |
| 83 | VERIFY(map.outerStride() == map.innerSize()+1); |
| 84 | for(int i = 0; i < m.outerSize(); ++i) |
| 85 | for(int j = 0; j < m.innerSize(); ++j) |
| 86 | { |
| 87 | VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j)); |
| 88 | VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j)); |
| 89 | } |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 90 | VERIFY_IS_APPROX(s1*map,s1*m); |
| 91 | map *= s1; |
| 92 | VERIFY_IS_APPROX(map,s1*m); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // test no inner stride and an outer stride of +4. This is quite important as for fixed-size matrices, |
| 96 | // this allows to hit the special case where it's vectorizable. |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 97 | for(int k=0; k<2; ++k) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 98 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 99 | if(k==1 && (m.innerSize()+4)*m.outerSize() > maxsize2) |
| 100 | break; |
| 101 | Scalar* array = (k==0 ? array1 : array2); |
| 102 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 103 | enum { |
| 104 | InnerSize = MatrixType::InnerSizeAtCompileTime, |
| 105 | OuterStrideAtCompileTime = InnerSize==Dynamic ? Dynamic : InnerSize+4 |
| 106 | }; |
| 107 | Map<MatrixType, Alignment, OuterStride<OuterStrideAtCompileTime> > |
| 108 | map(array, rows, cols, OuterStride<OuterStrideAtCompileTime>(m.innerSize()+4)); |
| 109 | map = m; |
| 110 | VERIFY(map.outerStride() == map.innerSize()+4); |
| 111 | for(int i = 0; i < m.outerSize(); ++i) |
| 112 | for(int j = 0; j < m.innerSize(); ++j) |
| 113 | { |
| 114 | VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j)); |
| 115 | VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j)); |
| 116 | } |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 117 | VERIFY_IS_APPROX(s1*map,s1*m); |
| 118 | map *= s1; |
| 119 | VERIFY_IS_APPROX(map,s1*m); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // test both inner stride and outer stride |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 123 | for(int k=0; k<2; ++k) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 124 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 125 | if(k==1 && (2*m.innerSize()+1)*(m.outerSize()*2) > maxsize2) |
| 126 | break; |
| 127 | Scalar* array = (k==0 ? array1 : array2); |
| 128 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 129 | Map<MatrixType, Alignment, Stride<Dynamic,Dynamic> > map(array, rows, cols, Stride<Dynamic,Dynamic>(2*m.innerSize()+1, 2)); |
| 130 | map = m; |
| 131 | VERIFY(map.outerStride() == 2*map.innerSize()+1); |
| 132 | VERIFY(map.innerStride() == 2); |
| 133 | for(int i = 0; i < m.outerSize(); ++i) |
| 134 | for(int j = 0; j < m.innerSize(); ++j) |
| 135 | { |
| 136 | VERIFY(array[map.outerStride()*i+map.innerStride()*j] == m.coeffByOuterInner(i,j)); |
| 137 | VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j)); |
| 138 | } |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 139 | VERIFY_IS_APPROX(s1*map,s1*m); |
| 140 | map *= s1; |
| 141 | VERIFY_IS_APPROX(map,s1*m); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 144 | // test inner stride and no outer stride |
| 145 | for(int k=0; k<2; ++k) |
| 146 | { |
| 147 | if(k==1 && (m.innerSize()*2)*m.outerSize() > maxsize2) |
| 148 | break; |
| 149 | Scalar* array = (k==0 ? array1 : array2); |
| 150 | |
| 151 | Map<MatrixType, Alignment, InnerStride<Dynamic> > map(array, rows, cols, InnerStride<Dynamic>(2)); |
| 152 | map = m; |
| 153 | VERIFY(map.outerStride() == map.innerSize()*2); |
| 154 | for(int i = 0; i < m.outerSize(); ++i) |
| 155 | for(int j = 0; j < m.innerSize(); ++j) |
| 156 | { |
| 157 | VERIFY(array[map.innerSize()*i*2+j*2] == m.coeffByOuterInner(i,j)); |
| 158 | VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j)); |
| 159 | } |
| 160 | VERIFY_IS_APPROX(s1*map,s1*m); |
| 161 | map *= s1; |
| 162 | VERIFY_IS_APPROX(map,s1*m); |
| 163 | } |
| 164 | |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 165 | // test negative strides |
| 166 | { |
| 167 | Matrix<Scalar,Dynamic,1>::Map(a_array1, arraysize+1).setRandom(); |
| 168 | Index outerstride = m.innerSize()+4; |
| 169 | Scalar* array = array1; |
| 170 | |
| 171 | { |
| 172 | Map<MatrixType, Alignment, OuterStride<> > map1(array, rows, cols, OuterStride<>( outerstride)); |
| 173 | Map<MatrixType, Unaligned, OuterStride<> > map2(array+(m.outerSize()-1)*outerstride, rows, cols, OuterStride<>(-outerstride)); |
| 174 | if(MatrixType::IsRowMajor) VERIFY_IS_APPROX(map1.colwise().reverse(), map2); |
| 175 | else VERIFY_IS_APPROX(map1.rowwise().reverse(), map2); |
| 176 | } |
| 177 | |
| 178 | { |
| 179 | Map<MatrixType, Alignment, OuterStride<> > map1(array, rows, cols, OuterStride<>( outerstride)); |
| 180 | Map<MatrixType, Unaligned, Stride<Dynamic,Dynamic> > map2(array+(m.outerSize()-1)*outerstride+m.innerSize()-1, rows, cols, Stride<Dynamic,Dynamic>(-outerstride,-1)); |
| 181 | VERIFY_IS_APPROX(map1.reverse(), map2); |
| 182 | } |
| 183 | |
| 184 | { |
| 185 | Map<MatrixType, Alignment, OuterStride<> > map1(array, rows, cols, OuterStride<>( outerstride)); |
| 186 | Map<MatrixType, Unaligned, Stride<Dynamic,-1> > map2(array+(m.outerSize()-1)*outerstride+m.innerSize()-1, rows, cols, Stride<Dynamic,-1>(-outerstride,-1)); |
| 187 | VERIFY_IS_APPROX(map1.reverse(), map2); |
| 188 | } |
| 189 | } |
| 190 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 191 | internal::aligned_delete(a_array1, arraysize+1); |
| 192 | } |
| 193 | |
| 194 | // Additional tests for inner-stride but no outer-stride |
| 195 | template<int> |
| 196 | void bug1453() |
| 197 | { |
| 198 | const int data[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 199 | typedef Matrix<int,Dynamic,Dynamic,RowMajor> RowMatrixXi; |
| 200 | typedef Matrix<int,2,3,ColMajor> ColMatrix23i; |
| 201 | typedef Matrix<int,3,2,ColMajor> ColMatrix32i; |
| 202 | typedef Matrix<int,2,3,RowMajor> RowMatrix23i; |
| 203 | typedef Matrix<int,3,2,RowMajor> RowMatrix32i; |
| 204 | |
| 205 | VERIFY_IS_APPROX(MatrixXi::Map(data, 2, 3, InnerStride<2>()), MatrixXi::Map(data, 2, 3, Stride<4,2>())); |
| 206 | VERIFY_IS_APPROX(MatrixXi::Map(data, 2, 3, InnerStride<>(2)), MatrixXi::Map(data, 2, 3, Stride<4,2>())); |
| 207 | VERIFY_IS_APPROX(MatrixXi::Map(data, 3, 2, InnerStride<2>()), MatrixXi::Map(data, 3, 2, Stride<6,2>())); |
| 208 | VERIFY_IS_APPROX(MatrixXi::Map(data, 3, 2, InnerStride<>(2)), MatrixXi::Map(data, 3, 2, Stride<6,2>())); |
| 209 | |
| 210 | VERIFY_IS_APPROX(RowMatrixXi::Map(data, 2, 3, InnerStride<2>()), RowMatrixXi::Map(data, 2, 3, Stride<6,2>())); |
| 211 | VERIFY_IS_APPROX(RowMatrixXi::Map(data, 2, 3, InnerStride<>(2)), RowMatrixXi::Map(data, 2, 3, Stride<6,2>())); |
| 212 | VERIFY_IS_APPROX(RowMatrixXi::Map(data, 3, 2, InnerStride<2>()), RowMatrixXi::Map(data, 3, 2, Stride<4,2>())); |
| 213 | VERIFY_IS_APPROX(RowMatrixXi::Map(data, 3, 2, InnerStride<>(2)), RowMatrixXi::Map(data, 3, 2, Stride<4,2>())); |
| 214 | |
| 215 | VERIFY_IS_APPROX(ColMatrix23i::Map(data, InnerStride<2>()), MatrixXi::Map(data, 2, 3, Stride<4,2>())); |
| 216 | VERIFY_IS_APPROX(ColMatrix23i::Map(data, InnerStride<>(2)), MatrixXi::Map(data, 2, 3, Stride<4,2>())); |
| 217 | VERIFY_IS_APPROX(ColMatrix32i::Map(data, InnerStride<2>()), MatrixXi::Map(data, 3, 2, Stride<6,2>())); |
| 218 | VERIFY_IS_APPROX(ColMatrix32i::Map(data, InnerStride<>(2)), MatrixXi::Map(data, 3, 2, Stride<6,2>())); |
| 219 | |
| 220 | VERIFY_IS_APPROX(RowMatrix23i::Map(data, InnerStride<2>()), RowMatrixXi::Map(data, 2, 3, Stride<6,2>())); |
| 221 | VERIFY_IS_APPROX(RowMatrix23i::Map(data, InnerStride<>(2)), RowMatrixXi::Map(data, 2, 3, Stride<6,2>())); |
| 222 | VERIFY_IS_APPROX(RowMatrix32i::Map(data, InnerStride<2>()), RowMatrixXi::Map(data, 3, 2, Stride<4,2>())); |
| 223 | VERIFY_IS_APPROX(RowMatrix32i::Map(data, InnerStride<>(2)), RowMatrixXi::Map(data, 3, 2, Stride<4,2>())); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 226 | EIGEN_DECLARE_TEST(mapstride) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 227 | { |
| 228 | for(int i = 0; i < g_repeat; i++) { |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 229 | int maxn = 3; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 230 | CALL_SUBTEST_1( map_class_vector<Aligned>(Matrix<float, 1, 1>()) ); |
| 231 | CALL_SUBTEST_1( map_class_vector<Unaligned>(Matrix<float, 1, 1>()) ); |
| 232 | CALL_SUBTEST_2( map_class_vector<Aligned>(Vector4d()) ); |
| 233 | CALL_SUBTEST_2( map_class_vector<Unaligned>(Vector4d()) ); |
| 234 | CALL_SUBTEST_3( map_class_vector<Aligned>(RowVector4f()) ); |
| 235 | CALL_SUBTEST_3( map_class_vector<Unaligned>(RowVector4f()) ); |
| 236 | CALL_SUBTEST_4( map_class_vector<Aligned>(VectorXcf(internal::random<int>(1,maxn))) ); |
| 237 | CALL_SUBTEST_4( map_class_vector<Unaligned>(VectorXcf(internal::random<int>(1,maxn))) ); |
| 238 | CALL_SUBTEST_5( map_class_vector<Aligned>(VectorXi(internal::random<int>(1,maxn))) ); |
| 239 | CALL_SUBTEST_5( map_class_vector<Unaligned>(VectorXi(internal::random<int>(1,maxn))) ); |
| 240 | |
| 241 | CALL_SUBTEST_1( map_class_matrix<Aligned>(Matrix<float, 1, 1>()) ); |
| 242 | CALL_SUBTEST_1( map_class_matrix<Unaligned>(Matrix<float, 1, 1>()) ); |
| 243 | CALL_SUBTEST_2( map_class_matrix<Aligned>(Matrix4d()) ); |
| 244 | CALL_SUBTEST_2( map_class_matrix<Unaligned>(Matrix4d()) ); |
| 245 | CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,3,5>()) ); |
| 246 | CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,3,5>()) ); |
| 247 | CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,4,8>()) ); |
| 248 | CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,4,8>()) ); |
| 249 | CALL_SUBTEST_4( map_class_matrix<Aligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) ); |
| 250 | CALL_SUBTEST_4( map_class_matrix<Unaligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) ); |
| 251 | CALL_SUBTEST_5( map_class_matrix<Aligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) ); |
| 252 | CALL_SUBTEST_5( map_class_matrix<Unaligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) ); |
| 253 | CALL_SUBTEST_6( map_class_matrix<Aligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) ); |
| 254 | CALL_SUBTEST_6( map_class_matrix<Unaligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) ); |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 255 | |
| 256 | CALL_SUBTEST_5( bug1453<0>() ); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 257 | |
| 258 | TEST_SET_BUT_UNUSED_VARIABLE(maxn); |
| 259 | } |
| 260 | } |