def test_float_int32_casts(self): myjitdriver = JitDriver(greens = [], reds = 'auto', vectorize=True) def f(bytecount, va, vb, vc): i = 0 j = 0 while j < bytecount: myjitdriver.jit_merge_point() a = raw_storage_getitem(rffi.DOUBLE,va,j) b = raw_storage_getitem(rffi.INT,vb,i) c = a+rffi.cast(rffi.DOUBLE,b) raw_storage_setitem(vc, j, c) i += 4 j += 8 count = 32 va = alloc_raw_storage(8*count, zero=True) vb = alloc_raw_storage(4*count, zero=True) for i,v in enumerate([1.0,2.0,3.0,4.0]*(count/4)): raw_storage_setitem(va, i*8, rffi.cast(rffi.DOUBLE,v)) for i,v in enumerate([-1,-2,-3,-4]*(count/4)): raw_storage_setitem(vb, i*4, rffi.cast(rffi.INT,v)) vc = alloc_raw_storage(8*count, zero=True) self.meta_interp(f, [8*count, va, vb, vc], vec=True) for i in range(count): assert raw_storage_getitem(rffi.DOUBLE,vc,i*8) == 0.0 free_raw_storage(va) free_raw_storage(vb) free_raw_storage(vc)
def f(bytecount, va, vc): i = 0 while i < bytecount: myjitdriver.jit_merge_point() a = raw_storage_getitem(type,va,i) c = func(a) raw_storage_setitem(vc, i, rffi.cast(type,c)) i += size
def setitem(self, idx, item): if count < 2: raw_storage_setitem(self.values, idx * self.stride_size + self.start, rffi.cast(TP, item)) else: i = 0 for val in item: raw_storage_setitem(self.values, idx * self.stride_size + self.start + i*step, rffi.cast(TP, val)) i += 1
def test_untranslated_storage(): r = alloc_raw_storage(37) raw_storage_setitem(r, 8, 1<<30) res = raw_storage_getitem(lltype.Signed, r, 8) assert res == 1<<30 raw_storage_setitem(r, 8, 3.14) res = raw_storage_getitem(lltype.Float, r, 8) assert res == 3.14 py.test.raises(AlignmentError, raw_storage_getitem, lltype.Signed, r, 3) py.test.raises(AlignmentError, raw_storage_setitem, r, 3, 42.5) free_raw_storage(r)
def test_untranslated_storage(): r = alloc_raw_storage(37) raw_storage_setitem(r, 8, 1 << 30) res = raw_storage_getitem(lltype.Signed, r, 8) assert res == 1 << 30 raw_storage_setitem(r, 8, 3.14) res = raw_storage_getitem(lltype.Float, r, 8) assert res == 3.14 py.test.raises(AlignmentError, raw_storage_getitem, lltype.Signed, r, 3) py.test.raises(AlignmentError, raw_storage_setitem, r, 3, 42.5) free_raw_storage(r)
def f(bytecount, va, vb, vc): i = 0 j = 0 while i < bytecount: myjitdriver.jit_merge_point() a = raw_storage_getitem(rffi.INT,va,j) b = raw_storage_getitem(rffi.DOUBLE,vb,i) c = rffi.cast(rffi.DOUBLE,a)+b raw_storage_setitem(vc, i, c) j += 4 i += 8
def f(bytecount, va, vb, vc): i = 0 j = 0 while j < bytecount: myjitdriver.jit_merge_point() a = raw_storage_getitem(rffi.DOUBLE,va,j) b = raw_storage_getitem(rffi.INT,vb,i) c = a+rffi.cast(rffi.DOUBLE,b) raw_storage_setitem(vc, j, c) i += 4 j += 8
def setitem(self, idx, item): if count < 2: raw_storage_setitem(self.values, idx * self.stride_size + self.start, rffi.cast(TP, item)) else: i = 0 for val in item: raw_storage_setitem( self.values, idx * self.stride_size + self.start + i * step, rffi.cast(TP, val)) i += 1
def new(self, values, type, size=None, zero=True): bytecount = rffi.sizeof(type) if not values: array = alloc_raw_storage(size * bytecount, zero=zero) self.arrays.append(array) return array else: size = len(values) * bytecount array = alloc_raw_storage(size, zero=zero) for i, v in enumerate(values): raw_storage_setitem(array, i * bytecount, rffi.cast(type, v)) self.arrays.append(array) return array
def new(self, values, type, size=None, zero=True): bytecount = rffi.sizeof(type) if not values: array = alloc_raw_storage(size*bytecount, zero=zero) self.arrays.append(array) return array else: size = len(values)*bytecount array = alloc_raw_storage(size, zero=zero) for i,v in enumerate(values): raw_storage_setitem(array, i*bytecount, rffi.cast(type,v)) self.arrays.append(array) return array
def argsort(arr, space, w_axis): if w_axis is space.w_None: # note that it's fine ot pass None here as we're not going # to pass the result around (None is the link to base in slices) if arr.get_size() > 0: arr = arr.reshape(None, [arr.get_size()]) axis = 0 elif w_axis is None: axis = -1 else: axis = space.int_w(w_axis) # create array of indexes dtype = descriptor.get_dtype_cache(space).w_longdtype index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype) with index_arr.implementation as storage, arr as arr_storage: if len(arr.get_shape()) == 1: for i in range(arr.get_size()): raw_storage_setitem(storage, i * INT_SIZE, i) r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage, storage, 0, arr.start) ArgSort(r).sort() else: shape = arr.get_shape() if axis < 0: axis = len(shape) + axis if axis < 0 or axis >= len(shape): raise oefmt(space.w_IndexError, "Wrong axis %d", axis) arr_iter = AllButAxisIter(arr, axis) arr_state = arr_iter.reset() index_impl = index_arr.implementation index_iter = AllButAxisIter(index_impl, axis) index_state = index_iter.reset() stride_size = arr.strides[axis] index_stride_size = index_impl.strides[axis] axis_size = arr.shape[axis] while not arr_iter.done(arr_state): for i in range(axis_size): raw_storage_setitem( storage, i * index_stride_size + index_state.offset, i) r = Repr(index_stride_size, stride_size, axis_size, arr_storage, storage, index_state.offset, arr_state.offset) ArgSort(r).sort() arr_state = arr_iter.next(arr_state) index_state = index_iter.next(index_state) return index_arr
def argsort(arr, space, w_axis, itemsize): if w_axis is space.w_None: # note that it's fine ot pass None here as we're not going # to pass the result around (None is the link to base in slices) arr = arr.reshape(space, None, [arr.get_size()]) axis = 0 elif w_axis is None: axis = -1 else: axis = space.int_w(w_axis) # create array of indexes dtype = interp_dtype.get_dtype_cache(space).w_longdtype index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype) storage = index_arr.implementation.get_storage() if len(arr.get_shape()) == 1: for i in range(arr.get_size()): raw_storage_setitem(storage, i * INT_SIZE, i) r = Repr(INT_SIZE, itemsize, arr.get_size(), arr.get_storage(), storage, 0, arr.start) ArgSort(r).sort() else: shape = arr.get_shape() if axis < 0: axis = len(shape) + axis if axis < 0 or axis >= len(shape): raise OperationError(space.w_IndexError, space.wrap("Wrong axis %d" % axis)) iterable_shape = shape[:axis] + [0] + shape[axis + 1:] iter = AxisIterator(arr, iterable_shape, axis, False) index_impl = index_arr.implementation index_iter = AxisIterator(index_impl, iterable_shape, axis, False) stride_size = arr.strides[axis] index_stride_size = index_impl.strides[axis] axis_size = arr.shape[axis] while not iter.done(): for i in range(axis_size): raw_storage_setitem( storage, i * index_stride_size + index_iter.offset, i) r = Repr(index_stride_size, stride_size, axis_size, arr.get_storage(), storage, index_iter.offset, iter.offset) ArgSort(r).sort() iter.next() index_iter.next() return index_arr
def argsort(arr, space, w_axis): if w_axis is space.w_None: # note that it's fine ot pass None here as we're not going # to pass the result around (None is the link to base in slices) if arr.get_size() > 0: arr = arr.reshape(None, [arr.get_size()]) axis = 0 elif w_axis is None: axis = -1 else: axis = space.int_w(w_axis) # create array of indexes dtype = descriptor.get_dtype_cache(space).w_longdtype index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype) with index_arr.implementation as storage, arr as arr_storage: if len(arr.get_shape()) == 1: for i in range(arr.get_size()): raw_storage_setitem(storage, i * INT_SIZE, i) r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage, storage, 0, arr.start) ArgSort(r).sort() else: shape = arr.get_shape() if axis < 0: axis = len(shape) + axis if axis < 0 or axis >= len(shape): raise oefmt(space.w_IndexError, "Wrong axis %d", axis) arr_iter = AllButAxisIter(arr, axis) arr_state = arr_iter.reset() index_impl = index_arr.implementation index_iter = AllButAxisIter(index_impl, axis) index_state = index_iter.reset() stride_size = arr.strides[axis] index_stride_size = index_impl.strides[axis] axis_size = arr.shape[axis] while not arr_iter.done(arr_state): for i in range(axis_size): raw_storage_setitem(storage, i * index_stride_size + index_state.offset, i) r = Repr(index_stride_size, stride_size, axis_size, arr_storage, storage, index_state.offset, arr_state.offset) ArgSort(r).sort() arr_state = arr_iter.next(arr_state) index_state = index_iter.next(index_state) return index_arr
def argsort(arr, space, w_axis, itemsize): if w_axis is space.w_None: # note that it's fine ot pass None here as we're not going # to pass the result around (None is the link to base in slices) arr = arr.reshape(space, None, [arr.get_size()]) axis = 0 elif w_axis is None: axis = -1 else: axis = space.int_w(w_axis) # create array of indexes dtype = interp_dtype.get_dtype_cache(space).w_longdtype index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype) storage = index_arr.implementation.get_storage() if len(arr.get_shape()) == 1: for i in range(arr.get_size()): raw_storage_setitem(storage, i * INT_SIZE, i) r = Repr(INT_SIZE, itemsize, arr.get_size(), arr.get_storage(), storage, 0, arr.start) ArgSort(r).sort() else: shape = arr.get_shape() if axis < 0: axis = len(shape) + axis - 1 if axis < 0 or axis > len(shape): raise OperationError(space.w_IndexError, space.wrap( "Wrong axis %d" % axis)) iterable_shape = shape[:axis] + [0] + shape[axis + 1:] iter = AxisIterator(arr, iterable_shape, axis, False) index_impl = index_arr.implementation index_iter = AxisIterator(index_impl, iterable_shape, axis, False) stride_size = arr.strides[axis] index_stride_size = index_impl.strides[axis] axis_size = arr.shape[axis] while not iter.done(): for i in range(axis_size): raw_storage_setitem(storage, i * index_stride_size + index_iter.offset, i) r = Repr(index_stride_size, stride_size, axis_size, arr.get_storage(), storage, index_iter.offset, iter.offset) ArgSort(r).sort() iter.next() index_iter.next() return index_arr
def f(d): bc = d * rffi.sizeof(rffi.SIGNED) va = alloc_raw_storage(bc, zero=True) vb = alloc_raw_storage(bc, zero=True) vc = alloc_raw_storage(bc, zero=True) x = 1 for i in range(d): j = i * rffi.sizeof(rffi.SIGNED) raw_storage_setitem(va, j, rffi.cast(rffi.SIGNED, i)) raw_storage_setitem(vb, j, rffi.cast(rffi.SIGNED, i)) i = 0 while i < bc: myjitdriver.jit_merge_point() a = raw_storage_getitem(rffi.SIGNED, va, i) b = raw_storage_getitem(rffi.SIGNED, vb, i) c = a + b raw_storage_setitem(vc, i, rffi.cast(rffi.SIGNED, c)) i += 1 * rffi.sizeof(rffi.SIGNED) res = 0 for i in range(d): res += raw_storage_getitem(rffi.SIGNED, vc, i * rffi.sizeof(rffi.SIGNED)) free_raw_storage(va) free_raw_storage(vb) free_raw_storage(vc) return res
def setitem(self, index, v): raw_storage_setitem(self.impl.storage, index + self.impl.start, rffi.cast(lltype.Char, v))
def f(i): r = alloc_raw_storage(24) raw_storage_setitem(r, 8, i) res = raw_storage_getitem(lltype.Signed, r, 8) free_raw_storage(r) return res != i
def test_untranslated_storage(): r = alloc_raw_storage(15) raw_storage_setitem(r, 3, 1<<30) res = raw_storage_getitem(lltype.Signed, r, 3) free_raw_storage(r) assert res == 1<<30
def setitem(self, index, v): # XXX what if self.readonly? raw_storage_setitem(self.impl.storage, index + self.impl.start, rffi.cast(lltype.Char, v))
def f(): p = alloc_raw_storage(15, track_allocation=False, zero=True) raw_storage_setitem(p, 3, 24) res = raw_storage_getitem(lltype.Signed, p, 3) free_raw_storage(p, track_allocation=False) return res
def f(): p = alloc_raw_storage(15) raw_storage_setitem(p, 5, rffi.cast(rffi.UCHAR, 254)) res = raw_storage_getitem(rffi.UCHAR, p, 5) free_raw_storage(p) return rffi.cast(lltype.Signed, res)
def f(): p = alloc_raw_storage(15) raw_storage_setitem(p, 4, 2.4e15) res = raw_storage_getitem(lltype.Float, p, 4) free_raw_storage(p) return res
def f(): p = alloc_raw_storage(15) raw_storage_setitem(p, 3, 24) res = raw_storage_getitem(lltype.Signed, p, 3) free_raw_storage(p) return res
def setitem(self, item, v): raw_storage_setitem(self.impl.storage, item, rffi.cast(lltype.Char, v))