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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
2acc6660
Commit
2acc6660
authored
Mar 27, 2015
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extend PyLong optimisation to '>>' (rshift) operator
parent
259b3635
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
11 deletions
+70
-11
CHANGES.rst
CHANGES.rst
+2
-2
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+16
-8
Cython/Utility/Optimize.c
Cython/Utility/Optimize.c
+1
-1
tests/run/pyintop.pyx
tests/run/pyintop.pyx
+51
-0
No files found.
CHANGES.rst
View file @
2acc6660
...
...
@@ -19,8 +19,8 @@ Features added
* Adding/subtracting constant Python floats and small integers is faster.
* Binary and/or/xor
operations and modulus with small constant Python integers
are faster.
* Binary and/or/xor
/rshift operations and modulus with small constant Python
integers
are faster.
Bugs fixed
----------
...
...
Cython/Compiler/Optimize.py
View file @
2acc6660
...
...
@@ -2811,6 +2811,13 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
def
_handle_simple_method_object___xor__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
return
self
.
_optimise_num_binop
(
'Xor'
,
node
,
function
,
args
,
is_unbound_method
)
def
_handle_simple_method_object___rshift__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
if
len
(
args
)
!=
2
or
not
isinstance
(
args
[
1
],
ExprNodes
.
IntNode
):
return
node
if
not
args
[
1
].
has_constant_result
()
or
not
(
1
<=
args
[
1
].
constant_result
<=
63
):
return
node
return
self
.
_optimise_num_binop
(
'Rshift'
,
node
,
function
,
args
,
is_unbound_method
)
def
_handle_simple_method_object___mod__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
if
len
(
args
)
!=
2
or
not
isinstance
(
args
[
1
],
ExprNodes
.
IntNode
):
return
node
...
...
@@ -2826,25 +2833,26 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
def
_optimise_num_binop
(
self
,
operator
,
node
,
function
,
args
,
is_unbound_method
):
"""
Optimise
'+' / '-' operator
for (likely) float or small integer operations.
Optimise
math operators
for (likely) float or small integer operations.
"""
if
len
(
args
)
!=
2
:
return
node
if
not
node
.
type
.
is_pyobject
:
return
node
# when adding IntNode/FloatNode to something else, assume other operand is also numeric
# When adding IntNode/FloatNode to something else, assume other operand is also numeric.
# Prefer constants on RHS as they allows better size control for some operators.
num_nodes
=
(
ExprNodes
.
IntNode
,
ExprNodes
.
FloatNode
)
if
isinstance
(
args
[
0
],
num_nodes
):
if
args
[
1
].
type
is
not
PyrexTypes
.
py_object_type
:
return
node
numval
=
args
[
0
]
arg_order
=
'CObj'
elif
isinstance
(
args
[
1
],
num_nodes
):
if
isinstance
(
args
[
1
],
num_nodes
):
if
args
[
0
].
type
is
not
PyrexTypes
.
py_object_type
:
return
node
numval
=
args
[
1
]
arg_order
=
'ObjC'
elif
isinstance
(
args
[
0
],
num_nodes
):
if
args
[
1
].
type
is
not
PyrexTypes
.
py_object_type
:
return
node
numval
=
args
[
0
]
arg_order
=
'CObj'
else
:
return
node
...
...
Cython/Utility/Optimize.c
View file @
2acc6660
...
...
@@ -494,7 +494,7 @@ static PyObject* __Pyx_PyInt_{{op}}{{order}}(PyObject *op1, PyObject *op2, long
#if CYTHON_COMPILING_IN_CPYTHON
{{
py
:
pyval
,
ival
=
(
'
op2
'
,
'b'
)
if
order
==
'
CObj
'
else
(
'
op1
'
,
'a'
)
}}
{{
py
:
c_op
=
{
'
Add
'
:
'+'
,
'
Subtract
'
:
'-'
,
'
Remainder
'
:
'%'
,
'
Or
'
:
'|'
,
'
Xor
'
:
'^'
,
'
And
'
:
'&'
}[
op
]
}}
{{
py
:
c_op
=
{
'
Add
'
:
'+'
,
'
Subtract
'
:
'-'
,
'
Remainder
'
:
'%'
,
'
Or
'
:
'|'
,
'
Xor
'
:
'^'
,
'
And
'
:
'&'
,
'
Rshift
'
:
'
>>
'
}[
op
]
}}
static
PyObject
*
__Pyx_PyInt_
{{
op
}}{{
order
}}(
PyObject
*
op1
,
PyObject
*
op2
,
CYTHON_UNUSED
long
intval
,
int
inplace
)
{
#if PY_MAJOR_VERSION < 3
...
...
tests/run/pyintop.pyx
View file @
2acc6660
# mode: run
def
bigint
(
x
):
# avoid 'L' postfix in Py2.x
print
(
str
(
x
).
rstrip
(
'L'
))
def
or_obj
(
obj2
,
obj3
):
"""
>>> or_obj(2, 3)
...
...
@@ -79,6 +84,52 @@ def rshift_obj(obj2, obj3):
return
obj1
def
rshift_int
(
obj2
):
"""
>>> rshift_int(2)
0
>>> rshift_int(27)
3
>>> (-27) >> 3
-4
>>> rshift_int(-27)
-4
>>> rshift_int(32)
4
>>> (-32) >> 3
-4
>>> rshift_int(-32)
-4
>>> (2**28) >> 3
33554432
>>> rshift_int(2**28)
33554432
>>> (-2**28) >> 3
-33554432
>>> rshift_int(-2**28)
-33554432
>>> (2**30) >> 3
134217728
>>> rshift_int(2**30)
134217728
>>> rshift_int(-2**30)
-134217728
>>> bigint((2**60) >> 3)
144115188075855872
>>> bigint(rshift_int(2**60))
144115188075855872
>>> bigint(rshift_int(-2**60))
-144115188075855872
"""
obj1
=
obj2
>>
3
return
obj1
def
mixed_obj
(
obj2
,
obj3
):
"""
>>> mixed_obj(2, 3)
...
...
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