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.

131 lines
3.4 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 Gael Guennebaud <g.gael@free.fr>
  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. #if EIGEN_ARCH_WANTS_ALIGNMENT
  11. #define ALIGNMENT 16
  12. #else
  13. #define ALIGNMENT 1
  14. #endif
  15. void check_handmade_aligned_malloc()
  16. {
  17. for(int i = 1; i < 1000; i++)
  18. {
  19. char *p = (char*)ei_handmade_aligned_malloc(i);
  20. VERIFY(std::size_t(p)%ALIGNMENT==0);
  21. // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
  22. for(int j = 0; j < i; j++) p[j]=0;
  23. ei_handmade_aligned_free(p);
  24. }
  25. }
  26. void check_aligned_malloc()
  27. {
  28. for(int i = 1; i < 1000; i++)
  29. {
  30. char *p = (char*)ei_aligned_malloc(i);
  31. VERIFY(std::size_t(p)%ALIGNMENT==0);
  32. // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
  33. for(int j = 0; j < i; j++) p[j]=0;
  34. ei_aligned_free(p);
  35. }
  36. }
  37. void check_aligned_new()
  38. {
  39. for(int i = 1; i < 1000; i++)
  40. {
  41. float *p = ei_aligned_new<float>(i);
  42. VERIFY(std::size_t(p)%ALIGNMENT==0);
  43. // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
  44. for(int j = 0; j < i; j++) p[j]=0;
  45. ei_aligned_delete(p,i);
  46. }
  47. }
  48. void check_aligned_stack_alloc()
  49. {
  50. for(int i = 1; i < 1000; i++)
  51. {
  52. ei_declare_aligned_stack_constructed_variable(float, p, i, 0);
  53. VERIFY(std::size_t(p)%ALIGNMENT==0);
  54. // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
  55. for(int j = 0; j < i; j++) p[j]=0;
  56. }
  57. }
  58. // test compilation with both a struct and a class...
  59. struct MyStruct
  60. {
  61. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  62. char dummychar;
  63. Vector4f avec;
  64. };
  65. class MyClassA
  66. {
  67. public:
  68. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  69. char dummychar;
  70. Vector4f avec;
  71. };
  72. template<typename T> void check_dynaligned()
  73. {
  74. T* obj = new T;
  75. VERIFY(std::size_t(obj)%ALIGNMENT==0);
  76. delete obj;
  77. }
  78. void test_eigen2_dynalloc()
  79. {
  80. // low level dynamic memory allocation
  81. CALL_SUBTEST(check_handmade_aligned_malloc());
  82. CALL_SUBTEST(check_aligned_malloc());
  83. CALL_SUBTEST(check_aligned_new());
  84. CALL_SUBTEST(check_aligned_stack_alloc());
  85. for (int i=0; i<g_repeat*100; ++i)
  86. {
  87. CALL_SUBTEST( check_dynaligned<Vector4f>() );
  88. CALL_SUBTEST( check_dynaligned<Vector2d>() );
  89. CALL_SUBTEST( check_dynaligned<Matrix4f>() );
  90. CALL_SUBTEST( check_dynaligned<Vector4d>() );
  91. CALL_SUBTEST( check_dynaligned<Vector4i>() );
  92. }
  93. // check static allocation, who knows ?
  94. {
  95. MyStruct foo0; VERIFY(std::size_t(foo0.avec.data())%ALIGNMENT==0);
  96. MyClassA fooA; VERIFY(std::size_t(fooA.avec.data())%ALIGNMENT==0);
  97. }
  98. // dynamic allocation, single object
  99. for (int i=0; i<g_repeat*100; ++i)
  100. {
  101. MyStruct *foo0 = new MyStruct(); VERIFY(std::size_t(foo0->avec.data())%ALIGNMENT==0);
  102. MyClassA *fooA = new MyClassA(); VERIFY(std::size_t(fooA->avec.data())%ALIGNMENT==0);
  103. delete foo0;
  104. delete fooA;
  105. }
  106. // dynamic allocation, array
  107. const int N = 10;
  108. for (int i=0; i<g_repeat*100; ++i)
  109. {
  110. MyStruct *foo0 = new MyStruct[N]; VERIFY(std::size_t(foo0->avec.data())%ALIGNMENT==0);
  111. MyClassA *fooA = new MyClassA[N]; VERIFY(std::size_t(fooA->avec.data())%ALIGNMENT==0);
  112. delete[] foo0;
  113. delete[] fooA;
  114. }
  115. }