-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathfast_intdiv.h
203 lines (181 loc) · 4.26 KB
/
fast_intdiv.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2014 Maxim Milakov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _INT_FASTDIV_KJGIUHFG
#define _INT_FASTDIV_KJGIUHFG
class int_fastdiv
{
public:
// divisor != 0
__host__ __device__ __forceinline__
int_fastdiv(int divisor = 0)
: d(divisor)
{
update_magic_numbers();
}
__host__ __device__ __forceinline__
int_fastdiv& operator =(int divisor)
{
this->d = divisor;
update_magic_numbers();
return *this;
}
__host__ __device__ __forceinline__
operator int() const
{
return d;
}
private:
int d;
int M;
int s;
int n_add_sign;
// Hacker's Delight, Second Edition, Chapter 10, Integer Division By Constants
__host__ __device__ __forceinline__
void update_magic_numbers()
{
if (d == 1)
{
M = 0;
s = -1;
n_add_sign = 1;
return;
}
else if (d == -1)
{
M = 0;
s = -1;
n_add_sign = -1;
return;
}
int p;
unsigned int ad, anc, delta, q1, r1, q2, r2, t;
const unsigned two31 = 0x80000000;
ad = (d == 0) ? 1 : abs(d);
t = two31 + ((unsigned int)d >> 31);
anc = t - 1 - t % ad;
p = 31;
q1 = two31 / anc;
r1 = two31 - q1 * anc;
q2 = two31 / ad;
r2 = two31 - q2 * ad;
do
{
++p;
q1 = 2 * q1;
r1 = 2 * r1;
if (r1 >= anc)
{
++q1;
r1 -= anc;
}
q2 = 2 * q2;
r2 = 2 * r2;
if (r2 >= ad)
{
++q2;
r2 -= ad;
}
delta = ad - r2;
} while (q1 < delta || (q1 == delta && r1 == 0));
this->M = q2 + 1;
if (d < 0)
this->M = -this->M;
this->s = p - 32;
if ((d > 0) && (M < 0))
n_add_sign = 1;
else if ((d < 0) && (M > 0))
n_add_sign = -1;
else
n_add_sign = 0;
}
__host__ __device__ __forceinline__
friend int operator/(const int divident, const int_fastdiv& divisor);
};
__host__ __device__ __forceinline__
int operator/(const int n, const int_fastdiv& divisor)
{
int q;
#ifdef __CUDA_ARCH__
asm("mul.hi.s32 %0, %1, %2;" : "=r"(q) : "r"(divisor.M), "r"(n));
#else
q = (((unsigned long long)((long long)divisor.M * (long long)n)) >> 32);
#endif
q += n * divisor.n_add_sign;
if (divisor.s >= 0)
{
q >>= divisor.s; // we rely on this to be implemented as arithmetic shift
q += (((unsigned int)q) >> 31);
}
return q;
}
__host__ __device__ __forceinline__
int operator%(const int n, const int_fastdiv& divisor)
{
int quotient = n / divisor;
int remainder = n - quotient * divisor;
return remainder;
}
__host__ __device__ __forceinline__
int operator/(const unsigned int n, const int_fastdiv& divisor)
{
return ((int)n) / divisor;
}
__host__ __device__ __forceinline__
int operator%(const unsigned int n, const int_fastdiv& divisor)
{
return ((int)n) % divisor;
}
__host__ __device__ __forceinline__
int operator/(const short n, const int_fastdiv& divisor)
{
return ((int)n) / divisor;
}
__host__ __device__ __forceinline__
int operator%(const short n, const int_fastdiv& divisor)
{
return ((int)n) % divisor;
}
__host__ __device__ __forceinline__
int operator/(const unsigned short n, const int_fastdiv& divisor)
{
return ((int)n) / divisor;
}
__host__ __device__ __forceinline__
int operator%(const unsigned short n, const int_fastdiv& divisor)
{
return ((int)n) % divisor;
}
__host__ __device__ __forceinline__
int operator/(const char n, const int_fastdiv& divisor)
{
return ((int)n) / divisor;
}
__host__ __device__ __forceinline__
int operator%(const char n, const int_fastdiv& divisor)
{
return ((int)n) % divisor;
}
__host__ __device__ __forceinline__
int operator/(const unsigned char n, const int_fastdiv& divisor)
{
return ((int)n) / divisor;
}
__host__ __device__ __forceinline__
int operator%(const unsigned char n, const int_fastdiv& divisor)
{
return ((int)n) % divisor;
}
#endif