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
Kirill Smelkov
cython
Commits
03af367c
Commit
03af367c
authored
Mar 30, 2015
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise Python float modulus
parent
360ebb7e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
7 deletions
+33
-7
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+5
-6
Cython/Utility/Optimize.c
Cython/Utility/Optimize.c
+9
-1
tests/run/modop.pyx
tests/run/modop.pyx
+19
-0
No files found.
Cython/Compiler/Optimize.py
View file @
03af367c
...
...
@@ -2852,11 +2852,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
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
if
not
args
[
1
].
has_constant_result
()
or
not
(
2
<=
args
[
1
].
constant_result
<=
2
**
30
):
return
node
return
self
.
_optimise_num_binop
(
'Remainder'
,
node
,
function
,
args
,
is_unbound_method
)
return
self
.
_optimise_num_div
(
'Remainder'
,
node
,
function
,
args
,
is_unbound_method
)
def
_handle_simple_method_object___floordiv__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
return
self
.
_optimise_num_div
(
'FloorDivide'
,
node
,
function
,
args
,
is_unbound_method
)
...
...
@@ -2892,6 +2888,9 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
def
_handle_simple_method_float___div__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
return
self
.
_optimise_num_binop
(
'Divide'
,
node
,
function
,
args
,
is_unbound_method
)
def
_handle_simple_method_float___mod__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
return
self
.
_optimise_num_binop
(
'Remainder'
,
node
,
function
,
args
,
is_unbound_method
)
def
_handle_simple_method_float___eq__
(
self
,
node
,
function
,
args
,
is_unbound_method
):
return
self
.
_optimise_num_binop
(
'Eq'
,
node
,
function
,
args
,
is_unbound_method
)
...
...
@@ -2928,7 +2927,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
is_float
=
isinstance
(
numval
,
ExprNodes
.
FloatNode
)
if
is_float
:
if
operator
not
in
(
'Add'
,
'Subtract'
,
'TrueDivide'
,
'Divide'
,
'Eq'
,
'Ne'
):
if
operator
not
in
(
'Add'
,
'Subtract'
,
'
Remainder'
,
'
TrueDivide'
,
'Divide'
,
'Eq'
,
'Ne'
):
return
node
elif
operator
==
'Divide'
:
# mixed old-/new-style division is not currently optimised for integers
...
...
Cython/Utility/Optimize.c
View file @
03af367c
...
...
@@ -687,7 +687,7 @@ static PyObject* __Pyx_PyFloat_{{op}}{{order}}(PyObject *op1, PyObject *op2, dou
{{
py
:
pyval
,
fval
=
(
'
op2
'
,
'b'
)
if
order
==
'
CObj
'
else
(
'
op1
'
,
'a'
)
}}
{{
py
:
c_op
=
{
'
Add
'
:
'+'
,
'
Subtract
'
:
'-'
,
'
TrueDivide
'
:
'/'
,
'
Divide
'
:
'/'
,
'
Add
'
:
'+'
,
'
Subtract
'
:
'-'
,
'
TrueDivide
'
:
'/'
,
'
Divide
'
:
'/'
,
'
Remainder
'
:
'%'
,
'
Eq
'
:
'
==
'
,
'
Ne
'
:
'
!=
'
,
}[
op
]
}}
...
...
@@ -768,7 +768,15 @@ static PyObject* __Pyx_PyFloat_{{op}}{{order}}(PyObject *op1, PyObject *op2, dou
{{
else
}}
// copied from floatobject.c in Py3.5:
PyFPE_START_PROTECT
(
"{{op.lower() if not op.endswith('Divide') else 'divide'}}"
,
return
NULL
)
{{
if
c_op
==
'%'
}}
result
=
fmod
(
a
,
b
);
if
(
result
)
result
+=
((
result
<
0
)
^
(
b
<
0
))
*
b
;
else
result
=
copysign
(
0
.
0
,
b
);
{{
else
}}
result
=
a
{{
c_op
}}
b
;
{{
endif
}}
PyFPE_END_PROTECT
(
result
)
return
PyFloat_FromDouble
(
result
);
{{
endif
}}
...
...
tests/run/modop.pyx
View file @
03af367c
...
...
@@ -173,6 +173,25 @@ def mod_obj_m2(int2):
return
int1
def
mod_obj_m2f
(
obj2
):
"""
>>> 0 % -2.0
-0.0
>>> mod_obj_m2f(0)
-0.0
>>> 1 % -2.0
-1.0
>>> mod_obj_m2f(1)
-1.0
>>> 9 % -2.0
-1.0
>>> mod_obj_m2f(9)
-1.0
"""
result
=
obj2
%
-
2.0
return
result
def
modint
(
int
int2
,
int
int3
):
"""
>>> modint(9,2)
...
...
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