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

Functions

template<typename T >
int ref_clz (T val)
 
template<typename T >
int ref_ctz (T val)
 
template<typename T >
void test_clz_ctz ()
 
template<typename T >
void test_clz_ctz_random ()
 
 EIGEN_DECLARE_TEST (clz)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( clz  )
64  {
65  CALL_SUBTEST_1(test_clz_ctz<uint8_t>());
66  CALL_SUBTEST_2(test_clz_ctz<uint16_t>());
67  CALL_SUBTEST_3(test_clz_ctz<uint32_t>());
68  CALL_SUBTEST_4(test_clz_ctz<uint64_t>());
69 
70  for (int i = 0; i < g_repeat; i++) {
71  test_clz_ctz_random<uint32_t>();
72  test_clz_ctz_random<uint64_t>();
73  }
74 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
static int g_repeat
Definition: main.h:191
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, Eigen::g_repeat, and i.

◆ ref_clz()

template<typename T >
int ref_clz ( T  val)
13  {
14  constexpr int kNumBits = sizeof(T) * CHAR_BIT;
15  T kMsbMask = T(1) << (kNumBits - 1);
16  int z = 0;
17  for (; z < kNumBits && ((val & kMsbMask) == 0); ++z) {
18  val <<= 1;
19  }
20  return z;
21 }
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
val
Definition: calibrate.py:119

References calibrate::val.

Referenced by test_clz_ctz(), and test_clz_ctz_random().

◆ ref_ctz()

template<typename T >
int ref_ctz ( T  val)
24  {
25  constexpr int kNumBits = sizeof(T) * CHAR_BIT;
26  T kLsbMask = T(1);
27  int z = 0;
28  for (; z < kNumBits && ((val & kLsbMask) == 0); ++z) {
29  val >>= 1;
30  }
31  return z;
32 }

References calibrate::val.

Referenced by test_clz_ctz(), and test_clz_ctz_random().

◆ test_clz_ctz()

template<typename T >
void test_clz_ctz ( )
35  {
36  T step = sizeof(T) <= 2 ? 1 : (Eigen::NumTraits<T>::highest() / (T(1) << 16));
37  T iters = Eigen::NumTraits<T>::highest() / step;
38  for (T i = 0; i < iters; ++i) {
39  T val = i * step;
40  int expected_clz = ref_clz(val);
41  int actual_clz = Eigen::internal::clz(val);
42  VERIFY(expected_clz == actual_clz);
43 
44  int expected_ctz = ref_ctz(val);
45  int actual_ctz = Eigen::internal::ctz(val);
46  VERIFY(expected_ctz == actual_ctz);
47  }
48 }
int ref_ctz(T val)
Definition: clz.cpp:24
int ref_clz(T val)
Definition: clz.cpp:13
#define VERIFY(a)
Definition: main.h:362
EIGEN_DEVICE_FUNC int clz(BitsType bits)
Definition: MathFunctions.h:644
EIGEN_DEVICE_FUNC int ctz(BitsType bits)
Definition: MathFunctions.h:650
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References Eigen::internal::clz(), Eigen::internal::ctz(), i, ref_clz(), ref_ctz(), calibrate::val, and VERIFY.

◆ test_clz_ctz_random()

template<typename T >
void test_clz_ctz_random ( )
51  {
52  for (int i = 0; i < 1024 * 1024; ++i) {
53  T val = Eigen::internal::random<T>();
54  int expected_clz = ref_clz(val);
55  int actual_clz = Eigen::internal::clz(val);
56  VERIFY(expected_clz == actual_clz);
57 
58  int expected_ctz = ref_ctz(val);
59  int actual_ctz = Eigen::internal::ctz(val);
60  VERIFY(expected_ctz == actual_ctz);
61  }
62 }

References Eigen::internal::clz(), Eigen::internal::ctz(), i, ref_clz(), ref_ctz(), calibrate::val, and VERIFY.