changeset 75:2cad6090a114

Fixed some corner cases concerning multiplication.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Wed, 27 May 2009 14:59:22 +0200
parents 25268f5d228e
children 48093be5ebb5
files pysmcl/range_analysis.py pysmcl/test/unit/test_rangeanalysis.py
diffstat 2 files changed, 46 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/range_analysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/range_analysis.py	Wed May 27 14:59:22 2009 +0200
@@ -169,12 +169,16 @@
             return (r0, r1)
 
         if isinstance(node.op, ast.Mult):
-            r0 = left[0] * right[0]
-            r1 = left[1] * right[1]
-            if r0 >= self.prime:
-                r0 = Bottom()
-            if r1 >= self.prime:
-                r1 = Bottom()
+            def liftMult(a, b):
+                if(isinstance(a, Bottom) or
+                   isinstance(b, Bottom)):
+                    return Bottom()
+                c = a * b
+                if(c > self.prime):
+                    return Bottom()
+                return c
+            r0 = liftMult(left[0], right[0])
+            r1 = liftMult(left[1], right[1])
             return (r0, r1)
         raise Exception("Operator not implemented: ", node.op)
 
--- a/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
@@ -101,6 +101,42 @@
         r = prog.body[0].body[2].out_values["range"]
         self.assertEquals(r, {'y': (3, 3), 'x': (3, 3), 'z': (Bottom(), Bottom())})
 
+    def test_range_mult_bottom(self):
+        p = 7
+        prog = parse(
+"""
+def f(x):
+    x=2
+    y=2
+    if x:
+         y=y+3
+    z=x*y
+""")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[-1].out_values["range"]
+        self.assertEquals(r, {'x': (2, 2), 'y': (2, 5), 'z': (4, Bottom())})
+
+    def test_range_mult_top_bottom(self):
+        p = 7
+        prog = parse(
+"""
+def f(x):
+    x=2
+    y=1
+    if x:
+         y=y+6
+    y=y+1
+    z=x*y
+""")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[-1].out_values["range"]
+        self.assertEquals(r, {'x': (2, 2), 'y': (2, Bottom()), 'z': (4, Bottom())})
+
+
     def test_range_minus(self):
         p = 7
         prog = parse(