10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
34 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
int count_leading_zeros(
const T val)
42 DWORD leading_zeros = 0;
44 _BitScanReverse64(&leading_zero, val);
47 _BitScanReverse(&leading_zero, val);
51 return __builtin_clzl(static_cast<uint64_t>(val));
53 return __builtin_clz(static_cast<uint32_t>(val));
58 struct UnsignedTraits {
59 typedef typename conditional<sizeof(T) == 8, uint64_t, uint32_t>::type type;
63 struct DividerTraits {
64 #if defined(__SIZEOF_INT128__) && !defined(__CUDACC__)
65 typedef typename UnsignedTraits<T>::type type;
66 static const int N =
sizeof(T) * 8;
68 typedef uint32_t type;
69 static const int N = 32;
74 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t muluh(
const uint32_t a,
const T b) {
75 #if defined(__CUDA_ARCH__)
76 return __umulhi(a, b);
78 return (static_cast<uint64_t>(a) * b) >> 32;
82 #if defined(__CUDA_ARCH__)
84 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint64_t muluh(
const uint64_t a,
const T b) {
85 return __umul64hi(a, b);
89 EIGEN_ALWAYS_INLINE uint64_t muluh(
const uint64_t a,
const T b) {
90 #if defined(__SIZEOF_INT128__) && !defined(__CUDACC__)
91 __uint128_t v =
static_cast<__uint128_t
>(a) * static_cast<__uint128_t>(b);
92 return static_cast<uint64_t
>(v >> 64);
94 EIGEN_STATIC_ASSERT(
sizeof(T) == 4, YOU_MADE_A_PROGRAMMING_MISTAKE);
100 template <
int N,
typename T>
101 struct DividerHelper {
102 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE uint32_t computeMultiplier (
const int log_div,
const T divider) {
103 EIGEN_STATIC_ASSERT(N == 32, YOU_MADE_A_PROGRAMMING_MISTAKE);
104 return static_cast<uint32_t
>((
static_cast<uint64_t
>(1) << (N+log_div)) / divider - (static_cast<uint64_t>(1) << N) + 1);
108 #if defined(__SIZEOF_INT128__) && !defined(__CUDACC__)
109 template <
typename T>
110 struct DividerHelper<64, T> {
111 static EIGEN_ALWAYS_INLINE uint64_t computeMultiplier(
const int log_div,
const T divider) {
112 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);
119 template <
typename T>
120 struct TensorIntDivisor {
122 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() {
131 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor(
const T divider) {
132 const int N = DividerTraits<T>::N;
133 eigen_assert(
static_cast<typename UnsignedTraits<T>::type
>(divider) < NumTraits<UnsignedType>::highest()/2);
134 eigen_assert(divider > 0);
137 const int leading_zeros = count_leading_zeros(static_cast<UnsignedType>(divider));
138 int log_div = N - leading_zeros;
140 if ((
static_cast<typename UnsignedTraits<T>::type
>(1) << (log_div-1)) ==
static_cast<typename UnsignedTraits<T>::type
>(divider))
143 multiplier = DividerHelper<N, T>::computeMultiplier(log_div, divider);
144 shift1 = log_div > 1 ? 1 : log_div;
145 shift2 = log_div > 1 ? log_div-1 : 0;
150 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T divide(
const T numerator)
const {
151 eigen_assert(
static_cast<typename UnsignedTraits<T>::type
>(numerator) < NumTraits<UnsignedType>::highest()/2);
154 UnsignedType t1 = muluh(multiplier, numerator);
155 UnsignedType t = (
static_cast<UnsignedType
>(numerator) - t1) >> shift1;
156 return (t1 + t) >> shift2;
160 typedef typename DividerTraits<T>::type UnsignedType;
161 UnsignedType multiplier;
170 class TensorIntDivisor<int32_t> {
172 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() {
177 EIGEN_DEVICE_FUNC TensorIntDivisor(int32_t divider) {
178 eigen_assert(divider >= 2);
182 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
int divide(
const int32_t n)
const {
184 return (__umulhi(magic, n) >> shift);
186 uint64_t v =
static_cast<uint64_t
>(magic) * static_cast<uint64_t>(n);
187 return (static_cast<uint32_t>(v >> 32) >> shift);
194 EIGEN_DEVICE_FUNC
void calcMagic(int32_t d) {
195 const unsigned two31 = 0x80000000;
197 unsigned t = two31 + (ad >> 31);
198 unsigned anc = t - 1 - t%ad;
200 unsigned q1 = two31/anc;
201 unsigned r1 = two31 - q1*anc;
202 unsigned q2 = two31/ad;
203 unsigned r2 = two31 - q2*ad;
218 }
while (q1 < delta || (q1 == delta && r1 == 0));
220 magic = (unsigned)(q2 + 1);
229 template <
typename T>
230 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T operator / (
const T& numerator,
const TensorIntDivisor<T>& divisor) {
231 return divisor.divide(numerator);
238 #endif // EIGEN_CXX11_TENSOR_TENSOR_INTDIV_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13