TensorIntDiv.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) 2014 Benoit Steiner <benoit.steiner.goog@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 #ifndef EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
31 namespace internal {
32 
33 // Note: result is undefined if val == 0
34 template <typename T>
35 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<sizeof(T) == 4, int> count_leading_zeros(const T val) {
36 #ifdef EIGEN_GPU_COMPILE_PHASE
37  return __clz(val);
38 #elif defined(SYCL_DEVICE_ONLY)
39  return cl::sycl::clz(val);
40 #elif EIGEN_COMP_MSVC
41  unsigned long index;
42  _BitScanReverse(&index, val);
43  return 31 - index;
44 #else
45  EIGEN_STATIC_ASSERT(sizeof(unsigned long long) == 8, YOU_MADE_A_PROGRAMMING_MISTAKE);
46  return __builtin_clz(static_cast<uint32_t>(val));
47 #endif
48 }
49 
50 template <typename T>
51 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<sizeof(T) == 8, int> count_leading_zeros(const T val) {
52 #ifdef EIGEN_GPU_COMPILE_PHASE
53  return __clzll(val);
54 #elif defined(SYCL_DEVICE_ONLY)
55  return static_cast<int>(cl::sycl::clz(val));
56 #elif EIGEN_COMP_MSVC && EIGEN_ARCH_x86_64
57  unsigned long index;
58  _BitScanReverse64(&index, val);
59  return 63 - index;
60 #elif EIGEN_COMP_MSVC
61  // MSVC's _BitScanReverse64 is not available for 32bits builds.
62  unsigned int lo = (unsigned int)(val & 0xffffffff);
63  unsigned int hi = (unsigned int)((val >> 32) & 0xffffffff);
64  int n;
65  if (hi == 0)
66  n = 32 + count_leading_zeros<unsigned int>(lo);
67  else
68  n = count_leading_zeros<unsigned int>(hi);
69  return n;
70 #else
71  EIGEN_STATIC_ASSERT(sizeof(unsigned long long) == 8, YOU_MADE_A_PROGRAMMING_MISTAKE);
72  return __builtin_clzll(static_cast<uint64_t>(val));
73 #endif
74 }
75 
76 template <typename T>
78  typedef std::conditional_t<sizeof(T) == 8, uint64_t, uint32_t> type;
79 };
80 
81 template <typename T>
82 struct DividerTraits {
83  typedef typename UnsignedTraits<T>::type type;
84  static constexpr int N = sizeof(T) * 8;
85 };
86 
87 template <typename T>
89 #if defined(EIGEN_GPU_COMPILE_PHASE)
90  return __umulhi(a, b);
91 #elif defined(SYCL_DEVICE_ONLY)
92  return cl::sycl::mul_hi(a, static_cast<uint32_t>(b));
93 #else
94  return (static_cast<uint64_t>(a) * b) >> 32;
95 #endif
96 }
97 
98 template <typename T>
100 #if defined(EIGEN_GPU_COMPILE_PHASE)
101  return __umul64hi(a, b);
102 #elif defined(SYCL_DEVICE_ONLY)
103  return cl::sycl::mul_hi(a, static_cast<uint64_t>(b));
104 #elif EIGEN_COMP_MSVC && (EIGEN_ARCH_x86_64 || EIGEN_ARCH_ARM64)
105  return __umulh(a, static_cast<uint64_t>(b));
106 #elif EIGEN_HAS_BUILTIN_INT128
107  __uint128_t v = static_cast<__uint128_t>(a) * static_cast<__uint128_t>(b);
108  return static_cast<uint64_t>(v >> 64);
109 #else
111 #endif
112 }
113 
114 template <int N, typename T>
116  static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t computeMultiplier(const int log_div, const T divider) {
117  EIGEN_STATIC_ASSERT(N == 32, YOU_MADE_A_PROGRAMMING_MISTAKE);
118  return static_cast<uint32_t>((static_cast<uint64_t>(1) << (N + log_div)) / divider -
119  (static_cast<uint64_t>(1) << N) + 1);
120  }
121 };
122 
123 template <typename T>
124 struct DividerHelper<64, T> {
125  static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint64_t computeMultiplier(const int log_div, const T divider) {
126 #if EIGEN_HAS_BUILTIN_INT128 && !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
127  return static_cast<uint64_t>((static_cast<__uint128_t>(1) << (64 + log_div)) / static_cast<__uint128_t>(divider) -
128  (static_cast<__uint128_t>(1) << 64) + 1);
129 #else
130  const uint64_t shift = 1ULL << log_div;
134  return static_cast<uint64_t>(result);
135 #endif
136  }
137 };
138 
139 template <typename T, bool div_gt_one = false>
141  public:
143  multiplier = 0;
144  shift1 = 0;
145  shift2 = 0;
146  }
147 
148  // Must have 0 < divider < 2^31. This is relaxed to
149  // 0 < divider < 2^63 when using 64-bit indices on platforms that support
150  // the __uint128_t type.
152  const int N = DividerTraits<T>::N;
153  eigen_assert(static_cast<typename UnsignedTraits<T>::type>(divider) < NumTraits<UnsignedType>::highest() / 2);
154  eigen_assert(divider > 0);
155 
156  // fast ln2
157  const int leading_zeros = count_leading_zeros(static_cast<UnsignedType>(divider));
158  int log_div = N - leading_zeros;
159  // if divider is a power of two then log_div is 1 more than it should be.
160  if ((static_cast<typename UnsignedTraits<T>::type>(1) << (log_div - 1)) ==
161  static_cast<typename UnsignedTraits<T>::type>(divider))
162  log_div--;
163 
165  shift1 = log_div > 1 ? 1 : log_div;
166  shift2 = log_div > 1 ? log_div - 1 : 0;
167  }
168 
169  // Must have 0 <= numerator. On platforms that don't support the __uint128_t
170  // type numerator should also be less than 2^32-1.
171  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T divide(const T numerator) const {
172  eigen_assert(static_cast<typename UnsignedTraits<T>::type>(numerator) < NumTraits<UnsignedType>::highest() / 2);
173  // eigen_assert(numerator >= 0); // this is implicitly asserted by the line above
174 
175  UnsignedType t1 = muluh(multiplier, numerator);
176  UnsignedType t = (static_cast<UnsignedType>(numerator) - t1) >> shift1;
177  return (t1 + t) >> shift2;
178  }
179 
180  private:
185 };
186 
187 // Optimized version for signed 32 bit integers.
188 // Derived from Hacker's Delight.
189 // Only works for divisors strictly greater than one
190 template <>
191 class TensorIntDivisor<int32_t, true> {
192  public:
194  magic = 0;
195  shift = 0;
196  }
197  // Must have 2 <= divider
199  eigen_assert(divider >= 2);
200  calcMagic(divider);
201  }
202 
204 #ifdef EIGEN_GPU_COMPILE_PHASE
205  return (__umulhi(magic, n) >> shift);
206 #elif defined(SYCL_DEVICE_ONLY)
207  return (cl::sycl::mul_hi(magic, static_cast<uint32_t>(n)) >> shift);
208 #else
209  uint64_t v = static_cast<uint64_t>(magic) * static_cast<uint64_t>(n);
210  return (static_cast<uint32_t>(v >> 32) >> shift);
211 #endif
212  }
213 
214  private:
215  // Compute the magic numbers. See Hacker's Delight section 10 for an in
216  // depth explanation.
218  const unsigned two31 = 0x80000000; // 2**31.
219  unsigned ad = d;
220  unsigned t = two31 + (ad >> 31);
221  unsigned anc = t - 1 - t % ad; // Absolute value of nc.
222  int p = 31; // Init. p.
223  unsigned q1 = two31 / anc; // Init. q1 = 2**p/|nc|.
224  unsigned r1 = two31 - q1 * anc; // Init. r1 = rem(2**p, |nc|).
225  unsigned q2 = two31 / ad; // Init. q2 = 2**p/|d|.
226  unsigned r2 = two31 - q2 * ad; // Init. r2 = rem(2**p, |d|).
227  unsigned delta = 0;
228  do {
229  p = p + 1;
230  q1 = 2 * q1; // Update q1 = 2**p/|nc|.
231  r1 = 2 * r1; // Update r1 = rem(2**p, |nc|).
232  if (r1 >= anc) { // (Must be an unsigned
233  q1 = q1 + 1; // comparison here).
234  r1 = r1 - anc;
235  }
236  q2 = 2 * q2; // Update q2 = 2**p/|d|.
237  r2 = 2 * r2; // Update r2 = rem(2**p, |d|).
238  if (r2 >= ad) { // (Must be an unsigned
239  q2 = q2 + 1; // comparison here).
240  r2 = r2 - ad;
241  }
242  delta = ad - r2;
243  } while (q1 < delta || (q1 == delta && r1 == 0));
244 
245  magic = (unsigned)(q2 + 1);
246  shift = p - 32;
247  }
248 
251 };
252 
253 template <typename T, bool div_gt_one>
255  return divisor.divide(numerator);
256 }
257 
258 } // end namespace internal
259 } // end namespace Eigen
260 
261 #endif // EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define EIGEN_ALWAYS_INLINE
Definition: Macros.h:845
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
float * p
Definition: Tutorial_Map_using.cpp:9
Scalar * b
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC void calcMagic(int32_t d)
Definition: TensorIntDiv.h:217
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor()
Definition: TensorIntDiv.h:193
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE int divide(const int32_t n) const
Definition: TensorIntDiv.h:203
EIGEN_DEVICE_FUNC TensorIntDivisor(int32_t divider)
Definition: TensorIntDiv.h:198
int32_t shift
Definition: TensorIntDiv.h:250
uint32_t magic
Definition: TensorIntDiv.h:249
@ N
Definition: constructor.cpp:22
return int(ret)+1
const Scalar * a
Definition: level2_cplx_impl.h:32
EIGEN_DEVICE_FUNC int clz(BitsType bits)
Definition: MathFunctions.h:644
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T operator/(const T &numerator, const TensorIntDivisor< T, div_gt_one > &divisor)
Definition: TensorIntDiv.h:254
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t muluh(const uint32_t a, const T b)
Definition: TensorIntDiv.h:88
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t< sizeof(T)==4, int > count_leading_zeros(const T val)
Definition: TensorIntDiv.h:35
std::int32_t int32_t
Definition: Meta.h:41
std::uint32_t uint32_t
Definition: Meta.h:40
std::uint64_t uint64_t
Definition: Meta.h:42
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
int delta
Definition: MultiOpt.py:96
val
Definition: calibrate.py:119
Definition: Eigen_Colamd.h:49
t
Definition: plotPSD.py:36
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint64_t computeMultiplier(const int log_div, const T divider)
Definition: TensorIntDiv.h:125
Definition: TensorIntDiv.h:115
static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t computeMultiplier(const int log_div, const T divider)
Definition: TensorIntDiv.h:116
Definition: TensorIntDiv.h:82
UnsignedTraits< T >::type type
Definition: TensorIntDiv.h:83
static constexpr int N
Definition: TensorIntDiv.h:84
Definition: TensorIntDiv.h:140
int32_t shift2
Definition: TensorIntDiv.h:184
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor(const T divider)
Definition: TensorIntDiv.h:151
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor()
Definition: TensorIntDiv.h:142
int32_t shift1
Definition: TensorIntDiv.h:183
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T divide(const T numerator) const
Definition: TensorIntDiv.h:171
DividerTraits< T >::type UnsignedType
Definition: TensorIntDiv.h:181
UnsignedType multiplier
Definition: TensorIntDiv.h:182
Definition: TensorUInt128.h:34
Definition: TensorIntDiv.h:77
std::conditional_t< sizeof(T)==8, uint64_t, uint32_t > type
Definition: TensorIntDiv.h:78
Definition: TensorUInt128.h:20