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 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
import py, sys
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC
class TestShift(BaseTestPyPyC):
def test_shift_intbound(self):
def main(b):
res = 0
a = 0
while a < 300:
assert a >= 0
assert 0 <= b <= 10
val = a >> b
if val >= 0: # ID: rshift
res += 1
val = a << b
if val >= 0: # ID: lshift
res += 2
a += 1
return res
#
log = self.run(main, [2])
assert log.result == 300*3
loop, = log.loops_by_filename(self.filepath)
assert loop.match_by_id('rshift', "") # guard optimized away
assert loop.match_by_id('lshift', "") # guard optimized away
def test_lshift_and_then_rshift(self):
py.test.skip('fixme, this optimization is disabled')
def main(b):
res = 0
a = 0
while res < 300:
assert a >= 0
assert 0 <= b <= 10
res = (a << b) >> b # ID: shift
a += 1
return res
#
log = self.run(main, [2])
assert log.result == 300
loop, = log.loops_by_filename(self.filepath)
assert loop.match_by_id('shift', "") # optimized away
def test_division_to_rshift(self):
def main(b):
res = 0
a = 0
while a < 300:
res1 = a // b # ID: div
res2 = a // 2 # ID: shift
res3 = a // 11 # ID: mul
res += res1 + res2 + res3
a += 1
return res
#
log = self.run(main, [3])
assert log.result == main(3)
loop, = log.loops_by_filename(self.filepath)
assert loop.match_by_id('div', """
i56 = int_eq(i48, %d)
i57 = int_and(i56, i37)
guard_false(i57, descr=...)
i1 = call_i(_, i48, i3, descr=...)
""" % (-sys.maxint-1,))
assert loop.match_by_id('shift', """
i1 = int_rshift(i2, 1)
""")
if sys.maxint > 2**32:
args = (63, -5030930201920786804, 3)
else:
args = (31, -1171354717, 3)
assert loop.match_by_id('mul', """
i2 = int_rshift(i1, %d)
i3 = int_xor(i1, i2)
i4 = uint_mul_high(i3, %d)
i5 = uint_rshift(i4, %d)
i6 = int_xor(i5, i2)
""" % args)
def test_modulo_optimization(self):
def main(b):
res = 0
a = 0
while a < 300:
res1 = a%b # ID: mod
res2 = a%2 # ID: and
res3 = a%11 # ID: mul
res += res1 + res2 + res3
a += 1
return res
#
log = self.run(main, [3])
assert log.result == main(3)
loop, = log.loops_by_filename(self.filepath)
assert loop.match_by_id('mod', """
i56 = int_eq(i48, %d)
i57 = int_and(i56, i37)
guard_false(i57, descr=...)
i1 = call_i(_, i48, i3, descr=...)
""" % (-sys.maxint-1,))
assert loop.match_by_id('and', """
i1 = int_and(i2, 1)
""")
if sys.maxint > 2**32:
args = (63, -5030930201920786804, 3)
else:
args = (31, -1171354717, 3)
assert loop.match_by_id('mul', """
i2 = int_rshift(i1, %d)
i3 = int_xor(i1, i2)
i4 = uint_mul_high(i3, %d)
i5 = uint_rshift(i4, %d)
i6 = int_xor(i5, i2)
i7 = int_mul(i6, 11)
i8 = int_sub(i1, i7)
""" % args)
def test_division_to_rshift_allcases(self):
"""
This test only checks that we get the expected result, not that any
optimization has been applied.
"""
avalues = ('a', 'b', 7, -42, 8)
bvalues = ['b'] + range(-10, 0) + range(1,10)
code = ''
for a in avalues:
for b in bvalues:
code += ' sa += %s / %s\n' % (a, b)
src = """
def main(a, b):
i = sa = 0
while i < 300:
%s
i += 1
return sa
""" % code
self.run_and_check(src, [ 10, 20])
self.run_and_check(src, [ 10, -20])
self.run_and_check(src, [-10, -20])
def test_mod(self):
"""
This test only checks that we get the expected result, not that any
optimization has been applied.
"""
avalues = ('a', 'b', 7, -42, 8)
bvalues = ['b'] + range(-10, 0) + range(1,10)
code = ''
for a in avalues:
for b in bvalues:
code += ' sa += %s %% %s\n' % (a, b)
src = """
def main(a, b):
i = sa = 0
while i < 2000:
if a > 0: pass
if 1 < b < 2: pass
%s
i += 1
return sa
""" % code
self.run_and_check(src, [ 10, 20])
self.run_and_check(src, [ 10, -20])
self.run_and_check(src, [-10, -20])
def test_shift_allcases(self):
"""
This test only checks that we get the expected result, not that any
optimization has been applied.
"""
from sys import maxint
def main(a, b):
i = sa = 0
while i < 300:
if a > 0: # Specialises the loop
pass
if b < 2 and b > 0:
pass
if (a >> b) >= 0:
sa += 1
if (a << b) > 2:
sa += 10000
i += 1
return sa
#
maxvals = (-maxint-1, -maxint, maxint-1, maxint)
for a in (-4, -3, -2, -1, 0, 1, 2, 3, 4) + maxvals:
for b in (0, 1, 2, 31, 32, 33, 61, 62, 63):
yield self.run_and_check, main, [a, b]
def test_revert_shift_allcases(self):
"""
This test only checks that we get the expected result, not that any
optimization has been applied.
"""
from sys import maxint
def main(a, b, c, maxint):
i = sa = 0
while i < 300:
if 0 < a < 10: pass
if -100 < b < 100: pass
if -maxint/2 < c < maxint/2: pass
sa += (a<<a)>>a
sa += (b<<a)>>a
sa += (c<<a)>>a
sa += (a<<100)>>100
sa += (b<<100)>>100
sa += (c<<100)>>100
i += 1
return sa
for a in (1, 4, 8, 100):
for b in (-10, 10, -201, 201, -maxint/3, maxint/3):
for c in (-10, 10, -maxint/3, maxint/3):
yield self.run_and_check, main, [a, b, c, maxint]
|