Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 1 | // 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 | |
| 27 | struct Good1 |
| 28 | { |
| 29 | MatrixXd m; // good: m will allocate its own array, taking care of alignment. |
| 30 | Good1() : m(20,20) {} |
| 31 | }; |
| 32 | |
| 33 | struct 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 | |
| 38 | struct Good3 |
| 39 | { |
| 40 | Vector2f m; // good: same reason |
| 41 | }; |
| 42 | |
| 43 | struct Bad4 |
| 44 | { |
| 45 | Vector2d m; // bad: sizeof(m)%16==0 so alignment is required |
| 46 | }; |
| 47 | |
| 48 | struct Bad5 |
| 49 | { |
| 50 | Matrix<float, 2, 6> m; // bad: same reason |
| 51 | }; |
| 52 | |
| 53 | struct Bad6 |
| 54 | { |
| 55 | Matrix<double, 3, 4> m; // bad: same reason |
| 56 | }; |
| 57 | |
| 58 | struct 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 | |
| 64 | struct 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 Jacob | 15ca665 | 2009-01-04 15:26:32 +0000 | [diff] [blame^] | 70 | struct Good9 |
| 71 | { |
| 72 | Matrix<float,2,2,Matrix_DontAlign> m; // good: no alignment requested |
| 73 | float f; |
| 74 | }; |
| 75 | |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 76 | template<typename T> |
| 77 | void 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 | |
| 86 | template<typename T> |
| 87 | void check_unalignedassert_bad() |
| 88 | { |
Benoit Jacob | 15ca665 | 2009-01-04 15:26:32 +0000 | [diff] [blame^] | 89 | float buf[sizeof(T)+16]; |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 90 | 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 | |
| 96 | void 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 Jacob | 15ca665 | 2009-01-04 15:26:32 +0000 | [diff] [blame^] | 106 | check_unalignedassert_good<Good9>(); |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void test_unalignedassert() |
| 110 | { |
| 111 | CALL_SUBTEST(unalignedassert()); |
| 112 | } |