unalignedassert.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 
00027 struct TestNew1
00028 {
00029   MatrixXd m; // good: m will allocate its own array, taking care of alignment.
00030   TestNew1() : m(20,20) {}
00031 };
00032 
00033 struct TestNew2
00034 {
00035   Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned,
00036               // 8-byte alignment is good enough here, which we'll get automatically
00037 };
00038 
00039 struct TestNew3
00040 {
00041   Vector2f m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned
00042 };
00043 
00044 struct TestNew4
00045 {
00046   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00047   Vector2d m;
00048   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
00049 };
00050 
00051 struct TestNew5
00052 {
00053   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00054   float f; // try the f at first -- the EIGEN_ALIGN16 attribute of m should make that still work
00055   Matrix4f m;
00056 };
00057 
00058 struct TestNew6
00059 {
00060   Matrix<float,2,2,DontAlign> m; // good: no alignment requested
00061   float f;
00062 };
00063 
00064 template<bool Align> struct Depends
00065 {
00066   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align)
00067   Vector2d m;
00068   float f;
00069 };
00070 
00071 template<typename T>
00072 void check_unalignedassert_good()
00073 {
00074   T *x, *y;
00075   x = new T;
00076   delete x;
00077   y = new T[2];
00078   delete[] y;
00079 }
00080 
00081 #if EIGEN_ALIGN_STATICALLY
00082 template<typename T>
00083 void construct_at_boundary(int boundary)
00084 {
00085   char buf[sizeof(T)+256];
00086   size_t _buf = reinterpret_cast<size_t>(buf);
00087   _buf += (16 - (_buf % 16)); // make 16-byte aligned
00088   _buf += boundary; // make exact boundary-aligned
00089   T *x = ::new(reinterpret_cast<void*>(_buf)) T;
00090   x[0].setZero(); // just in order to silence warnings
00091   x->~T();
00092 }
00093 #endif
00094 
00095 void unalignedassert()
00096 {
00097   #if EIGEN_ALIGN_STATICALLY
00098   construct_at_boundary<Vector2f>(4);
00099   construct_at_boundary<Vector3f>(4);
00100   construct_at_boundary<Vector4f>(16);
00101   construct_at_boundary<Matrix2f>(16);
00102   construct_at_boundary<Matrix3f>(4);
00103   construct_at_boundary<Matrix4f>(16);
00104 
00105   construct_at_boundary<Vector2d>(16);
00106   construct_at_boundary<Vector3d>(4);
00107   construct_at_boundary<Vector4d>(16);
00108   construct_at_boundary<Matrix2d>(16);
00109   construct_at_boundary<Matrix3d>(4);
00110   construct_at_boundary<Matrix4d>(16);
00111 
00112   construct_at_boundary<Vector2cf>(16);
00113   construct_at_boundary<Vector3cf>(4);
00114   construct_at_boundary<Vector2cd>(16);
00115   construct_at_boundary<Vector3cd>(16);
00116   #endif
00117 
00118   check_unalignedassert_good<TestNew1>();
00119   check_unalignedassert_good<TestNew2>();
00120   check_unalignedassert_good<TestNew3>();
00121 
00122   check_unalignedassert_good<TestNew4>();
00123   check_unalignedassert_good<TestNew5>();
00124   check_unalignedassert_good<TestNew6>();
00125   check_unalignedassert_good<Depends<true> >();
00126 
00127 #if EIGEN_ALIGN_STATICALLY
00128   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4f>(8));
00129   VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4f>(8));
00130   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2d>(8));
00131   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4d>(8));
00132   VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix2d>(8));
00133   VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4d>(8));
00134   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cf>(8));
00135   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cd>(8));
00136 #endif
00137 }
00138 
00139 void test_unalignedassert()
00140 {
00141   CALL_SUBTEST(unalignedassert());
00142 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:35