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
33e7929b
Commit
33e7929b
authored
Oct 31, 2012
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'overflow'
parents
8d2a7185
c93a2314
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
647 additions
and
10 deletions
+647
-10
Cython/Compiler/Code.py
Cython/Compiler/Code.py
+14
-2
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+42
-6
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+1
-0
Cython/Compiler/PyrexTypes.py
Cython/Compiler/PyrexTypes.py
+66
-2
Cython/Utility/Overflow.c
Cython/Utility/Overflow.c
+283
-0
docs/src/reference/compilation.rst
docs/src/reference/compilation.rst
+5
-0
tests/run/overflow_check.pxi
tests/run/overflow_check.pxi
+224
-0
tests/run/overflow_check_int.pyx
tests/run/overflow_check_int.pyx
+3
-0
tests/run/overflow_check_longlong.pyx
tests/run/overflow_check_longlong.pyx
+3
-0
tests/run/overflow_check_uint.pyx
tests/run/overflow_check_uint.pyx
+3
-0
tests/run/overflow_check_ulonglong.pyx
tests/run/overflow_check_ulonglong.pyx
+3
-0
No files found.
Cython/Compiler/Code.py
View file @
33e7929b
...
...
@@ -68,10 +68,18 @@ class UtilityCodeBase(object):
Code sections in the file can be specified as follows:
##### MyUtility.proto #####
[proto declarations]
##### MyUtility.init #####
[code run at module initialization]
##### MyUtility #####
#@requires: MyOtherUtility
#@substitute: naming
[definitions]
for prototypes and implementation respectively. For non-python or
-cython files backslashes should be used instead. 5 to 30 comment
...
...
@@ -374,10 +382,13 @@ class UtilityCode(UtilityCodeBase):
output
[
'utility_code_def'
].
put
(
self
.
format_code
(
self
.
impl
))
if
self
.
init
:
writer
=
output
[
'init_globals'
]
writer
.
putln
(
"/* %s.init */"
%
self
.
name
)
if
isinstance
(
self
.
init
,
basestring
):
writer
.
put
(
self
.
format_code
(
self
.
init
))
else
:
self
.
init
(
writer
,
output
.
module_pos
)
writer
.
putln
(
writer
.
error_goto_if_PyErr
(
output
.
module_pos
))
writer
.
putln
()
if
self
.
cleanup
and
Options
.
generate_cleanup_code
:
writer
=
output
[
'cleanup_globals'
]
if
isinstance
(
self
.
cleanup
,
basestring
):
...
...
@@ -400,13 +411,14 @@ def sub_tempita(s, context, file=None, name=None):
return
sub
(
s
,
**
context
)
class
TempitaUtilityCode
(
UtilityCode
):
def
__init__
(
self
,
name
=
None
,
proto
=
None
,
impl
=
None
,
file
=
None
,
context
=
None
,
**
kwargs
):
def
__init__
(
self
,
name
=
None
,
proto
=
None
,
impl
=
None
,
init
=
None
,
file
=
None
,
context
=
None
,
**
kwargs
):
if
context
is
None
:
context
=
{}
proto
=
sub_tempita
(
proto
,
context
,
file
,
name
)
impl
=
sub_tempita
(
impl
,
context
,
file
,
name
)
init
=
sub_tempita
(
init
,
context
,
file
,
name
)
super
(
TempitaUtilityCode
,
self
).
__init__
(
proto
,
impl
,
name
=
name
,
file
=
file
,
**
kwargs
)
proto
,
impl
,
init
=
init
,
name
=
name
,
file
=
file
,
**
kwargs
)
def
none_or_sub
(
self
,
s
,
context
):
"""
...
...
Cython/Compiler/ExprNodes.py
View file @
33e7929b
...
...
@@ -7341,6 +7341,9 @@ class TypecastNode(ExprNode):
if
self
.
type
is
None
:
base_type
=
self
.
base_type
.
analyse
(
env
)
_
,
self
.
type
=
self
.
declarator
.
analyse
(
base_type
,
env
)
if
self
.
operand
.
has_constant_result
():
# Must be done after self.type is resolved.
self
.
calculate_constant_result
()
if
self
.
type
.
is_cfunction
:
error
(
self
.
pos
,
"Cannot cast to a function type"
)
...
...
@@ -7400,11 +7403,11 @@ class TypecastNode(ExprNode):
return
self
.
operand
.
check_const
()
def
calculate_constant_result
(
self
):
# we usually do not know the result of a type cast at code
# generation time
pass
self
.
constant_result
=
self
.
calculate_result_code
(
self
.
operand
.
constant_result
)
def
calculate_result_code
(
self
):
def
calculate_result_code
(
self
,
operand_result
=
None
):
if
operand_result
is
None
:
operand_result
=
self
.
operand
.
result
()
if
self
.
type
.
is_complex
:
operand_result
=
self
.
operand
.
result
()
if
self
.
operand
.
type
.
is_complex
:
...
...
@@ -7418,7 +7421,7 @@ class TypecastNode(ExprNode):
real_part
,
imag_part
)
else
:
return
self
.
type
.
cast_code
(
self
.
operand
.
result
()
)
return
self
.
type
.
cast_code
(
operand_result
)
def
get_constant_c_result_code
(
self
):
operand_result
=
self
.
operand
.
get_constant_c_result_code
()
...
...
@@ -7997,6 +8000,7 @@ class NumBinopNode(BinopNode):
# Binary operation taking numeric arguments.
infix
=
True
overflow_check
=
False
def
analyse_c_operation
(
self
,
env
):
type1
=
self
.
operand1
.
type
...
...
@@ -8007,6 +8011,13 @@ class NumBinopNode(BinopNode):
return
if
self
.
type
.
is_complex
:
self
.
infix
=
False
if
self
.
type
.
is_int
and
env
.
directives
[
'overflowcheck'
]
and
self
.
operator
in
self
.
overflow_op_names
:
self
.
overflow_check
=
True
self
.
func
=
self
.
type
.
overflow_check_binop
(
self
.
overflow_op_names
[
self
.
operator
],
env
,
const_rhs
=
self
.
operand2
.
has_constant_result
())
self
.
is_temp
=
True
if
not
self
.
infix
or
(
type1
.
is_numeric
and
type2
.
is_numeric
):
self
.
operand1
=
self
.
operand1
.
coerce_to
(
self
.
type
,
env
)
self
.
operand2
=
self
.
operand2
.
coerce_to
(
self
.
type
,
env
)
...
...
@@ -8048,8 +8059,26 @@ class NumBinopNode(BinopNode):
return
(
type1
.
is_numeric
or
type1
.
is_enum
)
\
and
(
type2
.
is_numeric
or
type2
.
is_enum
)
def
generate_result_code
(
self
,
code
):
super
(
NumBinopNode
,
self
).
generate_result_code
(
code
)
if
self
.
overflow_check
:
self
.
overflow_bit
=
code
.
funcstate
.
allocate_temp
(
PyrexTypes
.
c_int_type
,
manage_ref
=
False
)
code
.
putln
(
"%s = 0;"
%
self
.
overflow_bit
);
code
.
putln
(
"%s = %s;"
%
(
self
.
result
(),
self
.
calculate_result_code
()))
code
.
putln
(
"if (unlikely(%s)) {"
%
self
.
overflow_bit
)
code
.
putln
(
'PyErr_Format(PyExc_OverflowError, "value too large");'
)
code
.
putln
(
code
.
error_goto
(
self
.
pos
))
code
.
putln
(
"}"
)
code
.
funcstate
.
release_temp
(
self
.
overflow_bit
)
def
calculate_result_code
(
self
):
if
self
.
infix
:
if
self
.
overflow_check
:
return
"%s(%s, %s, &%s)"
%
(
self
.
func
,
self
.
operand1
.
result
(),
self
.
operand2
.
result
(),
self
.
overflow_bit
)
elif
self
.
infix
:
return
"(%s %s %s)"
%
(
self
.
operand1
.
result
(),
self
.
operator
,
...
...
@@ -8088,6 +8117,13 @@ class NumBinopNode(BinopNode):
"%"
:
"PyNumber_Remainder"
,
"**"
:
"PyNumber_Power"
}
overflow_op_names
=
{
"+"
:
"add"
,
"-"
:
"sub"
,
"*"
:
"mul"
,
"<<"
:
"lshift"
,
}
class
IntBinopNode
(
NumBinopNode
):
# Binary operation taking integer arguments.
...
...
Cython/Compiler/Options.py
View file @
33e7929b
...
...
@@ -81,6 +81,7 @@ directive_defaults = {
'auto_cpdef'
:
False
,
'cdivision'
:
False
,
# was True before 0.12
'cdivision_warnings'
:
False
,
'overflowcheck'
:
False
,
'always_allow_keywords'
:
False
,
'allow_none_for_extension_args'
:
True
,
'wraparound'
:
True
,
...
...
Cython/Compiler/PyrexTypes.py
View file @
33e7929b
...
...
@@ -25,7 +25,7 @@ class BaseType(object):
# This is not entirely robust.
safe
=
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789'
all
=
[]
for
c
in
self
.
declaration_code
(
""
).
replace
(
" "
,
"__"
):
for
c
in
self
.
declaration_code
(
""
).
replace
(
"
unsigned "
,
"unsigned_"
).
replace
(
"long long"
,
"long_long"
).
replace
(
"
"
,
"__"
):
if
c
in
safe
:
all
.
append
(
c
)
else
:
...
...
@@ -402,6 +402,26 @@ class CTypedefType(BaseType):
# delegation
return
self
.
typedef_base_type
.
create_from_py_utility_code
(
env
)
def
overflow_check_binop
(
self
,
binop
,
env
,
const_rhs
=
False
):
env
.
use_utility_code
(
UtilityCode
.
load
(
"Common"
,
"Overflow.c"
))
type
=
self
.
declaration_code
(
""
)
name
=
self
.
specialization_name
()
if
binop
==
"lshift"
:
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"LeftShift"
,
"Overflow.c"
,
context
=
{
'TYPE'
:
type
,
'NAME'
:
name
,
'SIGNED'
:
self
.
signed
}))
else
:
if
const_rhs
:
binop
+=
"_const"
_load_overflow_base
(
env
)
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"SizeCheck"
,
"Overflow.c"
,
context
=
{
'TYPE'
:
type
,
'NAME'
:
name
}))
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"Binop"
,
"Overflow.c"
,
context
=
{
'TYPE'
:
type
,
'NAME'
:
name
,
'BINOP'
:
binop
}))
return
"__Pyx_%s_%s_checking_overflow"
%
(
binop
,
name
)
def
error_condition
(
self
,
result_code
):
if
self
.
typedef_is_external
:
if
self
.
exception_value
:
...
...
@@ -1546,7 +1566,51 @@ class CIntType(CNumericType):
# We do not really know the size of the type, so return
# a 32-bit literal and rely on casting to final type. It will
# be negative for signed ints, which is good.
return
"0xbad0bad0"
;
return
"0xbad0bad0"
def
overflow_check_binop
(
self
,
binop
,
env
,
const_rhs
=
False
):
env
.
use_utility_code
(
UtilityCode
.
load
(
"Common"
,
"Overflow.c"
))
type
=
self
.
declaration_code
(
""
)
name
=
self
.
specialization_name
()
if
binop
==
"lshift"
:
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"LeftShift"
,
"Overflow.c"
,
context
=
{
'TYPE'
:
type
,
'NAME'
:
name
,
'SIGNED'
:
not
self
.
signed
}))
else
:
if
const_rhs
:
binop
+=
"_const"
if
type
in
(
'int'
,
'long'
,
'long long'
):
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"BaseCaseSigned"
,
"Overflow.c"
,
context
=
{
'INT'
:
type
,
'NAME'
:
name
}))
elif
type
in
(
'unsigned int'
,
'unsigned long'
,
'unsigned long long'
):
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"BaseCaseUnsigned"
,
"Overflow.c"
,
context
=
{
'UINT'
:
type
,
'NAME'
:
name
}))
elif
self
.
rank
<=
1
:
# sizeof(short) < sizeof(int)
return
"__Pyx_%s_%s_no_overflow"
%
(
binop
,
name
)
else
:
_load_overflow_base
(
env
)
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"SizeCheck"
,
"Overflow.c"
,
context
=
{
'TYPE'
:
type
,
'NAME'
:
name
}))
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"Binop"
,
"Overflow.c"
,
context
=
{
'TYPE'
:
type
,
'NAME'
:
name
,
'BINOP'
:
binop
}))
return
"__Pyx_%s_%s_checking_overflow"
%
(
binop
,
name
)
def
_load_overflow_base
(
env
):
env
.
use_utility_code
(
UtilityCode
.
load
(
"Common"
,
"Overflow.c"
))
for
type
in
(
'int'
,
'long'
,
'long long'
):
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"BaseCaseSigned"
,
"Overflow.c"
,
context
=
{
'INT'
:
type
,
'NAME'
:
type
.
replace
(
' '
,
'_'
)}))
for
type
in
(
'unsigned int'
,
'unsigned long'
,
'unsigned long long'
):
env
.
use_utility_code
(
TempitaUtilityCode
.
load
(
"BaseCaseUnsigned"
,
"Overflow.c"
,
context
=
{
'UINT'
:
type
,
'NAME'
:
type
.
replace
(
' '
,
'_'
)}))
class
CAnonEnumType
(
CIntType
):
...
...
Cython/Utility/Overflow.c
0 → 100644
View file @
33e7929b
/*
These functions provide integer arithmetic with integer checking. They do not
actually raise an exception when an overflow is detected, but rather set a bit
in the overflow parameter. (This parameter may be re-used accross several
arithmetic operations, so should be or-ed rather than assigned to.)
The implementation is divided into two parts, the signed and unsigned basecases,
which is where the magic happens, and a generic template matching a specific
type to an implementation based on its (c-compile-time) size and signedness.
When possible, branching is avoided, and preference is given to speed over
accuracy (a low rate of falsely "detected" overflows are acceptable,
undetected overflows are not).
TODO: Hook up checking.
TODO: Conditionally support 128-bit with intmax_t?
*/
/////////////// Common.proto ///////////////
static
int
__Pyx_check_twos_complement
()
{
if
(
-
1
!=
~
0
)
{
PyErr_SetString
(
PyExc_RuntimeError
,
"Two's complement required for overflow checks."
);
return
1
;
}
else
if
(
sizeof
(
short
)
==
sizeof
(
int
))
{
PyErr_SetString
(
PyExc_RuntimeError
,
"sizeof(short) < sizeof(int) required for overflow checks."
);
return
1
;
}
else
{
return
0
;
}
}
#define __PYX_IS_UNSIGNED(type) ((type) -1 > 0)
#define __PYX_SIGN_BIT(type) ((unsigned type) 1 << (sizeof(type) * 8 - 1))
#define __PYX_HALF_MAX(type) (((type) 1) << (sizeof(type) * 8 - 2))
#define __PYX_MIN(type) (__PYX_IS_UNSIGNED(type) ? (type) 0 : 0 - __PYX_HALF_MAX(type) - __PYX_HALF_MAX(type))
#define __PYX_MAX(type) (~__PYX_MIN(type))
#define __Pyx_add_no_overflow(a, b, overflow) ((a) + (b))
#define __Pyx_add_const_no_overflow(a, b, overflow) ((a) + (b))
#define __Pyx_sub_no_overflow(a, b, overflow) ((a) - (b))
#define __Pyx_sub_const_no_overflow(a, b, overflow) ((a) - (b))
#define __Pyx_mul_no_overflow(a, b, overflow) ((a) * (b))
#define __Pyx_mul_const_no_overflow(a, b, overflow) ((a) * (b))
#define __Pyx_div_no_overflow(a, b, overflow) ((a) / (b))
#define __Pyx_div_const_no_overflow(a, b, overflow) ((a) / (b))
/////////////// Common.init ///////////////
__Pyx_check_twos_complement
();
/////////////// BaseCaseUnsigned.proto ///////////////
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_add_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_sub_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_mul_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_div_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
);
// Use these when b is known at compile time.
#define __Pyx_add_const_{{NAME}}_checking_overflow __Pyx_add_{{NAME}}_checking_overflow
#define __Pyx_sub_const_{{NAME}}_checking_overflow __Pyx_sub_{{NAME}}_checking_overflow
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_mul_const_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
constant
,
int
*
overflow
);
#define __Pyx_div_const_{{NAME}}_checking_overflow __Pyx_div_{{NAME}}_checking_overflow
/////////////// BaseCaseUnsigned ///////////////
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_add_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
)
{
{{
UINT
}}
r
=
a
+
b
;
*
overflow
|=
r
<
a
;
return
r
;
}
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_sub_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
)
{
{{
UINT
}}
r
=
a
-
b
;
*
overflow
|=
r
>
a
;
return
r
;
}
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_mul_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
)
{
if
(
sizeof
({{
UINT
}})
<
sizeof
(
long
))
{
unsigned
long
big_r
=
((
unsigned
long
)
a
)
*
((
unsigned
long
)
b
);
{{
UINT
}}
r
=
({{
UINT
}})
big_r
;
*
overflow
|=
big_r
!=
r
;
return
r
;
}
else
if
(
sizeof
({{
UINT
}})
<
sizeof
(
long
long
))
{
unsigned
long
long
big_r
=
((
unsigned
long
long
)
a
)
*
((
unsigned
long
long
)
b
);
{{
UINT
}}
r
=
({{
UINT
}})
big_r
;
*
overflow
|=
big_r
!=
r
;
return
r
;
}
else
{
{{
UINT
}}
prod
=
a
*
b
;
double
dprod
=
((
double
)
a
)
*
((
double
)
b
);
// Overflow results in an error of at least 2^sizeof(UINT),
// whereas rounding represents an error on the order of 2^(sizeof(UINT)-53).
*
overflow
|=
fabs
(
dprod
-
prod
)
>
(
__PYX_MAX
({{
UINT
}})
/
2
);
return
prod
;
}
}
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_mul_const_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
)
{
if
(
b
>
1
)
{
*
overflow
|=
a
>
__PYX_MAX
({{
UINT
}})
/
b
;
}
return
a
*
b
;
}
static
CYTHON_INLINE
{{
UINT
}}
__Pyx_div_
{{
NAME
}}
_checking_overflow
({{
UINT
}}
a
,
{{
UINT
}}
b
,
int
*
overflow
)
{
if
(
b
==
0
)
{
*
overflow
|=
1
;
return
0
;
}
return
a
/
b
;
}
/////////////// BaseCaseSigned.proto ///////////////
static
CYTHON_INLINE
{{
INT
}}
__Pyx_add_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
INT
}}
__Pyx_sub_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
INT
}}
__Pyx_mul_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
INT
}}
__Pyx_div_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
);
// Use when b is known at compile time.
static
CYTHON_INLINE
{{
INT
}}
__Pyx_add_const_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
INT
}}
__Pyx_sub_const_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
);
static
CYTHON_INLINE
{{
INT
}}
__Pyx_mul_const_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
constant
,
int
*
overflow
);
#define __Pyx_div_const_{{NAME}}_checking_overflow __Pyx_div_{{NAME}}_checking_overflow
/////////////// BaseCaseSigned ///////////////
static
CYTHON_INLINE
{{
INT
}}
__Pyx_add_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
if
(
sizeof
({{
INT
}})
<
sizeof
(
long
))
{
long
big_r
=
((
long
)
a
)
+
((
long
)
b
);
{{
INT
}}
r
=
({{
INT
}})
big_r
;
*
overflow
|=
big_r
!=
r
;
return
r
;
}
else
if
(
sizeof
({{
INT
}})
<
sizeof
(
long
long
))
{
long
long
big_r
=
((
long
long
)
a
)
+
((
long
long
)
b
);
{{
INT
}}
r
=
({{
INT
}})
big_r
;
*
overflow
|=
big_r
!=
r
;
return
r
;
}
else
{
// Signed overflow undefined, but unsigned overflow is well defined.
{{
INT
}}
r
=
({{
INT
}})
((
unsigned
{{
INT
}})
a
+
(
unsigned
{{
INT
}})
b
);
// Overflow happened if the operands have the same sign, but the result
// has opposite sign.
// sign(a) == sign(b) != sign(r)
{{
INT
}}
sign_a
=
__PYX_SIGN_BIT
({{
INT
}})
&
a
;
{{
INT
}}
sign_b
=
__PYX_SIGN_BIT
({{
INT
}})
&
b
;
{{
INT
}}
sign_r
=
__PYX_SIGN_BIT
({{
INT
}})
&
r
;
*
overflow
|=
(
sign_a
==
sign_b
)
&
(
sign_a
!=
sign_r
);
return
r
;
}
}
static
CYTHON_INLINE
{{
INT
}}
__Pyx_add_const_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
if
(
b
>
0
)
{
*
overflow
|=
a
>
__PYX_MAX
({{
INT
}})
-
b
;
}
else
if
(
b
<
0
)
{
*
overflow
|=
a
<
__PYX_MIN
({{
INT
}})
-
b
;
}
return
a
+
b
;
}
static
CYTHON_INLINE
{{
INT
}}
__Pyx_sub_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
*
overflow
|=
b
==
__PYX_MIN
({{
INT
}});
return
__Pyx_add_
{{
NAME
}}
_checking_overflow
(
a
,
-
b
,
overflow
);
}
static
CYTHON_INLINE
{{
INT
}}
__Pyx_sub_const_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
*
overflow
|=
b
==
__PYX_MIN
({{
INT
}});
return
__Pyx_add_const_
{{
NAME
}}
_checking_overflow
(
a
,
-
b
,
overflow
);
}
static
CYTHON_INLINE
{{
INT
}}
__Pyx_mul_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
if
(
sizeof
({{
INT
}})
<
sizeof
(
long
))
{
long
big_r
=
((
long
)
a
)
*
((
long
)
b
);
{{
INT
}}
r
=
({{
INT
}})
big_r
;
*
overflow
|=
big_r
!=
r
;
return
({{
INT
}})
r
;
}
else
if
(
sizeof
({{
INT
}})
<
sizeof
(
long
long
))
{
long
long
big_r
=
((
long
long
)
a
)
*
((
long
long
)
b
);
{{
INT
}}
r
=
({{
INT
}})
big_r
;
*
overflow
|=
big_r
!=
r
;
return
({{
INT
}})
r
;
}
else
{
{{
INT
}}
prod
=
a
*
b
;
double
dprod
=
((
double
)
a
)
*
((
double
)
b
);
// Overflow results in an error of at least 2^sizeof(INT),
// whereas rounding represents an error on the order of 2^(sizeof(INT)-53).
*
overflow
|=
fabs
(
dprod
-
prod
)
>
(
__PYX_MAX
({{
INT
}})
/
2
);
return
prod
;
}
}
static
CYTHON_INLINE
{{
INT
}}
__Pyx_mul_const_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
if
(
b
>
1
)
{
*
overflow
|=
a
>
__PYX_MAX
({{
INT
}})
/
b
;
*
overflow
|=
a
<
__PYX_MIN
({{
INT
}})
/
b
;
}
else
if
(
b
==
-
1
)
{
*
overflow
|=
a
==
__PYX_MIN
({{
INT
}});
}
else
if
(
b
<
-
1
)
{
*
overflow
|=
a
>
__PYX_MIN
({{
INT
}})
/
b
;
*
overflow
|=
a
<
__PYX_MAX
({{
INT
}})
/
b
;
}
return
a
*
b
;
}
static
CYTHON_INLINE
{{
INT
}}
__Pyx_div_
{{
NAME
}}
_checking_overflow
({{
INT
}}
a
,
{{
INT
}}
b
,
int
*
overflow
)
{
if
(
b
==
0
)
{
*
overflow
|=
1
;
return
0
;
}
*
overflow
|=
(
a
==
__PYX_MIN
({{
INT
}}))
&
(
b
==
-
1
);
return
a
/
b
;
}
/////////////// SizeCheck.init ///////////////
__Pyx_check_sane_
{{
NAME
}}();
/////////////// SizeCheck.proto ///////////////
static
int
__Pyx_check_sane_
{{
NAME
}}()
{
if
(
sizeof
({{
TYPE
}})
<=
sizeof
(
int
)
||
sizeof
({{
TYPE
}})
==
sizeof
(
long
)
||
sizeof
({{
TYPE
}})
==
sizeof
(
long
long
))
{
return
0
;
}
else
{
PyErr_Format
(
PyExc_RuntimeError
,
"Bad size for int type %s: %d"
,
"{{TYPE}}"
,
(
int
)
sizeof
({{
TYPE
}}));
return
1
;
}
}
/////////////// Binop.proto ///////////////
static
CYTHON_INLINE
{{
TYPE
}}
__Pyx_
{{
BINOP
}}
_
{{
NAME
}}
_checking_overflow
({{
TYPE
}}
a
,
{{
TYPE
}}
b
,
int
*
overflow
);
/////////////// Binop ///////////////
static
CYTHON_INLINE
{{
TYPE
}}
__Pyx_
{{
BINOP
}}
_
{{
NAME
}}
_checking_overflow
({{
TYPE
}}
a
,
{{
TYPE
}}
b
,
int
*
overflow
)
{
if
(
sizeof
({{
TYPE
}})
<
sizeof
(
int
))
{
return
__Pyx_
{{
BINOP
}}
_no_overflow
(
a
,
b
,
overflow
);
}
else
if
(
__PYX_IS_UNSIGNED
({{
TYPE
}}))
{
if
(
sizeof
({{
TYPE
}})
==
sizeof
(
int
))
{
return
__Pyx_
{{
BINOP
}}
_unsigned_int_checking_overflow
(
a
,
b
,
overflow
);
}
else
if
(
sizeof
({{
TYPE
}})
==
sizeof
(
long
))
{
return
__Pyx_
{{
BINOP
}}
_unsigned_long_checking_overflow
(
a
,
b
,
overflow
);
}
else
if
(
sizeof
({{
TYPE
}})
==
sizeof
(
long
long
))
{
return
__Pyx_
{{
BINOP
}}
_unsigned_long_long_checking_overflow
(
a
,
b
,
overflow
);
}
else
{
abort
();
return
0
;
// handled elsewhere
}
}
else
{
if
(
sizeof
({{
TYPE
}})
==
sizeof
(
int
))
{
return
__Pyx_
{{
BINOP
}}
_int_checking_overflow
(
a
,
b
,
overflow
);
}
else
if
(
sizeof
({{
TYPE
}})
==
sizeof
(
long
))
{
return
__Pyx_
{{
BINOP
}}
_long_checking_overflow
(
a
,
b
,
overflow
);
}
else
if
(
sizeof
({{
TYPE
}})
==
sizeof
(
long
long
))
{
return
__Pyx_
{{
BINOP
}}
_long_long_checking_overflow
(
a
,
b
,
overflow
);
}
else
{
abort
();
return
0
;
// handled elsewhere
}
}
}
/////////////// LeftShift.proto ///////////////
static
CYTHON_INLINE
{{
TYPE
}}
__Pyx_lshift_
{{
NAME
}}
_checking_overflow
({{
TYPE
}}
a
,
{{
TYPE
}}
b
,
int
*
overflow
)
{
*
overflow
|=
#if {{SIGNED}}
(
b
<
0
)
|
#endif
(
b
>
(
8
*
sizeof
({{
TYPE
}})))
|
(
a
>
(
__PYX_MAX
({{
TYPE
}})
>>
b
));
return
a
<<
b
;
}
#define __Pyx_lshift_const_{{NAME}}_checking_overflow __Pyx_lshift_{{NAME}}_checking_overflow
docs/src/reference/compilation.rst
View file @
33e7929b
...
...
@@ -148,6 +148,11 @@ Cython code. Here is the list of currently supported directives:
set to ``None``. Otherwise a check is inserted and the
appropriate exception is raised. This is off by default for
performance reasons. Default is False.
``overflowcheck`` (True / False)
If set to True, raise errors on overflowing C integer arithmetic
operations. Incurs a slight runtime penalty, but much faster than
using Python ints. Default is False.
``embedsignature`` (True / False)
If set to True, Cython will embed a textual copy of the call
...
...
tests/run/overflow_check.pxi
0 → 100644
View file @
33e7929b
cimport
cython
cdef
object
two
=
2
cdef
int
size_in_bits
=
sizeof
(
INT
)
*
8
cdef
bint
is_signed_
=
(
<
INT
>-
1
<
0
)
cdef
INT
max_value_
=
<
INT
>
(
two
**
(
size_in_bits
-
is_signed_
)
-
1
)
cdef
INT
min_value_
=
~
max_value_
cdef
INT
half_
=
max_value_
//
2
# Python visible.
is_signed
=
is_signed_
max_value
=
max_value_
min_value
=
min_value_
half
=
half_
import
operator
from
libc.math
cimport
sqrt
cpdef
check
(
func
,
op
,
a
,
b
):
cdef
INT
res
,
op_res
cdef
bint
func_overflow
=
False
cdef
bint
assign_overflow
=
False
try
:
res
=
func
(
a
,
b
)
except
OverflowError
:
func_overflow
=
True
try
:
op_res
=
op
(
a
,
b
)
except
OverflowError
:
assign_overflow
=
True
assert
func_overflow
==
assign_overflow
,
"Inconsistant overflow: %s(%s, %s)"
%
(
func
,
a
,
b
)
if
not
func_overflow
:
assert
res
==
op_res
,
"Inconsistant values: %s(%s, %s) == %s != %s"
%
(
func
,
a
,
b
,
res
,
op_res
)
medium_values
=
(
max_value_
/
2
,
max_value_
/
3
,
min_value_
/
2
,
<
INT
>
sqrt
(
max_value_
)
-
1
,
<
INT
>
sqrt
(
max_value_
)
+
1
)
def
run_test
(
func
,
op
):
cdef
INT
offset
,
b
check
(
func
,
op
,
300
,
200
)
check
(
func
,
op
,
max_value_
,
max_value_
)
check
(
func
,
op
,
max_value_
,
min_value_
)
if
not
is_signed_
or
not
func
is
test_sub
:
check
(
func
,
op
,
min_value_
,
min_value_
)
for
offset
in
range
(
5
):
check
(
func
,
op
,
max_value_
-
1
,
offset
)
check
(
func
,
op
,
min_value_
+
1
,
offset
)
if
is_signed_
:
check
(
func
,
op
,
max_value_
-
1
,
2
-
offset
)
check
(
func
,
op
,
min_value_
+
1
,
2
-
offset
)
for
offset
in
range
(
9
):
check
(
func
,
op
,
max_value_
/
2
,
offset
)
check
(
func
,
op
,
min_value_
/
3
,
offset
)
check
(
func
,
op
,
max_value_
/
4
,
offset
)
check
(
func
,
op
,
min_value_
/
5
,
offset
)
if
is_signed_
:
check
(
func
,
op
,
max_value_
/
2
,
4
-
offset
)
check
(
func
,
op
,
min_value_
/
3
,
4
-
offset
)
check
(
func
,
op
,
max_value_
/
-
4
,
3
-
offset
)
check
(
func
,
op
,
min_value_
/
-
5
,
3
-
offset
)
for
offset
in
range
(
-
3
,
4
):
for
a
in
medium_values
:
for
b
in
medium_values
:
check
(
func
,
op
,
a
,
b
+
offset
)
@
cython
.
overflowcheck
(
True
)
def
test_add
(
INT
a
,
INT
b
):
"""
>>> test_add(1, 2)
3
>>> test_add(max_value, max_value) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: value too large
>>> run_test(test_add, operator.add)
"""
return
int
(
a
+
b
)
@
cython
.
overflowcheck
(
True
)
def
test_sub
(
INT
a
,
INT
b
):
"""
>>> test_sub(10, 1)
9
>>> test_sub(min_value, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: value too large
>>> run_test(test_sub, operator.sub)
"""
return
int
(
a
-
b
)
@
cython
.
overflowcheck
(
True
)
def
test_mul
(
INT
a
,
INT
b
):
"""
>>> test_mul(11, 13)
143
>>> test_mul(max_value / 2, max_value / 2) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: value too large
>>> run_test(test_mul, operator.mul)
"""
return
int
(
a
*
b
)
@
cython
.
overflowcheck
(
True
)
def
test_nested
(
INT
a
,
INT
b
,
INT
c
):
"""
>>> test_nested(1, 2, 3)
6
>>> expect_overflow(test_nested, half + 1, half + 1, half + 1)
>>> expect_overflow(test_nested, half - 1, half - 1, half - 1)
"""
return
int
(
a
+
b
+
c
)
def
expect_overflow
(
func
,
*
args
):
try
:
res
=
func
(
*
args
)
except
OverflowError
:
return
assert
False
,
"Expected OverflowError, got %s"
%
res
cpdef
format
(
INT
value
):
"""
>>> format(1)
'1'
>>> format(half - 1)
'half - 1'
>>> format(half)
'half'
>>> format(half + 2)
'half + 2'
>>> format(half + half - 3)
'half + half - 3'
>>> format(max_value)
'max_value'
"""
if
value
==
max_value_
:
return
"max_value"
elif
value
==
half_
:
return
"half"
elif
max_value_
-
value
<=
max_value_
//
4
:
return
"half + half - %s"
%
(
half_
+
half_
-
value
)
elif
max_value_
-
value
<=
half_
:
return
"half + %s"
%
(
value
-
half_
)
elif
max_value_
-
value
<=
half_
+
max_value_
//
4
:
return
"half - %s"
%
(
half_
-
value
)
else
:
return
"%s"
%
value
cdef
INT
called
(
INT
value
):
print
(
"called(%s)"
%
format
(
value
))
return
value
@
cython
.
overflowcheck
(
True
)
def
test_nested_func
(
INT
a
,
INT
b
,
INT
c
):
"""
>>> test_nested_func(1, 2, 3)
called(5)
6
>>> expect_overflow(test_nested_func, half + 1, half + 1, half + 1)
>>> expect_overflow(test_nested_func, half - 1, half - 1, half - 1)
called(half + half - 2)
>>> print(format(test_nested_func(1, half - 1, half - 1)))
called(half + half - 2)
half + half - 1
>>>
"""
return
int
(
a
+
called
(
b
+
c
))
@
cython
.
overflowcheck
(
True
)
def
test_add_const
(
INT
a
):
"""
>>> test_add_const(1)
101
>>> expect_overflow(test_add_const, max_value)
>>> expect_overflow(test_add_const , max_value - 99)
>>> test_add_const(max_value - 100) == max_value
True
"""
return
int
(
a
+
<
INT
>
100
)
@
cython
.
overflowcheck
(
True
)
def
test_sub_const
(
INT
a
):
"""
>>> test_sub_const(101)
1
>>> expect_overflow(test_sub_const, min_value)
>>> expect_overflow(test_sub_const, min_value + 99)
>>> test_sub_const(min_value + 100) == min_value
True
"""
return
int
(
a
-
<
INT
>
100
)
@
cython
.
overflowcheck
(
True
)
def
test_mul_const
(
INT
a
):
"""
>>> test_mul_const(2)
200
>>> expect_overflow(test_mul_const, max_value)
>>> expect_overflow(test_mul_const, max_value // 99)
>>> test_mul_const(max_value // 100) == max_value - max_value % 100
True
"""
return
int
(
a
*
<
INT
>
100
)
@
cython
.
overflowcheck
(
True
)
def
test_lshift
(
INT
a
,
int
b
):
"""
>>> test_lshift(1, 10)
1024
>>> expect_overflow(test_lshift, 1, 100)
>>> expect_overflow(test_lshift, max_value, 1)
>>> test_lshift(max_value, 0) == max_value
True
>>> check(test_lshift, operator.lshift, 10, 15)
>>> check(test_lshift, operator.lshift, 10, 30)
>>> check(test_lshift, operator.lshift, 100, 60)
"""
return
int
(
a
<<
b
)
tests/run/overflow_check_int.pyx
0 → 100644
View file @
33e7929b
ctypedef
int
INT
include
"overflow_check.pxi"
tests/run/overflow_check_longlong.pyx
0 → 100644
View file @
33e7929b
ctypedef
long
long
INT
include
"overflow_check.pxi"
tests/run/overflow_check_uint.pyx
0 → 100644
View file @
33e7929b
ctypedef
unsigned
int
INT
include
"overflow_check.pxi"
tests/run/overflow_check_ulonglong.pyx
0 → 100644
View file @
33e7929b
ctypedef
unsigned
long
long
INT
include
"overflow_check.pxi"
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