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 | // |
| 4 | // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> |
| 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Lesser General Public |
| 8 | // License as published by the Free Software Foundation; either |
| 9 | // version 3 of the License, or (at your option) any later version. |
| 10 | // |
| 11 | // Alternatively, you can redistribute it and/or |
| 12 | // modify it under the terms of the GNU General Public License as |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 14 | // the License, or (at your option) any later version. |
| 15 | // |
| 16 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 19 | // GNU General Public License for more details. |
| 20 | // |
| 21 | // You should have received a copy of the GNU Lesser General Public |
| 22 | // License and a copy of the GNU General Public License along with |
| 23 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include "main.h" |
| 26 | |
Benoit Jacob | 95bda5e | 2009-05-03 13:50:56 +0000 | [diff] [blame] | 27 | #if EIGEN_ALIGN |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 28 | #define ALIGNMENT 16 |
| 29 | #else |
| 30 | #define ALIGNMENT 1 |
| 31 | #endif |
| 32 | |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 33 | void check_handmade_aligned_malloc() |
| 34 | { |
| 35 | for(int i = 1; i < 1000; i++) |
| 36 | { |
| 37 | char *p = (char*)ei_handmade_aligned_malloc(i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 38 | VERIFY(size_t(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; |
| 41 | ei_handmade_aligned_free(p); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void check_aligned_malloc() |
| 46 | { |
| 47 | for(int i = 1; i < 1000; i++) |
| 48 | { |
| 49 | char *p = (char*)ei_aligned_malloc(i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 50 | VERIFY(size_t(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; |
| 53 | ei_aligned_free(p); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void check_aligned_new() |
| 58 | { |
| 59 | for(int i = 1; i < 1000; i++) |
| 60 | { |
| 61 | float *p = ei_aligned_new<float>(i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 62 | VERIFY(size_t(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; |
| 65 | ei_aligned_delete(p,i); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void check_aligned_stack_alloc() |
| 70 | { |
| 71 | for(int i = 1; i < 1000; i++) |
| 72 | { |
| 73 | float *p = ei_aligned_stack_new(float,i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 74 | VERIFY(size_t(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 75 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind |
| 76 | for(int j = 0; j < i; j++) p[j]=0; |
| 77 | ei_aligned_stack_delete(float,p,i); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 82 | // test compilation with both a struct and a class... |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 83 | struct MyStruct |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 84 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 85 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 86 | char dummychar; |
| 87 | Vector4f avec; |
| 88 | }; |
| 89 | |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 90 | class MyClassA |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 91 | { |
| 92 | public: |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 93 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 94 | char dummychar; |
| 95 | Vector4f avec; |
| 96 | }; |
| 97 | |
| 98 | template<typename T> void check_dynaligned() |
| 99 | { |
| 100 | T* obj = new T; |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 101 | VERIFY(size_t(obj)%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 102 | delete obj; |
| 103 | } |
| 104 | |
| 105 | void test_dynalloc() |
| 106 | { |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 107 | // low level dynamic memory allocation |
| 108 | CALL_SUBTEST(check_handmade_aligned_malloc()); |
| 109 | CALL_SUBTEST(check_aligned_malloc()); |
| 110 | CALL_SUBTEST(check_aligned_new()); |
| 111 | CALL_SUBTEST(check_aligned_stack_alloc()); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 112 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 113 | for (int i=0; i<g_repeat*100; ++i) |
| 114 | { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 115 | CALL_SUBTEST(check_dynaligned<Vector4f>() ); |
| 116 | CALL_SUBTEST(check_dynaligned<Vector2d>() ); |
| 117 | CALL_SUBTEST(check_dynaligned<Matrix4f>() ); |
| 118 | CALL_SUBTEST(check_dynaligned<Vector4d>() ); |
| 119 | CALL_SUBTEST(check_dynaligned<Vector4i>() ); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // check static allocation, who knows ? |
| 123 | { |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 124 | MyStruct foo0; VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0); |
| 125 | MyClassA fooA; VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // dynamic allocation, single object |
| 129 | for (int i=0; i<g_repeat*100; ++i) |
| 130 | { |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 131 | MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0); |
| 132 | MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 133 | delete foo0; |
| 134 | delete fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // dynamic allocation, array |
| 138 | const int N = 10; |
| 139 | for (int i=0; i<g_repeat*100; ++i) |
| 140 | { |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 141 | MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0); |
| 142 | MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 143 | delete[] foo0; |
| 144 | delete[] fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 145 | } |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 146 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 147 | } |