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):
sequence_node.mult_factor = factor
self.visitchildren(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)
def visit_PrimaryCmpNode(self, node):
......
......@@ -8,7 +8,7 @@ DEF INT_VAL = 1
def _func(a,b,c):
return a+b+c
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//AddNode")
def add():
"""
>>> add() == 1+2+3+4
......@@ -16,7 +16,7 @@ def add():
"""
return 1+2+3+4
@cython.test_fail_if_path_exists("//BinopNode")
#@cython.test_fail_if_path_exists("//AddNode")
def add_var(a):
"""
>>> add_var(10) == 1+2+10+3+4
......@@ -24,7 +24,7 @@ def add_var(a):
"""
return 1+2 +a+ 3+4
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//AddNode", "//SubNode")
def neg():
"""
>>> neg() == -1 -2 - (-3+4)
......@@ -32,7 +32,7 @@ def neg():
"""
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():
"""
>>> long_int_mix() == 1 + (2 * 3) // 2
......@@ -43,7 +43,7 @@ def long_int_mix():
"""
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():
"""
>>> char_int_mix() == 1 + (ord(' ') * 3) // 2 + ord('A')
......@@ -51,7 +51,7 @@ def char_int_mix():
"""
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():
"""
>>> int_cast() == 1 + 2 * 6000
......@@ -59,7 +59,7 @@ def int_cast():
"""
return <int>(1 + 2 * 6000)
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//MulNode")
def mul():
"""
>>> mul() == 1*60*1000
......@@ -67,7 +67,7 @@ def mul():
"""
return 1*60*1000
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def arithm():
"""
>>> arithm() == 9*2+3*8//6-10
......@@ -75,7 +75,7 @@ def arithm():
"""
return 9*2+3*8//6-10
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//AddNode", "//MulNode")
def parameters():
"""
>>> parameters() == _func(-1 -2, - (-3+4), 1*2*3)
......@@ -83,7 +83,7 @@ def parameters():
"""
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():
"""
>>> lists() == [1,2,3] + [4,5,6]
......@@ -91,15 +91,23 @@ def lists():
"""
return [1,2,3] + [4,5,6]
@cython.test_fail_if_path_exists("//BinopNode")
def multiplied_lists():
@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_right():
"""
>>> multiplied_lists() == [1,2,3] * 5
>>> multiplied_lists_right() == [1,2,3] * 5
True
"""
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():
"""
>>> multiplied_lists_neg() == [1,2,3] * -5
......@@ -107,7 +115,7 @@ def multiplied_lists_neg():
"""
return [1,2,3] * -5
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_nonconst(x):
"""
>>> multiplied_lists_nonconst(5) == [1,2,3] * 5
......@@ -119,7 +127,8 @@ def multiplied_lists_nonconst(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):
"""
>>> multiplied_lists_nonconst_expression(5) == [1,2,3] * (5 * 2)
......@@ -135,7 +144,7 @@ cdef side_effect(int x):
print x
return x
@cython.test_fail_if_path_exists("//BinopNode")
@cython.test_fail_if_path_exists("//MulNode")
def multiplied_lists_with_side_effects():
"""
>>> 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