Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
8368e533
Commit
8368e533
authored
Feb 23, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix constant folding for repeated negation
parent
68421a13
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
9 deletions
+54
-9
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+16
-9
tests/run/constant_folding.py
tests/run/constant_folding.py
+38
-0
No files found.
Cython/Compiler/Optimize.py
View file @
8368e533
...
...
@@ -3025,20 +3025,27 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
return
node
def
_handle_UnaryMinusNode
(
self
,
node
):
def
_negate
(
value
):
if
value
.
startswith
(
'-'
):
value
=
value
[
1
:]
else
:
value
=
'-'
+
value
return
value
if
isinstance
(
node
.
operand
,
ExprNodes
.
LongNode
):
return
ExprNodes
.
LongNode
(
node
.
pos
,
value
=
'-'
+
node
.
operand
.
value
,
constant_result
=
node
.
constant_result
)
return
ExprNodes
.
LongNode
(
node
.
pos
,
value
=
_negate
(
node
.
operand
.
value
)
,
constant_result
=
node
.
constant_result
)
if
isinstance
(
node
.
operand
,
ExprNodes
.
FloatNode
):
# this is a safe operation
return
ExprNodes
.
FloatNode
(
node
.
pos
,
value
=
'-'
+
node
.
operand
.
value
,
constant_result
=
node
.
constant_result
)
return
ExprNodes
.
FloatNode
(
node
.
pos
,
value
=
_negate
(
node
.
operand
.
value
)
,
constant_result
=
node
.
constant_result
)
node_type
=
node
.
operand
.
type
if
node_type
.
is_int
and
node_type
.
signed
or
\
isinstance
(
node
.
operand
,
ExprNodes
.
IntNode
)
and
node_type
.
is_pyobject
:
return
ExprNodes
.
IntNode
(
node
.
pos
,
value
=
'-'
+
node
.
operand
.
value
,
type
=
node_type
,
longness
=
node
.
operand
.
longness
,
constant_result
=
node
.
constant_result
)
return
ExprNodes
.
IntNode
(
node
.
pos
,
value
=
_negate
(
node
.
operand
.
value
)
,
type
=
node_type
,
longness
=
node
.
operand
.
longness
,
constant_result
=
node
.
constant_result
)
return
node
def
_handle_UnaryPlusNode
(
self
,
node
):
...
...
tests/run/constant_folding.py
0 → 100644
View file @
8368e533
# mode: run
# tag: constant_folding
import
cython
@
cython
.
test_fail_if_path_exists
(
"//UnaryMinusNode"
,
"//UnaryPlusNode"
,
)
def
unop_floats
():
"""
>>> unop_floats()
(1.0, -1.0, 1.0, -1.0, -1.0)
"""
plus1
=
+
1.0
minus1
=
-
1.0
plus3
=
+++
1.0
minus3
=
---
1.0
mix
=
+-++--
1.0
return
plus1
,
minus1
,
plus3
,
minus3
,
mix
@
cython
.
test_fail_if_path_exists
(
"//UnaryMinusNode"
,
"//UnaryPlusNode"
,
)
def
unop_ints
():
"""
>>> unop_ints()
(1, -1, 1, -1, -1)
"""
plus1
=
+
1
minus1
=
-
1
plus3
=
+++
1
minus3
=
---
1
mix
=
+-++--
1
return
plus1
,
minus1
,
plus3
,
minus3
,
mix
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment