numext.cpp File Reference
#include "main.h"

Classes

struct  check_sqrt_impl< T >
 
struct  check_sqrt_impl< std::complex< T > >
 
struct  check_rsqrt_impl< T >
 
struct  check_rsqrt_impl< std::complex< T > >
 
struct  check_signbit_impl< T >
 

Macros

#define VERIFY_IS_EQUAL_OR_NANS(a, b)   VERIFY(test_is_equal_or_nans(a, b))
 

Functions

template<typename T , typename U >
bool check_if_equal_or_nans (const T &actual, const U &expected)
 
template<typename T , typename U >
bool check_if_equal_or_nans (const std::complex< T > &actual, const std::complex< U > &expected)
 
template<typename T , typename U >
bool test_is_equal_or_nans (const T &actual, const U &expected)
 
template<typename T >
void check_negate ()
 
template<typename T >
void check_abs ()
 
template<>
void check_abs< bool > ()
 
template<typename T >
void check_arg ()
 
template<typename T >
void check_sqrt ()
 
template<typename T >
void check_rsqrt ()
 
template<typename T >
void check_signbit ()
 
template<typename T >
void check_shift ()
 
 EIGEN_DECLARE_TEST (numext)
 

Macro Definition Documentation

◆ VERIFY_IS_EQUAL_OR_NANS

#define VERIFY_IS_EQUAL_OR_NANS (   a,
  b 
)    VERIFY(test_is_equal_or_nans(a, b))

Function Documentation

◆ check_abs()

template<typename T >
void check_abs ( )
48  {
49  typedef typename NumTraits<T>::Real Real;
50  Real zero(0);
51 
53  VERIFY_IS_EQUAL(numext::abs(T(0)), T(0));
54  VERIFY_IS_EQUAL(numext::abs(T(1)), T(1));
55 
56  for (int k = 0; k < 100; ++k) {
57  T x = internal::random<T>();
58  x = x / Real(2);
62  }
63  VERIFY(numext::abs(x) >= zero);
65  }
66 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 100 >, boost::multiprecision::et_on > Real
Definition: boostmultiprec.cpp:77
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY(a)
Definition: main.h:362
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
T negate(const T &x)
Definition: packetmath_test_shared.h:26
list x
Definition: plotDoE.py:28
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
EIGEN_DONT_INLINE Scalar zero()
Definition: svd_common.h:232

References abs(), Eigen::numext::abs2(), k, Eigen::test::negate(), VERIFY, VERIFY_IS_APPROX, VERIFY_IS_EQUAL, plotDoE::x, and zero().

Referenced by EIGEN_DECLARE_TEST().

◆ check_abs< bool >()

template<>
void check_abs< bool > ( )
69  {
70  for (bool x : {true, false}) {
72  VERIFY(numext::abs(x) >= false);
74  }
75 }

References abs(), Eigen::numext::abs2(), VERIFY, VERIFY_IS_EQUAL, and plotDoE::x.

Referenced by EIGEN_DECLARE_TEST().

◆ check_arg()

template<typename T >
void check_arg ( )
78  {
79  typedef typename NumTraits<T>::Real Real;
80  VERIFY_IS_EQUAL(numext::abs(T(0)), T(0));
81  VERIFY_IS_EQUAL(numext::abs(T(1)), T(1));
82 
83  for (int k = 0; k < 100; ++k) {
84  T x = internal::random<T>();
85  Real y = numext::arg(x);
86  VERIFY_IS_APPROX(y, std::arg(x));
87  }
88 }
Scalar * y
Definition: level1_cplx_impl.h:128

References abs(), k, VERIFY_IS_APPROX, VERIFY_IS_EQUAL, plotDoE::x, and y.

Referenced by EIGEN_DECLARE_TEST().

◆ check_if_equal_or_nans() [1/2]

template<typename T , typename U >
bool check_if_equal_or_nans ( const std::complex< T > &  actual,
const std::complex< U > &  expected 
)
18  {
19  return check_if_equal_or_nans(numext::real(actual), numext::real(expected)) &&
21 }
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:132
float real
Definition: datatypes.h:10
bool check_if_equal_or_nans(const T &actual, const U &expected)
Definition: numext.cpp:13

References check_if_equal_or_nans(), and imag().

◆ check_if_equal_or_nans() [2/2]

