blob: d829008d59eed1d6ded1a360582db1dff4f40ff6 [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...
Benoit Jacob1d52bd42009-01-08 15:20:21 +000028struct MyStruct
Gael Guennebaudf52d1192008-09-03 00:32:56 +000029{
Benoit Jacob1d52bd42009-01-08 15:20:21 +000030 EIGEN_MAKE_ALIGNED_OPERATOR_NEW(MyStruct)
Gael Guennebaudf52d1192008-09-03 00:32:56 +000031 char dummychar;
32 Vector4f avec;
33};
34
Benoit Jacob1d52bd42009-01-08 15:20:21 +000035class MyClassA
Gael Guennebaudf52d1192008-09-03 00:32:56 +000036{
37 public:
Benoit Jacob1d52bd42009-01-08 15:20:21 +000038 EIGEN_MAKE_ALIGNED_OPERATOR_NEW(MyClassA)
Gael Guennebaudf52d1192008-09-03 00:32:56 +000039 char dummychar;
40 Vector4f avec;
41};
42
43template<typename T> void check_dynaligned()
44{
45 T* obj = new T;
46 VERIFY(size_t(obj)%16==0);
47 delete obj;
48}
49
50void 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 Guennebaudf52d1192008-09-03 00:32:56 +000067 }
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 Guennebaudf52d1192008-09-03 00:32:56 +000074 delete foo0;
75 delete fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +000076 }
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 Guennebaudf52d1192008-09-03 00:32:56 +000084 delete[] foo0;
85 delete[] fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +000086 }
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}