Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def cast_str_to_float64(context, builder, fromty, toty, val):
fnty = lir.FunctionType(lir.DoubleType(), [lir.IntType(8).as_pointer()])
fn = builder.module.get_or_insert_function(fnty, name="str_to_float64")
return builder.call(fn, (val,))
"""
Generate ir code to convert a pointer-to-daal-numeric-table to a ndarray
FIXME: handle NULL pointer/table
"""
# we need to prepare the shape array and a pointer
shape_type = lir.ArrayType(lir.IntType(64), 2)
shape = cgutils.alloca_once(builder, shape_type)
data = cgutils.alloca_once(builder, lir.DoubleType().as_pointer())
assert(ary_type in [dtable_type, ftable_type, itable_type,])
# we also need indicate the type of the array (e.g. what we expect)
d4ptype = context.get_constant(types.byte, d4ptypes[ary_type])
# we can now declare and call our conversion function
fnty = lir.FunctionType(lir.VoidType(),
[lir.IntType(8).as_pointer(), # actually pointer to numeric table
lir.DoubleType().as_pointer().as_pointer(),
shape_type.as_pointer(),
lir.IntType(8)])
fn = builder.module.get_or_insert_function(fnty, name="to_c_array")
builder.call(fn, [ptr, data, shape, d4ptype])
# convert to ndarray
shape = cgutils.unpack_tuple(builder, builder.load(shape))
ary = _empty_nd_impl(context, builder, ary_type, shape)
cgutils.raw_memcpy(builder, ary.data, builder.load(data), ary.nitems, ary.itemsize, align=1)
# we are done!
return impl_ret_new_ref(context, builder, ary_type, ary._getvalue())
if node.op in BUILTIN_UNARY_OP:
if node.op == Op.NOT:
if isinstance(operand.type, (ir.IntType, ir.DoubleType)):
cond_expr = Binary(node.position, Op.EQ, node.rhs,
Number(node.position, 0, operand.type))
return self._codegen_If(
If(
node.position,
cond_expr,
Number(node.position, 1, operand.type),
Number(node.position, 0, operand.type), ))
elif node.op == Op.NEG:
lhs = ir.Constant(operand.type, 0)
if isinstance(operand.type, ir.IntType):
return self.builder.sub(lhs, operand, 'negop')
elif isinstance(operand.type, ir.DoubleType):
return self.builder.fsub(lhs, operand, 'fnegop')
else:
func = self.module.globals.get(
f'unary.{node.op}{mangle_args((operand.type,))}')
if not func:
raise CodegenError(
f'Undefined unary operator "{node.op}" for "{operand.type.describe()}"',
node.position)
return self.builder.call(func, [operand], 'unop')
node.args[0].position,
).print(self)
if convert_from.type.signed:
op = self.builder.fptosi
else:
op = self.builder.fptoui
break
# Convert from ints
if isinstance(convert_from.type, ir.IntType):
# int to float
if isinstance(convert_to, ir.DoubleType):
CodegenWarning(
f'Integer to float conversions ("{convert_from.type.describe()}" to "{convert_to.describe()}") are inherently imprecise',
node.args[0].position,
).print(self)
if convert_from.type.signed:
op = self.builder.sitofp
else:
op = self.builder.uitofp
break
# int to int
if isinstance(convert_to, ir.IntType):
def constant_to_typed_llvm_value(module, builder, c):
if c.matches.Float and c.bits == 64:
return TypedLLVMValue(
llvmlite.ir.Constant(llvmlite.ir.DoubleType(), c.val),
native_ast.Type.Float(bits=64)
)
if c.matches.Float and c.bits == 32:
return TypedLLVMValue(
llvmlite.ir.Constant(llvmlite.ir.FloatType(), c.val),
native_ast.Type.Float(bits=32)
)
if c.matches.Int:
return TypedLLVMValue(
llvmlite.ir.Constant(llvmlite.ir.IntType(c.bits), c.val),
native_ast.Type.Int(bits=c.bits, signed=c.signed)
)
if c.matches.NullPointer:
nt = native_ast.Type.Pointer(value_type=c.value_type)
t = type_to_llvm_type(nt)
from numba.extending import overload, register_jitable
from numba.targets.imputils import (Registry, impl_ret_untracked,
impl_ret_new_ref)
from numba.typing import signature
from numba import _helperlib, cgutils, types
registry = Registry()
lower = registry.lower
int32_t = ir.IntType(32)
int64_t = ir.IntType(64)
def const_int(x):
return ir.Constant(int32_t, x)
double = ir.DoubleType()
N = 624
N_const = ir.Constant(int32_t, N)
# This is the same struct as rnd_state_t in _random.c.
rnd_state_t = ir.LiteralStructType([
# index
int32_t,
# mt[N]
ir.ArrayType(int32_t, N),
# has_gauss
int32_t,
# gauss
double,
# is_initialized
def viewer(tyctx, val, viewty):
""" Bitcast a scalar 'val' to the given type 'viewty'. """
bits = val.bitwidth
if isinstance(viewty.dtype, types.Integer):
bitcastty = ir.IntType(bits)
elif isinstance(viewty.dtype, types.Float):
bitcastty = ir.FloatType() if bits == 32 else ir.DoubleType()
else:
assert 0, "unreachable"
def codegen(cgctx, builder, typ, args):
flt = args[0]
return builder.bitcast(flt, bitcastty)
retty = viewty.dtype
sig = retty(val, viewty)
return sig, codegen
def nt2nd(context, builder, ptr, ary_type):
"""
Generate ir code to convert a pointer-to-daal-numeric-table to a ndarray
FIXME: handle NULL pointer/table
"""
# we need to prepare the shape array and a pointer
shape_type = lir.ArrayType(lir.IntType(64), 2)
shape = cgutils.alloca_once(builder, shape_type)
data = cgutils.alloca_once(builder, lir.DoubleType().as_pointer())
assert(ary_type in [dtable_type, ftable_type, itable_type,])
# we also need indicate the type of the array (e.g. what we expect)
d4ptype = context.get_constant(types.byte, d4ptypes[ary_type])
# we can now declare and call our conversion function
fnty = lir.FunctionType(lir.VoidType(),
[lir.IntType(8).as_pointer(), # actually pointer to numeric table
lir.DoubleType().as_pointer().as_pointer(),
shape_type.as_pointer(),
lir.IntType(8)])
fn = builder.module.get_or_insert_function(fnty, name="to_c_array")
builder.call(fn, [ptr, data, shape, d4ptype])
# convert to ndarray
shape = cgutils.unpack_tuple(builder, builder.load(shape))
ary = _empty_nd_impl(context, builder, ary_type, shape)
cgutils.raw_memcpy(builder, ary.data, builder.load(data), ary.nitems, ary.itemsize, align=1)
# we are done!
# If we're casting non-pointers of the same bitwidth,
# just use bitcast
if cast_from.type.width == cast_to.width:
op = self.builder.bitcast
break
# If we're going from a smaller to a larger bitwidth,
# we need to use the right instruction
if cast_from.type.width < cast_to.width:
if isinstance(cast_from.type, ir.IntType):
if isinstance(cast_to, ir.IntType):
op = self.builder.zext
break
if isinstance(cast_to, ir.DoubleType):
if cast_from.type.signed:
op = self.builder.sitofp
break
else:
op = self.builder.uitofp
break
else:
op = self.builder.trunc
break
# cast_exception.msg += ' (data would be truncated)'
# raise cast_exception
raise cast_exception
result = op(cast_from, cast_to)
result.type = cast_to
# From LLVMLite documentation.
"""
This file demonstrates a trivial function "fpadd" returning the sum of
two floating-point numbers.
"""
from llvmlite import ir
# Create some useful types
double = ir.DoubleType()
fnty = ir.FunctionType(double, (double, double))
# Create an empty module...
module = ir.Module(name=__file__)
# and declare a function named "fpadd" inside it
func = ir.Function(module, fnty, name="fpadd")
# Now implement the function
block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
a, b = func.args
result = builder.fadd(a, b, name="res")
builder.ret(result)
# Print the module IR
print(module)