blob: 0b5bf0c770e3e6c4dfc3e5e4b3099c57ac1b5c48 [file] [log] [blame]
Benoit Jacob3958e7f2008-12-31 00:05:22 +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 Benoit Jacob <jacob.benoit.1@gmail.com>
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
27struct Good1
28{
29 MatrixXd m; // good: m will allocate its own array, taking care of alignment.
30 Good1() : m(20,20) {}
31};
32
33struct Good2
34{
35 Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be aligned
36};
37
38struct Good3
39{
40 Vector2f m; // good: same reason
41};
42
43struct Bad4
44{
45 Vector2d m; // bad: sizeof(m)%16==0 so alignment is required
46};
47
48struct Bad5
49{
50 Matrix<float, 2, 6> m; // bad: same reason
51};
52
53struct Bad6
54{
55 Matrix<double, 3, 4> m; // bad: same reason
56};
57
58struct Good7 : Eigen::WithAlignedOperatorNew
59{
60 Vector2d m;
61 float f; // make the struct have sizeof%16!=0 to make it a little more tricky when we allow an array of 2 such objects
62};
63
64struct Good8 : Eigen::WithAlignedOperatorNew
65{
66 float f; // try the f at first -- the EIGEN_ALIGN_128 attribute of m should make that still work
67 Matrix4f m;
68};
69
Benoit Jacob15ca6652009-01-04 15:26:32 +000070struct Good9
71{
72 Matrix<float,2,2,Matrix_DontAlign> m; // good: no alignment requested
73 float f;
74};
75
Benoit Jacob3958e7f2008-12-31 00:05:22 +000076template<typename T>
77void check_unalignedassert_good()
78{
79 T *x, *y;
80 x = new T;
81 delete x;
82 y = new T[2];
83 delete[] y;
84}
85
86template<typename T>
87void check_unalignedassert_bad()
88{
Benoit Jacob15ca6652009-01-04 15:26:32 +000089 float buf[sizeof(T)+16];
Benoit Jacob3958e7f2008-12-31 00:05:22 +000090 float *unaligned = buf;
91 while((reinterpret_cast<size_t>(unaligned)&0xf)==0) ++unaligned; // make sure unaligned is really unaligned
92 T *x = new(static_cast<void*>(unaligned)) T;
93 x->~T();
94}
95
96void unalignedassert()
97{
98 check_unalignedassert_good<Good1>();
99 check_unalignedassert_good<Good2>();
100 check_unalignedassert_good<Good3>();
101 VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad4>());
102 VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad5>());
103 VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad6>());
104 check_unalignedassert_good<Good7>();
105 check_unalignedassert_good<Good8>();
Benoit Jacob15ca6652009-01-04 15:26:32 +0000106 check_unalignedassert_good<Good9>();
Benoit Jacob3958e7f2008-12-31 00:05:22 +0000107}
108
109void test_unalignedassert()
110{
111 CALL_SUBTEST(unalignedassert());
112}