blob: 4f9495a51fa72b770ffcd693188f1eddb25408b0 [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
70template<typename T>
71void check_unalignedassert_good()
72{
73 T *x, *y;
74 x = new T;
75 delete x;
76 y = new T[2];
77 delete[] y;
78}
79
80template<typename T>
81void check_unalignedassert_bad()
82{
83 float buf[1000];
84 float *unaligned = buf;
85 while((reinterpret_cast<size_t>(unaligned)&0xf)==0) ++unaligned; // make sure unaligned is really unaligned
86 T *x = new(static_cast<void*>(unaligned)) T;
87 x->~T();
88}
89
90void unalignedassert()
91{
92 check_unalignedassert_good<Good1>();
93 check_unalignedassert_good<Good2>();
94 check_unalignedassert_good<Good3>();
95 VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad4>());
96 VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad5>());
97 VERIFY_RAISES_ASSERT(check_unalignedassert_bad<Bad6>());
98 check_unalignedassert_good<Good7>();
99 check_unalignedassert_good<Good8>();
100}
101
102void test_unalignedassert()
103{
104 CALL_SUBTEST(unalignedassert());
105}