Loading...
Searching...
No Matches
TensorIntDiv.h
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
14namespace Eigen {
15
16namespace internal {
17
18namespace {
19
20 // Note: result is undefined if val == 0
21 template <typename T>
22 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
23 typename internal::enable_if<sizeof(T)==4,int>::type count_leading_zeros(const T val)
24 {
25#ifdef __CUDA_ARCH__
26 return __clz(val);
27#elif EIGEN_COMP_MSVC
28 unsigned long index;
29 _BitScanReverse(&index, val);
30 return 31 - index;
31#else
32 EIGEN_STATIC_ASSERT(sizeof(unsigned long long) == 8, YOU_MADE_A_PROGRAMMING_MISTAKE);
33 return __builtin_clz(static_cast<uint32_t>(val));
34#endif
35 }
36
37 template <typename T>
38 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
39 typename internal::enable_if<sizeof(T)==8,int>::type count_leading_zeros(const T val)
40 {
41#ifdef __CUDA_ARCH__
42 return __clzll(val);
43#elif EIGEN_COMP_MSVC && EIGEN_ARCH_x86_64
44 unsigned long index;
45 _BitScanReverse64(&index, val);
46 return 63 - index;
47#elif EIGEN_COMP_MSVC
48 // MSVC's _BitScanReverse64 is not available for 32bits builds.
49 unsigned int lo = (unsigned int)(val&0xffffffff);
50 unsigned int hi = (unsigned int)((val>>32)&0xffffffff);
51 int n;
52 if(hi==0)
53 n = 32 + count_leading_zeros<unsigned int>(lo);
54 else
55 n = count_leading_zeros<unsigned int>(hi);
56 return n;
57#else
58 EIGEN_STATIC_ASSERT(sizeof(unsigned long long) == 8, YOU_MADE_A_PROGRAMMING_MISTAKE);
59 return __builtin_clzll(static_cast<uint64_t>(val));
60#endif
61 }
62
63 template <typename T>
64 struct UnsignedTraits {
65 typedef typename conditional<sizeof(T) == 8, uint64_t, uint32_t>::type type;
66 };
67
68 template <typename T>
69 struct DividerTraits {
70 typedef typename UnsignedTraits<T>::type type;
71 static const int N = sizeof(T) * 8;
72 };
73
74 template <typename T>
75 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t muluh(const uint32_t a, const T b) {
76#if defined(__CUDA_ARCH__)
77 return __umulhi(a, b);
78#else
79 return (static_cast<uint64_t>(a) * b) >> 32;
80#endif
81 }
82
83 template <typename T>
84 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint64_t muluh(const uint64_t a, const T b) {
85#if defined(__CUDA_ARCH__)
86 return __umul64hi(a, b);
87#elif defined(__SIZEOF_INT128__)
88 __uint128_t v = static_cast<__uint128_t>(a) * static_cast<__uint128_t>(b);
89 return static_cast<uint64_t>(v >> 64);
90#else
91 return (TensorUInt128<static_val<0>, uint64_t>(a) * TensorUInt128<static_val<0>, uint64_t>(b)).upper();
92#endif
93 }
94
95 template <int N, typename T>
96 struct DividerHelper {
97 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t computeMultiplier(const int log_div, const T divider) {
98 EIGEN_STATIC_ASSERT(N == 32, YOU_MADE_A_PROGRAMMING_MISTAKE);
99 return static_cast<uint32_t>((static_cast<uint64_t>(1) << (N+log_div)) / divider - (static_cast<uint64_t>(1) << N) + 1);
100 }
101 };
102
103 template <typename T>
104 struct DividerHelper<64, T> {
105 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint64_t computeMultiplier(const int log_div, const T divider) {
106#if defined(__SIZEOF_INT128__) && !defined(__CUDA_ARCH__)
107 return static_cast<uint64_t>((static_cast<__uint128_t>(1) << (64+log_div)) / static_cast<__uint128_t>(divider) - (static_cast<__uint128_t>(1) << 64) + 1);
108#else
109 const uint64_t shift = 1ULL << log_div;
110 TensorUInt128<uint64_t, uint64_t> result = TensorUInt128<uint64_t, static_val<0> >(shift, 0) / TensorUInt128<static_val<0>, uint64_t>(divider)
111 - TensorUInt128<static_val<1>, static_val<0> >(1, 0)
112 + TensorUInt128<static_val<0>, static_val<1> >(1);
113 return static_cast<uint64_t>(result);
114#endif
115 }
116 };
117}
118
119
131template <typename T, bool div_gt_one = false>
132struct TensorIntDivisor {
133 public:
134 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() {
135 multiplier = 0;
136 shift1 = 0;
137 shift2 = 0;
138 }
139
140 // Must have 0 < divider < 2^31. This is relaxed to
141 // 0 < divider < 2^63 when using 64-bit indices on platforms that support
142 // the __uint128_t type.
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor(const T divider) {
144 const int N = DividerTraits<T>::N;
145 eigen_assert(static_cast<typename UnsignedTraits<T>::type>(divider) < NumTraits<UnsignedType>::highest()/2);
146 eigen_assert(divider > 0);
147
148 // fast ln2
149 const int leading_zeros = count_leading_zeros(static_cast<UnsignedType>(divider));
150 int log_div = N - leading_zeros;
151 // if divider is a power of two then log_div is 1 more than it should be.
152 if ((static_cast<typename UnsignedTraits<T>::type>(1) << (log_div-1)) == static_cast<typename UnsignedTraits<T>::type>(divider))
153 log_div--;
154
155 multiplier = DividerHelper<N, T>::computeMultiplier(log_div, divider);
156 shift1 = log_div > 1 ? 1 : log_div;
157 shift2 = log_div > 1 ? log_div-1 : 0;
158 }
159
160 // Must have 0 <= numerator. On platforms that dont support the __uint128_t
161 // type numerator should also be less than 2^32-1.
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T divide(const T numerator) const {
163 eigen_assert(static_cast<typename UnsignedTraits<T>::type>(numerator) < NumTraits<UnsignedType>::highest()/2);
164 //eigen_assert(numerator >= 0); // this is implicitly asserted by the line above
165
166 UnsignedType t1 = muluh(multiplier, numerator);
167 UnsignedType t = (static_cast<UnsignedType>(numerator) - t1) >> shift1;
168 return (t1 + t) >> shift2;
169 }
170
171 private:
172 typedef typename DividerTraits<T>::type UnsignedType;
173 UnsignedType multiplier;
174 int32_t shift1;
175 int32_t shift2;
176};
177
178
179// Optimized version for signed 32 bit integers.
180// Derived from Hacker's Delight.
181// Only works for divisors strictly greater than one
182template <>
183class TensorIntDivisor<int32_t, true> {
184 public:
185 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() {
186 magic = 0;
187 shift = 0;
188 }
189 // Must have 2 <= divider
190 EIGEN_DEVICE_FUNC TensorIntDivisor(int32_t divider) {
191 eigen_assert(divider >= 2);
192 calcMagic(divider);
193 }
194
195 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE int divide(const int32_t n) const {
196#ifdef __CUDA_ARCH__
197 return (__umulhi(magic, n) >> shift);
198#else
199 uint64_t v = static_cast<uint64_t>(magic) * static_cast<uint64_t>(n);
200 return (static_cast<uint32_t>(v >> 32) >> shift);
201#endif
202 }
203
204private:
205 // Compute the magic numbers. See Hacker's Delight section 10 for an in
206 // depth explanation.
207 EIGEN_DEVICE_FUNC void calcMagic(int32_t d) {
208 const unsigned two31 = 0x80000000; // 2**31.
209 unsigned ad = d;
210 unsigned t = two31 + (ad >> 31);
211 unsigned anc = t - 1 - t%ad; // Absolute value of nc.
212 int p = 31; // Init. p.
213 unsigned q1 = two31/anc; // Init. q1 = 2**p/|nc|.
214 unsigned r1 = two31 - q1*anc; // Init. r1 = rem(2**p, |nc|).
215 unsigned q2 = two31/ad; // Init. q2 = 2**p/|d|.
216 unsigned r2 = two31 - q2*ad; // Init. r2 = rem(2**p, |d|).
217 unsigned delta = 0;
218 do {
219 p = p + 1;
220 q1 = 2*q1; // Update q1 = 2**p/|nc|.
221 r1 = 2*r1; // Update r1 = rem(2**p, |nc|).
222 if (r1 >= anc) { // (Must be an unsigned
223 q1 = q1 + 1; // comparison here).
224 r1 = r1 - anc;}
225 q2 = 2*q2; // Update q2 = 2**p/|d|.
226 r2 = 2*r2; // Update r2 = rem(2**p, |d|).
227 if (r2 >= ad) { // (Must be an unsigned
228 q2 = q2 + 1; // comparison here).
229 r2 = r2 - ad;}
230 delta = ad - r2;
231 } while (q1 < delta || (q1 == delta && r1 == 0));
232
233 magic = (unsigned)(q2 + 1);
234 shift = p - 32;
235 }
236
237 uint32_t magic;
238 int32_t shift;
239};
240
241
242template <typename T, bool div_gt_one>
243static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T operator / (const T& numerator, const TensorIntDivisor<T, div_gt_one>& divisor) {
244 return divisor.divide(numerator);
245}
246
247
248} // end namespace internal
249} // end namespace Eigen
250
251#endif // EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
Namespace containing all symbols from the Eigen library.