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