xref: /aosp_15_r20/external/eigen/test/mapstaticmethods.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Benoit Jacob <[email protected]>
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 // GCC<=4.8 has spurious shadow warnings, because `ptr` re-appears inside template instantiations
13 // workaround: put these in an anonymous namespace
14 namespace {
15 float *ptr;
16 const float *const_ptr;
17 }
18 
19 template<typename PlainObjectType,
20          bool IsDynamicSize = PlainObjectType::SizeAtCompileTime == Dynamic,
21          bool IsVector = PlainObjectType::IsVectorAtCompileTime
22 >
23 struct mapstaticmethods_impl {};
24 
25 template<typename PlainObjectType, bool IsVector>
26 struct mapstaticmethods_impl<PlainObjectType, false, IsVector>
27 {
runmapstaticmethods_impl28   static void run(const PlainObjectType& m)
29   {
30     mapstaticmethods_impl<PlainObjectType, true, IsVector>::run(m);
31 
32     int i = internal::random<int>(2,5), j = internal::random<int>(2,5);
33 
34     PlainObjectType::Map(ptr).setZero();
35     PlainObjectType::MapAligned(ptr).setZero();
36     PlainObjectType::Map(const_ptr).sum();
37     PlainObjectType::MapAligned(const_ptr).sum();
38 
39     PlainObjectType::Map(ptr, InnerStride<>(i)).setZero();
40     PlainObjectType::MapAligned(ptr, InnerStride<>(i)).setZero();
41     PlainObjectType::Map(const_ptr, InnerStride<>(i)).sum();
42     PlainObjectType::MapAligned(const_ptr, InnerStride<>(i)).sum();
43 
44     PlainObjectType::Map(ptr, InnerStride<2>()).setZero();
45     PlainObjectType::MapAligned(ptr, InnerStride<3>()).setZero();
46     PlainObjectType::Map(const_ptr, InnerStride<4>()).sum();
47     PlainObjectType::MapAligned(const_ptr, InnerStride<5>()).sum();
48 
49     PlainObjectType::Map(ptr, OuterStride<>(i)).setZero();
50     PlainObjectType::MapAligned(ptr, OuterStride<>(i)).setZero();
51     PlainObjectType::Map(const_ptr, OuterStride<>(i)).sum();
52     PlainObjectType::MapAligned(const_ptr, OuterStride<>(i)).sum();
53 
54     PlainObjectType::Map(ptr, OuterStride<2>()).setZero();
55     PlainObjectType::MapAligned(ptr, OuterStride<3>()).setZero();
56     PlainObjectType::Map(const_ptr, OuterStride<4>()).sum();
57     PlainObjectType::MapAligned(const_ptr, OuterStride<5>()).sum();
58 
59     PlainObjectType::Map(ptr, Stride<Dynamic, Dynamic>(i,j)).setZero();
60     PlainObjectType::MapAligned(ptr, Stride<2,Dynamic>(2,i)).setZero();
61     PlainObjectType::Map(const_ptr, Stride<Dynamic,3>(i,3)).sum();
62     PlainObjectType::MapAligned(const_ptr, Stride<Dynamic, Dynamic>(i,j)).sum();
63 
64     PlainObjectType::Map(ptr, Stride<2,3>()).setZero();
65     PlainObjectType::MapAligned(ptr, Stride<3,4>()).setZero();
66     PlainObjectType::Map(const_ptr, Stride<2,4>()).sum();
67     PlainObjectType::MapAligned(const_ptr, Stride<5,3>()).sum();
68   }
69 };
70 
71 template<typename PlainObjectType>
72 struct mapstaticmethods_impl<PlainObjectType, true, false>
73 {
runmapstaticmethods_impl74   static void run(const PlainObjectType& m)
75   {
76     Index rows = m.rows(), cols = m.cols();
77 
78     int i = internal::random<int>(2,5), j = internal::random<int>(2,5);
79 
80     PlainObjectType::Map(ptr, rows, cols).setZero();
81     PlainObjectType::MapAligned(ptr, rows, cols).setZero();
82     PlainObjectType::Map(const_ptr, rows, cols).sum();
83     PlainObjectType::MapAligned(const_ptr, rows, cols).sum();
84 
85     PlainObjectType::Map(ptr, rows, cols, InnerStride<>(i)).setZero();
86     PlainObjectType::MapAligned(ptr, rows, cols, InnerStride<>(i)).setZero();
87     PlainObjectType::Map(const_ptr, rows, cols, InnerStride<>(i)).sum();
88     PlainObjectType::MapAligned(const_ptr, rows, cols, InnerStride<>(i)).sum();
89 
90     PlainObjectType::Map(ptr, rows, cols, InnerStride<2>()).setZero();
91     PlainObjectType::MapAligned(ptr, rows, cols, InnerStride<3>()).setZero();
92     PlainObjectType::Map(const_ptr, rows, cols, InnerStride<4>()).sum();
93     PlainObjectType::MapAligned(const_ptr, rows, cols, InnerStride<5>()).sum();
94 
95     PlainObjectType::Map(ptr, rows, cols, OuterStride<>(i)).setZero();
96     PlainObjectType::MapAligned(ptr, rows, cols, OuterStride<>(i)).setZero();
97     PlainObjectType::Map(const_ptr, rows, cols, OuterStride<>(i)).sum();
98     PlainObjectType::MapAligned(const_ptr, rows, cols, OuterStride<>(i)).sum();
99 
100     PlainObjectType::Map(ptr, rows, cols, OuterStride<2>()).setZero();
101     PlainObjectType::MapAligned(ptr, rows, cols, OuterStride<3>()).setZero();
102     PlainObjectType::Map(const_ptr, rows, cols, OuterStride<4>()).sum();
103     PlainObjectType::MapAligned(const_ptr, rows, cols, OuterStride<5>()).sum();
104 
105     PlainObjectType::Map(ptr, rows, cols, Stride<Dynamic, Dynamic>(i,j)).setZero();
106     PlainObjectType::MapAligned(ptr, rows, cols, Stride<2,Dynamic>(2,i)).setZero();
107     PlainObjectType::Map(const_ptr, rows, cols, Stride<Dynamic,3>(i,3)).sum();
108     PlainObjectType::MapAligned(const_ptr, rows, cols, Stride<Dynamic, Dynamic>(i,j)).sum();
109 
110     PlainObjectType::Map(ptr, rows, cols, Stride<2,3>()).setZero();
111     PlainObjectType::MapAligned(ptr, rows, cols, Stride<3,4>()).setZero();
112     PlainObjectType::Map(const_ptr, rows, cols, Stride<2,4>()).sum();
113     PlainObjectType::MapAligned(const_ptr, rows, cols, Stride<5,3>()).sum();
114   }
115 };
116 
117 template<typename PlainObjectType>
118 struct mapstaticmethods_impl<PlainObjectType, true, true>
119 {
runmapstaticmethods_impl120   static void run(const PlainObjectType& v)
121   {
122     Index size = v.size();
123 
124     int i = internal::random<int>(2,5);
125 
126     PlainObjectType::Map(ptr, size).setZero();
127     PlainObjectType::MapAligned(ptr, size).setZero();
128     PlainObjectType::Map(const_ptr, size).sum();
129     PlainObjectType::MapAligned(const_ptr, size).sum();
130 
131     PlainObjectType::Map(ptr, size, InnerStride<>(i)).setZero();
132     PlainObjectType::MapAligned(ptr, size, InnerStride<>(i)).setZero();
133     PlainObjectType::Map(const_ptr, size, InnerStride<>(i)).sum();
134     PlainObjectType::MapAligned(const_ptr, size, InnerStride<>(i)).sum();
135 
136     PlainObjectType::Map(ptr, size, InnerStride<2>()).setZero();
137     PlainObjectType::MapAligned(ptr, size, InnerStride<3>()).setZero();
138     PlainObjectType::Map(const_ptr, size, InnerStride<4>()).sum();
139     PlainObjectType::MapAligned(const_ptr, size, InnerStride<5>()).sum();
140   }
141 };
142 
143 template<typename PlainObjectType>
mapstaticmethods(const PlainObjectType & m)144 void mapstaticmethods(const PlainObjectType& m)
145 {
146   mapstaticmethods_impl<PlainObjectType>::run(m);
147   VERIFY(true); // just to avoid 'unused function' warning
148 }
149 
EIGEN_DECLARE_TEST(mapstaticmethods)150 EIGEN_DECLARE_TEST(mapstaticmethods)
151 {
152   ptr = internal::aligned_new<float>(1000);
153   for(int i = 0; i < 1000; i++) ptr[i] = float(i);
154 
155   const_ptr = ptr;
156 
157   CALL_SUBTEST_1(( mapstaticmethods(Matrix<float, 1, 1>()) ));
158   CALL_SUBTEST_1(( mapstaticmethods(Vector2f()) ));
159   CALL_SUBTEST_2(( mapstaticmethods(Vector3f()) ));
160   CALL_SUBTEST_2(( mapstaticmethods(Matrix2f()) ));
161   CALL_SUBTEST_3(( mapstaticmethods(Matrix4f()) ));
162   CALL_SUBTEST_3(( mapstaticmethods(Array4f()) ));
163   CALL_SUBTEST_4(( mapstaticmethods(Array3f()) ));
164   CALL_SUBTEST_4(( mapstaticmethods(Array33f()) ));
165   CALL_SUBTEST_5(( mapstaticmethods(Array44f()) ));
166   CALL_SUBTEST_5(( mapstaticmethods(VectorXf(1)) ));
167   CALL_SUBTEST_5(( mapstaticmethods(VectorXf(8)) ));
168   CALL_SUBTEST_6(( mapstaticmethods(MatrixXf(1,1)) ));
169   CALL_SUBTEST_6(( mapstaticmethods(MatrixXf(5,7)) ));
170   CALL_SUBTEST_7(( mapstaticmethods(ArrayXf(1)) ));
171   CALL_SUBTEST_7(( mapstaticmethods(ArrayXf(5)) ));
172   CALL_SUBTEST_8(( mapstaticmethods(ArrayXXf(1,1)) ));
173   CALL_SUBTEST_8(( mapstaticmethods(ArrayXXf(8,6)) ));
174 
175   internal::aligned_delete(ptr, 1000);
176 }
177 
178