def f(i): interp = InterpreterState(codes[i]) interp.run(space) if not len(interp.results): raise Exception("need results") w_res = interp.results[-1] if isinstance(w_res, W_NDimArray): i, s = w_res.create_iter() w_res = i.getitem(s) if isinstance(w_res, boxes.W_Float64Box): return w_res.value if isinstance(w_res, boxes.W_Float32Box): return float(w_res.value) elif isinstance(w_res, boxes.W_Int64Box): return float(w_res.value) elif isinstance(w_res, boxes.W_Int32Box): return float(int(w_res.value)) elif isinstance(w_res, boxes.W_Int16Box): return float(int(w_res.value)) elif isinstance(w_res, boxes.W_Int8Box): return float(int(w_res.value)) elif isinstance(w_res, boxes.W_UInt64Box): return float(intmask(w_res.value)) elif isinstance(w_res, boxes.W_UInt32Box): return float(intmask(w_res.value)) elif isinstance(w_res, boxes.W_UInt16Box): return float(intmask(w_res.value)) elif isinstance(w_res, boxes.W_UInt8Box): return float(intmask(w_res.value)) elif isinstance(w_res, boxes.W_LongBox): return float(w_res.value) elif isinstance(w_res, boxes.W_BoolBox): return float(w_res.value) print "ERROR: did not implement return type for interpreter" raise TypeError(w_res)
def set_video_mode(self, w, h, d): if not (w > 0 and h > 0): return assert d in [1, 2, 4, 8, 16, 32] if d < MINIMUM_DEPTH: d = MINIMUM_DEPTH self.width = intmask(w) self.height = intmask(h) self.depth = intmask(d) if self.window == lltype.nullptr(RSDL.WindowPtr.TO): self.create_window_and_renderer(x=RSDL.WINDOWPOS_UNDEFINED, y=RSDL.WINDOWPOS_UNDEFINED, width=w, height=h) if self.screen_texture != lltype.nullptr(RSDL.TexturePtr.TO): RSDL.DestroyTexture(self.screen_texture) if self.screen_surface != lltype.nullptr(RSDL.Surface): RSDL.FreeSurface(self.screen_surface) self.has_surface = True self.screen_texture = RSDL.CreateTexture( self.renderer, RSDL.PIXELFORMAT_ARGB8888, RSDL.TEXTUREACCESS_STREAMING, w, h) if not self.screen_texture: print "Could not create screen texture" raise RuntimeError(RSDL.GetError()) self.screen_surface = RSDL.CreateRGBSurface(0, w, h, d, 0, 0, 0, 0) assert self.screen_surface, RSDL.GetError() self.bpp = intmask(self.screen_surface.c_format.c_BytesPerPixel) if d == MINIMUM_DEPTH: self.set_squeak_colormap(self.screen_surface) self.pitch = self.width * self.bpp
def realize_global_int(ffi, g, gindex): fetch_fnptr = rffi.cast(FUNCPTR_FETCH_LONGLONG, g.c_address) with lltype.scoped_alloc(parse_c_type.GETCONST_S) as p_value: p_value.c_ctx = ffi.ctxobj.ctx rffi.setintfield(p_value, 'c_gindex', gindex) neg = fetch_fnptr(p_value) value = p_value.c_value neg = rffi.cast(lltype.Signed, neg) if neg == 0: # positive if value <= rffi.cast(rffi.ULONGLONG, sys.maxint): return ffi.space.wrap(intmask(value)) else: return ffi.space.wrap(value) elif neg == 1: # negative value = rffi.cast(rffi.LONGLONG, value) if value >= -sys.maxint-1: return ffi.space.wrap(intmask(value)) else: return ffi.space.wrap(value) if neg == 2: got = "%d (0x%x)" % (value, value) else: got = "%d" % (rffi.cast(rffi.LONGLONG, value),) raise oefmt(ffi.w_FFIError, "the C compiler says '%s' is equal to %s, " "but the cdef disagrees", rffi.charp2str(g.c_name), got)
def wrap_int(self, val): if isinstance(val, rbigint.rbigint): return self.wrap_rbigint(val) elif isinstance(val, int): return self.wrap_smallint_unsafe(val) elif isinstance(val, r_uint): if val <= r_uint(constants.MAXINT): return self.wrap_smallint_unsafe(intmask(val)) else: return self.wrap_wordint_direct(val, self.w_LargePositiveInteger) elif IS_64BIT and isinstance(val, r_uint32): return self.wrap_smallint_unsafe(intmask(val)) elif isinstance(val, r_longlong) or isinstance(val, r_int64): # use '&' instead of 'and' in these checks, so we only generate 1 guard instead of two if (constants.MININT <= val) & (val <= constants.MAXINT): return self.wrap_smallint_unsafe(intmask(val)) elif (0 <= val) & (val <= r_longlong(constants.U_MAXINT)): return self.wrap_wordint_direct(r_uint(val), self.w_LargePositiveInteger) elif (0 > val) & (-r_longlong(constants.U_MAXINT) <= val): return self.wrap_wordint_direct(r_uint(-val), self.w_LargeNegativeInteger) else: return self.wrap_rbigint_direct(rbigint.rbigint.fromrarith_int(val)) elif isinstance(val, r_ulonglong): if val <= r_ulonglong(constants.MAXINT): return self.wrap_smallint_unsafe(intmask(val)) elif val <= constants.U_MAXINT: return self.wrap_wordint_direct(r_uint(val), self.w_LargePositiveInteger) else: return self.wrap_rbigint_direct(rbigint.rbigint.fromrarith_int(val)) else: raise WrappingError
def fake_call_impl_any(cif_description, func_addr, exchange_buffer): ofs = 16 for avalue in unroll_avalues: TYPE = rffi.CArray(lltype.typeOf(avalue)) data = rffi.ptradd(exchange_buffer, ofs) got = rffi.cast(lltype.Ptr(TYPE), data)[0] if lltype.typeOf(avalue) is lltype.SingleFloat: got = float(got) avalue = float(avalue) elif (lltype.typeOf(avalue) is rffi.SIGNEDCHAR or lltype.typeOf(avalue) is rffi.UCHAR): got = intmask(got) avalue = intmask(avalue) assert got == avalue ofs += 16 write_to_ofs = 0 if rvalue is not None: write_rvalue = rvalue if BIG_ENDIAN: if (lltype.typeOf(write_rvalue) is rffi.SIGNEDCHAR or lltype.typeOf(write_rvalue) is rffi.UCHAR): # 'write_rvalue' is an int type smaller than Signed write_to_ofs = rffi.sizeof(rffi.LONG) - 1 else: write_rvalue = 12923 # ignored TYPE = rffi.CArray(lltype.typeOf(write_rvalue)) data = rffi.ptradd(exchange_buffer, ofs) rffi.cast(lltype.Ptr(TYPE), data)[write_to_ofs] = write_rvalue
def _PyLong_FromByteArray(space, bytes, n, little_endian, signed): little_endian = rffi.cast(lltype.Signed, little_endian) signed = rffi.cast(lltype.Signed, signed) result = rbigint() negative = False for i in range(0, n): if little_endian: c = intmask(bytes[i]) else: c = intmask(bytes[n - i - 1]) if i == 0 and signed and c & 0x80: negative = True if negative: c = c ^ 0xFF digit = rbigint.fromint(c) result = result.lshift(8) result = result.add(digit) if negative: result = result.neg() return space.newlong_from_rbigint(result)
def strtol(s): """Returns (int_value_forced_in_bounds, flag_any_digit_processed)""" i = _whitespaces_in_front(s) negative_sign = False at_least_one_digit = False if nextchr(s, i) == '-': negative_sign = True i += 1 elif nextchr(s, i) == '+': i += 1 value_int = 0 value_float = 0.0 while nextchr(s, i).isdigit(): digit = ord(s[i]) - ord('0') value_int = intmask((value_int * 10) + digit) value_float = (value_float * 10.0) + digit if abs(value_int - value_float) < _OVERFLOWED: value_float = float(value_int) # force equal at_least_one_digit = True i += 1 if negative_sign: value_int = intmask(-value_int) value_float = -value_float if abs(float(value_int) - value_float) > _OVERFLOWED: if negative_sign: value_int = -sys.maxint-1 else: value_int = sys.maxint return value_int, at_least_one_digit
def do_poll(self, space, timeout): from pypy.module._multiprocessing.interp_win32 import ( _PeekNamedPipe, _GetTickCount, _Sleep) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if timeout == 0.0: return bytes > 0 block = timeout < 0 if not block: # XXX does not check for overflow deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5) else: deadline = 0 _Sleep(0) delay = 1 while True: bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if bytes > 0: return True if not block: now = intmask(_GetTickCount()) if now > deadline: return False diff = deadline - now if delay > diff: delay = diff else: delay += 1 if delay >= 20: delay = 20 _Sleep(delay)
def ll_dict_lookup(d, key, hash): entries = d.entries ENTRIES = lltype.typeOf(entries).TO direct_compare = not hasattr(ENTRIES, 'no_direct_compare') mask = len(entries) - 1 i = r_uint(hash & mask) # do the first try before any looping if entries.valid(i): checkingkey = entries[i].key if direct_compare and checkingkey == key: return i # found the entry if d.keyeq is not None and entries.hash(i) == hash: # correct hash, maybe the key is e.g. a different pointer to # an equal object found = d.keyeq(checkingkey, key) if d.paranoia: if (entries != d.entries or not entries.valid(i) or entries[i].key != checkingkey): # the compare did major nasty stuff to the dict: start over return ll_dict_lookup(d, key, hash) if found: return i # found the entry freeslot = -1 elif entries.everused(i): freeslot = intmask(i) else: return i | HIGHEST_BIT # pristine entry -- lookup failed # In the loop, a deleted entry (everused and not valid) is by far # (factor of 100s) the least likely outcome, so test for that last. perturb = r_uint(hash) while 1: # compute the next index using unsigned arithmetic i = (i << 2) + i + perturb + 1 i = i & mask # keep 'i' as a signed number here, to consistently pass signed # arguments to the small helper methods. if not entries.everused(i): if freeslot == -1: freeslot = intmask(i) return r_uint(freeslot) | HIGHEST_BIT elif entries.valid(i): checkingkey = entries[i].key if direct_compare and checkingkey == key: return i if d.keyeq is not None and entries.hash(i) == hash: # correct hash, maybe the key is e.g. a different pointer to # an equal object found = d.keyeq(checkingkey, key) if d.paranoia: if (entries != d.entries or not entries.valid(i) or entries[i].key != checkingkey): # the compare did major nasty stuff to the dict: # start over return ll_dict_lookup(d, key, hash) if found: return i # found the entry elif freeslot == -1: freeslot = intmask(i) perturb >>= PERTURB_SHIFT
def _int_binary_operations(): minint = -sys.maxint-1 # Test cases. Note that for each operation there should be at least # one case in which the two input arguments are equal. for opnum, testcases in [ (rop.INT_ADD, [(10, -2, 8), (-60, -60, -120)]), (rop.INT_SUB, [(10, -2, 12), (133, 133, 0)]), (rop.INT_MUL, [(-6, -3, 18), (15, 15, 225)]), (rop.INT_AND, [(0xFF00, 0x0FF0, 0x0F00), (-111, -111, -111)]), (rop.INT_OR, [(0xFF00, 0x0FF0, 0xFFF0), (-111, -111, -111)]), (rop.INT_XOR, [(0xFF00, 0x0FF0, 0xF0F0), (-111, -111, 0)]), (rop.INT_LSHIFT, [(10, 4, 10<<4), (-5, 2, -20), (-5, 0, -5), (3, 3, 24)]), (rop.INT_RSHIFT, [(-17, 2, -5), (19, 1, 9), (3, 3, 0)]), (rop.UINT_RSHIFT, [(-1, 4, intmask(r_uint(-1) >> r_uint(4))), ( 1, 4, intmask(r_uint(1) >> r_uint(4))), ( 3, 3, 0)]), (rop.UINT_MUL_HIGH, [(5, 6, 0), (0xffff, 0xffff, 0), (-1, -1, -2), (-1, 123, 122)]), ]: for x, y, z in testcases: yield opnum, [x, y], z
def compute_hash(self, space, x): from rpython.rlib.rarithmetic import intmask if self.fields is None and self.subdtype is None: endian = self.byteorder if endian == NPY.NATIVE: endian = NPY.NATBYTE flags = 0 y = 0x345678 for v in (ord(self.kind[0]), ord(endian[0]), flags, self.elsize, self.alignment): y = intmask((1000003 * y) ^ v) return intmask((1000003 * x) ^ y) if self.fields is not None: fields = self.fields.items() DescrFieldSort(fields).sort() for name, (offset, subdtype) in fields: assert isinstance(subdtype, W_Dtype) x = intmask((1000003 * x) ^ compute_hash(name)) x = subdtype.compute_hash(space, x) x = intmask((1000003 * x) ^ compute_hash(offset)) if self.subdtype is not None: for s in self.shape: x = intmask((1000003 * x) ^ compute_hash(s)) x = self.base.compute_hash(space, x) return x
def find_map_attr(self, name, index): # attr cache space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() # unrolled hash computation for 2-tuple c1 = 0x345678 c2 = 1000003 hash_name = objectmodel.compute_hash(name) hash_selector = intmask((c2 * ((c2 * c1) ^ hash_name)) ^ index) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_name = cache.names[attr_hash] cached_index = cache.indexes[attr_hash] if cached_name == name and cached_index == index: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(name, index) cache.attrs[attr_hash] = self cache.names[attr_hash] = name cache.indexes[attr_hash] = index cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def read(self, size=-1): # XXX CPython uses a more delicate logic here ll_file = self.ll_file if not ll_file: raise ValueError("I/O operation on closed file") if size < 0: # read the entire contents buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw') try: s = StringBuilder() while True: returned_size = c_fread(buf, 1, BASE_BUF_SIZE, ll_file) returned_size = intmask(returned_size) # is between 0 and BASE_BUF_SIZE if returned_size == 0: if c_feof(ll_file): # ok, finished return s.build() raise _error(ll_file) s.append_charpsize(buf, returned_size) finally: lltype.free(buf, flavor='raw') else: raw_buf, gc_buf = rffi.alloc_buffer(size) try: returned_size = c_fread(raw_buf, 1, size, ll_file) returned_size = intmask(returned_size) # is between 0 and size if returned_size == 0: if not c_feof(ll_file): raise _error(ll_file) s = rffi.str_from_buffer(raw_buf, gc_buf, size, returned_size) finally: rffi.keep_buffer_alive_until_here(raw_buf, gc_buf) return s
def time_time_llimpl(): void = lltype.nullptr(rffi.VOIDP.TO) result = -1.0 if self.HAVE_GETTIMEOFDAY: t = lltype.malloc(self.TIMEVAL, flavor='raw') errcode = -1 if self.GETTIMEOFDAY_NO_TZ: errcode = c_gettimeofday(t) else: errcode = c_gettimeofday(t, void) if rffi.cast(rffi.LONG, errcode) == 0: result = decode_timeval(t) lltype.free(t, flavor='raw') if result != -1: return result else: # assume using ftime(3) t = lltype.malloc(self.TIMEB, flavor='raw') c_ftime(t) result = (float(intmask(t.c_time)) + float(intmask(t.c_millitm)) * 0.001) lltype.free(t, flavor='raw') return result return float(c_time(void))
def f(d): va = lltype.malloc(T, d, flavor='raw', zero=True) vb = lltype.malloc(T, d, flavor='raw', zero=True) for j in range(d): va[j] = rffi.r_int(j) vb[j] = rffi.r_int(j) i = 0 while i < d: myjitdriver.jit_merge_point() if i < 0: raise IndexError if i >= d: raise IndexError a = va[i] if i < 0: raise IndexError if i >= d: raise IndexError b = vb[i] ec = intmask(a)+intmask(b) if i < 0: raise IndexError if i >= d: raise IndexError va[i] = rffi.r_int(ec) i += 1 lltype.free(va, flavor='raw') lltype.free(vb, flavor='raw') return 0
def read_interpreter_code_info(rdr): from pixie.vm.object import InterpreterCodeInfo line = read_obj(rdr) line_number = read_raw_integer(rdr) column_number = read_raw_integer(rdr) file = read_raw_string(rdr) return InterpreterCodeInfo(line, intmask(line_number), intmask(column_number), file)
def _hash_float(space, v): if not isfinite(v): if isinf(v): return HASH_INF if v > 0 else -HASH_INF return HASH_NAN m, e = math.frexp(v) sign = 1 if m < 0: sign = -1 m = -m # process 28 bits at a time; this should work well both for binary # and hexadecimal floating point. x = r_uint(0) while m: x = ((x << 28) & HASH_MODULUS) | x >> (HASH_BITS - 28) m *= 268435456.0 # 2**28 e -= 28 y = r_uint(m) # pull out integer part m -= y x += y if x >= HASH_MODULUS: x -= HASH_MODULUS # adjust for the exponent; first reduce it modulo HASH_BITS e = e % HASH_BITS if e >= 0 else HASH_BITS - 1 - ((-1 - e) % HASH_BITS) x = ((x << e) & HASH_MODULUS) | x >> (HASH_BITS - e) x = intmask(intmask(x) * sign) return -2 if x == -1 else x
def set_video_mode(self, w, h, d): if not (w > 0 and h > 0): return assert d in (1, 2, 4, 8, 16, 32) if d < MINIMUM_DEPTH: d = BELOW_MINIMUM_DEPTH self.width = intmask(w) self.height = intmask(h) self.depth = intmask(d) if self.window == lltype.nullptr(RSDL.WindowPtr.TO): self.create_window_and_renderer(x=RSDL.WINDOWPOS_UNDEFINED, y=RSDL.WINDOWPOS_UNDEFINED, width=w, height=h) if self.screen_texture != lltype.nullptr(RSDL.TexturePtr.TO): RSDL.DestroyTexture(self.screen_texture) self.screen_texture = RSDL.CreateTexture( self.renderer, DEPTH_TO_PIXELFORMAT[d], RSDL.TEXTUREACCESS_STREAMING, w, h) if not self.screen_texture: print 'Could not create screen texture' raise RuntimeError(RSDL.GetError()) self.lock() if d == 16: self.bpp = 2 elif d == 32: self.bpp = 4 else: assert False self.pitch = self.width * self.bpp self.full_damage()
def test_id(self): class A: pass def fn(): a1 = A() a2 = A() return ( compute_unique_id(a1), current_object_addr_as_int(a1), compute_unique_id(a2), current_object_addr_as_int(a2), ) res = self.interpret(fn, []) x0, x1, x2, x3 = self.ll_unpack_tuple(res, 4) assert isinstance(x0, (int, r_longlong)) assert isinstance(x1, int) assert isinstance(x2, (int, r_longlong)) assert isinstance(x3, int) assert x0 != x2 # the following checks are probably too precise, but work at # least on top of llinterp assert x1 == intmask(x0) assert x3 == intmask(x2)
def read(self, size=-1): # XXX CPython uses a more delicate logic here self._check_closed() ll_file = self._ll_file if size == 0: return "" elif size < 0: # read the entire contents buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw') try: s = StringBuilder() while True: returned_size = self._fread(buf, BASE_BUF_SIZE, ll_file) returned_size = intmask(returned_size) # is between 0 and BASE_BUF_SIZE if returned_size == 0: if c_feof(ll_file): # ok, finished return s.build() raise _error(ll_file) s.append_charpsize(buf, returned_size) finally: lltype.free(buf, flavor='raw') else: # size > 0 with rffi.scoped_alloc_buffer(size) as buf: returned_size = self._fread(buf.raw, size, ll_file) returned_size = intmask(returned_size) # is between 0 and size if returned_size == 0: if not c_feof(ll_file): raise _error(ll_file) s = buf.str(returned_size) assert s is not None return s
def func(no): m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE) f_size = os.fstat(no).st_size assert intmask(m.file_size()) == f_size == 6 m.resize(10) f_size = os.fstat(no).st_size assert intmask(m.file_size()) == f_size == 10 m.close()
def unbiased_rand(mn, mx): # migu way _range = mx - mn unbiased_range = _range + RANDMAX % _range rnd = intmask(_random.genrand32()) % unbiased_range while rnd >= _range: rnd = intmask(_random.genrand32()) % unbiased_range return rnd + mn
def test_truncate(self): def f(n): m = r_longlong(n) << 20 return r_uint(m) res = self.interp_operations(f, [0x01234567]) assert res == 0x56700000 res = self.interp_operations(f, [0x56789ABC]) assert intmask(res) == intmask(0xABC00000)
def test_WordsObject_short_at(): target = model.W_WordsObject(space, None, 2) target.setword(0, r_uint(0x00018000)) target.setword(1, r_uint(0x80010111)) assert target.short_at0(space, 0).value == intmask(0xffff8000) assert target.short_at0(space, 1).value == intmask(0x0001) assert target.short_at0(space, 2).value == intmask(0x0111) assert target.short_at0(space, 3).value == intmask(0xffff8001)
def float_unpack(Q, size): """Convert a 16-bit, 32-bit, or 64-bit integer created by float_pack into a Python float.""" if size == 8: MIN_EXP = -1021 # = sys.float_info.min_exp MAX_EXP = 1024 # = sys.float_info.max_exp MANT_DIG = 53 # = sys.float_info.mant_dig BITS = 64 elif size == 4: MIN_EXP = -125 # C's FLT_MIN_EXP MAX_EXP = 128 # FLT_MAX_EXP MANT_DIG = 24 # FLT_MANT_DIG BITS = 32 elif size == 2: MIN_EXP = -13 MAX_EXP = 16 MANT_DIG = 11 BITS = 16 else: raise ValueError("invalid size value") if not objectmodel.we_are_translated(): # This tests generates wrong code when translated: # with gcc, shifting a 64bit int by 64 bits does # not change the value. if Q >> BITS: raise ValueError("input '%r' out of range '%r'" % (Q, Q >> BITS)) # extract pieces with assumed 1.mant values one = r_ulonglong(1) sign = rarithmetic.intmask(Q >> BITS - 1) exp = rarithmetic.intmask((Q & ((one << BITS - 1) - (one << MANT_DIG - 1))) >> MANT_DIG - 1) mant = Q & ((one << MANT_DIG - 1) - 1) if exp == MAX_EXP - MIN_EXP + 2: # nan or infinity if mant == 0: result = rfloat.INFINITY else: # preserve at most 52 bits of mant value, but pad w/zeros exp = r_ulonglong(0x7FF) << 52 sign = r_ulonglong(sign) << 63 if MANT_DIG < 53: mant = r_ulonglong(mant) << (53 - MANT_DIG) if mant == 0: result = rfloat.NAN else: uint = exp | mant | sign result = longlong2float(cast(LONGLONG, uint)) return result elif exp == 0: # subnormal or zero result = math.ldexp(mant, MIN_EXP - MANT_DIG) else: # normal: add implicit one value mant += one << MANT_DIG - 1 result = math.ldexp(mant, exp + MIN_EXP - MANT_DIG - 1) return -result if sign else result
def g(d, va, vb): i = 0 while i < d: myjitdriver.jit_merge_point() a = va[i] b = vb[i] ec = intmask(intmask(a) + intmask(b)) va[i] = rffi.r_short(ec) i += 1
def fetch(self, space, n0): from rpython.rlib.rstruct.ieee import float_pack r = float_pack(self.value, 8) # C double if n0 == 0: return space.wrap_uint(r_uint(intmask(r >> 32))) else: # bounds-check for primitive access is done in the primitive assert n0 == 1 return space.wrap_uint(r_uint(intmask(r)))
def unwrap_int(self, w_value): if isinstance(w_value, model.W_SmallInteger): return intmask(w_value.value) elif isinstance(w_value, model.W_LargePositiveInteger1Word): if w_value.value >= 0: return intmask(w_value.value) else: raise UnwrappingError("The value is negative when interpreted as 32bit value.") raise UnwrappingError("expected a W_SmallInteger or W_LargePositiveInteger1Word, got %s" % (w_value,))
def matching_result(res, rvalue): if rvalue is None: return res == 654321 if isinstance(rvalue, r_singlefloat): rvalue = float(rvalue) if lltype.typeOf(rvalue) is rffi.ULONG: res = intmask(res) rvalue = intmask(rvalue) return res == rvalue
def lshift(self, space, shift): # shift > 0, therefore the highest bit of upperbound is not set, # i.e. upperbound is positive upperbound = intmask(r_uint(-1) >> shift) if 0 <= self.value <= upperbound: shifted = intmask(self.value << shift) return space.wrap_positive_32bit_int(shifted) else: raise error.PrimitiveFailedError()
def get_state(self): return ContextState.states[intmask((self._get_state_stackptr_pc() & ~state_mask) >> state_shift)]
def stack_ptr(self): return intmask((self._get_state_stackptr_pc() & ~stackptr_mask) >> stackptr_shift)
def _count(self): return rt.wrap(intmask(self.count()))
def test_translated(): d1 = {"foo": 123} d2 = {u"foo": 456, u"\u1234\u5678": 789} class G: pass g = G() g.v1 = d1.copy() g.v2 = d2.copy() def fetch(n): if n == 0: return d1.get("foo", -1) if n == 1: return g.v1.get("foo", -1) if n == 2: return compute_hash("foo") if n == 3: return d2.get(u"foo", -1) if n == 4: return g.v2.get(u"foo", -1) if n == 5: return compute_hash(u"foo") if n == 6: return d2.get(u"\u1234\u5678", -1) if n == 7: return g.v2.get(u"\u1234\u5678", -1) if n == 8: return compute_hash(u"\u1234\u5678") assert 0 def entrypoint(n): enable_siphash24() g.v1["bar"] = -2 g.v2[u"bar"] = -2 if n >= 0: # get items one by one, because otherwise it may # be the case that one line influences the next return str(fetch(n)) else: # ...except in random mode, because we want all results # to be computed with the same seed return ' '.join([str(fetch(n)) for n in range(9)]) fn = compile(entrypoint, [int]) def getall(): return [int(fn(i)) for i in range(9)] old_val = os.environ.get('PYTHONHASHSEED', None) try: os.environ['PYTHONHASHSEED'] = '0' s1 = getall() assert s1[:8] == [ 123, 123, intmask(15988776847138518036), 456, 456, intmask(15988776847138518036), 789, 789] assert s1[8] in [intmask(17593683438421985039), # ucs2 mode little endian intmask(94801584261658677), # ucs4 mode little endian intmask(3849431280840015342),] # ucs4 mode big endian os.environ['PYTHONHASHSEED'] = '3987654321' s1 = getall() assert s1[:8] == [ 123, 123, intmask(5890804383681474441), 456, 456, intmask(5890804383681474441), 789, 789] assert s1[8] in [intmask(4192582507672183374), # ucs2 mode little endian intmask(7179255293164649778), # ucs4 mode little endian intmask(-3945781295304514711),] # ucs4 mode big endian for env in ['', 'random']: os.environ['PYTHONHASHSEED'] = env s1 = map(int, fn(-1).split()) s2 = map(int, fn(-1).split()) assert s1[0:2]+s1[3:5]+s1[6:8] == [123, 123, 456, 456, 789, 789] assert s1[2] == s1[5] assert s2[0:2]+s2[3:5]+s2[6:8] == [123, 123, 456, 456, 789, 789] assert s2[2] == s2[5] # assert len(set([s1[2], s2[2], s1[8], s2[8]])) == 4 finally: if old_val is None: del os.environ['PYTHONHASHSEED'] else: os.environ['PYTHONHASHSEED'] = old_val
def get_position(self): # p is the signed 32-bit position, from self.position_and_flags p = rffi.cast(rffi.INT, self.position_and_flags) return intmask(p) >> FO_POSITION_SHIFT
def positive32BitValueOf(n): from rpython.rlib.rarithmetic import intmask return intmask(IProxy.space.unwrap_positive_32bit_int(n))
def is_int32_from_longlong_nan(value): return intmask(value >> 32) == nan_high_word_int32
if i + 1 == length: raise oefmt(space.w_ValueError, NON_HEX_MSG, i) top = _hex_digit_to_int(s[i]) if top == -1: raise oefmt(space.w_ValueError, NON_HEX_MSG, i) bot = _hex_digit_to_int(s[i + 1]) if bot == -1: raise oefmt(space.w_ValueError, NON_HEX_MSG, i + 1) data.append(chr(top * 16 + bot)) i += 2 return data HEXDIGITS = "0123456789abcdef" PY_SIZE_T_MAX = intmask(2**(rffi.sizeof(rffi.SIZE_T) * 8 - 1) - 1) @specialize.arg(5) # raw access def _array_to_hexstring(space, buf, start, step, length, rawaccess=False): hexstring = StringBuilder(length * 2) if length > PY_SIZE_T_MAX / 2: raise OperationError(space.w_MemoryError, space.w_None) stepped = 0 i = start while stepped < length: if rawaccess: byte = ord(buf[i]) else:
def seek(self, offset, whence): # This may fail on the do_seek() or on the tell() call. # But it won't depend on either on a relative forward seek. # Nor on a seek to the very end. if whence == 0 or whence == 1: if whence == 0: difpos = offset - self.tell() # may clean up self.buf/self.pos else: difpos = offset currentsize = len(self.buf) - self.pos if -self.pos <= difpos <= currentsize: self.pos += intmask(difpos) return if whence == 1: offset -= currentsize try: self.do_seek(offset, whence) except MyNotImplementedError: self.buf = "" self.pos = 0 if difpos < 0: raise if whence == 0: offset = difpos - currentsize intoffset = offset2int(offset) self.read(intoffset) else: self.buf = "" self.pos = 0 return if whence == 2: try: self.do_seek(offset, 2) except MyNotImplementedError: pass else: self.pos = 0 self.buf = "" return # Skip relative to EOF by reading and saving only just as # much as needed intoffset = offset2int(offset) pos = self.pos assert pos >= 0 buffers = [self.buf[pos:]] total = len(buffers[0]) self.buf = "" self.pos = 0 while 1: data = self.do_read(self.bufsize) if not data: break buffers.append(data) total += len(data) while buffers and total >= len(buffers[0]) - intoffset: total -= len(buffers[0]) del buffers[0] cutoff = total + intoffset if cutoff < 0: raise StreamError("cannot seek back") if buffers: assert cutoff >= 0 buffers[0] = buffers[0][cutoff:] self.buf = "".join(buffers) return raise StreamError("whence should be 0, 1 or 2")
def pc(self): return intmask((self._get_state_stackptr_pc() & ~pc_mask) >> pc_shift)
def buffer_capacity(buffer): return rt.wrap(intmask(buffer.capacity()))
def _count(self): assert isinstance(self, PersistentHashMap) return rt.wrap(intmask(self._cnt))
def descr_hash(self, space): hashreal = _hash_float(space, self.realval) hashimg = _hash_float(space, self.imagval) # 0 if self.imagval == 0 h = intmask(hashreal + 1000003 * hashimg) h -= (h == -1) return space.newint(h)
def _ll_1_ullong_from_int(x): return r_ulonglong(intmask(x))
def _ll_1_llong_to_int(xll): return intmask(xll)
def unsigned_to_signed_32bit(x): return intmask(rffi.cast(rffi.INT, x))
def _contains_key(self, key): assert isinstance(self, PersistentVector) if not isinstance(key, Integer): return false else: return true if key.int_val() >= 0 and key.int_val() < intmask(self._cnt) else false
def time_now(self): import time from rpython.rlib.rarithmetic import intmask return intmask((int(time.time() * 1000) - self.startup_time))
def hash_equal(self, info=None): x = 0x567890 for i in range(self.len): hash = self.ref(i).hash_equal(info=info) x = intmask((1000003 * x) ^ hash) return x
import sys from pypy.interpreter.gateway import interp2app, unwrap_spec from pypy.interpreter.baseobjspace import W_Root from pypy.interpreter.typedef import TypeDef, interp_attrproperty from pypy.interpreter.error import OperationError, oefmt from rpython.rlib.rarithmetic import intmask, r_uint, r_uint32 from rpython.rlib.objectmodel import keepalive_until_here from rpython.rtyper.lltypesystem import rffi from rpython.rlib import rzlib if intmask(2**31) == -2**31: # 32-bit platforms unsigned_to_signed_32bit = intmask else: # 64-bit platforms def unsigned_to_signed_32bit(x): return intmask(rffi.cast(rffi.INT, x)) @unwrap_spec(string='bufferstr', start='truncatedint_w') def crc32(space, string, start = rzlib.CRC32_DEFAULT_START): """ crc32(string[, start]) -- Compute a CRC-32 checksum of string. An optional starting value can be specified. The returned checksum is an integer. """ ustart = r_uint(r_uint32(start)) checksum = rzlib.crc32(string, ustart)
def _count(self): assert isinstance(self, TransientVector) return rt.wrap(intmask(self._cnt))
def high_part(self): return intmask(self.aslonglong >> 32)
def can_encode_float(value): return intmask(float2longlong(value) >> 32) != nan_high_word_int32
def check_latin1(s, expected, test_prebuilt=False): with choosen_seed(0x8a9f065a358479f4, 0x11cb1e9ee7f40e1f, test_misaligned_path=True, test_prebuilt=test_prebuilt): z = ll_hash_string_siphash24(llunicode(s)) assert z == intmask(expected)
def _hash(self): assert isinstance(self, Symbol) if self._hash == 0: self._hash = util.hash_unencoded_chars(self._str) return rt.wrap(intmask(self._hash))
def low_part(self): return intmask(self.aslonglong)
def get_rpy_type_index(gcref): from rpython.rlib.rarithmetic import intmask Class = gcref._x.__class__ return intmask(id(Class))
def size_alignment(ffi_type): return intmask(ffi_type.c_size), intmask(ffi_type.c_alignment)
has_clock_gettime = platform.Has('clock_gettime') CLOCK_PROF = platform.DefinedConstantInteger('CLOCK_PROF') @@ -224,7 +224,6 @@ if _POSIX: CLOCKS_PER_SEC = cConfig.CLOCKS_PER_SEC HAS_CLOCK_GETTIME = cConfig.has_clock_gettime -clock_t = cConfig.clock_t tm = cConfig.tm glob_buf = lltype.malloc(tm, flavor='raw', zero=True, immortal=True) @@ -966,7 +965,9 @@ else: with lltype.scoped_alloc(rposix.TMS) as tms: ret = rposix.c_times(tms) if rffi.cast(lltype.Signed, ret) != -1: - cpu_time = float(tms.c_tms_utime + tms.c_tms_stime) + c_tms_utime = rffi.cast(lltype.Signed, tms.c_tms_utime) + c_tms_stime = rffi.cast(lltype.Signed, tms.c_tms_stime) + cpu_time = float(c_tms_utime + c_tms_stime) if w_info is not None: _setinfo(space, w_info, "times()", 1.0 / rposix.CLOCK_TICKS_PER_SECOND, @@ -993,7 +994,7 @@ else: records.""" value = _clock() # Is this casting correct? - if value == rffi.cast(clock_t, -1): + if intmask(value) == intmask(rffi.cast(clock_t, -1)): raise oefmt(space.w_RuntimeError, "the processor time used is not available or its value" "cannot be represented")
class LLOrderedDict(object): INIT_SIZE = 8 HIGHEST_BIT = intmask(1 << (LONG_BIT - 1)) MASK = intmask(HIGHEST_BIT - 1) PERTURB_SHIFT = 5 @staticmethod def ll_valid_from_flag(entries, i): return entries[i].valid @staticmethod def ll_everused_from_flag(entries, i): return entries[i].everused @staticmethod def ll_mark_deleted_in_flag(entries, i): entries[i].valid = False @staticmethod def ll_hashkey_custom(d, key): DICT = lltype.typeOf(d).TO return hlinvoke(DICT.r_hashkey, d.hashkey_func, key) @staticmethod def ll_keyeq_custom(d, key1, key2): DICT = lltype.typeOf(d).TO return hlinvoke(DICT.r_keyeq, d.keyeq_func, key1, key2) @staticmethod def ll_hash_recompute(entries, i): ENTRIES = lltype.typeOf(entries).TO return ENTRIES.fast_hash_func(entries[i].key) @staticmethod def ll_hash_from_cache(entries, i): return entries[i].hash @staticmethod def recast(P, v): if isinstance(P, lltype.Ptr): return lltype.cast_pointer(P, v) else: return v @staticmethod def ll_newdict(DICT): d = lltype.malloc(DICT) d.entries = lltype.malloc(DICT.entries.TO, LLOrderedDict.INIT_SIZE, zero=True) d.num_items = 0 d.first_entry = -1 d.last_entry = -1 d.resize_counter = LLOrderedDict.INIT_SIZE * 2 return d @staticmethod def ll_len(d): return d.num_items @staticmethod @jit.look_inside_iff(lambda d, key, hash: jit.isvirtual(d) and jit.isconstant(key)) def ll_lookup(d, key, hash): entries = d.entries mask = len(entries) - 1 i = hash & mask if entries.valid(i): checkingkey = entries[i].key if checkingkey == key: return i if d.keyeq is not None and entries.hash(i) == hash: found = d.keyeq(checkingkey, key) if d.paranoia: if (entries != d.entries or not entries.valid(i) or entries[i].key != checkingkey): return LLOrderedDict.ll_lookup(d, key, hash) if found: return i freeslot = -1 elif entries.everused(i): freeslot = i else: return i | LLOrderedDict.HIGHEST_BIT perturb = r_uint(hash) while True: i = r_uint(i) i = (i << 2) + i + perturb + 1 i = intmask(i) & mask if not entries.everused(i): if freeslot == -1: freeslot = i return freeslot | LLOrderedDict.HIGHEST_BIT elif entries.valid(i): checkingkey = entries[i].key if checkingkey == key: return i if d.keyeq is not None and entries.hash(i) == hash: found = d.keyeq(checkingkey, key) if d.paranoia: if (entries != d.entries or not entries.valid(i) or entries[i].key != checkingkey): return LLOrderedDict.ll_lookup(d, key, hash) if found: return i elif freeslot == -1: freeslot = i perturb >>= LLOrderedDict.PERTURB_SHIFT @staticmethod def ll_setitem(d, key, value): hash = d.hashkey(key) i = LLOrderedDict.ll_lookup(d, key, hash) LLOrderedDict.ll_setitem_lookup_done(d, key, value, hash, i) @staticmethod def ll_setitem_lookup_done(d, key, value, hash, i): valid = (i & LLOrderedDict.HIGHEST_BIT) == 0 i &= LLOrderedDict.MASK everused = d.entries.everused(i) ENTRY = lltype.typeOf(d.entries).TO.OF entry = d.entries[i] entry.value = value if valid: return entry.key = key if hasattr(ENTRY, "hash"): entry.hash = hash if hasattr(ENTRY, "valid"): entry.valid = True d.num_items += 1 if d.first_entry == -1: d.first_entry = i else: d.entries[d.last_entry].next = i entry.prev = d.last_entry d.last_entry = i entry.next = -1 if not everused: if hasattr(ENTRY, "everused"): entry.everused = True d.resize_counter -= 3 if d.resize_counter <= 0: LLOrderedDict.ll_resize(d) @staticmethod def ll_getitem(d, key): i = LLOrderedDict.ll_lookup(d, key, d.hashkey(key)) if not i & LLOrderedDict.HIGHEST_BIT: return d.entries[i].value else: raise KeyError @staticmethod def ll_delitem(d, key): i = LLOrderedDict.ll_lookup(d, key, d.hashkey(key)) if i & LLOrderedDict.HIGHEST_BIT: raise KeyError LLOrderedDict._ll_del(d, i) @staticmethod def _ll_del(d, i): d.entries.mark_deleted(i) d.num_items -= 1 entry = d.entries[i] if entry.prev == -1: d.first_entry = entry.next else: d.entries[entry.prev].next = entry.next if entry.next == -1: d.last_entry = entry.prev else: d.entries[entry.next].prev = entry.prev ENTRIES = lltype.typeOf(d.entries).TO ENTRY = ENTRIES.OF if ENTRIES.must_clear_key: entry.key = lltype.nullptr(ENTRY.key.TO) if ENTRIES.must_clear_value: entry.value = lltype.nullptr(ENTRY.value.TO) @staticmethod def ll_contains(d, key): i = LLOrderedDict.ll_lookup(d, key, d.hashkey(key)) return not bool(i & LLOrderedDict.HIGHEST_BIT) @staticmethod def ll_resize(d): old_entries = d.entries if d.num_items > 50000: new_estimate = d.num_items * 2 else: new_estimate = d.num_items * 4 new_size = LLOrderedDict.INIT_SIZE while new_size <= new_estimate: new_size *= 2 d.entries = lltype.malloc(lltype.typeOf(old_entries).TO, new_size, zero=True) d.num_items = 0 d.resize_counter = new_size * 2 i = d.first_entry d.first_entry = -1 d.last_entry = -1 while i != -1: hash = old_entries.hash(i) entry = old_entries[i] LLOrderedDict.ll_insert_clean(d, entry.key, entry.value, hash) i = entry.next @staticmethod def ll_insert_clean(d, key, value, hash): i = LLOrderedDict.ll_lookup_clean(d, hash) ENTRY = lltype.typeOf(d.entries).TO.OF entry = d.entries[i] entry.value = value entry.key = key if hasattr(ENTRY, "hash"): entry.hash = hash if hasattr(ENTRY, "valid"): entry.valid = True if hasattr(ENTRY, "everused"): entry.everused = True d.num_items += 1 if d.first_entry == -1: d.first_entry = i else: d.entries[d.last_entry].next = i entry.prev = d.last_entry d.last_entry = i entry.next = -1 d.resize_counter -= 3 @staticmethod def ll_lookup_clean(d, hash): entries = d.entries mask = len(entries) - 1 i = hash & mask perturb = r_uint(hash) while entries.everused(i): i = r_uint(i) i = (i << 2) + i + perturb + 1 i = intmask(i) & mask perturb >>= LLOrderedDict.PERTURB_SHIFT return i @staticmethod def ll_keys(LIST, d): res = LIST.ll_newlist(d.num_items) ELEM = lltype.typeOf(res.ll_items()).TO.OF i = 0 idx = d.first_entry while idx != -1: res.ll_items()[i] = LLOrderedDict.recast(ELEM, d.entries[idx].key) idx = d.entries[idx].next i += 1 return res @staticmethod def ll_values(LIST, d): res = LIST.ll_newlist(d.num_items) ELEM = lltype.typeOf(res.ll_items()).TO.OF i = 0 idx = d.first_entry while idx != -1: res.ll_items()[i] = LLOrderedDict.recast(ELEM, d.entries[idx].value) idx = d.entries[idx].next i += 1 return res @staticmethod def ll_get(d, key, default): i = LLOrderedDict.ll_lookup(d, key, d.hashkey(key)) if not i & LLOrderedDict.HIGHEST_BIT: return d.entries[i].value else: return default @staticmethod def ll_pop(d, key): i = LLOrderedDict.ll_lookup(d, key, d.hashkey(key)) if not i & LLOrderedDict.HIGHEST_BIT: value = d.entries[i].value LLOrderedDict._ll_del(d, i) return value else: raise KeyError @staticmethod def ll_pop_default(d, key, default): try: return LLOrderedDict.ll_pop(d, key) except KeyError: return default @staticmethod def ll_popitem(RESTYPE, d): if not d.num_items: raise KeyError entry = d.entries[d.first_entry] r = lltype.malloc(RESTYPE.TO) r.item0 = LLOrderedDict.recast(RESTYPE.TO.item0, entry.key) r.item1 = LLOrderedDict.recast(RESTYPE.TO.item1, entry.value) LLOrderedDict._ll_del(d, d.first_entry) return r @staticmethod def ll_update(d, other): idx = other.first_entry while idx != -1: entry = other.entries[idx] i = LLOrderedDict.ll_lookup(d, entry.key, other.entries.hash(idx)) LLOrderedDict.ll_setitem_lookup_done(d, entry.key, entry.value, other.entries.hash(idx), i) idx = entry.next @staticmethod def ll_clear(d): if d.num_items == 0: return d.entries = lltype.malloc(lltype.typeOf(d.entries).TO, LLOrderedDict.INIT_SIZE, zero=True) d.num_items = 0 d.first_entry = -1 d.last_entry = -1 d.resize_counter = LLOrderedDict.INIT_SIZE * 2 @staticmethod def ll_copy(d): DICT = lltype.typeOf(d).TO new_d = lltype.malloc(DICT) new_d.entries = lltype.malloc(DICT.entries.TO, len(d.entries), zero=True) new_d.num_items = d.num_items new_d.resize_counter = d.resize_counter new_d.first_entry = d.first_entry new_d.last_entry = d.last_entry if hasattr(DICT, "hashkey_func"): new_d.hashkey_func = d.hashkey_func if hasattr(DICT, "keyeq_func"): new_d.keyeq_func = d.keyeq_func for i in xrange(len(d.entries)): entry = d.entries[i] new_entry = new_d.entries[i] new_entry.key = entry.key new_entry.value = entry.value new_entry.next = entry.next new_entry.prev = entry.prev new_entry.everused = entry.everused new_entry.valid = entry.valid if hasattr(DICT.entries.TO.OF, "hash"): new_entry.hash = entry.hash return new_d @staticmethod def ll_newdictiter(ITER, d): it = lltype.malloc(ITER) it.d = d it.index = d.first_entry return it @staticmethod def ll_dictiternext(RESTYPE, it): if it.index == -1: raise StopIteration r = lltype.malloc(RESTYPE.TO) entry = it.d.entries[it.index] r.item0 = LLOrderedDict.recast(RESTYPE.TO.item0, entry.key) r.item1 = LLOrderedDict.recast(RESTYPE.TO.item1, entry.value) it.index = entry.next return r