Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 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/. |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 9 | |
| 10 | #include "main.h" |
| 11 | |
Gael Guennebaud | aec4814 | 2015-07-29 11:11:23 +0200 | [diff] [blame] | 12 | #if EIGEN_MAX_ALIGN_BYTES>0 |
| 13 | #define ALIGNMENT EIGEN_MAX_ALIGN_BYTES |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 14 | #else |
| 15 | #define ALIGNMENT 1 |
| 16 | #endif |
| 17 | |
Gustavo Lima Chaves | 2bf1cc8 | 2018-08-02 15:55:36 -0700 | [diff] [blame^] | 18 | typedef Matrix<float,16,1> Vector16f; |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 19 | typedef Matrix<float,8,1> Vector8f; |
| 20 | |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 21 | void check_handmade_aligned_malloc() |
| 22 | { |
| 23 | for(int i = 1; i < 1000; i++) |
| 24 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 25 | char *p = (char*)internal::handmade_aligned_malloc(i); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 26 | VERIFY(internal::UIntPtr(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 27 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind |
| 28 | for(int j = 0; j < i; j++) p[j]=0; |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 29 | internal::handmade_aligned_free(p); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | void check_aligned_malloc() |
| 34 | { |
Gael Guennebaud | 5b2d287 | 2016-02-05 21:46:39 +0100 | [diff] [blame] | 35 | for(int i = ALIGNMENT; i < 1000; i++) |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 36 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 37 | char *p = (char*)internal::aligned_malloc(i); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 38 | VERIFY(internal::UIntPtr(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 39 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind |
| 40 | for(int j = 0; j < i; j++) p[j]=0; |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 41 | internal::aligned_free(p); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | void check_aligned_new() |
| 46 | { |
Gael Guennebaud | 5b2d287 | 2016-02-05 21:46:39 +0100 | [diff] [blame] | 47 | for(int i = ALIGNMENT; i < 1000; i++) |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 48 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 49 | float *p = internal::aligned_new<float>(i); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 50 | VERIFY(internal::UIntPtr(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 51 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind |
| 52 | for(int j = 0; j < i; j++) p[j]=0; |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 53 | internal::aligned_delete(p,i); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void check_aligned_stack_alloc() |
| 58 | { |
Gael Guennebaud | 5b2d287 | 2016-02-05 21:46:39 +0100 | [diff] [blame] | 59 | for(int i = ALIGNMENT; i < 400; i++) |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 60 | { |
Gael Guennebaud | bbb4b35 | 2011-03-19 08:51:38 +0100 | [diff] [blame] | 61 | ei_declare_aligned_stack_constructed_variable(float,p,i,0); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 62 | VERIFY(internal::UIntPtr(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 63 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind |
| 64 | for(int j = 0; j < i; j++) p[j]=0; |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 69 | // test compilation with both a struct and a class... |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 70 | struct MyStruct |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 71 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 72 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 73 | char dummychar; |
Gustavo Lima Chaves | 2bf1cc8 | 2018-08-02 15:55:36 -0700 | [diff] [blame^] | 74 | Vector16f avec; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 77 | class MyClassA |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 78 | { |
| 79 | public: |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 80 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 81 | char dummychar; |
Gustavo Lima Chaves | 2bf1cc8 | 2018-08-02 15:55:36 -0700 | [diff] [blame^] | 82 | Vector16f avec; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | template<typename T> void check_dynaligned() |
| 86 | { |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 87 | // TODO have to be updated once we support multiple alignment values |
| 88 | if(T::SizeAtCompileTime % ALIGNMENT == 0) |
| 89 | { |
| 90 | T* obj = new T; |
| 91 | VERIFY(T::NeedsToAlign==1); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 92 | VERIFY(internal::UIntPtr(obj)%ALIGNMENT==0); |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 93 | delete obj; |
| 94 | } |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Gael Guennebaud | ba694ce | 2014-07-30 09:32:35 +0200 | [diff] [blame] | 97 | template<typename T> void check_custom_new_delete() |
| 98 | { |
| 99 | { |
| 100 | T* t = new T; |
| 101 | delete t; |
| 102 | } |
| 103 | |
| 104 | { |
| 105 | std::size_t N = internal::random<std::size_t>(1,10); |
| 106 | T* t = new T[N]; |
| 107 | delete[] t; |
| 108 | } |
| 109 | |
Gael Guennebaud | aec4814 | 2015-07-29 11:11:23 +0200 | [diff] [blame] | 110 | #if EIGEN_MAX_ALIGN_BYTES>0 |
Gael Guennebaud | ba694ce | 2014-07-30 09:32:35 +0200 | [diff] [blame] | 111 | { |
| 112 | T* t = static_cast<T *>((T::operator new)(sizeof(T))); |
| 113 | (T::operator delete)(t, sizeof(T)); |
| 114 | } |
| 115 | |
| 116 | { |
| 117 | T* t = static_cast<T *>((T::operator new)(sizeof(T))); |
| 118 | (T::operator delete)(t); |
| 119 | } |
| 120 | #endif |
| 121 | } |
| 122 | |
Gael Guennebaud | 82f0ce2 | 2018-07-17 14:46:15 +0200 | [diff] [blame] | 123 | EIGEN_DECLARE_TEST(dynalloc) |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 124 | { |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 125 | // low level dynamic memory allocation |
| 126 | CALL_SUBTEST(check_handmade_aligned_malloc()); |
| 127 | CALL_SUBTEST(check_aligned_malloc()); |
| 128 | CALL_SUBTEST(check_aligned_new()); |
| 129 | CALL_SUBTEST(check_aligned_stack_alloc()); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 130 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 131 | for (int i=0; i<g_repeat*100; ++i) |
| 132 | { |
Gael Guennebaud | ba694ce | 2014-07-30 09:32:35 +0200 | [diff] [blame] | 133 | CALL_SUBTEST( check_custom_new_delete<Vector4f>() ); |
| 134 | CALL_SUBTEST( check_custom_new_delete<Vector2f>() ); |
| 135 | CALL_SUBTEST( check_custom_new_delete<Matrix4f>() ); |
| 136 | CALL_SUBTEST( check_custom_new_delete<MatrixXi>() ); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // check static allocation, who knows ? |
Gael Guennebaud | aec4814 | 2015-07-29 11:11:23 +0200 | [diff] [blame] | 140 | #if EIGEN_MAX_STATIC_ALIGN_BYTES |
Gael Guennebaud | 8445619 | 2015-11-30 22:36:14 +0100 | [diff] [blame] | 141 | for (int i=0; i<g_repeat*100; ++i) |
| 142 | { |
| 143 | CALL_SUBTEST(check_dynaligned<Vector4f>() ); |
| 144 | CALL_SUBTEST(check_dynaligned<Vector2d>() ); |
| 145 | CALL_SUBTEST(check_dynaligned<Matrix4f>() ); |
| 146 | CALL_SUBTEST(check_dynaligned<Vector4d>() ); |
| 147 | CALL_SUBTEST(check_dynaligned<Vector4i>() ); |
| 148 | CALL_SUBTEST(check_dynaligned<Vector8f>() ); |
Gustavo Lima Chaves | 2bf1cc8 | 2018-08-02 15:55:36 -0700 | [diff] [blame^] | 149 | CALL_SUBTEST(check_dynaligned<Vector16f>() ); |
Gael Guennebaud | 8445619 | 2015-11-30 22:36:14 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 152 | { |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 153 | MyStruct foo0; VERIFY(internal::UIntPtr(foo0.avec.data())%ALIGNMENT==0); |
| 154 | MyClassA fooA; VERIFY(internal::UIntPtr(fooA.avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 155 | } |
Gael Guennebaud | 2359486 | 2011-03-15 09:53:23 +0100 | [diff] [blame] | 156 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 157 | // dynamic allocation, single object |
| 158 | for (int i=0; i<g_repeat*100; ++i) |
| 159 | { |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 160 | MyStruct *foo0 = new MyStruct(); VERIFY(internal::UIntPtr(foo0->avec.data())%ALIGNMENT==0); |
| 161 | MyClassA *fooA = new MyClassA(); VERIFY(internal::UIntPtr(fooA->avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 162 | delete foo0; |
| 163 | delete fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // dynamic allocation, array |
| 167 | const int N = 10; |
| 168 | for (int i=0; i<g_repeat*100; ++i) |
| 169 | { |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 170 | MyStruct *foo0 = new MyStruct[N]; VERIFY(internal::UIntPtr(foo0->avec.data())%ALIGNMENT==0); |
| 171 | MyClassA *fooA = new MyClassA[N]; VERIFY(internal::UIntPtr(fooA->avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 172 | delete[] foo0; |
| 173 | delete[] fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 174 | } |
Gael Guennebaud | 2359486 | 2011-03-15 09:53:23 +0100 | [diff] [blame] | 175 | #endif |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 176 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 177 | } |