Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 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 | // 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 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 27 | // test compilation with both a struct and a class... |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 28 | struct MyStruct |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 29 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 30 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 31 | char dummychar; |
| 32 | Vector4f avec; |
| 33 | }; |
| 34 | |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 35 | class MyClassA |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 36 | { |
| 37 | public: |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 38 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 39 | char dummychar; |
| 40 | Vector4f avec; |
| 41 | }; |
| 42 | |
| 43 | template<typename T> void check_dynaligned() |
| 44 | { |
| 45 | T* obj = new T; |
| 46 | VERIFY(size_t(obj)%16==0); |
| 47 | delete obj; |
| 48 | } |
| 49 | |
| 50 | void test_dynalloc() |
| 51 | { |
| 52 | |
| 53 | #ifdef EIGEN_VECTORIZE |
| 54 | for (int i=0; i<g_repeat*100; ++i) |
| 55 | { |
| 56 | CALL_SUBTEST( check_dynaligned<Vector4f>() ); |
| 57 | CALL_SUBTEST( check_dynaligned<Vector2d>() ); |
| 58 | CALL_SUBTEST( check_dynaligned<Matrix4f>() ); |
| 59 | CALL_SUBTEST( check_dynaligned<Vector4d>() ); |
| 60 | CALL_SUBTEST( check_dynaligned<Vector4i>() ); |
| 61 | } |
| 62 | |
| 63 | // check static allocation, who knows ? |
| 64 | { |
| 65 | MyStruct foo0; VERIFY(size_t(foo0.avec.data())%16==0); |
| 66 | MyClassA fooA; VERIFY(size_t(fooA.avec.data())%16==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // dynamic allocation, single object |
| 70 | for (int i=0; i<g_repeat*100; ++i) |
| 71 | { |
| 72 | MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%16==0); |
| 73 | MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%16==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 74 | delete foo0; |
| 75 | delete fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // dynamic allocation, array |
| 79 | const int N = 10; |
| 80 | for (int i=0; i<g_repeat*100; ++i) |
| 81 | { |
| 82 | MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%16==0); |
| 83 | MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%16==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 84 | delete[] foo0; |
| 85 | delete[] fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // std::vector |
| 89 | for (int i=0; i<g_repeat*100; ++i) |
| 90 | { |
| 91 | std::vector<Vector4f, ei_new_allocator<Vector4f> > vecs(N); |
| 92 | for (int j=0; j<N; ++j) |
| 93 | { |
| 94 | VERIFY(size_t(vecs[j].data())%16==0); |
| 95 | } |
| 96 | std::vector<MyStruct,ei_new_allocator<MyStruct> > foos(N); |
| 97 | for (int j=0; j<N; ++j) |
| 98 | { |
| 99 | VERIFY(size_t(foos[j].avec.data())%16==0); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #endif // EIGEN_VECTORIZE |
| 104 | |
| 105 | } |