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 | |
| 27 | #include <ext/malloc_allocator.h> |
| 28 | |
| 29 | // test compilation with both a struct and a class... |
| 30 | struct MyStruct : WithAlignedOperatorNew |
| 31 | { |
| 32 | char dummychar; |
| 33 | Vector4f avec; |
| 34 | }; |
| 35 | |
| 36 | class MyClassA : public WithAlignedOperatorNew |
| 37 | { |
| 38 | public: |
| 39 | char dummychar; |
| 40 | Vector4f avec; |
| 41 | }; |
| 42 | |
| 43 | // ..as well as with some other base classes |
| 44 | |
| 45 | class MyBaseClass |
| 46 | { |
| 47 | public: |
| 48 | char dummychar; |
| 49 | float afloat; |
| 50 | }; |
| 51 | |
| 52 | class MyClassB : public WithAlignedOperatorNew, public MyBaseClass |
| 53 | { |
| 54 | public: |
| 55 | char dummychar; |
| 56 | Vector4f avec; |
| 57 | }; |
| 58 | |
| 59 | class MyClassC : public MyBaseClass, public WithAlignedOperatorNew |
| 60 | { |
| 61 | public: |
| 62 | char dummychar; |
| 63 | Vector4f avec; |
| 64 | }; |
| 65 | |
| 66 | template<typename T> void check_dynaligned() |
| 67 | { |
| 68 | T* obj = new T; |
| 69 | VERIFY(size_t(obj)%16==0); |
| 70 | delete obj; |
| 71 | } |
| 72 | |
| 73 | void test_dynalloc() |
| 74 | { |
| 75 | |
| 76 | #ifdef EIGEN_VECTORIZE |
| 77 | for (int i=0; i<g_repeat*100; ++i) |
| 78 | { |
| 79 | CALL_SUBTEST( check_dynaligned<Vector4f>() ); |
| 80 | CALL_SUBTEST( check_dynaligned<Vector2d>() ); |
| 81 | CALL_SUBTEST( check_dynaligned<Matrix4f>() ); |
| 82 | CALL_SUBTEST( check_dynaligned<Vector4d>() ); |
| 83 | CALL_SUBTEST( check_dynaligned<Vector4i>() ); |
| 84 | } |
| 85 | |
| 86 | // check static allocation, who knows ? |
| 87 | { |
| 88 | MyStruct foo0; VERIFY(size_t(foo0.avec.data())%16==0); |
| 89 | MyClassA fooA; VERIFY(size_t(fooA.avec.data())%16==0); |
| 90 | MyClassB fooB; VERIFY(size_t(fooB.avec.data())%16==0); |
| 91 | MyClassC fooC; VERIFY(size_t(fooC.avec.data())%16==0); |
| 92 | } |
| 93 | |
| 94 | // dynamic allocation, single object |
| 95 | for (int i=0; i<g_repeat*100; ++i) |
| 96 | { |
| 97 | MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%16==0); |
| 98 | MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%16==0); |
| 99 | MyClassB *fooB = new MyClassB(); VERIFY(size_t(fooB->avec.data())%16==0); |
| 100 | MyClassC *fooC = new MyClassC(); VERIFY(size_t(fooC->avec.data())%16==0); |
| 101 | delete foo0; |
| 102 | delete fooA; |
| 103 | delete fooB; |
| 104 | delete fooC; |
| 105 | } |
| 106 | |
| 107 | // dynamic allocation, array |
| 108 | const int N = 10; |
| 109 | for (int i=0; i<g_repeat*100; ++i) |
| 110 | { |
| 111 | MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%16==0); |
| 112 | MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%16==0); |
| 113 | MyClassB *fooB = new MyClassB[N]; VERIFY(size_t(fooB->avec.data())%16==0); |
| 114 | MyClassC *fooC = new MyClassC[N]; VERIFY(size_t(fooC->avec.data())%16==0); |
| 115 | delete[] foo0; |
| 116 | delete[] fooA; |
| 117 | delete[] fooB; |
| 118 | delete[] fooC; |
| 119 | } |
| 120 | |
| 121 | // std::vector |
| 122 | for (int i=0; i<g_repeat*100; ++i) |
| 123 | { |
| 124 | std::vector<Vector4f, ei_new_allocator<Vector4f> > vecs(N); |
| 125 | for (int j=0; j<N; ++j) |
| 126 | { |
| 127 | VERIFY(size_t(vecs[j].data())%16==0); |
| 128 | } |
| 129 | std::vector<MyStruct,ei_new_allocator<MyStruct> > foos(N); |
| 130 | for (int j=0; j<N; ++j) |
| 131 | { |
| 132 | VERIFY(size_t(foos[j].avec.data())%16==0); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #endif // EIGEN_VECTORIZE |
| 137 | |
| 138 | } |