From a734b360bd85210351926a074b9a06d98ccf5fda Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Wed, 24 Oct 2012 23:40:57 -0700 Subject: [PATCH] (Cheaper) overflow check for const rhs. --- Cython/Compiler/ExprNodes.py | 5 ++++- Cython/Compiler/PyrexTypes.py | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index ffabe13..762ef26 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -8013,7 +8013,10 @@ class NumBinopNode(BinopNode): self.infix = False if self.type.is_int and env.directives['overflowcheck'] and self.operator in ('+', '-', '*'): self.overflow_check = True - self.func = self.type.overflow_check_binop(self.op_names[self.operator], env) + self.func = self.type.overflow_check_binop( + self.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) diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index d39cbf5..c0a51c0 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -402,7 +402,9 @@ class CTypedefType(BaseType): # delegation return self.typedef_base_type.create_from_py_utility_code(env) - def overflow_check_binop(self, binop, env): + def overflow_check_binop(self, binop, env, const_rhs=False): + if const_rhs: + binop += "_const" env.use_utility_code(UtilityCode.load("Common", "Overflow.c")) type = self.declaration_code("") name = self.specialization_name() @@ -1557,10 +1559,12 @@ class CIntType(CNumericType): # be negative for signed ints, which is good. return "0xbad0bad0" - def overflow_check_binop(self, binop, 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 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'): -- 2.7.4