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.

127 lines
3.4 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. //
  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. struct TestNew1
  11. {
  12. MatrixXd m; // good: m will allocate its own array, taking care of alignment.
  13. TestNew1() : m(20,20) {}
  14. };
  15. struct TestNew2
  16. {
  17. Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned,
  18. // 8-byte alignment is good enough here, which we'll get automatically
  19. };
  20. struct TestNew3
  21. {
  22. Vector2f m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned
  23. };
  24. struct TestNew4
  25. {
  26. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  27. Vector2d m;
  28. 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
  29. };
  30. struct TestNew5
  31. {
  32. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  33. float f; // try the f at first -- the EIGEN_ALIGN16 attribute of m should make that still work
  34. Matrix4f m;
  35. };
  36. struct TestNew6
  37. {
  38. Matrix<float,2,2,DontAlign> m; // good: no alignment requested
  39. float f;
  40. };
  41. template<bool Align> struct Depends
  42. {
  43. EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align)
  44. Vector2d m;
  45. float f;
  46. };
  47. template<typename T>
  48. void check_unalignedassert_good()
  49. {
  50. T *x, *y;
  51. x = new T;
  52. delete x;
  53. y = new T[2];
  54. delete[] y;
  55. }
  56. #if EIGEN_ALIGN_STATICALLY
  57. template<typename T>
  58. void construct_at_boundary(int boundary)
  59. {
  60. char buf[sizeof(T)+256];
  61. size_t _buf = reinterpret_cast<size_t>(buf);
  62. _buf += (16 - (_buf % 16)); // make 16-byte aligned
  63. _buf += boundary; // make exact boundary-aligned
  64. T *x = ::new(reinterpret_cast<void*>(_buf)) T;
  65. x[0].setZero(); // just in order to silence warnings
  66. x->~T();
  67. }
  68. #endif
  69. void unalignedassert()
  70. {
  71. #if EIGEN_ALIGN_STATICALLY
  72. construct_at_boundary<Vector2f>(4);
  73. construct_at_boundary<Vector3f>(4);
  74. construct_at_boundary<Vector4f>(16);
  75. construct_at_boundary<Matrix2f>(16);
  76. construct_at_boundary<Matrix3f>(4);
  77. construct_at_boundary<Matrix4f>(16);
  78. construct_at_boundary<Vector2d>(16);
  79. construct_at_boundary<Vector3d>(4);
  80. construct_at_boundary<Vector4d>(16);
  81. construct_at_boundary<Matrix2d>(16);
  82. construct_at_boundary<Matrix3d>(4);
  83. construct_at_boundary<Matrix4d>(16);
  84. construct_at_boundary<Vector2cf>(16);
  85. construct_at_boundary<Vector3cf>(4);
  86. construct_at_boundary<Vector2cd>(16);
  87. construct_at_boundary<Vector3cd>(16);
  88. #endif
  89. check_unalignedassert_good<TestNew1>();
  90. check_unalignedassert_good<TestNew2>();
  91. check_unalignedassert_good<TestNew3>();
  92. check_unalignedassert_good<TestNew4>();
  93. check_unalignedassert_good<TestNew5>();
  94. check_unalignedassert_good<TestNew6>();
  95. check_unalignedassert_good<Depends<true> >();
  96. #if EIGEN_ALIGN_STATICALLY
  97. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4f>(8));
  98. VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4f>(8));
  99. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2d>(8));
  100. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4d>(8));
  101. VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix2d>(8));
  102. VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4d>(8));
  103. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cf>(8));
  104. VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cd>(8));
  105. #endif
  106. }
  107. void test_unalignedassert()
  108. {
  109. CALL_SUBTEST(unalignedassert());
  110. }