10#ifndef EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
11#define EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
20 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
21 typename internal::enable_if<
sizeof(T)==4,
int>::type count_leading_zeros(
const T val)
23#ifdef EIGEN_GPU_COMPILE_PHASE
25#elif defined(SYCL_DEVICE_ONLY)
26 return cl::sycl::clz(val);
29 _BitScanReverse(&index, val);
32 EIGEN_STATIC_ASSERT(
sizeof(
unsigned long long) == 8, YOU_MADE_A_PROGRAMMING_MISTAKE);
33 return __builtin_clz(
static_cast<uint32_t
>(val));
38 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
39 typename internal::enable_if<
sizeof(T)==8,
int>::type count_leading_zeros(
const T val)
41#ifdef EIGEN_GPU_COMPILE_PHASE
43#elif defined(SYCL_DEVICE_ONLY)
44 return static_cast<int>(cl::sycl::clz(val));
45#elif EIGEN_COMP_MSVC && EIGEN_ARCH_x86_64
47 _BitScanReverse64(&index, val);
51 unsigned int lo = (
unsigned int)(val&0xffffffff);
52 unsigned int hi = (
unsigned int)((val>>32)&0xffffffff);
55 n = 32 + count_leading_zeros<unsigned int>(lo);
57 n = count_leading_zeros<unsigned int>(hi);
60 EIGEN_STATIC_ASSERT(
sizeof(
unsigned long long) == 8, YOU_MADE_A_PROGRAMMING_MISTAKE);
61 return __builtin_clzll(
static_cast<uint64_t
>(val));
66 struct UnsignedTraits {
67 typedef typename conditional<
sizeof(T) == 8, uint64_t, uint32_t>::type type;
71 struct DividerTraits {
72 typedef typename UnsignedTraits<T>::type type;
73 static const int N =
sizeof(T) * 8;
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));
83 return (
static_cast<uint64_t
>(a) * b) >> 32;
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);
97 return (TensorUInt128<static_val<0>, uint64_t>(a) * TensorUInt128<static_val<0>, uint64_t>(b)).upper();
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);
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);
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);
135template <
typename T,
bool div_gt_one = false>
136struct TensorIntDivisor {
138 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() {
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);
153 const int leading_zeros = count_leading_zeros(
static_cast<UnsignedType
>(divider));
154 int log_div = N - leading_zeros;
156 if ((
static_cast<typename UnsignedTraits<T>::type
>(1) << (log_div-1)) ==
static_cast<typename UnsignedTraits<T>::type
>(divider))
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;
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);
170 UnsignedType t1 = muluh(multiplier, numerator);
171 UnsignedType t = (
static_cast<UnsignedType
>(numerator) - t1) >> shift1;
172 return (t1 + t) >> shift2;
176 typedef typename DividerTraits<T>::type UnsignedType;
177 UnsignedType multiplier;
187class TensorIntDivisor<int32_t, true> {
189 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() {
194 EIGEN_DEVICE_FUNC TensorIntDivisor(int32_t divider) {
195 eigen_assert(divider >= 2);
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);
205 uint64_t v =
static_cast<uint64_t
>(magic) *
static_cast<uint64_t
>(n);
206 return (
static_cast<uint32_t
>(v >> 32) >> shift);
213 EIGEN_DEVICE_FUNC
void calcMagic(int32_t d) {
214 const unsigned two31 = 0x80000000;
216 unsigned t = two31 + (ad >> 31);
217 unsigned anc = t - 1 - t%ad;
219 unsigned q1 = two31/anc;
220 unsigned r1 = two31 - q1*anc;
221 unsigned q2 = two31/ad;
222 unsigned r2 = two31 - q2*ad;
237 }
while (q1 < delta || (q1 == delta && r1 == 0));
239 magic = (unsigned)(q2 + 1);
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);
Namespace containing all symbols from the Eigen library.