packetmath_test_shared.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #include "main.h"
12 #include <typeinfo>
13 
14 #if defined __GNUC__ && __GNUC__>=6
15  #pragma GCC diagnostic ignored "-Wignored-attributes"
16 #endif
17 // using namespace Eigen;
18 
19 bool g_first_pass = true;
20 
21 namespace Eigen {
22 namespace internal {
23 
24 template<typename T> T negate(const T& x) { return -x; }
25 
26 template<typename T>
27 Map<const Array<unsigned char,sizeof(T),1> >
28 bits(const T& x) {
29  return Map<const Array<unsigned char,sizeof(T),1> >(reinterpret_cast<const unsigned char *>(&x));
30 }
31 
32 // The following implement bitwise operations on floating point types
33 template<typename T,typename Bits,typename Func>
34 T apply_bit_op(Bits a, Bits b, Func f) {
35  Array<unsigned char,sizeof(T),1> data;
36  T res;
37  for(Index i = 0; i < data.size(); ++i)
38  data[i] = f(a[i], b[i]);
39  // Note: The reinterpret_cast works around GCC's class-memaccess warnings:
40  std::memcpy(reinterpret_cast<unsigned char*>(&res), data.data(), sizeof(T));
41  return res;
42 }
43 
44 #define EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,T) \
45  template<> T EIGEN_CAT(p,OP)(const T& a,const T& b) { \
46  return apply_bit_op<T>(bits(a),bits(b),FUNC); \
47  }
48 
49 #define EIGEN_TEST_MAKE_BITWISE(OP,FUNC) \
50  EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,float) \
51  EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,double) \
52  EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,half) \
53  EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,bfloat16) \
54  EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,std::complex<float>) \
55  EIGEN_TEST_MAKE_BITWISE2(OP,FUNC,std::complex<double>)
56 
57 EIGEN_TEST_MAKE_BITWISE(xor,std::bit_xor<unsigned char>())
58 EIGEN_TEST_MAKE_BITWISE(and,std::bit_and<unsigned char>())
59 EIGEN_TEST_MAKE_BITWISE(or, std::bit_or<unsigned char>())
60 struct bit_andnot{
61  template<typename T> T
62  operator()(T a, T b) const { return a & (~b); }
63 };
64 EIGEN_TEST_MAKE_BITWISE(andnot, bit_andnot())
65 template<typename T>
66 bool biteq(T a, T b) {
67  return (bits(a) == bits(b)).all();
68 }
69 
70 }
71 
72 namespace test {
73 
74 // NOTE: we disable inlining for this function to workaround a GCC issue when using -O3 and the i387 FPU.
75 template<typename Scalar> EIGEN_DONT_INLINE
76 bool isApproxAbs(const Scalar& a, const Scalar& b, const typename NumTraits<Scalar>::Real& refvalue)
77 {
78  return internal::isMuchSmallerThan(a-b, refvalue);
79 }
80 
81 template<typename Scalar>
82 inline void print_mismatch(const Scalar* ref, const Scalar* vec, int size) {
83  std::cout << "ref: [" << Map<const Matrix<Scalar,1,Dynamic> >(ref,size) << "]" << " != vec: [" << Map<const Matrix<Scalar,1,Dynamic> >(vec,size) << "]\n";
84 }
85 
86 template<typename Scalar> bool areApproxAbs(const Scalar* a, const Scalar* b, int size, const typename NumTraits<Scalar>::Real& refvalue)
87 {
88  for (int i=0; i<size; ++i)
89  {
90  if (!isApproxAbs(a[i],b[i],refvalue))
91  {
93  return false;
94  }
95  }
96  return true;
97 }
98 
99 template<typename Scalar> bool areApprox(const Scalar* a, const Scalar* b, int size)
100 {
101  for (int i=0; i<size; ++i)
102  {
103  if ( a[i]!=b[i] && !internal::isApprox(a[i],b[i])
104  && !((numext::isnan)(a[i]) && (numext::isnan)(b[i])) )
105  {
106  print_mismatch(a, b, size);
107  return false;
108  }
109  }
110  return true;
111 }
112 
113 template<typename Scalar> bool areEqual(const Scalar* a, const Scalar* b, int size)
114 {
115  for (int i=0; i<size; ++i)
116  {
117  if ( (a[i] != b[i]) && !((numext::isnan)(a[i]) && (numext::isnan)(b[i])) )
118  {
119  print_mismatch(a, b, size);
120  return false;
121  }
122  }
123  return true;
124 }
125 
126 #define CHECK_CWISE1(REFOP, POP) { \
127  for (int i=0; i<PacketSize; ++i) \
128  ref[i] = REFOP(data1[i]); \
129  internal::pstore(data2, POP(internal::pload<Packet>(data1))); \
130  VERIFY(test::areApprox(ref, data2, PacketSize) && #POP); \
131 }
132 
133 // Checks component-wise for input of size N. All of data1, data2, and ref
134 // should have size at least ceil(N/PacketSize)*PacketSize to avoid memory
135 // access errors.
136 #define CHECK_CWISE1_N(REFOP, POP, N) { \
137  for (int i=0; i<N; ++i) \
138  ref[i] = REFOP(data1[i]); \
139  for (int j=0; j<N; j+=PacketSize) \
140  internal::pstore(data2 + j, POP(internal::pload<Packet>(data1 + j))); \
141  VERIFY(test::areApprox(ref, data2, N) && #POP); \
142 }
143 
144 template<bool Cond,typename Packet>
146 {
147  template<typename T>
148  inline Packet load(const T* from) const { return internal::pload<Packet>(from); }
149 
150  template<typename T>
151  inline Packet loadu(const T* from) const { return internal::ploadu<Packet>(from); }
152 
153  template<typename T>
154  inline Packet load(const T* from, unsigned long long umask) const { return internal::ploadu<Packet>(from, umask); }
155 
156  template<typename T>
157  inline void store(T* to, const Packet& x) const { internal::pstore(to,x); }
158 
159  template<typename T>
160  inline void store(T* to, const Packet& x, unsigned long long umask) const { internal::pstoreu(to, x, umask); }
161 
162  template<typename T>
163  inline Packet& forward_reference(Packet& packet, T& /*scalar*/) const { return packet; }
164 };
165 
166 template<typename Packet>
167 struct packet_helper<false,Packet>
168 {
169  template<typename T>
170  inline T load(const T* from) const { return *from; }
171 
172  template<typename T>
173  inline T loadu(const T* from) const { return *from; }
174 
175  template<typename T>
176  inline T load(const T* from, unsigned long long) const { return *from; }
177 
178  template<typename T>
179  inline void store(T* to, const T& x) const { *to = x; }
180 
181  template<typename T>
182  inline void store(T* to, const T& x, unsigned long long) const { *to = x; }
183 
184  template<typename T>
185  inline T& forward_reference(Packet& /*packet*/, T& scalar) const { return scalar; }
186 };
187 
188 #define CHECK_CWISE1_IF(COND, REFOP, POP) if(COND) { \
189  test::packet_helper<COND,Packet> h; \
190  for (int i=0; i<PacketSize; ++i) \
191  ref[i] = Scalar(REFOP(data1[i])); \
192  h.store(data2, POP(h.load(data1))); \
193  VERIFY(test::areApprox(ref, data2, PacketSize) && #POP); \
194 }
195 
196 #define CHECK_CWISE1_EXACT_IF(COND, REFOP, POP) if(COND) { \
197  test::packet_helper<COND,Packet> h; \
198  for (int i=0; i<PacketSize; ++i) \
199  ref[i] = Scalar(REFOP(data1[i])); \
200  h.store(data2, POP(h.load(data1))); \
201  VERIFY(test::areEqual(ref, data2, PacketSize) && #POP); \
202 }
203 
204 #define CHECK_CWISE2_IF(COND, REFOP, POP) if(COND) { \
205  test::packet_helper<COND,Packet> h; \
206  for (int i=0; i<PacketSize; ++i) \
207  ref[i] = Scalar(REFOP(data1[i], data1[i+PacketSize])); \
208  h.store(data2, POP(h.load(data1),h.load(data1+PacketSize))); \
209  VERIFY(test::areApprox(ref, data2, PacketSize) && #POP); \
210 }
211 
212 // One input, one output by reference.
213 #define CHECK_CWISE1_BYREF1_IF(COND, REFOP, POP) if(COND) { \
214  test::packet_helper<COND,Packet> h; \
215  for (int i=0; i<PacketSize; ++i) \
216  ref[i] = Scalar(REFOP(data1[i], ref[i+PacketSize])); \
217  Packet pout; \
218  Scalar sout; \
219  h.store(data2, POP(h.load(data1), h.forward_reference(pout, sout))); \
220  h.store(data2+PacketSize, h.forward_reference(pout, sout)); \
221  VERIFY(test::areApprox(ref, data2, 2 * PacketSize) && #POP); \
222 }
223 
224 #define CHECK_CWISE3_IF(COND, REFOP, POP) if (COND) { \
225  test::packet_helper<COND, Packet> h; \
226  for (int i = 0; i < PacketSize; ++i) \
227  ref[i] = Scalar(REFOP(data1[i], data1[i + PacketSize], \
228  data1[i + 2 * PacketSize])); \
229  h.store(data2, POP(h.load(data1), h.load(data1 + PacketSize), \
230  h.load(data1 + 2 * PacketSize))); \
231  VERIFY(test::areApprox(ref, data2, PacketSize) && #POP); \
232 }
233 
234 // Specialize the runall struct in your test file by defining run().
235 template<
236  typename Scalar,
237  typename PacketType,
239  bool IsInteger = NumTraits<Scalar>::IsInteger>
240 struct runall;
241 
242 template<
243  typename Scalar,
247 struct runner;
248 
249 template<typename Scalar,typename PacketType>
250 struct runner<Scalar,PacketType,true,true>
251 {
252  static void run() {
255  }
256 };
257 
258 template<typename Scalar,typename PacketType>
259 struct runner<Scalar,PacketType,true,false>
260 {
261  static void run() {
263  }
264 };
265 
266 template<typename Scalar,typename PacketType>
267 struct runner<Scalar,PacketType,false,false>
268 {
269  static void run() {
271  }
272 };
273 
274 }
275 }
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::test::runall::run
static void run()
Definition: special_packetmath.cpp:130
Eigen::internal::biteq
bool biteq(T a, T b)
Definition: packetmath_test_shared.h:66
Eigen::test::areApproxAbs
bool areApproxAbs(const Scalar *a, const Scalar *b, int size, const typename NumTraits< Scalar >::Real &refvalue)
Definition: packetmath_test_shared.h:86
Eigen::test::packet_helper::store
void store(T *to, const Packet &x, unsigned long long umask) const
Definition: packetmath_test_shared.h:160
Eigen::internal::bit_andnot::operator()
T operator()(T a, T b) const
Definition: packetmath_test_shared.h:62
Eigen::internal::pstoreu
EIGEN_DEVICE_FUNC void pstoreu(Scalar *to, const Packet &from)
Definition: GenericPacketMath.h:700
Eigen::internal::bits
Map< const Array< unsigned char, sizeof(T), 1 > > bits(const T &x)
Definition: packetmath_test_shared.h:28
Eigen::test::packet_helper::load
Packet load(const T *from) const
Definition: packetmath_test_shared.h:148
b
Scalar * b
Definition: benchVecAdd.cpp:17
Eigen::test::areEqual
bool areEqual(const Scalar *a, const Scalar *b, int size)
Definition: packetmath_test_shared.h:113
Eigen::test::runner< Scalar, PacketType, true, false >::run
static void run()
Definition: packetmath_test_shared.h:261
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
Eigen::test::runall
Definition: packetmath_test_shared.h:240
Eigen::internal::packet_traits
Definition: GenericPacketMath.h:106
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
Eigen::internal::isApprox
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1947
Eigen::test::packet_helper< false, Packet >::loadu
T loadu(const T *from) const
Definition: packetmath_test_shared.h:173
Eigen::test::packet_helper::forward_reference
Packet & forward_reference(Packet &packet, T &) const
Definition: packetmath_test_shared.h:163
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
Eigen::internal::isMuchSmallerThan
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1940
Eigen::test::packet_helper< false, Packet >::store
void store(T *to, const T &x, unsigned long long) const
Definition: packetmath_test_shared.h:182
IsComplex
@ IsComplex
Definition: gtsam/3rdparty/Eigen/blas/common.h:98
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
test
Definition: test.py:1
Eigen::PacketType
Definition: TensorMeta.h:50
data
int data[]
Definition: Map_placement_new.cpp:1
Eigen::internal::apply_bit_op
T apply_bit_op(Bits a, Bits b, Func f)
Definition: packetmath_test_shared.h:34
Eigen::internal::negate
T negate(const T &x)
Definition: packetmath_test_shared.h:24
Eigen::test::runner< Scalar, PacketType, false, false >::run
static void run()
Definition: packetmath_test_shared.h:269
scalar
mxArray * scalar(mxClassID classid)
Definition: matlab.h:82
Eigen::test::packet_helper::load
Packet load(const T *from, unsigned long long umask) const
Definition: packetmath_test_shared.h:154
Eigen::test::packet_helper::store
void store(T *to, const Packet &x) const
Definition: packetmath_test_shared.h:157
Eigen::internal::bit_andnot
Definition: packetmath_test_shared.h:60
Eigen::numext::isnan
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool() isnan(const Eigen::bfloat16 &h)
Definition: BFloat16.h:659
Eigen::test::packet_helper::loadu
Packet loadu(const T *from) const
Definition: packetmath_test_shared.h:151
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
Eigen::Triplet< double >
Eigen::test::packet_helper
Definition: packetmath_test_shared.h:145
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Eigen::test::areApprox
bool areApprox(const Scalar *a, const Scalar *b, int size)
Definition: packetmath_test_shared.h:99
Eigen::internal::pstore
EIGEN_DEVICE_FUNC void pstore(Scalar *to, const Packet &from)
Definition: GenericPacketMath.h:696
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
EIGEN_TEST_MAKE_BITWISE
#define EIGEN_TEST_MAKE_BITWISE(OP, FUNC)
Definition: packetmath_test_shared.h:49
Eigen::test::packet_helper< false, Packet >::store
void store(T *to, const T &x) const
Definition: packetmath_test_shared.h:179
std
Definition: BFloat16.h:88
ref
Reference counting helper.
Definition: object.h:67
Eigen::test::print_mismatch
void print_mismatch(const Scalar *ref, const Scalar *vec, int size)
Definition: packetmath_test_shared.h:82
g_first_pass
bool g_first_pass
Definition: packetmath_test_shared.h:19
Eigen::test::runner< Scalar, PacketType, true, true >::run
static void run()
Definition: packetmath_test_shared.h:252
Eigen::internal::is_same
Definition: Meta.h:148
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
Eigen::test::isApproxAbs
EIGEN_DONT_INLINE bool isApproxAbs(const Scalar &a, const Scalar &b, const typename NumTraits< Scalar >::Real &refvalue)
Definition: packetmath_test_shared.h:76
internal
Definition: BandTriangularSolver.h:13
Eigen::test::packet_helper< false, Packet >::load
T load(const T *from, unsigned long long) const
Definition: packetmath_test_shared.h:176
Eigen::test::packet_helper< false, Packet >::forward_reference
T & forward_reference(Packet &, T &scalar) const
Definition: packetmath_test_shared.h:185
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::test::packet_helper< false, Packet >::load
T load(const T *from) const
Definition: packetmath_test_shared.h:170
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
EIGEN_DONT_INLINE
#define EIGEN_DONT_INLINE
Definition: Macros.h:940
test_eigen.ref
ref
Definition: test_eigen.py:9
Eigen::test::runner
Definition: packetmath_test_shared.h:247


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:12:26