mapstaticmethods.cpp 7.3 KB

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