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
Gwenaël Samain
cython
Commits
4f2aa9c1
Commit
4f2aa9c1
authored
Oct 14, 2016
by
Jeroen Demeyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add declarations for longintrepr.h
parent
15a40e21
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
Cython/Includes/cpython/longintrepr.pxd
Cython/Includes/cpython/longintrepr.pxd
+16
-0
tests/run/longintrepr.pyx
tests/run/longintrepr.pyx
+64
-0
No files found.
Cython/Includes/cpython/longintrepr.pxd
0 → 100644
View file @
4f2aa9c1
# Internals of the "long" type (Python 2) or "int" type (Python 3).
# This is not part of Python's published API.
cdef
extern
from
"longintrepr.h"
:
# Add explicit cast to avoid compiler warnings
cdef
_PyLong_New
"(PyObject*)_PyLong_New"
(
Py_ssize_t
s
)
ctypedef
unsigned
int
digit
ctypedef
int
sdigit
# Python >= 2.7 only
ctypedef
struct
PyLongObject
:
digit
*
ob_digit
cdef
long
PyLong_SHIFT
cdef
digit
PyLong_BASE
cdef
digit
PyLong_MASK
tests/run/longintrepr.pyx
0 → 100644
View file @
4f2aa9c1
# mode: run
# Test longintrepr declarations by implementing a simple function
from
cpython.longintrepr
cimport
*
cimport
cython
cdef
extern
from
*
:
Py_ssize_t
*
Py_SIZE_PTR
"&Py_SIZE"
(
object
)
@
cython
.
cdivision
(
True
)
def
lshift
(
long
a
,
unsigned
long
n
):
"""
Return a * 2^n as Python long.
>>> print(lshift(3, 1))
6
>>> print(lshift(-3, 1))
-6
>>> print(lshift(1, 30))
1073741824
>>> print(lshift(-12345, 115))
-512791237748899576593671817473776680960
>>> print(-12345 << 115)
-512791237748899576593671817473776680960
>>> [i for i in range(100) if (65535 << i) != lshift(65535, i)]
[]
>>> print(lshift(0, 12345))
0
>>> print(lshift(2**62, 0)) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError...
"""
if
not
a
:
return
_PyLong_New
(
0
)
cdef
unsigned
long
apos
=
a
if
a
>
0
else
-
a
if
(
apos
>>
1
)
>=
<
unsigned
long
>
PyLong_BASE
:
raise
OverflowError
cdef
unsigned
long
index
=
n
//
PyLong_SHIFT
cdef
unsigned
long
shift
=
n
%
PyLong_SHIFT
cdef
digit
d
=
apos
cdef
digit
low
=
(
d
<<
shift
)
&
PyLong_MASK
cdef
digit
high
=
(
d
>>
(
PyLong_SHIFT
-
shift
))
if
high
==
0
:
ret
=
_PyLong_New
(
index
+
1
)
(
<
PyLongObject
*>
ret
).
ob_digit
[
index
]
=
low
else
:
ret
=
_PyLong_New
(
index
+
2
)
(
<
PyLongObject
*>
ret
).
ob_digit
[
index
]
=
low
(
<
PyLongObject
*>
ret
).
ob_digit
[
index
+
1
]
=
high
while
index
>=
1
:
index
-=
1
(
<
PyLongObject
*>
ret
).
ob_digit
[
index
]
=
0
if
a
<
0
:
Py_SIZE_PTR
(
ret
)[
0
]
*=
-
1
return
ret
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