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
21aeab70
Commit
21aeab70
authored
May 06, 2021
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimise float(int).
parent
c137916c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
5 deletions
+51
-5
Cython/Utility/TypeConversion.c
Cython/Utility/TypeConversion.c
+28
-5
tests/run/builtin_float.py
tests/run/builtin_float.py
+23
-0
No files found.
Cython/Utility/TypeConversion.c
View file @
21aeab70
...
...
@@ -474,20 +474,43 @@ static CYTHON_INLINE PyObject* __Pyx__PyNumber_Float(PyObject* obj); /* proto */
//@requires: Optimize.c::pyunicode_as_double
static
CYTHON_INLINE
PyObject
*
__Pyx__PyNumber_Float
(
PyObject
*
obj
)
{
//
obj is PyFloat
is handled in the calling macro
//
'obj is PyFloat'
is handled in the calling macro
double
val
;
if
(
PyUnicode_CheckExact
(
obj
))
{
if
(
PyLong_CheckExact
(
obj
))
{
#if CYTHON_USE_PYLONG_INTERNALS
const
digit
*
digits
=
((
PyLongObject
*
)
obj
)
->
ob_digit
;
switch
(
Py_SIZE
(
obj
))
{
case
0
:
val
=
0
.
0
;
goto
no_error
;
// single digit PyLong values always cast safely to double
case
1
:
val
=
(
double
)
digits
[
0
];
goto
no_error
;
case
-
1
:
val
=
(
double
)
-
(
sdigit
)
digits
[
0
];
goto
no_error
;
default:
val
=
PyLong_AsDouble
(
obj
);
}
#else
val
=
PyLong_AsDouble
(
obj
);
#endif
}
else
if
(
PyUnicode_CheckExact
(
obj
))
{
val
=
__Pyx_PyUnicode_AsDouble
(
obj
);
return
unlikely
(
val
==
-
1
&&
PyErr_Occurred
())
?
NULL
:
PyFloat_FromDouble
(
val
);
}
else
if
(
PyBytes_CheckExact
(
obj
))
{
val
=
__Pyx_PyBytes_AsDouble
(
obj
);
return
unlikely
(
val
==
-
1
&&
PyErr_Occurred
())
?
NULL
:
PyFloat_FromDouble
(
val
);
}
else
if
(
PyByteArray_CheckExact
(
obj
))
{
val
=
__Pyx_PyByteArray_AsDouble
(
obj
);
return
unlikely
(
val
==
-
1
&&
PyErr_Occurred
())
?
NULL
:
PyFloat_FromDouble
(
val
);
}
else
{
return
PyNumber_Float
(
obj
);
}
if
(
unlikely
(
val
==
-
1
&&
PyErr_Occurred
()))
{
return
NULL
;
}
no_error:
return
PyFloat_FromDouble
(
val
);
}
/////////////// GCCDiagnostics.proto ///////////////
...
...
tests/run/builtin_float.py
View file @
21aeab70
...
...
@@ -44,6 +44,29 @@ def float_call_conjugate():
return
x
def
from_int
(
i
):
"""
>>> from_int(0)
0.0
>>> from_int(1)
1.0
>>> from_int(-1)
-1.0
>>> from_int(99)
99.0
>>> from_int(-99)
-99.0
>>> for exp in (14, 15, 16, 30, 31, 32, 52, 53, 54, 60, 61, 62, 63, 64):
... for sign in (1, 0, -1):
... value = (sign or 1) * 2**exp + sign
... float_value = from_int(value)
... assert float_value == float(value), "expected %s2**%s+%s == %r, got %r, difference %r" % (
... '-' if sign < 0 else '', exp, sign, float(value), float_value, float_value - float(value))
"""
return
float
(
i
)
@
cython
.
test_assert_path_exists
(
"//CoerceToPyTypeNode"
,
"//CoerceToPyTypeNode//PythonCapiCallNode"
,
...
...
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