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.

116 lines
2.6 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  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 Good1
  11. {
  12. MatrixXd m; // good: m will allocate its own array, taking care of alignment.
  13. Good1() : m(20,20) {}
  14. };
  15. struct Good2
  16. {
  17. Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be aligned
  18. };
  19. struct Good3
  20. {
  21. Vector2f m; // good: same reason
  22. };
  23. struct Bad4
  24. {
  25. Vector2d m; // bad: sizeof(m)%16==0 so alignment is required
  26. };
  27. struct Bad5
  28. {
  29. Matrix<float, 2, 6> m; // bad: same reason
  30. };
  31. struct Bad6
  32. {
  33. Matrix<double, 3, 4> m; // bad: same reason
  34. };
  35. struct Good7
  36. {
  37. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  38. Vector2d m;
  39. 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
  40. };
  41. struct Good8
  42. {
  43. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  44. float f; // try the f at first -- the EIGEN_ALIGN_128 attribute of m should make that still work
  45. Matrix4f m;
  46. };
  47. struct Good9
  48. {
  49. Matrix<float,2,2,DontAlign> m; // good: no alignment requested
  50. float f;
  51. };
  52. template<bool Align> struct Depends
  53. {
  54. EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align)
  55. Vector2d m;
  56. float f;
  57. };
  58. template<typename T>
  59. void check_unalignedassert_good()
  60. {
  61. T *x, *y;
  62. x = new T;
  63. delete x;
  64. y = new T[2];
  65. delete[] y;
  66. }
  67. #if EIGEN_ARCH_WANTS_ALIGNMENT
  68. template<typename T>
  69. void check_unalignedassert_bad()
  70. {
  71. float buf[sizeof(T)+16];
  72. float *unaligned = buf;
  73. while((reinterpret_cast<std::size_t>(unaligned)&0xf)==0) ++unaligned; // make sure unaligned is really unaligned
  74. T *x = ::new(static_cast<void*>(unaligned)) T;
  75. x->~T();
  76. }
  77. #endif
  78. void unalignedassert()
  79. {
  80. check_unalignedassert_good<Good1>();
  81. check_unalignedassert_good<Good2>();
  82. check_unalignedassert_good<Good3>();
  83. #if EIGEN_ARCH_WANTS_ALIGNMENT
  84. VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad4>());
  85. VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad5>());
  86. VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad6>());
  87. #endif
  88. check_unalignedassert_good<Good7>();
  89. check_unalignedassert_good<Good8>();
  90. check_unalignedassert_good<Good9>();
  91. check_unalignedassert_good<Depends<true> >();
  92. #if EIGEN_ARCH_WANTS_ALIGNMENT
  93. VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Depends<false> >());
  94. #endif
  95. }
  96. void test_eigen2_unalignedassert()
  97. {
  98. CALL_SUBTEST(unalignedassert());
  99. }