Debian Codegraph @

rpython/jit/metainterp/optimizeopt/intdiv.py

raw symbols SCIP
1 from rpython.rlib.rarithmetic import LONG_BIT, intmask, r_uint, \
2 uint_mul_high
3
4
5 from rpython.jit.metainterp.history import ConstInt
6 from rpython.jit.metainterp.resoperation import ResOperation, rop
7
8
9 # Logic to replace the signed integer division by a constant
10 # by a few operations involving a UINT_MUL_HIGH.
11
12
13 def magic_numbers(m):
14 assert m == intmask(m) # as a signed int, we have m < 2**63
15 assert m & (m-1) != 0 # not a power of two
16 assert m >= 3
17 i = 1
18 while (r_uint(1) << (i+1)) < r_uint(m):
19 i += 1
20
21 # quotient = 2**(64+i) // m
22 high_word_dividend = r_uint(1) << i
23 quotient = r_uint(0)
24 for bit in range(LONG_BIT-1, -1, -1):
25 t = quotient + (r_uint(1) << bit)
26 # check: is 't * m' small enough to be < 2**(64+i), or not?
27 # note that we're really computing (2**(64+i)-1) // m, but the result
28 # is the same, because powers of two are not multiples of m.
29 if uint_mul_high(t, r_uint(m)) < high_word_dividend:
30 quotient = t # yes, small enough
31
32 # k = 2**(64+i) // m + 1
33 k = quotient + r_uint(1)
34
35 assert k != r_uint(0)
36 # Proof that k < 2**64 holds in all cases, even with the "+1":
37 #
38 # starting point: 2**i < m < 2**(i+1) with i < 63
39 # 2**i < m
40 # 2**i <= m - (2.0**(i-63)) as real number, because (2.0**(i-63))<=1.0
41 # 2**(64+i) <= 2**64 * m - 2**(i+1) as integers again
42 # 2**(64+i) < 2**64 * m - m
43 # 2**(64+i) / float(m) < 2**64-1 real numbers division
44 # 2**(64+i) // m < 2**64-1 with the integer division
45 # k < 2**64
46
47 assert k > (r_uint(1) << (LONG_BIT-1))
48 # This is because m < 2**(i+1), so 2**(64+i) // m >= 2**63
49
50 return (k, i)
51
52
53 def division_operations(n_box, m, known_nonneg=False):
54 kk, ii = magic_numbers(m)
55
56 # Turn the division into:
57 # t = n >> 63 # t == 0 or t == -1
58 # return (((n^t) * k) >> (64 + i)) ^ t
59
60 # Proof that this gives exactly a = n // m = floor(q), where q
61 # is the real number quotient:
62 #
63 # case t == 0, i.e. 0 <= n < 2**63
64 #
65 # a <= q <= a + (m-1)/m (we use '/' for the real quotient here)
66 #
67 # n * k == n * (2**(64+i) // m + 1)
68 # == n * ceil(2**(64+i) / m)
69 # == n * (2**(64+i) / m + ferr) for 0 < ferr < 1
70 # == q * 2**(64+i) + err for 0 < err < n
71 # < q * 2**(64+i) + n
72 # <= (a + (m-1)/m) * 2**(64+i) + n
73 # == 2**(64+i) * (a + extra) for 0 <= extra < ?
74 #
75 # extra == (m-1)/m + (n / 2**(64+i))
76 #
77 # but n < 2**63 < 2**(64+i)/m because m < 2**(i+1)
78 #
79 # extra < (m-1)/m + 1/m
80 # extra < 1.
81 #
82 # case t == -1, i.e. -2**63 <= n <= -1
83 #
84 # (note that n^(-1) == ~n)
85 # 0 <= ~n < 2**63
86 # by the previous case we get an answer a == (~n) // m
87 # ~a == n // m because it's a division truncating towards -inf.
88
89 if not known_nonneg:
90 t_box = ResOperation(rop.INT_RSHIFT, [n_box, ConstInt(LONG_BIT - 1)])
91 nt_box = ResOperation(rop.INT_XOR, [n_box, t_box])
92 else:
93 t_box = None
94 nt_box = n_box
95 mul_box = ResOperation(rop.UINT_MUL_HIGH, [nt_box, ConstInt(intmask(kk))])
96 sh_box = ResOperation(rop.UINT_RSHIFT, [mul_box, ConstInt(ii)])
97 if not known_nonneg:
98 final_box = ResOperation(rop.INT_XOR, [sh_box, t_box])
99 return [t_box, nt_box, mul_box, sh_box, final_box]
100 else:
101 return [mul_box, sh_box]
102
103
104 def modulo_operations(n_box, m, known_nonneg=False):
105 operations = division_operations(n_box, m, known_nonneg)
106
107 mul_box = ResOperation(rop.INT_MUL, [operations[-1], ConstInt(m)])
108 diff_box = ResOperation(rop.INT_SUB, [n_box, mul_box])
109 return operations + [mul_box, diff_box]
110
111