-
Notifications
You must be signed in to change notification settings - Fork 1
/
IntegerDivider.hpp
72 lines (56 loc) · 1.58 KB
/
IntegerDivider.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#if defined(__SYCL_DEVICE_ONLY__)
#include <CL/sycl.hpp>
#endif
#include <assert.h>
#include <cstdint>
// Porting same functionality from PyTorch/Eigen GPU code.
namespace porting {
template <typename T>
struct DivMod {
T div, mod;
DivMod(T div, T mod) : div(div), mod(mod) { }
};
template <typename T>
struct IntDivider {
IntDivider() = default;
IntDivider(T d) : divisor(d) { }
inline T div(T n) const { return n / divisor; }
inline T mod(T n) const { return n % divisor; }
inline DivMod<T> divmod(T n) const {
return DivMod<T>(n / divisor, n % divisor);
}
T divisor;
};
template <>
struct IntDivider<uint32_t> {
static_assert(sizeof(unsigned int) == 4, "Assumes 32-bit unsigned int.");
IntDivider() = default;
IntDivider(uint32_t d) : divisor(d) {
assert(divisor >= 1 && divisor <= INT32_MAX);
for (shift = 0; shift < 32; ++ shift) if ((1U << shift) >= divisor) break;
uint64_t one = 1;
uint64_t magic = ((one <<32) * ((one << shift) - divisor)) / divisor + 1;
m1 = magic;
assert(m1 > 0 && m1 == magic); // m1 must fit in 32 bits.
}
inline uint32_t div(uint32_t n) const {
#if defined(__SYCL_DEVICE_ONLY__)
uint32_t t = cl::sycl::mul_hi(n, static_cast<uint32_t>(m1));
#else
uint64_t t = ((uint64_t) n * m1) >> 32;
#endif
return (t + n) >> shift;
}
inline uint32_t mod(uint32_t n) const {
return n - div(n) * divisor;
}
inline DivMod<uint32_t> divmod(uint32_t n) const {
uint32_t q = div(n);
return DivMod<uint32_t>(q, n - q * divisor);
}
uint32_t divisor;
uint32_t m1;
uint32_t shift;
};
}