1 #ifndef CAFFE2_UTILS_FIXED_DIVISOR_H_ 2 #define CAFFE2_UTILS_FIXED_DIVISOR_H_ 25 uint64_t getMagic()
const {
29 int getShift()
const {
34 inline int32_t
div(int32_t n)
const {
37 uint64_t mul64 = magic_ * (uint64_t) n;
38 return (int32_t) (mul64 >> shift_);
42 inline int32_t
mod(int32_t n)
const {
43 return n - d_ * div(n);
47 inline void divMod(int32_t n, int32_t& q, int32_t& r)
const {
48 const int32_t quotient = div(n);
50 r = n - d_ * quotient;
59 void calcSignedMagic() {
61 magic_ = UINT64_C(0x1) << 32;
66 const uint32_t two31 = UINT32_C(0x80000000);
67 uint32_t ad = std::abs(d_);
68 uint32_t t = two31 + ((uint32_t) d_ >> 31);
69 uint32_t anc = t - 1 - t % ad;
71 uint32_t q1 = two31 / anc;
72 uint32_t r1 = two31 - q1 * anc;
73 uint32_t q2 = two31 / ad;
74 uint32_t r2 = two31 - q2 * ad;
96 }
while (q1 < delta || (q1 == delta && r1 == 0));
98 int32_t magic = q2 + 1;
103 magic_ = (uint64_t) (uint32_t) magic;
113 #endif // CAFFE2_UTILS_FIXED_DIVISOR_H_ int32_t div(int32_t n) const
Calculates q = n / d.
int32_t mod(int32_t n) const
Calculates r = n % d.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
void divMod(int32_t n, int32_t &q, int32_t &r) const
Calculates q = n / d and r = n % d together.