Line 0
Link Here
|
|
|
1 |
// This file is part of Eigen, a lightweight C++ template library |
2 |
// for linear algebra. |
3 |
// |
4 |
// Copyright (C) 2011 John Roll <john@rkroll.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 |
#define EIGEN2_SUPPORT |
26 |
#define EIGEN_NO_STATIC_ASSERT |
27 |
#define EIGEN_INCLUDE_ARRAY_BIT_SHIFT |
28 |
#include "main.h" |
29 |
#include <functional> |
30 |
|
31 |
#include <iostream> |
32 |
using namespace std; |
33 |
|
34 |
#ifdef min |
35 |
#undef min |
36 |
#endif |
37 |
|
38 |
#ifdef max |
39 |
#undef max |
40 |
#endif |
41 |
|
42 |
template<typename ArrayType> void array_bit_ops(ArrayType a) |
43 |
{ |
44 |
typename ArrayType::Scalar pow = 1; |
45 |
typename ArrayType::Scalar one = 1; |
46 |
|
47 |
a.setConstant(1); |
48 |
|
49 |
for ( unsigned int i = 0; i < sizeof(typename ArrayType::Scalar)*8; i++ ) { |
50 |
|
51 |
/* The Lhs is generated with bit shifting native types and multiplication. |
52 |
The Rhs is generated with the array bit operations. |
53 |
*/ |
54 |
VERIFY(((a*((one<<i) & 1)) == ((a*(one<<i)) & 1 )).all()); |
55 |
VERIFY(((a*((one<<i) & 1)) == ( 1 & (a*(one<<i)) )).all()); |
56 |
VERIFY(((a*((one<<i) & 1)) == ((a*(one<<i)) & (a*1) )).all()); |
57 |
|
58 |
VERIFY(((a*((one<<i) | 1)) == ((a*(one<<i)) | 1 )).all()); |
59 |
VERIFY(((a*((one<<i) | 1)) == ( 1 | (a*(one<<i)) )).all()); |
60 |
VERIFY(((a*((one<<i) | 1)) == ((a*(one<<i)) | (a*1) )).all()); |
61 |
|
62 |
VERIFY(((a*((one<<i) ^ 1)) == ((a*(one<<i)) ^ 1 )).all()); |
63 |
VERIFY(((a*((one<<i) ^ 1)) == ( 1 ^ (a*(one<<i)) )).all()); |
64 |
VERIFY(((a*((one<<i) ^ 1)) == ((a*(one<<i)) ^ (a*1) )).all()); |
65 |
|
66 |
VERIFY((a*pow == a << i ).all()); |
67 |
VERIFY((a*pow == a << (a*i)).all()); |
68 |
|
69 |
if ( i != sizeof(typename ArrayType::Scalar)*8-1 ) { // Don't shift out of sign bit |
70 |
VERIFY((((a*pow) >> i ) == a).all()); |
71 |
VERIFY((((a*pow) >> (a*i)) == a).all()); |
72 |
} |
73 |
|
74 |
pow *= 2; |
75 |
} |
76 |
} |
77 |
|
78 |
void test_array_bit_ops() |
79 |
{ |
80 |
for(int i = 0; i < g_repeat ; i++) { |
81 |
CALL_SUBTEST_1( array_bit_ops(Array<int, EIGEN_TEST_MAX_SIZE, EIGEN_TEST_MAX_SIZE>()) ); |
82 |
CALL_SUBTEST_2( array_bit_ops(Array<short, EIGEN_TEST_MAX_SIZE, EIGEN_TEST_MAX_SIZE>()) ); |
83 |
CALL_SUBTEST_3( array_bit_ops(Array<unsigned short, EIGEN_TEST_MAX_SIZE, EIGEN_TEST_MAX_SIZE>()) ); |
84 |
CALL_SUBTEST_4( array_bit_ops(Array<long, EIGEN_TEST_MAX_SIZE, EIGEN_TEST_MAX_SIZE>()) ); |
85 |
CALL_SUBTEST_5( array_bit_ops(Array<long long, EIGEN_TEST_MAX_SIZE, EIGEN_TEST_MAX_SIZE>()) ); |
86 |
} |
87 |
} |