def _resolve_abs(self, func, node, argtype): if argtype.is_int and not argtype.signed: # abs() on unsigned integral value return node.args[0] elif not node.type.is_numeric: result = nodes.call_pyfunc(func, node.args) else: return math_call2(self.env, 'abs', node)
def _resolve_abs(self, func, node, argtype): if argtype.is_int and not argtype.signed: # abs() on unsigned integral value return node.args[0] elif not node.type.is_numeric: result = nodes.call_pyfunc(func, node.args) else: return math_call2('abs', node)
def resolve_pow(env, restype, args): promote = env.crnt.typesystem.promote if restype.is_numeric: type = reduce(promote, [double, restype] + [a.type for a in args]) signature = type(*[type] * len(args)) result = nodes.MathCallNode(signature, args, None, name='pow') else: result = nodes.call_pyfunc(pow, args) return nodes.CoercionNode(result, restype)
def resolve_pow(type, args): have_mod = len(args) == 3 if (type.is_int or type.is_float) and not have_mod and not is_win32: result = resolve_intrinsic(args, pow, type) else: result = nodes.call_pyfunc(pow, args) return nodes.CoercionNode(result, type)
def _resolve_builtin_call_or_object(self, node, func): """ Resolve an ast.Call() of a built-in function, or call the built-in through the object layer otherwise. """ result = self._resolve_builtin_call(node, func) if result is None: result = nodes.call_pyfunc(func, node.args) return nodes.CoercionNode(result, node.type)
def resolve_builtin_call_or_object(self, node, func): """ Resolve an ast.Call() of a built-in function, or call the built-in through the object layer otherwise. """ result = self.resolve_builtin_call(node, func) if result is None: result = nodes.call_pyfunc(func, node.args) return nodes.CoercionNode(result, node.type)
def pow_(context, node, power, mod=None): dst_type, pow_type, signature = binop_type(context, node, power) args = [node, power] if pow_type.is_float and mod is None: result = resolve_intrinsic(args, pow, signature) else: if mod is not None: args.append(mod) result = nodes.call_pyfunc(pow, args) return nodes.CoercionNode(result, dst_type)
def pow_(typesystem, node, base, exponent, mod=None): if mod: warnings.warn( "pow() with modulo (third) argument not natively supported") return nodes.call_pyfunc(pow, [base, exponent, mod]) from . import mathmodule dst_type = mathmodule.binop_type(typesystem, base, exponent) result = mathmodule.infer_math_call(typesystem, node, base, exponent, mod) if dst_type.is_int: # TODO: Implement pow(int) in llvmmath return nodes.CoercionNode(result, dst_type) return result
def visit_CoerceToObject(self, node): new_node = node node_type = node.node.type if node_type.is_bool: new_node = function_util.external_call(self.context, self.llvm_module, "PyBool_FromLong", args=[node.node]) elif node_type.is_numeric: cls = None args = node.node, if node_type.is_int: cls = self._get_int_conversion_func(node_type, pyapi._from_long) elif node_type.is_float: cls = pyapi.PyFloat_FromDouble elif node_type.is_complex: cls = pyapi.PyComplex_FromDoubles complex_value = nodes.CloneableNode(node.node) args = [ nodes.ComplexAttributeNode(complex_value, "real"), nodes.ComplexAttributeNode(complex_value.clone, "imag") ] else: raise error.NumbaError( node, "Don't know how to coerce type %r to PyObject" % node_type) if cls: new_node = function_util.external_call(self.context, self.llvm_module, cls.__name__, args=args) elif node_type.is_pointer and not node_type.is_string(): # Create ctypes pointer object ctypes_pointer_type = node_type.to_ctypes() args = [ nodes.CoercionNode(node.node, int64), nodes.ObjectInjectNode(ctypes_pointer_type, object_) ] new_node = nodes.call_pyfunc(ctypes.cast, args) self.generic_visit(new_node) return new_node
def visit_MathNode(self, math_node): "Translate a nodes.MathNode to an intrinsic or libc math call" from numba.type_inference.modules import mathmodule lowerable = is_math_function([math_node.arg], math_node.py_func) if math_node.type.is_array or not lowerable: # Generate a Python call assert math_node.py_func is not None result = nodes.call_pyfunc(math_node.py_func, [math_node.arg]) result = result.coerce(math_node.type) else: # Lower to intrinsic or libc math call args = [math_node.arg], math_node.py_func, math_node.type if is_intrinsic(math_node.py_func): result = resolve_intrinsic(*args) else: result = resolve_libc_math(*args) return self.visit(result)
def visit_CoerceToObject(self, node): new_node = node node_type = node.node.type if node_type.is_numeric: cls = None args = node.node, if node_type.is_int: cls = self._get_int_conversion_func(node_type, pyapi._from_long) elif node_type.is_float: cls = pyapi.PyFloat_FromDouble elif node_type.is_complex: cls = pyapi.PyComplex_FromDoubles complex_value = nodes.CloneableNode(node.node) args = [ nodes.ComplexAttributeNode(complex_value, "real"), nodes.ComplexAttributeNode(complex_value.clone, "imag") ] else: raise error.NumbaError( node, "Don't know how to coerce type %r to PyObject" % node_type) if cls: new_node = function_util.external_call(self.context, self.llvm_module, cls.__name__, args=args) elif node_type.is_pointer and not node_type.is_string(): # Create ctypes pointer object ctypes_pointer_type = node_type.to_ctypes() args = [nodes.CoercionNode(node.node, int64), nodes.ObjectInjectNode(ctypes_pointer_type, object_)] new_node = nodes.call_pyfunc(ctypes.cast, args) elif node_type.is_bool: new_node = function_util.external_call(self.context, self.llvm_module, "PyBool_FromLong", args=[node.node]) self.generic_visit(new_node) return new_node
def typedcontainer_infer(compile_typedcontainer, type_node, iterable_node): """ Type inferer for typed containers, register with numba.register_inferer(). :param compile_typedcontainer: item_type -> typed container extension class :param type_node: type parameter to typed container constructor :param iterable_node: value parameter to typed container constructor (optional) """ assert type_node is not None type = get_type(type_node) if type.is_cast: elem_type = type.dst_type # Pre-compile typed list implementation typedcontainer_ctor = compile_typedcontainer(elem_type) # Inject the typedlist directly to avoid runtime implementation lookup iterable_node = iterable_node or nodes.const(None, object_) result = nodes.call_pyfunc(typedcontainer_ctor, (iterable_node,)) return nodes.CoercionNode(result, typedcontainer_ctor.exttype) return object_
def _resolve_round(self, func, node, argtype): return nodes.call_pyfunc(round, node.args)