You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.7 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #if defined(EIGEN_TEST_PART_1)
  11. // default
  12. #elif defined(EIGEN_TEST_PART_2)
  13. #define EIGEN_MAX_STATIC_ALIGN_BYTES 16
  14. #define EIGEN_MAX_ALIGN_BYTES 16
  15. #elif defined(EIGEN_TEST_PART_3)
  16. #define EIGEN_MAX_STATIC_ALIGN_BYTES 32
  17. #define EIGEN_MAX_ALIGN_BYTES 32
  18. #elif defined(EIGEN_TEST_PART_4)
  19. #define EIGEN_MAX_STATIC_ALIGN_BYTES 64
  20. #define EIGEN_MAX_ALIGN_BYTES 64
  21. #endif
  22. #include "main.h"
  23. typedef Matrix<float, 6,1> Vector6f;
  24. typedef Matrix<float, 8,1> Vector8f;
  25. typedef Matrix<float, 12,1> Vector12f;
  26. typedef Matrix<double, 5,1> Vector5d;
  27. typedef Matrix<double, 6,1> Vector6d;
  28. typedef Matrix<double, 7,1> Vector7d;
  29. typedef Matrix<double, 8,1> Vector8d;
  30. typedef Matrix<double, 9,1> Vector9d;
  31. typedef Matrix<double,10,1> Vector10d;
  32. typedef Matrix<double,12,1> Vector12d;
  33. struct TestNew1
  34. {
  35. MatrixXd m; // good: m will allocate its own array, taking care of alignment.
  36. TestNew1() : m(20,20) {}
  37. };
  38. struct TestNew2
  39. {
  40. Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned,
  41. // 8-byte alignment is good enough here, which we'll get automatically
  42. };
  43. struct TestNew3
  44. {
  45. Vector2f m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned
  46. };
  47. struct TestNew4
  48. {
  49. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  50. Vector2d m;
  51. float f; // make the struct have sizeof%16!=0 to make it a little more tricky when we allow an array of 2 such objects
  52. };
  53. struct TestNew5
  54. {
  55. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  56. float f; // try the f at first -- the EIGEN_ALIGN_MAX attribute of m should make that still work
  57. Matrix4f m;
  58. };
  59. struct TestNew6
  60. {
  61. Matrix<float,2,2,DontAlign> m; // good: no alignment requested
  62. float f;
  63. };
  64. template<bool Align> struct Depends
  65. {
  66. EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align)
  67. Vector2d m;
  68. float f;
  69. };
  70. template<typename T>
  71. void check_unalignedassert_good()
  72. {
  73. T *x, *y;
  74. x = new T;
  75. delete x;
  76. y = new T[2];
  77. delete[] y;
  78. }
  79. #if EIGEN_MAX_STATIC_ALIGN_BYTES>0
  80. template<typename T>
  81. void construct_at_boundary(int boundary)
  82. {
  83. char buf[sizeof(T)+256];
  84. size_t _buf = reinterpret_cast<size_t>(buf);
  85. _buf += (EIGEN_MAX_ALIGN_BYTES - (_buf % EIGEN_MAX_ALIGN_BYTES)); // make 16/32/...-byte aligned
  86. _buf += boundary; // make exact boundary-aligned
  87. T *x = ::new(reinterpret_cast<void*>(_buf)) T;
  88. x[0].setZero(); // just in order to silence warnings
  89. x->~T();
  90. }
  91. #endif
  92. void unalignedassert()
  93. {
  94. #if EIGEN_MAX_STATIC_ALIGN_BYTES>0
  95. construct_at_boundary<Vector2f>(4);
  96. construct_at_boundary<Vector3f>(4);
  97. construct_at_boundary<Vector4f>(16);
  98. construct_at_boundary<Vector6f>(4);
  99. construct_at_boundary<Vector8f>(EIGEN_MAX_ALIGN_BYTES);
  100. construct_at_boundary<Vector12f>(16);
  101. construct_at_boundary<Matrix2f>(16);
  102. construct_at_boundary<Matrix3f>(4);
  103. construct_at_boundary<Matrix4f>(EIGEN_MAX_ALIGN_BYTES);
  104. construct_at_boundary<Vector2d>(16);
  105. construct_at_boundary<Vector3d>(4);
  106. construct_at_boundary<Vector4d>(EIGEN_MAX_ALIGN_BYTES);
  107. construct_at_boundary<Vector5d>(4);
  108. construct_at_boundary<Vector6d>(16);
  109. construct_at_boundary<Vector7d>(4);
  110. construct_at_boundary<Vector8d>(EIGEN_MAX_ALIGN_BYTES);
  111. construct_at_boundary<Vector9d>(4);
  112. construct_at_boundary<Vector10d>(16);
  113. construct_at_boundary<Vector12d>(EIGEN_MAX_ALIGN_BYTES);
  114. construct_at_boundary<Matrix2d>(EIGEN_MAX_ALIGN_BYTES);
  115. construct_at_boundary<Matrix3d>(4);
  116. construct_at_boundary<Matrix4d>(EIGEN_MAX_ALIGN_BYTES);
  117. construct_at_boundary<Vector2cf>(16);
  118. construct_at_boundary<Vector3cf>(4);
  119. construct_at_boundary<Vector2cd>(EIGEN_MAX_ALIGN_BYTES);
  120. construct_at_boundary<Vector3cd>(16);
  121. #endif
  122. check_unalignedassert_good<TestNew1>();
  123. check_unalignedassert_good<TestNew2>();
  124. check_unalignedassert_good<TestNew3>();
  125. check_unalignedassert_good<TestNew4>();
  126. check_unalignedassert_good<TestNew5>();
  127. check_unalignedassert_good<TestNew6>();
  128. check_unalignedassert_good<Depends<true> >();
  129. #if EIGEN_MAX_STATIC_ALIGN_BYTES>0
  130. if(EIGEN_MAX_ALIGN_BYTES>=16)
  131. {
  132. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4f>(8));
  133. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector8f>(8));
  134. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector12f>(8));
  135. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2d>(8));
  136. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4d>(8));
  137. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector6d>(8));
  138. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector8d>(8));
  139. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector10d>(8));
  140. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector12d>(8));
  141. // Complexes are disabled because the compiler might aggressively vectorize
  142. // the initialization of complex coeffs to 0 before we can check for alignedness
  143. //VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cf>(8));
  144. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4i>(8));
  145. }
  146. for(int b=8; b<EIGEN_MAX_ALIGN_BYTES; b+=8)
  147. {
  148. if(b<32) VERIFY_RAISES_ASSERT(construct_at_boundary<Vector8f>(b));
  149. if(b<64) VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4f>(b));
  150. if(b<32) VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4d>(b));
  151. if(b<32) VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix2d>(b));
  152. if(b<128) VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4d>(b));
  153. //if(b<32) VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cd>(b));
  154. }
  155. #endif
  156. }
  157. void test_unalignedassert()
  158. {
  159. CALL_SUBTEST(unalignedassert());
  160. }