def type_from_any(self, some_type): """ Return an LLVM type from some kind of type. """ # XXX support for other ctypes from ctypes import sizeof from qy import type_from_dtype ctype_integer_types = \ set([ ctypes.c_bool, ctypes.c_byte, ctypes.c_ubyte, ctypes.c_char, ctypes.c_wchar, ctypes.c_short, ctypes.c_ushort, ctypes.c_long, ctypes.c_longlong, ctypes.c_ulong, ctypes.c_ulonglong, ctypes.c_int8, ctypes.c_int16, ctypes.c_int32, ctypes.c_int64, ctypes.c_uint8, ctypes.c_uint16, ctypes.c_uint32, ctypes.c_uint64, ctypes.c_size_t, ]) if isinstance(some_type, type): return type_from_dtype(numpy.dtype(some_type)) elif isinstance(some_type, numpy.dtype): return type_from_dtype(some_type) elif isinstance(some_type, llvm.Type): return some_type elif some_type in ctype_integer_types: return llvm.Type.int(sizeof(some_type) * 8) else: raise TypeError("cannot build type from \"%s\" instance" % type(some_type))
def test_type_from_dtype_complex(): """ Test to-LLVM translation of a complex numpy dtype. """ from qy import type_from_dtype dtype = numpy.dtype([("d", [("k", numpy.uint32), ("n", numpy.uint32)], (4,))]) type_ = type_from_dtype(dtype) assert_equal(str(type_), "<{ [4 x <{ i32, i32 }>] }>")
def from_numpy(ndarray): """ Build an array from a particular numpy array. """ # XXX maintain reference to array in module; decref in destructor from qy import type_from_dtype type_ = type_from_dtype(ndarray.dtype) (location, _) = ndarray.__array_interface__["data"] data = llvm.Constant.int(iptr_type, location).inttoptr(llvm.Type.pointer(type_)) return StridedArray.from_raw(qy.value_from_any(data), ndarray.shape, ndarray.strides)
def __init__(self, dtype): """ Initialize. """ from qy import type_from_dtype self._dtype = numpy.dtype(dtype) self._type = type_from_dtype(self._dtype) # we can only support (for now) types that provide simple equality tests supported = set([ llvm.core.TYPE_DOUBLE, ]) if self._type.kind not in supported: raise ValueError("unsupported type for constant distribution")