Commit 1d86687b authored by Stefan Behnel's avatar Stefan Behnel

also optimise left-side multiplication of lists/tuples for integer factors,...

also optimise left-side multiplication of lists/tuples for integer factors, generally fix constant folding test
parent 55750171
...@@ -3510,6 +3510,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -3510,6 +3510,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
sequence_node.mult_factor = factor sequence_node.mult_factor = factor
self.visitchildren(sequence_node) self.visitchildren(sequence_node)
return sequence_node return sequence_node
if isinstance(node.operand1, ExprNodes.IntNode) and \
isinstance(node.operand2, (ExprNodes.ListNode, ExprNodes.TupleNode)):
sequence_node = node.operand2
factor = node.operand1
self._calculate_const(factor)
if factor.constant_result != 1:
sequence_node.mult_factor = factor
self.visitchildren(sequence_node)
return sequence_node
return self.visit_BinopNode(node) return self.visit_BinopNode(node)
def visit_PrimaryCmpNode(self, node): def visit_PrimaryCmpNode(self, node):
......
...@@ -8,7 +8,7 @@ DEF INT_VAL = 1 ...@@ -8,7 +8,7 @@ DEF INT_VAL = 1
def _func(a,b,c): def _func(a,b,c):
return a+b+c return a+b+c
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode")
def add(): def add():
""" """
>>> add() == 1+2+3+4 >>> add() == 1+2+3+4
...@@ -16,7 +16,7 @@ def add(): ...@@ -16,7 +16,7 @@ def add():
""" """
return 1+2+3+4 return 1+2+3+4
@cython.test_fail_if_path_exists("//BinopNode") #@cython.test_fail_if_path_exists("//AddNode")
def add_var(a): def add_var(a):
""" """
>>> add_var(10) == 1+2+10+3+4 >>> add_var(10) == 1+2+10+3+4
...@@ -24,7 +24,7 @@ def add_var(a): ...@@ -24,7 +24,7 @@ def add_var(a):
""" """
return 1+2 +a+ 3+4 return 1+2 +a+ 3+4
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode", "//SubNode")
def neg(): def neg():
""" """
>>> neg() == -1 -2 - (-3+4) >>> neg() == -1 -2 - (-3+4)
...@@ -32,7 +32,7 @@ def neg(): ...@@ -32,7 +32,7 @@ def neg():
""" """
return -1 -2 - (-3+4) return -1 -2 - (-3+4)
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode", "//MulNode", "//DivNode")
def long_int_mix(): def long_int_mix():
""" """
>>> long_int_mix() == 1 + (2 * 3) // 2 >>> long_int_mix() == 1 + (2 * 3) // 2
...@@ -43,7 +43,7 @@ def long_int_mix(): ...@@ -43,7 +43,7 @@ def long_int_mix():
""" """
return 1L + (2 * 3L) // 2 return 1L + (2 * 3L) // 2
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode", "//MulNode", "//DivNode")
def char_int_mix(): def char_int_mix():
""" """
>>> char_int_mix() == 1 + (ord(' ') * 3) // 2 + ord('A') >>> char_int_mix() == 1 + (ord(' ') * 3) // 2 + ord('A')
...@@ -51,7 +51,7 @@ def char_int_mix(): ...@@ -51,7 +51,7 @@ def char_int_mix():
""" """
return 1L + (c' ' * 3L) // 2 + c'A' return 1L + (c' ' * 3L) // 2 + c'A'
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def int_cast(): def int_cast():
""" """
>>> int_cast() == 1 + 2 * 6000 >>> int_cast() == 1 + 2 * 6000
...@@ -59,7 +59,7 @@ def int_cast(): ...@@ -59,7 +59,7 @@ def int_cast():
""" """
return <int>(1 + 2 * 6000) return <int>(1 + 2 * 6000)
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//MulNode")
def mul(): def mul():
""" """
>>> mul() == 1*60*1000 >>> mul() == 1*60*1000
...@@ -67,7 +67,7 @@ def mul(): ...@@ -67,7 +67,7 @@ def mul():
""" """
return 1*60*1000 return 1*60*1000
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def arithm(): def arithm():
""" """
>>> arithm() == 9*2+3*8//6-10 >>> arithm() == 9*2+3*8//6-10
...@@ -75,7 +75,7 @@ def arithm(): ...@@ -75,7 +75,7 @@ def arithm():
""" """
return 9*2+3*8//6-10 return 9*2+3*8//6-10
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def parameters(): def parameters():
""" """
>>> parameters() == _func(-1 -2, - (-3+4), 1*2*3) >>> parameters() == _func(-1 -2, - (-3+4), 1*2*3)
...@@ -83,7 +83,7 @@ def parameters(): ...@@ -83,7 +83,7 @@ def parameters():
""" """
return _func(-1 -2, - (-3+4), 1*2*3) return _func(-1 -2, - (-3+4), 1*2*3)
@cython.test_fail_if_path_exists("//BinopNode") #@cython.test_fail_if_path_exists("//AddNode")
def lists(): def lists():
""" """
>>> lists() == [1,2,3] + [4,5,6] >>> lists() == [1,2,3] + [4,5,6]
...@@ -91,15 +91,23 @@ def lists(): ...@@ -91,15 +91,23 @@ def lists():
""" """
return [1,2,3] + [4,5,6] return [1,2,3] + [4,5,6]
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists(): def multiplied_lists_right():
""" """
>>> multiplied_lists() == [1,2,3] * 5 >>> multiplied_lists_right() == [1,2,3] * 5
True True
""" """
return [1,2,3] * 5 return [1,2,3] * 5
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_left():
"""
>>> multiplied_lists_left() == [1,2,3] * 5
True
"""
return 5 * [1,2,3]
@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_neg(): def multiplied_lists_neg():
""" """
>>> multiplied_lists_neg() == [1,2,3] * -5 >>> multiplied_lists_neg() == [1,2,3] * -5
...@@ -107,7 +115,7 @@ def multiplied_lists_neg(): ...@@ -107,7 +115,7 @@ def multiplied_lists_neg():
""" """
return [1,2,3] * -5 return [1,2,3] * -5
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_nonconst(x): def multiplied_lists_nonconst(x):
""" """
>>> multiplied_lists_nonconst(5) == [1,2,3] * 5 >>> multiplied_lists_nonconst(5) == [1,2,3] * 5
...@@ -119,7 +127,8 @@ def multiplied_lists_nonconst(x): ...@@ -119,7 +127,8 @@ def multiplied_lists_nonconst(x):
""" """
return [1,2,3] * x return [1,2,3] * x
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//MulNode//ListNode")
@cython.test_assert_path_exists("//MulNode")
def multiplied_lists_nonconst_expression(x): def multiplied_lists_nonconst_expression(x):
""" """
>>> multiplied_lists_nonconst_expression(5) == [1,2,3] * (5 * 2) >>> multiplied_lists_nonconst_expression(5) == [1,2,3] * (5 * 2)
...@@ -135,7 +144,7 @@ cdef side_effect(int x): ...@@ -135,7 +144,7 @@ cdef side_effect(int x):
print x print x
return x return x
@cython.test_fail_if_path_exists("//BinopNode") @cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_with_side_effects(): def multiplied_lists_with_side_effects():
""" """
>>> multiplied_lists_with_side_effects() == [1,2,3] * 5 >>> multiplied_lists_with_side_effects() == [1,2,3] * 5
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment