-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteger_division_by_constant.py
More file actions
39 lines (28 loc) · 1 KB
/
Copy pathinteger_division_by_constant.py
File metadata and controls
39 lines (28 loc) · 1 KB
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
import math
print "Magic Numbers For Integer Division By Constants\n"
print "Computes the magic number and the exponent for unsinged"
print "integer devision by multiplying with a constant (and right"
print "shifting afterwards)\n"
print " q = n / d"
print " q ~ (n * m) >> p\n"
print "maximal n (divident):",
n = int(input())
print "devisor d: ",
d = int(input())
def magicnum(nmax, d):
if (d == 0): # check if d is zero
return "division by zero is not allowed!"
if ((d & (d - 1)) == 0): # check if d is a power of 2
p = -1
while(d):
d >>= 1
p += 1
return (1, p) # return #shifts
nc = (nmax // d) * d - 1
nbits = int(math.log(nmax, 2)) + 1
for p in range(0, 2 * nbits + 1):
if 2**p > nc * (d - 1 - (2**p - 1)%d):
m = (2**p + d - 1 - (2**p - 1)%d)//d
return (m, p)
print "can't find p, something is wrong."
print "\nmagic numbers: (m, p) =", magicnum(n,d)