Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 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 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 27 | struct TestNew1 |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 28 | { |
| 29 | MatrixXd m; // good: m will allocate its own array, taking care of alignment. |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 30 | TestNew1() : m(20,20) {} |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 33 | struct TestNew2 |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 34 | { |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 35 | Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned, |
| 36 | // 8-byte alignment is good enough here, which we'll get automatically |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 39 | struct TestNew3 |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 40 | { |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 41 | Vector2f m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 44 | struct TestNew4 |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 45 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 46 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 47 | Vector2d m; |
| 48 | 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 |
| 49 | }; |
| 50 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 51 | struct TestNew5 |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 52 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 53 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 54 | float f; // try the f at first -- the EIGEN_ALIGN16 attribute of m should make that still work |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 55 | Matrix4f m; |
| 56 | }; |
| 57 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 58 | struct TestNew6 |
Benoit Jacob | 15ca665 | 2009-01-04 15:26:32 +0000 | [diff] [blame] | 59 | { |
Benoit Jacob | 2db4342 | 2009-01-06 18:07:16 +0000 | [diff] [blame] | 60 | Matrix<float,2,2,DontAlign> m; // good: no alignment requested |
Benoit Jacob | 15ca665 | 2009-01-04 15:26:32 +0000 | [diff] [blame] | 61 | float f; |
| 62 | }; |
| 63 | |
Benoit Jacob | 1c29d70 | 2009-01-06 03:16:50 +0000 | [diff] [blame] | 64 | template<bool Align> struct Depends |
| 65 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 66 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align) |
Benoit Jacob | 1c29d70 | 2009-01-06 03:16:50 +0000 | [diff] [blame] | 67 | Vector2d m; |
| 68 | float f; |
| 69 | }; |
| 70 | |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 71 | template<typename T> |
| 72 | void check_unalignedassert_good() |
| 73 | { |
| 74 | T *x, *y; |
| 75 | x = new T; |
| 76 | delete x; |
| 77 | y = new T[2]; |
| 78 | delete[] y; |
| 79 | } |
| 80 | |
Benoit Jacob | 95bda5e | 2009-05-03 13:50:56 +0000 | [diff] [blame] | 81 | #if EIGEN_ALIGN |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 82 | template<typename T> |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 83 | void construct_at_boundary(int boundary) |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 84 | { |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 85 | char buf[sizeof(T)+256]; |
| 86 | size_t _buf = reinterpret_cast<size_t>(buf); |
| 87 | _buf += (16 - (_buf % 16)); // make 16-byte aligned |
| 88 | _buf += boundary; // make exact boundary-aligned |
| 89 | T *x = ::new(reinterpret_cast<void*>(_buf)) T; |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 90 | x->~T(); |
| 91 | } |
Benoit Jacob | f81479d | 2009-02-04 16:55:38 +0000 | [diff] [blame] | 92 | #endif |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 93 | |
| 94 | void unalignedassert() |
| 95 | { |
Benoit Jacob | bb1cc0d | 2009-10-05 10:55:42 -0400 | [diff] [blame^] | 96 | construct_at_boundary<Vector2f>(4); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 97 | construct_at_boundary<Vector3f>(4); |
| 98 | construct_at_boundary<Vector4f>(16); |
| 99 | construct_at_boundary<Matrix2f>(16); |
| 100 | construct_at_boundary<Matrix3f>(4); |
| 101 | construct_at_boundary<Matrix4f>(16); |
| 102 | |
| 103 | construct_at_boundary<Vector2d>(16); |
Benoit Jacob | bb1cc0d | 2009-10-05 10:55:42 -0400 | [diff] [blame^] | 104 | construct_at_boundary<Vector3d>(4); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 105 | construct_at_boundary<Vector4d>(16); |
| 106 | construct_at_boundary<Matrix2d>(16); |
Benoit Jacob | bb1cc0d | 2009-10-05 10:55:42 -0400 | [diff] [blame^] | 107 | construct_at_boundary<Matrix3d>(4); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 108 | construct_at_boundary<Matrix4d>(16); |
| 109 | |
Benoit Jacob | bb1cc0d | 2009-10-05 10:55:42 -0400 | [diff] [blame^] | 110 | construct_at_boundary<Vector2cf>(16); |
| 111 | construct_at_boundary<Vector3cf>(4); |
| 112 | construct_at_boundary<Vector2cd>(16); |
| 113 | construct_at_boundary<Vector3cd>(16); |
| 114 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 115 | check_unalignedassert_good<TestNew1>(); |
| 116 | check_unalignedassert_good<TestNew2>(); |
| 117 | check_unalignedassert_good<TestNew3>(); |
Benoit Jacob | f81479d | 2009-02-04 16:55:38 +0000 | [diff] [blame] | 118 | |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 119 | check_unalignedassert_good<TestNew4>(); |
| 120 | check_unalignedassert_good<TestNew5>(); |
| 121 | check_unalignedassert_good<TestNew6>(); |
Benoit Jacob | 1c29d70 | 2009-01-06 03:16:50 +0000 | [diff] [blame] | 122 | check_unalignedassert_good<Depends<true> >(); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 123 | |
Benoit Jacob | 95bda5e | 2009-05-03 13:50:56 +0000 | [diff] [blame] | 124 | #if EIGEN_ALIGN |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 125 | VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4f>(8)); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 126 | VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4f>(8)); |
| 127 | VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2d>(8)); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 128 | VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4d>(8)); |
| 129 | VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix2d>(8)); |
Benoit Jacob | d415778 | 2009-10-05 10:11:11 -0400 | [diff] [blame] | 130 | VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4d>(8)); |
Benoit Jacob | bb1cc0d | 2009-10-05 10:55:42 -0400 | [diff] [blame^] | 131 | VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cf>(8)); |
| 132 | VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cd>(8)); |
Benoit Jacob | f81479d | 2009-02-04 16:55:38 +0000 | [diff] [blame] | 133 | #endif |
Benoit Jacob | 3958e7f | 2008-12-31 00:05:22 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void test_unalignedassert() |
| 137 | { |
| 138 | CALL_SUBTEST(unalignedassert()); |
| 139 | } |