blob: ffe21771acb9a6cb60cdf96a5ab1708b9bf0108f [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
Benoit Jacobfd831d52009-01-09 14:56:44 +000027void check_handmade_aligned_malloc()
28{
29 for(int i = 1; i < 1000; i++)
30 {
31 char *p = (char*)ei_handmade_aligned_malloc(i);
32 VERIFY(size_t(p)%16==0);
33 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
34 for(int j = 0; j < i; j++) p[j]=0;
35 ei_handmade_aligned_free(p);
36 }
37}
38
39void check_aligned_malloc()
40{
41 for(int i = 1; i < 1000; i++)
42 {
43 char *p = (char*)ei_aligned_malloc(i);
44 VERIFY(size_t(p)%16==0);
45 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
46 for(int j = 0; j < i; j++) p[j]=0;
47 ei_aligned_free(p);
48 }
49}
50
51void check_aligned_new()
52{
53 for(int i = 1; i < 1000; i++)
54 {
55 float *p = ei_aligned_new<float>(i);
56 VERIFY(size_t(p)%16==0);
57 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
58 for(int j = 0; j < i; j++) p[j]=0;
59 ei_aligned_delete(p,i);
60 }
61}
62
63void check_aligned_stack_alloc()
64{
65 for(int i = 1; i < 1000; i++)
66 {
67 float *p = ei_aligned_stack_new(float,i);
68 VERIFY(size_t(p)%16==0);
69 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
70 for(int j = 0; j < i; j++) p[j]=0;
71 ei_aligned_stack_delete(float,p,i);
72 }
73}
74
75
Gael Guennebaudf52d1192008-09-03 00:32:56 +000076// test compilation with both a struct and a class...
Benoit Jacob1d52bd42009-01-08 15:20:21 +000077struct MyStruct
Gael Guennebaudf52d1192008-09-03 00:32:56 +000078{
Benoit Jacobeb7dcbb2009-01-08 15:37:13 +000079 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Gael Guennebaudf52d1192008-09-03 00:32:56 +000080 char dummychar;
81 Vector4f avec;
82};
83
Benoit Jacob1d52bd42009-01-08 15:20:21 +000084class MyClassA
Gael Guennebaudf52d1192008-09-03 00:32:56 +000085{
86 public:
Benoit Jacobeb7dcbb2009-01-08 15:37:13 +000087 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Gael Guennebaudf52d1192008-09-03 00:32:56 +000088 char dummychar;
89 Vector4f avec;
90};
91
92template<typename T> void check_dynaligned()
93{
94 T* obj = new T;
95 VERIFY(size_t(obj)%16==0);
96 delete obj;
97}
98
99void test_dynalloc()
100{
Benoit Jacobfd831d52009-01-09 14:56:44 +0000101 // low level dynamic memory allocation
102 CALL_SUBTEST(check_handmade_aligned_malloc());
103 CALL_SUBTEST(check_aligned_malloc());
104 CALL_SUBTEST(check_aligned_new());
105 CALL_SUBTEST(check_aligned_stack_alloc());
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000106
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000107 for (int i=0; i<g_repeat*100; ++i)
108 {
109 CALL_SUBTEST( check_dynaligned<Vector4f>() );
110 CALL_SUBTEST( check_dynaligned<Vector2d>() );
111 CALL_SUBTEST( check_dynaligned<Matrix4f>() );
112 CALL_SUBTEST( check_dynaligned<Vector4d>() );
113 CALL_SUBTEST( check_dynaligned<Vector4i>() );
114 }
115
116 // check static allocation, who knows ?
117 {
118 MyStruct foo0; VERIFY(size_t(foo0.avec.data())%16==0);
119 MyClassA fooA; VERIFY(size_t(fooA.avec.data())%16==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000120 }
121
122 // dynamic allocation, single object
123 for (int i=0; i<g_repeat*100; ++i)
124 {
125 MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%16==0);
126 MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%16==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000127 delete foo0;
128 delete fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000129 }
130
131 // dynamic allocation, array
132 const int N = 10;
133 for (int i=0; i<g_repeat*100; ++i)
134 {
135 MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%16==0);
136 MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%16==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000137 delete[] foo0;
138 delete[] fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000139 }
140
141 // std::vector
142 for (int i=0; i<g_repeat*100; ++i)
143 {
144 std::vector<Vector4f, ei_new_allocator<Vector4f> > vecs(N);
145 for (int j=0; j<N; ++j)
146 {
147 VERIFY(size_t(vecs[j].data())%16==0);
148 }
149 std::vector<MyStruct,ei_new_allocator<MyStruct> > foos(N);
150 for (int j=0; j<N; ++j)
151 {
152 VERIFY(size_t(foos[j].avec.data())%16==0);
153 }
154 }
155
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000156}