1 #ifndef CAFFE2_UTILS_FIXED_DIVISOR_H_ 2 #define CAFFE2_UTILS_FIXED_DIVISOR_H_ 10 #if defined(__CUDA_ARCH__) || defined(__HIP_ARCH__) || defined(__HIP__) 11 #define FIXED_DIVISOR_DECL inline __host__ __device__ 13 #define FIXED_DIVISOR_DECL inline 32 #ifndef __HIP_PLATFORM_HCC__ 34 #endif // __HIP_PLATFORM_HCC__ 37 FIXED_DIVISOR_DECL std::int32_t d()
const {
41 #ifndef __HIP_PLATFORM_HCC__ 42 FIXED_DIVISOR_DECL std::uint64_t magic()
const {
46 FIXED_DIVISOR_DECL
int shift()
const {
49 #endif // __HIP_PLATFORM_HCC__ 52 FIXED_DIVISOR_DECL std::int32_t
Div(
const std::int32_t n)
const {
53 #ifdef __HIP_PLATFORM_HCC__ 55 #else // __HIP_PLATFORM_HCC__ 58 return (int32_t)((magic_ * (uint64_t)n) >> shift_);
59 #endif // __HIP_PLATFORM_HCC__ 63 FIXED_DIVISOR_DECL std::int32_t
Mod(
const std::int32_t n)
const {
64 return n - d_ * Div(n);
68 FIXED_DIVISOR_DECL
void 69 DivMod(
const std::int32_t n, std::int32_t* q, int32_t* r)
const {
75 #ifndef __HIP_PLATFORM_HCC__ 79 void CalcSignedMagic() {
81 magic_ = UINT64_C(0x1) << 32;
86 const std::uint32_t two31 = UINT32_C(0x80000000);
87 const std::uint32_t ad = std::abs(d_);
88 const std::uint32_t t = two31 + ((uint32_t)d_ >> 31);
89 const std::uint32_t anc = t - 1 - t % ad;
91 std::uint32_t q1 = two31 / anc;
92 std::uint32_t r1 = two31 - q1 * anc;
93 std::uint32_t q2 = two31 / ad;
94 std::uint32_t r2 = two31 - q2 * ad;
95 std::uint32_t delta = 0;
111 }
while (q1 < delta || (q1 == delta && r1 == 0));
112 std::int32_t magic = q2 + 1;
117 magic_ = (std::uint64_t)(std::uint32_t)magic;
119 #endif // __HIP_PLATFORM_HCC__ 123 #ifndef __HIP_PLATFORM_HCC__ 124 std::uint64_t magic_;
126 #endif // __HIP_PLATFORM_HCC__ 131 #endif // CAFFE2_UTILS_FIXED_DIVISOR_H_
FIXED_DIVISOR_DECL std::int32_t Mod(const std::int32_t n) const
Calculates r = n % d.
FIXED_DIVISOR_DECL std::int32_t Div(const std::int32_t n) const
Calculates q = n / d.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
FIXED_DIVISOR_DECL void DivMod(const std::int32_t n, std::int32_t *q, int32_t *r) const
Calculates q = n / d and r = n % d together.