From 9f0578976a38665bbc1c86c087a668c9ef238d77 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 9 Feb 2013 17:23:21 +0100 Subject: [PATCH] externalise some utility code functions --- Cython/Compiler/Builtin.py | 53 ++++------------------------------------------ Cython/Utility/Builtins.c | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index d87d942..be53968 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -6,56 +6,11 @@ from Symtab import BuiltinScope, StructOrUnionScope from Code import UtilityCode from TypeSlots import Signature import PyrexTypes -import Naming import Options # C-level implementations of builtin types, functions and methods -pow2_utility_code = UtilityCode( -proto = """ -#define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None) -""") - -abs_int_utility_code = UtilityCode( -proto = ''' -static CYTHON_INLINE unsigned int __Pyx_abs_int(int x) { - if (unlikely(x == -INT_MAX-1)) - return ((unsigned int)INT_MAX) + 1U; - return (unsigned int) abs(x); -} -''') - -abs_long_utility_code = UtilityCode( -proto = ''' -static CYTHON_INLINE unsigned long __Pyx_abs_long(long x) { - if (unlikely(x == -LONG_MAX-1)) - return ((unsigned long)LONG_MAX) + 1U; - return (unsigned long) labs(x); -} -''') - -abs_longlong_utility_code = UtilityCode( - proto = ''' -static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_abs_longlong(PY_LONG_LONG x) { -#ifndef PY_LLONG_MAX -#ifdef LLONG_MAX - const PY_LONG_LONG PY_LLONG_MAX = LLONG_MAX; -#else - // copied from pyport.h in CPython 3.3, missing in 2.4 - const PY_LONG_LONG PY_LLONG_MAX = (1 + 2 * ((1LL << (CHAR_BIT * sizeof(PY_LONG_LONG) - 2)) - 1)); -#endif -#endif - if (unlikely(x == -PY_LLONG_MAX-1)) - return ((unsigned PY_LONG_LONG)PY_LLONG_MAX) + 1U; -#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L - return (unsigned PY_LONG_LONG) llabs(x); -#else - return (x<0) ? (unsigned PY_LONG_LONG)-x : (unsigned PY_LONG_LONG)x; -#endif -} -''') - iter_next_utility_code = UtilityCode.load_cached("IterNext", "ObjectHandling.c") getattr3_utility_code = UtilityCode.load_cached("GetAttr3", "Builtins.c") pyexec_utility_code = UtilityCode.load_cached("PyExec", "Builtins.c") @@ -178,21 +133,21 @@ builtin_function_table = [ BuiltinFunction('abs', "f", "f", "fabsf", is_strict_signature = True), BuiltinFunction('abs', None, None, "__Pyx_abs_int", - utility_code = abs_int_utility_code, + utility_code = UtilityCode.load("abs_int", "Builtins.c"), func_type = PyrexTypes.CFuncType( PyrexTypes.c_uint_type, [ PyrexTypes.CFuncTypeArg("arg", PyrexTypes.c_int_type, None) ], is_strict_signature = True)), BuiltinFunction('abs', None, None, "__Pyx_abs_long", - utility_code = abs_long_utility_code, + utility_code = UtilityCode.load("abs_long", "Builtins.c"), func_type = PyrexTypes.CFuncType( PyrexTypes.c_ulong_type, [ PyrexTypes.CFuncTypeArg("arg", PyrexTypes.c_long_type, None) ], is_strict_signature = True)), BuiltinFunction('abs', None, None, "__Pyx_abs_longlong", - utility_code = abs_longlong_utility_code, + utility_code = UtilityCode.load("abs_longlong", "Builtins.c"), func_type = PyrexTypes.CFuncType( PyrexTypes.c_ulonglong_type, [ PyrexTypes.CFuncTypeArg("arg", PyrexTypes.c_longlong_type, None) @@ -246,7 +201,7 @@ builtin_function_table = [ #('ord', "", "", ""), BuiltinFunction('pow', "OOO", "O", "PyNumber_Power"), BuiltinFunction('pow', "OO", "O", "__Pyx_PyNumber_Power2", - utility_code = pow2_utility_code), + utility_code = UtilityCode.load("pow2", "Builtins.c")), #('range', "", "", ""), #('raw_input', "", "", ""), #('reduce', "", "", ""), diff --git a/Cython/Utility/Builtins.c b/Cython/Utility/Builtins.c index f11a0d2..b10f996 100644 --- a/Cython/Utility/Builtins.c +++ b/Cython/Utility/Builtins.c @@ -203,3 +203,43 @@ static PyObject* __Pyx_Intern(PyObject* s) { #endif return s; } + +//////////////////// abs_int.proto //////////////////// + +static CYTHON_INLINE unsigned int __Pyx_abs_int(int x) { + if (unlikely(x == -INT_MAX-1)) + return ((unsigned int)INT_MAX) + 1U; + return (unsigned int) abs(x); +} + +//////////////////// abs_long.proto //////////////////// + +static CYTHON_INLINE unsigned long __Pyx_abs_long(long x) { + if (unlikely(x == -LONG_MAX-1)) + return ((unsigned long)LONG_MAX) + 1U; + return (unsigned long) labs(x); +} + +//////////////////// abs_longlong.proto //////////////////// + +static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_abs_longlong(PY_LONG_LONG x) { +#ifndef PY_LLONG_MAX +#ifdef LLONG_MAX + const PY_LONG_LONG PY_LLONG_MAX = LLONG_MAX; +#else + // copied from pyport.h in CPython 3.3, missing in 2.4 + const PY_LONG_LONG PY_LLONG_MAX = (1 + 2 * ((1LL << (CHAR_BIT * sizeof(PY_LONG_LONG) - 2)) - 1)); +#endif +#endif + if (unlikely(x == -PY_LLONG_MAX-1)) + return ((unsigned PY_LONG_LONG)PY_LLONG_MAX) + 1U; +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + return (unsigned PY_LONG_LONG) llabs(x); +#else + return (x<0) ? (unsigned PY_LONG_LONG)-x : (unsigned PY_LONG_LONG)x; +#endif +} + +//////////////////// pow2.proto //////////////////// + +#define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None) -- 2.7.4