Commit 813b5a9e authored by Stefan Behnel's avatar Stefan Behnel

optimise abs(double) and abs(int)

parent ae00ae99
......@@ -283,10 +283,12 @@ builtin_utility_code = {
class _BuiltinOverride(object):
def __init__(self, py_name, args, ret_type, cname, py_equiv = "*",
utility_code = None, sig = None, func_type = None):
utility_code = None, sig = None, func_type = None,
is_strict_signature = False):
self.py_name, self.cname, self.py_equiv = py_name, cname, py_equiv
self.args, self.ret_type = args, ret_type
self.func_type, self.sig = func_type, sig
self.is_strict_signature = is_strict_signature
self.utility_code = utility_code
class BuiltinAttribute(object):
......@@ -312,6 +314,8 @@ class BuiltinFunction(_BuiltinOverride):
if sig is None:
sig = Signature(self.args, self.ret_type)
func_type = sig.function_type()
if self.is_strict_signature:
func_type.is_strict_signature = True
scope.declare_builtin_cfunction(self.py_name, func_type, self.cname,
self.py_equiv, self.utility_code)
......@@ -332,6 +336,12 @@ class BuiltinMethod(_BuiltinOverride):
builtin_function_table = [
# name, args, return, C API func, py equiv = "*"
BuiltinFunction('abs', "d", "d", "fabs",
is_strict_signature = True),
BuiltinFunction('abs', "f", "f", "fabsf",
is_strict_signature = True),
BuiltinFunction('abs', "i", "l", "labs",
is_strict_signature = True),
BuiltinFunction('abs', "O", "O", "PyNumber_Absolute"),
#('chr', "", "", ""),
#('cmp', "", "", "", ""), # int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
......
......@@ -1697,9 +1697,12 @@ class CFuncType(CType):
# nogil boolean Can be called without gil
# with_gil boolean Acquire gil around function body
# templates [string] or None
# is_strict_signature boolean function refuses to accept coerced arguments
# (used for optimisation overrides)
is_cfunction = 1
original_sig = None
is_strict_signature = False
def __init__(self, return_type, args, has_varargs = 0,
exception_value = None, exception_check = 0, calling_convention = "",
......@@ -2592,6 +2595,8 @@ def best_match(args, functions, pos=None):
if dst_type.assignable_from(src_type):
if src_type == dst_type or dst_type.same_as(src_type):
pass # score 0
elif func_type.is_strict_signature:
break # exact match requested but not found
elif is_promotion(src_type, dst_type):
score[2] += 1
elif not src_type.is_pyobject:
......
......@@ -32,6 +32,8 @@ class Signature(object):
# 'b' bint
# 'I' int *
# 'l' long
# 'f' float
# 'd' double
# 'h' Py_hash_t
# 'z' Py_ssize_t
# 'Z' Py_ssize_t *
......@@ -53,6 +55,8 @@ class Signature(object):
'b': PyrexTypes.c_bint_type,
'I': PyrexTypes.c_int_ptr_type,
'l': PyrexTypes.c_long_type,
'f': PyrexTypes.c_float_type,
'd': PyrexTypes.c_double_type,
'h': PyrexTypes.c_py_hash_t_type,
'z': PyrexTypes.c_py_ssize_t_type,
'Z': PyrexTypes.c_py_ssize_t_ptr_type,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment