blob: 929e603bccb17367e04bc2a8af22ebe8f08fda58 [file] [log] [blame]
Gael Guennebaudf52d1192008-09-03 00:32:56 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaudf52d1192008-09-03 00:32:56 +00003//
Gael Guennebaud28e64b02010-06-24 23:21:58 +02004// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
Gael Guennebaudf52d1192008-09-03 00:32:56 +00005//
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 Jacob95bda5e2009-05-03 13:50:56 +000027#if EIGEN_ALIGN
Benoit Jacob93a089a2009-02-04 16:53:03 +000028#define ALIGNMENT 16
29#else
30#define ALIGNMENT 1
31#endif
32
Benoit Jacobfd831d52009-01-09 14:56:44 +000033void check_handmade_aligned_malloc()
34{
35 for(int i = 1; i < 1000; i++)
36 {
Benoit Jacob47160402010-10-25 10:15:22 -040037 char *p = (char*)internal::handmade_aligned_malloc(i);
Benoit Jacob93a089a2009-02-04 16:53:03 +000038 VERIFY(size_t(p)%ALIGNMENT==0);
Benoit Jacobfd831d52009-01-09 14:56:44 +000039 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
40 for(int j = 0; j < i; j++) p[j]=0;
Benoit Jacob47160402010-10-25 10:15:22 -040041 internal::handmade_aligned_free(p);
Benoit Jacobfd831d52009-01-09 14:56:44 +000042 }
43}
44
45void check_aligned_malloc()
46{
47 for(int i = 1; i < 1000; i++)
48 {
Benoit Jacob47160402010-10-25 10:15:22 -040049 char *p = (char*)internal::aligned_malloc(i);
Benoit Jacob93a089a2009-02-04 16:53:03 +000050 VERIFY(size_t(p)%ALIGNMENT==0);
Benoit Jacobfd831d52009-01-09 14:56:44 +000051 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
52 for(int j = 0; j < i; j++) p[j]=0;
Benoit Jacob47160402010-10-25 10:15:22 -040053 internal::aligned_free(p);
Benoit Jacobfd831d52009-01-09 14:56:44 +000054 }
55}
56
57void check_aligned_new()
58{
59 for(int i = 1; i < 1000; i++)
60 {
Benoit Jacob47160402010-10-25 10:15:22 -040061 float *p = internal::aligned_new<float>(i);
Benoit Jacob93a089a2009-02-04 16:53:03 +000062 VERIFY(size_t(p)%ALIGNMENT==0);
Benoit Jacobfd831d52009-01-09 14:56:44 +000063 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
64 for(int j = 0; j < i; j++) p[j]=0;
Benoit Jacob47160402010-10-25 10:15:22 -040065 internal::aligned_delete(p,i);
Benoit Jacobfd831d52009-01-09 14:56:44 +000066 }
67}
68
69void check_aligned_stack_alloc()
70{
71 for(int i = 1; i < 1000; i++)
72 {
Gael Guennebaudbbb4b352011-03-19 08:51:38 +010073 ei_declare_aligned_stack_constructed_variable(float,p,i,0);
Benoit Jacob93a089a2009-02-04 16:53:03 +000074 VERIFY(size_t(p)%ALIGNMENT==0);
Benoit Jacobfd831d52009-01-09 14:56:44 +000075 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
76 for(int j = 0; j < i; j++) p[j]=0;
Benoit Jacobfd831d52009-01-09 14:56:44 +000077 }
78}
79
80
Gael Guennebaudf52d1192008-09-03 00:32:56 +000081// test compilation with both a struct and a class...
Benoit Jacob1d52bd42009-01-08 15:20:21 +000082struct MyStruct
Gael Guennebaudf52d1192008-09-03 00:32:56 +000083{
Benoit Jacobeb7dcbb2009-01-08 15:37:13 +000084 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Gael Guennebaudf52d1192008-09-03 00:32:56 +000085 char dummychar;
86 Vector4f avec;
87};
88
Benoit Jacob1d52bd42009-01-08 15:20:21 +000089class MyClassA
Gael Guennebaudf52d1192008-09-03 00:32:56 +000090{
91 public:
Benoit Jacobeb7dcbb2009-01-08 15:37:13 +000092 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Gael Guennebaudf52d1192008-09-03 00:32:56 +000093 char dummychar;
94 Vector4f avec;
95};
96
97template<typename T> void check_dynaligned()
98{
99 T* obj = new T;
Benoit Jacob93a089a2009-02-04 16:53:03 +0000100 VERIFY(size_t(obj)%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000101 delete obj;
102}
103
104void test_dynalloc()
105{
Benoit Jacobfd831d52009-01-09 14:56:44 +0000106 // low level dynamic memory allocation
107 CALL_SUBTEST(check_handmade_aligned_malloc());
108 CALL_SUBTEST(check_aligned_malloc());
109 CALL_SUBTEST(check_aligned_new());
110 CALL_SUBTEST(check_aligned_stack_alloc());
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000111
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000112 for (int i=0; i<g_repeat*100; ++i)
113 {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400114 CALL_SUBTEST(check_dynaligned<Vector4f>() );
115 CALL_SUBTEST(check_dynaligned<Vector2d>() );
116 CALL_SUBTEST(check_dynaligned<Matrix4f>() );
117 CALL_SUBTEST(check_dynaligned<Vector4d>() );
118 CALL_SUBTEST(check_dynaligned<Vector4i>() );
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000119 }
120
121 // check static allocation, who knows ?
Gael Guennebaud23594862011-03-15 09:53:23 +0100122 #if EIGEN_ALIGN_STATICALLY
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000123 {
Benoit Jacob93a089a2009-02-04 16:53:03 +0000124 MyStruct foo0; VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0);
125 MyClassA fooA; VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000126 }
Gael Guennebaud23594862011-03-15 09:53:23 +0100127
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000128 // dynamic allocation, single object
129 for (int i=0; i<g_repeat*100; ++i)
130 {
Benoit Jacob93a089a2009-02-04 16:53:03 +0000131 MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
132 MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000133 delete foo0;
134 delete fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000135 }
136
137 // dynamic allocation, array
138 const int N = 10;
139 for (int i=0; i<g_repeat*100; ++i)
140 {
Benoit Jacob93a089a2009-02-04 16:53:03 +0000141 MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
142 MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000143 delete[] foo0;
144 delete[] fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000145 }
Gael Guennebaud23594862011-03-15 09:53:23 +0100146 #endif
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000147
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000148}