blob: 0b1d6c8677b8bdd25c149a669d53980542de9df9 [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 {
73 float *p = ei_aligned_stack_new(float,i);
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;
77 ei_aligned_stack_delete(float,p,i);
78 }
79}
80
81
Gael Guennebaudf52d1192008-09-03 00:32:56 +000082// test compilation with both a struct and a class...
Benoit Jacob1d52bd42009-01-08 15:20:21 +000083struct MyStruct
Gael Guennebaudf52d1192008-09-03 00:32:56 +000084{
Benoit Jacobeb7dcbb2009-01-08 15:37:13 +000085 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Gael Guennebaudf52d1192008-09-03 00:32:56 +000086 char dummychar;
87 Vector4f avec;
88};
89
Benoit Jacob1d52bd42009-01-08 15:20:21 +000090class MyClassA
Gael Guennebaudf52d1192008-09-03 00:32:56 +000091{
92 public:
Benoit Jacobeb7dcbb2009-01-08 15:37:13 +000093 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Gael Guennebaudf52d1192008-09-03 00:32:56 +000094 char dummychar;
95 Vector4f avec;
96};
97
98template<typename T> void check_dynaligned()
99{
100 T* obj = new T;
Benoit Jacob93a089a2009-02-04 16:53:03 +0000101 VERIFY(size_t(obj)%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000102 delete obj;
103}
104
105void test_dynalloc()
106{
Benoit Jacobfd831d52009-01-09 14:56:44 +0000107 // low level dynamic memory allocation
108 CALL_SUBTEST(check_handmade_aligned_malloc());
109 CALL_SUBTEST(check_aligned_malloc());
110 CALL_SUBTEST(check_aligned_new());
111 CALL_SUBTEST(check_aligned_stack_alloc());
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000112
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000113 for (int i=0; i<g_repeat*100; ++i)
114 {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400115 CALL_SUBTEST(check_dynaligned<Vector4f>() );
116 CALL_SUBTEST(check_dynaligned<Vector2d>() );
117 CALL_SUBTEST(check_dynaligned<Matrix4f>() );
118 CALL_SUBTEST(check_dynaligned<Vector4d>() );
119 CALL_SUBTEST(check_dynaligned<Vector4i>() );
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000120 }
121
122 // check static allocation, who knows ?
Gael Guennebaud23594862011-03-15 09:53:23 +0100123 #if EIGEN_ALIGN_STATICALLY
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000124 {
Benoit Jacob93a089a2009-02-04 16:53:03 +0000125 MyStruct foo0; VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0);
126 MyClassA fooA; VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000127 }
Gael Guennebaud23594862011-03-15 09:53:23 +0100128
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000129 // dynamic allocation, single object
130 for (int i=0; i<g_repeat*100; ++i)
131 {
Benoit Jacob93a089a2009-02-04 16:53:03 +0000132 MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
133 MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000134 delete foo0;
135 delete fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000136 }
137
138 // dynamic allocation, array
139 const int N = 10;
140 for (int i=0; i<g_repeat*100; ++i)
141 {
Benoit Jacob93a089a2009-02-04 16:53:03 +0000142 MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
143 MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000144 delete[] foo0;
145 delete[] fooA;
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000146 }
Gael Guennebaud23594862011-03-15 09:53:23 +0100147 #endif
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000148
Gael Guennebaudf52d1192008-09-03 00:32:56 +0000149}