Skip to content

Commit

Permalink
implemented abstract sub -- has bugs!
Browse files Browse the repository at this point in the history
  • Loading branch information
nirit100 committed Oct 30, 2024
1 parent 24e3307 commit efab918
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion rpython/jit/metainterp/optimizeopt/intrelational.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ def abstract_add(self, other):
res.make_lt(other)
return res

def abstract_sub(self, other):
# TODO: talk about
bounds = self.bounds.sub_bound(other.bounds)
res = IntOrderInfo(bounds)
if self.bounds.sub_bound_cannot_overflow(other.bounds):
if other.bounds.known_gt_const(0):
res._make_lt(self)
elif other.bounds.known_lt_const(0):
self._make_lt(res)
# refine resulting bounds by operand's relations
if self._known_lt(other):
res.bounds.make_gt_const(0)
elif other._known_lt(self):
res.bounds.make_lt_const(0)
return res


def _contains(self, concrete_values):
# concrete_values: dict[IntOrderInfo, int]
for order, value in concrete_values.iteritems():
Expand All @@ -67,7 +84,7 @@ def _contains(self, concrete_values):
return True

def _make_lt(self, other):
if other.known_lt(self):
if other.known_lt(self) or self is other:
raise InvalidLoop("Invalid relations: self < other < self")
if self.known_lt(other):
return
Expand Down
8 changes: 8 additions & 0 deletions rpython/jit/metainterp/optimizeopt/test/test_intrelational.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,11 @@ def test_abstract_sub():
b = IntOrderInfo(IntBound(-2, 1))
c = b.abstract_sub(a)
assert c.known_lt(IntOrderInfo(IntBound.from_constant(0)))

@given(order_info_and_contained_number2)
def test_abstract_add_random(args):
((b1, n1), (b2, n2)) = args
b3 = b1.abstract_sub(b2)
# the result bound works for unsigned addition, regardless of overflow
values = {b1: n1, b2: n2, b3: intmask(r_uint(n1) - r_uint(n2))}
assert b3.contains(values)

0 comments on commit efab918

Please sign in to comment.