| 1 |
import py, sys |
| 2 |
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC |
| 3 |
|
| 4 |
class TestShift(BaseTestPyPyC): |
| 5 |
|
| 6 |
def test_shift_intbound(self): |
| 7 |
def main(b): |
| 8 |
res = 0 |
| 9 |
a = 0 |
| 10 |
while a < 300: |
| 11 |
assert a >= 0 |
| 12 |
assert 0 <= b <= 10 |
| 13 |
val = a >> b |
| 14 |
if val >= 0: |
| 15 |
res += 1 |
| 16 |
val = a << b |
| 17 |
if val >= 0: |
| 18 |
res += 2 |
| 19 |
a += 1 |
| 20 |
return res |
| 21 |
|
| 22 |
log = self.run(main, [2]) |
| 23 |
assert log.result == 300*3 |
| 24 |
loop, = log.loops_by_filename(self.filepath) |
| 25 |
assert loop.match_by_id('rshift', "") |
| 26 |
assert loop.match_by_id('lshift', "") |
| 27 |
|
| 28 |
def test_lshift_and_then_rshift(self): |
| 29 |
def main(b): |
| 30 |
res = 0 |
| 31 |
a = 0 |
| 32 |
while res < 300: |
| 33 |
assert 0 <= a <= 300 |
| 34 |
assert 0 <= b <= 10 |
| 35 |
res = (a << b) >> b |
| 36 |
a += 1 |
| 37 |
return res |
| 38 |
|
| 39 |
log = self.run(main, [2]) |
| 40 |
assert log.result == 300 |
| 41 |
loop, = log.loops_by_filename(self.filepath) |
| 42 |
assert loop.match_by_id('shift', """ |
| 43 |
i2 = int_lshift(i0, i1) |
| 44 |
""") |
| 45 |
|
| 46 |
def test_division_to_rshift(self): |
| 47 |
def main(b): |
| 48 |
res = 0 |
| 49 |
a = 0 |
| 50 |
while a < 300: |
| 51 |
res1 = a // b |
| 52 |
res2 = a // 2 |
| 53 |
res3 = a // 11 |
| 54 |
res += res1 + res2 + res3 |
| 55 |
a += 1 |
| 56 |
return res |
| 57 |
|
| 58 |
log = self.run(main, [3]) |
| 59 |
assert log.result == main(3) |
| 60 |
loop, = log.loops_by_filename(self.filepath) |
| 61 |
assert loop.match_by_id('div', """ |
| 62 |
i56 = int_eq(i48, %d) |
| 63 |
i57 = int_and(i56, i37) |
| 64 |
guard_false(i57, descr=...) |
| 65 |
i1 = call_i(_, i48, i3, descr=...) |
| 66 |
""" % (-sys.maxint-1,)) |
| 67 |
assert loop.match_by_id('shift', """ |
| 68 |
i1 = int_rshift(i2, 1) |
| 69 |
""") |
| 70 |
if sys.maxint > 2**32: |
| 71 |
args = (63, -5030930201920786804, 3) |
| 72 |
else: |
| 73 |
args = (31, -1171354717, 3) |
| 74 |
assert loop.match_by_id('mul', """ |
| 75 |
i2 = int_rshift(i1, %d) |
| 76 |
i3 = int_xor(i1, i2) |
| 77 |
i4 = uint_mul_high(i3, %d) |
| 78 |
i5 = uint_rshift(i4, %d) |
| 79 |
i6 = int_xor(i5, i2) |
| 80 |
""" % args) |
| 81 |
|
| 82 |
def test_modulo_optimization(self): |
| 83 |
def main(b): |
| 84 |
res = 0 |
| 85 |
a = 0 |
| 86 |
while a < 300: |
| 87 |
res1 = a%b |
| 88 |
res2 = a%2 |
| 89 |
res3 = a%11 |
| 90 |
res += res1 + res2 + res3 |
| 91 |
a += 1 |
| 92 |
return res |
| 93 |
|
| 94 |
log = self.run(main, [3]) |
| 95 |
assert log.result == main(3) |
| 96 |
loop, = log.loops_by_filename(self.filepath) |
| 97 |
assert loop.match_by_id('mod', """ |
| 98 |
i56 = int_eq(i48, %d) |
| 99 |
i57 = int_and(i56, i37) |
| 100 |
guard_false(i57, descr=...) |
| 101 |
i1 = call_i(_, i48, i3, descr=...) |
| 102 |
""" % (-sys.maxint-1,)) |
| 103 |
assert loop.match_by_id('and', """ |
| 104 |
i1 = int_and(i2, 1) |
| 105 |
""") |
| 106 |
if sys.maxint > 2**32: |
| 107 |
args = (63, -5030930201920786804, 3) |
| 108 |
else: |
| 109 |
args = (31, -1171354717, 3) |
| 110 |
assert loop.match_by_id('mul', """ |
| 111 |
i2 = int_rshift(i1, %d) |
| 112 |
i3 = int_xor(i1, i2) |
| 113 |
i4 = uint_mul_high(i3, %d) |
| 114 |
i5 = uint_rshift(i4, %d) |
| 115 |
i6 = int_xor(i5, i2) |
| 116 |
i7 = int_mul(i6, 11) |
| 117 |
i8 = int_sub(i1, i7) |
| 118 |
""" % args) |
| 119 |
|
| 120 |
def test_division_to_rshift_allcases(self): |
| 121 |
""" |
| 122 |
This test only checks that we get the expected result, not that any |
| 123 |
optimization has been applied. |
| 124 |
""" |
| 125 |
avalues = ('a', 'b', 7, -42, 8) |
| 126 |
bvalues = ['b'] + range(-10, 0) + range(1,10) |
| 127 |
code = '' |
| 128 |
for a in avalues: |
| 129 |
for b in bvalues: |
| 130 |
code += ' sa += %s / %s\n' % (a, b) |
| 131 |
src = """ |
| 132 |
def main(a, b): |
| 133 |
i = sa = 0 |
| 134 |
while i < 300: |
| 135 |
%s |
| 136 |
i += 1 |
| 137 |
return sa |
| 138 |
""" % code |
| 139 |
self.run_and_check(src, [ 10, 20]) |
| 140 |
self.run_and_check(src, [ 10, -20]) |
| 141 |
self.run_and_check(src, [-10, -20]) |
| 142 |
|
| 143 |
def test_mod(self): |
| 144 |
""" |
| 145 |
This test only checks that we get the expected result, not that any |
| 146 |
optimization has been applied. |
| 147 |
""" |
| 148 |
avalues = ('a', 'b', 7, -42, 8) |
| 149 |
bvalues = ['b'] + range(-10, 0) + range(1,10) |
| 150 |
code = '' |
| 151 |
for a in avalues: |
| 152 |
for b in bvalues: |
| 153 |
code += ' sa += %s %% %s\n' % (a, b) |
| 154 |
src = """ |
| 155 |
def main(a, b): |
| 156 |
i = sa = 0 |
| 157 |
while i < 2000: |
| 158 |
if a > 0: pass |
| 159 |
if 1 < b < 2: pass |
| 160 |
%s |
| 161 |
i += 1 |
| 162 |
return sa |
| 163 |
""" % code |
| 164 |
self.run_and_check(src, [ 10, 20]) |
| 165 |
self.run_and_check(src, [ 10, -20]) |
| 166 |
self.run_and_check(src, [-10, -20]) |
| 167 |
|
| 168 |
def test_shift_allcases(self): |
| 169 |
""" |
| 170 |
This test only checks that we get the expected result, not that any |
| 171 |
optimization has been applied. |
| 172 |
""" |
| 173 |
from sys import maxint |
| 174 |
def main(a, b): |
| 175 |
i = sa = 0 |
| 176 |
while i < 300: |
| 177 |
if a > 0: |
| 178 |
pass |
| 179 |
if b < 2 and b > 0: |
| 180 |
pass |
| 181 |
if (a >> b) >= 0: |
| 182 |
sa += 1 |
| 183 |
if (a << b) > 2: |
| 184 |
sa += 10000 |
| 185 |
i += 1 |
| 186 |
return sa |
| 187 |
|
| 188 |
maxvals = (-maxint-1, -maxint, maxint-1, maxint) |
| 189 |
for a in (-4, -3, -2, -1, 0, 1, 2, 3, 4) + maxvals: |
| 190 |
for b in (0, 1, 2, 31, 32, 33, 61, 62, 63): |
| 191 |
yield self.run_and_check, main, [a, b] |
| 192 |
|
| 193 |
def test_revert_shift_allcases(self): |
| 194 |
""" |
| 195 |
This test only checks that we get the expected result, not that any |
| 196 |
optimization has been applied. |
| 197 |
""" |
| 198 |
from sys import maxint |
| 199 |
|
| 200 |
def main(a, b, c, maxint): |
| 201 |
i = sa = 0 |
| 202 |
while i < 300: |
| 203 |
if 0 < a < 10: pass |
| 204 |
if -100 < b < 100: pass |
| 205 |
if -maxint/2 < c < maxint/2: pass |
| 206 |
sa += (a<<a)>>a |
| 207 |
sa += (b<<a)>>a |
| 208 |
sa += (c<<a)>>a |
| 209 |
sa += (a<<100)>>100 |
| 210 |
sa += (b<<100)>>100 |
| 211 |
sa += (c<<100)>>100 |
| 212 |
i += 1 |
| 213 |
return sa |
| 214 |
|
| 215 |
for a in (1, 4, 8, 100): |
| 216 |
for b in (-10, 10, -201, 201, -maxint/3, maxint/3): |
| 217 |
for c in (-10, 10, -maxint/3, maxint/3): |
| 218 |
yield self.run_and_check, main, [a, b, c, maxint] |