template<typename T , typename U >
bool check_if_equal_or_nans ( const T actual,
const U &  expected 
)
13  {
14  return (numext::equal_strict(actual, expected) || ((numext::isnan)(actual) && (numext::isnan)(expected)));
15 }
#define isnan(X)
Definition: main.h:109
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool equal_strict(const X &x, const Y &y)
Definition: Meta.h:571

References Eigen::numext::equal_strict(), and isnan.

Referenced by check_if_equal_or_nans(), and test_is_equal_or_nans().

◆ check_negate()

template<typename T >
void check_negate ( )
37  {
38  Index size = 1000;
39  for (Index i = 0; i < size; i++) {
40  T val = i == 0 ? T(0) : internal::random<T>(T(0), NumTraits<T>::highest());
41  T neg_val = numext::negate(val);
42  VERIFY_IS_EQUAL(T(val + neg_val), T(0));
44  }
45 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
val
Definition: calibrate.py:119
Definition: Eigen_Colamd.h:49

References i, Eigen::test::negate(), size, calibrate::val, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ check_rsqrt()

template<typename T >
void check_rsqrt ( )
240  {
242 }
static void run()
Definition: numext.cpp:158

References check_rsqrt_impl< T >::run().

Referenced by EIGEN_DECLARE_TEST().

◆ check_shift()

template<typename T >
void check_shift ( )
296  {
297  using SignedT = typename numext::get_integer_by_size<sizeof(T)>::signed_type;
298  using UnsignedT = typename numext::get_integer_by_size<sizeof(T)>::unsigned_type;
299  constexpr int kNumBits = CHAR_BIT * sizeof(T);
300  for (int i = 0; i < 1000; ++i) {
301  const T a = internal::random<T>();
302  for (int s = 1; s < kNumBits; s++) {
303  T a_bsll = numext::logical_shift_left(a, s);
304  T a_bsll_ref = a << s;
305  VERIFY_IS_EQUAL(a_bsll, a_bsll_ref);
306  T a_bsrl = numext::logical_shift_right(a, s);
307  T a_bsrl_ref = numext::bit_cast<T, UnsignedT>(numext::bit_cast<UnsignedT, T>(a) >> s);
308  VERIFY_IS_EQUAL(a_bsrl, a_bsrl_ref);
309  T a_bsra = numext::arithmetic_shift_right(a, s);
310  T a_bsra_ref = numext::bit_cast<T, SignedT>(numext::bit_cast<SignedT, T>(a) >> s);
311  VERIFY_IS_EQUAL(a_bsra, a_bsra_ref);
312  }
313  }
314 }
RealScalar s
Definition: level1_cplx_impl.h:130
const Scalar * a
Definition: level2_cplx_impl.h:32
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_left(const Scalar &a, int n)
Definition: MathFunctions.h:1827
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar arithmetic_shift_right(const Scalar &a, int n)
Definition: MathFunctions.h:1838
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_right(const Scalar &a, int n)
Definition: MathFunctions.h:1832

References a, Eigen::numext::arithmetic_shift_right(), i, Eigen::numext::logical_shift_left(), Eigen::numext::logical_shift_right(), s, and VERIFY_IS_EQUAL.

◆ check_signbit()

template<typename T >
void check_signbit ( )
291  {
293 }
static void run()
Definition: numext.cpp:246

References check_signbit_impl< T >::run().

◆ check_sqrt()

template<typename T >
void check_sqrt ( )
152  {
154 }
static void run()
Definition: numext.cpp:92

References check_sqrt_impl< T >::run().

Referenced by EIGEN_DECLARE_TEST().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( numext  )
316  {
317  for (int k = 0; k < g_repeat; ++k) {
318  CALL_SUBTEST(check_negate<signed char>());
319  CALL_SUBTEST(check_negate<unsigned char>());
320  CALL_SUBTEST(check_negate<short>());
321  CALL_SUBTEST(check_negate<unsigned short>());
322  CALL_SUBTEST(check_negate<int>());
323  CALL_SUBTEST(check_negate<unsigned int>());
324  CALL_SUBTEST(check_negate<long>());
325  CALL_SUBTEST(check_negate<unsigned long>());
326  CALL_SUBTEST(check_negate<half>());
327  CALL_SUBTEST(check_negate<bfloat16>());
328  CALL_SUBTEST(check_negate<float>());
329  CALL_SUBTEST(check_negate<double>());
330  CALL_SUBTEST(check_negate<long double>());
331  CALL_SUBTEST(check_negate<std::complex<float> >());
332  CALL_SUBTEST(check_negate<std::complex<double> >());
333 
335  CALL_SUBTEST(check_abs<signed char>());
336  CALL_SUBTEST(check_abs<unsigned char>());
337  CALL_SUBTEST(check_abs<short>());
338  CALL_SUBTEST(check_abs<unsigned short>());
339  CALL_SUBTEST(check_abs<int>());
340  CALL_SUBTEST(check_abs<unsigned int>());
341  CALL_SUBTEST(check_abs<long>());
342  CALL_SUBTEST(check_abs<unsigned long>());
343  CALL_SUBTEST(check_abs<half>());
344  CALL_SUBTEST(check_abs<bfloat16>());
345  CALL_SUBTEST(check_abs<float>());
346  CALL_SUBTEST(check_abs<double>());
347  CALL_SUBTEST(check_abs<long double>());
348  CALL_SUBTEST(check_abs<std::complex<float> >());
349  CALL_SUBTEST(check_abs<std::complex<double> >());
350 
351  CALL_SUBTEST(check_arg<std::complex<float> >());
352  CALL_SUBTEST(check_arg<std::complex<double> >());
353 
354  CALL_SUBTEST(check_sqrt<float>());
355  CALL_SUBTEST(check_sqrt<double>());
356  CALL_SUBTEST(check_sqrt<std::complex<float> >());
357  CALL_SUBTEST(check_sqrt<std::complex<double> >());
358 
359  CALL_SUBTEST(check_rsqrt<float>());
360  CALL_SUBTEST(check_rsqrt<double>());
361  CALL_SUBTEST(check_rsqrt<std::complex<float> >());
362  CALL_SUBTEST(check_rsqrt<std::complex<double> >());
363 
364  CALL_SUBTEST(check_signbit<half>());
365  CALL_SUBTEST(check_signbit<bfloat16>());
366  CALL_SUBTEST(check_signbit<float>());
367  CALL_SUBTEST(check_signbit<double>());
368 
369  CALL_SUBTEST(check_signbit<uint8_t>());
370  CALL_SUBTEST(check_signbit<uint16_t>());
371  CALL_SUBTEST(check_signbit<uint32_t>());
372  CALL_SUBTEST(check_signbit<uint64_t>());
373 
374  CALL_SUBTEST(check_signbit<int8_t>());
375  CALL_SUBTEST(check_signbit<int16_t>());
376  CALL_SUBTEST(check_signbit<int32_t>());
377  CALL_SUBTEST(check_signbit<int64_t>());
378 
379  CALL_SUBTEST(check_shift<int8_t>());
380  CALL_SUBTEST(check_shift<int16_t>());
381  CALL_SUBTEST(check_shift<int32_t>());
382  CALL_SUBTEST(check_shift<int64_t>());
383 
384  CALL_SUBTEST(check_shift<uint8_t>());
385  CALL_SUBTEST(check_shift<uint16_t>());
386  CALL_SUBTEST(check_shift<uint32_t>());
387  CALL_SUBTEST(check_shift<uint64_t>());
388  }
389 }
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
static int g_repeat
Definition: main.h:191
void check_negate()
Definition: numext.cpp:37
void check_sqrt()
Definition: numext.cpp:152
void check_arg()
Definition: numext.cpp:78
void check_rsqrt()
Definition: numext.cpp:240
void check_abs< bool >()
Definition: numext.cpp:69
void check_abs()
Definition: numext.cpp:48

References CALL_SUBTEST, check_abs(), check_abs< bool >(), check_arg(), check_negate(), check_rsqrt(), check_sqrt(), Eigen::g_repeat, and k.

◆ test_is_equal_or_nans()

template<typename T , typename U >
bool test_is_equal_or_nans ( const T actual,
const U &  expected 
)
24  {
25  if (check_if_equal_or_nans(actual, expected)) {
26  return true;
27  }
28 
29  // false:
30  std::cerr << "\n actual = " << actual << "\n expected = " << expected << "\n\n";
31  return false;
32 }

References check_if_equal_or_nans().