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