Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #include "main.h"
     11 
     12 struct TestNew1
     13 {
     14   MatrixXd m; // good: m will allocate its own array, taking care of alignment.
     15   TestNew1() : m(20,20) {}
     16 };
     17 
     18 struct TestNew2
     19 {
     20   Matrix3d m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned,
     21               // 8-byte alignment is good enough here, which we'll get automatically
     22 };
     23 
     24 struct TestNew3
     25 {
     26   Vector2f m; // good: m's size isn't a multiple of 16 bytes, so m doesn't have to be 16-byte aligned
     27 };
     28 
     29 struct TestNew4
     30 {
     31   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
     32   Vector2d m;
     33   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
     34 };
     35 
     36 struct TestNew5
     37 {
     38   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
     39   float f; // try the f at first -- the EIGEN_ALIGN16 attribute of m should make that still work
     40   Matrix4f m;
     41 };
     42 
     43 struct TestNew6
     44 {
     45   Matrix<float,2,2,DontAlign> m; // good: no alignment requested
     46   float f;
     47 };
     48 
     49 template<bool Align> struct Depends
     50 {
     51   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align)
     52   Vector2d m;
     53   float f;
     54 };
     55 
     56 template<typename T>
     57 void check_unalignedassert_good()
     58 {
     59   T *x, *y;
     60   x = new T;
     61   delete x;
     62   y = new T[2];
     63   delete[] y;
     64 }
     65 
     66 #if EIGEN_ALIGN_STATICALLY
     67 template<typename T>
     68 void construct_at_boundary(int boundary)
     69 {
     70   char buf[sizeof(T)+256];
     71   size_t _buf = reinterpret_cast<size_t>(buf);
     72   _buf += (16 - (_buf % 16)); // make 16-byte aligned
     73   _buf += boundary; // make exact boundary-aligned
     74   T *x = ::new(reinterpret_cast<void*>(_buf)) T;
     75   x[0].setZero(); // just in order to silence warnings
     76   x->~T();
     77 }
     78 #endif
     79 
     80 void unalignedassert()
     81 {
     82   #if EIGEN_ALIGN_STATICALLY
     83   construct_at_boundary<Vector2f>(4);
     84   construct_at_boundary<Vector3f>(4);
     85   construct_at_boundary<Vector4f>(16);
     86   construct_at_boundary<Matrix2f>(16);
     87   construct_at_boundary<Matrix3f>(4);
     88   construct_at_boundary<Matrix4f>(16);
     89 
     90   construct_at_boundary<Vector2d>(16);
     91   construct_at_boundary<Vector3d>(4);
     92   construct_at_boundary<Vector4d>(16);
     93   construct_at_boundary<Matrix2d>(16);
     94   construct_at_boundary<Matrix3d>(4);
     95   construct_at_boundary<Matrix4d>(16);
     96 
     97   construct_at_boundary<Vector2cf>(16);
     98   construct_at_boundary<Vector3cf>(4);
     99   construct_at_boundary<Vector2cd>(16);
    100   construct_at_boundary<Vector3cd>(16);
    101   #endif
    102 
    103   check_unalignedassert_good<TestNew1>();
    104   check_unalignedassert_good<TestNew2>();
    105   check_unalignedassert_good<TestNew3>();
    106 
    107   check_unalignedassert_good<TestNew4>();
    108   check_unalignedassert_good<TestNew5>();
    109   check_unalignedassert_good<TestNew6>();
    110   check_unalignedassert_good<Depends<true> >();
    111 
    112 #if EIGEN_ALIGN_STATICALLY
    113   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4f>(8));
    114   VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4f>(8));
    115   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2d>(8));
    116   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector4d>(8));
    117   VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix2d>(8));
    118   VERIFY_RAISES_ASSERT(construct_at_boundary<Matrix4d>(8));
    119   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cf>(8));
    120   VERIFY_RAISES_ASSERT(construct_at_boundary<Vector2cd>(8));
    121 #endif
    122 }
    123 
    124 void test_unalignedassert()
    125 {
    126   CALL_SUBTEST(unalignedassert());
    127 }
    128