def rbigint_example(): r1 = rbigint.fromint((1 << 63 - 1)) r2 = rbigint.fromint(1 << 32) print r1.add(r2).str() print r1.sub(r2).str() print r1.mul(r2).str() print r1.div(r2).str() print r1.digit(1) # Return the x'th digit, as an int r2.setdigit(1, 5) r3 = rbigint.fromstr("1234567890000") print r3.str() r4 = rbigint.fromstr("1234567890000", base=16) print r4.str() print r3.lt(r4) # less than
def _string_to_w_long(space, w_longtype, w_source, string, base=10): try: bigint = rbigint.fromstr(string, base) except ParseStringError as e: from pypy.objspace.std.intobject import wrap_parsestringerror raise wrap_parsestringerror(space, e, w_source) return newbigint(space, w_longtype, bigint)
def parse_int(m): sign = 1 if m.group(1) == u'-': sign = -1 radix = 10 if m.group(7): num = m.group(7) elif m.group(2): radix = 16 num = m.group(3) elif m.group(4): radix = 8 num = m.group(4) elif m.group(5): radix = int(m.group(5)) num = m.group(6) else: return None if m.group(8): return rt.wrap(rbigint.fromstr(str(m.group(1) + num), radix)) else: return rt.wrap(sign * int(str(num), radix))
def random_bigint(max_size): from rpython.rlib.rbigint import rbigint import random size = random.randrange(1, max_size) sign = random.choice([-1, 1]) digits = "".join([random.choice("0123456789") for _ in range(size)]) bignum = rbigint.fromstr(digits, base=10) return bignum.int_mul(sign)
def atom(token): "Numbers become numbers; every other token is a symbol." try: return rbigint.fromstr(token).toint() except ParseStringError: try: return float(token) except ValueError: return Symbol(token)
def fromBytes(self, bs, ej): # Ruby-style underscores are legal here but can't be handled by # RPython, so remove them. bs = ''.join([c for c in bs if c != '_']) try: return rbigint.fromstr(bs, self.radix) except ParseStringError: throwStr(ej, u"_makeInt: Couldn't make int in radix %d from %s" % (self.radix, bytesToString(bs)))
def test_fromstr(self): from rpython.rlib.rstring import ParseStringError assert rbigint.fromstr('123L').tolong() == 123 assert rbigint.fromstr('123L ').tolong() == 123 py.test.raises(ParseStringError, rbigint.fromstr, 'L') py.test.raises(ParseStringError, rbigint.fromstr, 'L ') assert rbigint.fromstr('123L', 4).tolong() == 27 assert rbigint.fromstr('123L', 30).tolong() == 27000 + 1800 + 90 + 21 assert rbigint.fromstr('123L', 22).tolong() == 10648 + 968 + 66 + 21 assert rbigint.fromstr('123L', 21).tolong() == 441 + 42 + 3 assert rbigint.fromstr('1891234174197319').tolong() == 1891234174197319
def exact_from_node(self, node, radix): s = node.token.source i = 0 # Skip prefixes while s[i] == "#": i += 2 s = s[i:] src_pos = self.make_src_pos(node) try: return kt.Fixnum(string_to_int(s, radix), src_pos) except rstring.ParseStringOverflowError: if radix == 10: bi = rbigint.fromdecimalstr(s) else: bi = rbigint.fromstr(s, radix) return kt.Bignum(bi, src_pos)
def _literal_integer(self, negate_value): try: i = int(self._text) if negate_value: i = 0 - i result = self._universe.new_integer(i) except ParseStringOverflowError: bigint = rbigint.fromstr(self._text) if negate_value: bigint.sign = -1 result = self._universe.new_biginteger(bigint) except ValueError: raise ParseError("Could not parse integer. " "Expected a number but got '%s'" % self._text, Symbol.NONE, self) self._expect(Symbol.Integer) return result
def match_number(s): m = int_pat.match(s) if m: if m.group(2) is not None: if m.group(8) is not None: return bigint_zero return int_zero sign = -1 if m.group(1) == '-' else 1 radix = 10 n = m.group(3) if n is not None: radix = 10 if n is None: n = m.group(4) if n is not None: radix = 16 if n is None: n = m.group(5) if n is not None: radix = 8 if n is None: n = m.group(7) if n is not None: radix = int(m.group(6)) if n is None: return None bn = rbigint.fromstr( n, radix) # throws rpython.rlib.rstring.InvalidBaseError bn.sign = sign if m.group(8) is not None: return wrap_bigint(bn) elif bn.bit_length() < LONG_BIT: return wrap_int(bn.toint()) else: return wrap_bigint(bn) else: assert False, "TODO: implement other number types" return None
def match_number(s): m = int_pat.match(s) if m: if m.group(2) is not None: if m.group(8) is not None: return bigint_zero return int_zero sign = -1 if m.group(1) == '-' else 1 radix = 10 n = m.group(3) if n is not None: radix = 10 if n is None: n = m.group(4) if n is not None: radix = 16 if n is None: n = m.group(5) if n is not None: radix = 8 if n is None: n = m.group(7) if n is not None: radix = int(m.group(6)) if n is None: return None bn = rbigint.fromstr(n, radix) # throws rpython.rlib.rstring.InvalidBaseError bn.sign = sign if m.group(8) is not None: return wrap_bigint(bn) elif bn.bit_length() < LONG_BIT: return wrap_int(bn.toint()) else: return wrap_bigint(bn) else: assert False, "TODO: implement other number types" return None
def _string_to_w_long(space, w_longtype, w_source, string, base=10): try: bigint = rbigint.fromstr(string, base) except ParseStringError as e: raise wrap_parsestringerror(space, e, w_source) return newbigint(space, w_longtype, bigint)
def fromstr(s): return rbigint.fromstr(s)
def _lex(tokens): """Given a Python list of unicodes that look roughly like tokens, lex them. Returns a Trifle exception if they aren't valid tokens. """ lexed_tokens = [] for token in tokens: found_match = False for lexeme_name, regexp in LEXEMES: match = rsre_core.match(regexp, token) if match: found_match = True if lexeme_name == OPEN_PAREN: lexed_tokens.append(OpenParen()) elif lexeme_name == CLOSE_PAREN: lexed_tokens.append(CloseParen()) elif lexeme_name == OPEN_CURLY_PAREN: lexed_tokens.append(OpenCurlyParen()) elif lexeme_name == CLOSE_CURLY_PAREN: lexed_tokens.append(CloseCurlyParen()) elif lexeme_name == BOOLEAN: if token == u'#true': lexed_tokens.append(TRUE) else: lexed_tokens.append(FALSE) elif lexeme_name == NULL_TYPE: lexed_tokens.append(NULL) elif lexeme_name == INTEGER: integer_string = remove_char(token, "_") # TODO: validate that the integer string is only numbers lexed_tokens.append(Integer.fromstr(integer_string)) elif lexeme_name == FLOAT: float_string = remove_char(token, "_") try: lexed_tokens.append(Float(float(float_string))) except ValueError: return TrifleExceptionInstance( lex_failed, u"Invalid float: '%s'" % token) elif lexeme_name == FRACTION: fraction_string = remove_char(token, "_") fraction_parts = fraction_string.split('/') numerator = fraction_parts[0] denominator = fraction_parts[1] # TODO: validate that the fraction string is only numbers numerator = RBigInt.fromstr(numerator) denominator = RBigInt.fromstr(denominator) if denominator.eq(RBigInt.fromint(0)): return TrifleExceptionInstance( division_by_zero, u"Can't have fraction denominator of zero: '%s'" % token) fraction = Fraction(numerator, denominator) if fraction.denominator.eq(RBigInt.fromint(1)): lexed_tokens.append(Integer(fraction.numerator)) else: lexed_tokens.append(fraction) elif lexeme_name == SYMBOL: lexed_tokens.append(Symbol(token)) elif lexeme_name == KEYWORD: # todoc lexed_tokens.append(Keyword(token[1:])) elif lexeme_name == BYTESTRING: string_end = match.match_end - 2 # This is always true, but RPython doesn't support # negative indexes on slices and can't prove the # slice is non-negative. if string_end >= 0: contents = token[8:string_end] else: # Unreachable. contents = u"" lexed_tokens.append(Bytestring(unescape_bytestring_chars(contents))) elif lexeme_name == STRING: string_end = match.match_end - 1 # This is always true, but RPython doesn't support # negative indexes on slices and can't prove the # slice is non-negative. if string_end >= 0: string_contents = token[1:string_end] lexed_tokens.append(String(unescape_chars(string_contents, u'"'))) elif lexeme_name == CHARACTER: # TODO: use unescape_chars if token == u"'\\n'": lexed_tokens.append(Character(u'\n')) elif token == u"'\\\\'": lexed_tokens.append(Character(u'\\')) elif token == u"'\\''": lexed_tokens.append(Character(u"'")) else: lexed_tokens.append(Character(token[1])) else: assert False, u"Unrecognised token '%s'" % token break if not found_match: return TrifleExceptionInstance( lex_failed, u"Could not lex token: '%s'" % token) return List(lexed_tokens)
def test_fromstr_hypothesis(self, l): assert rbigint.fromstr(str(l)).tolong() == l
def read_obj(rdr): tag = read_tag(rdr) if tag == INT: return Integer(intmask(read_raw_integer(rdr))) elif tag == BIGINT: return BigInteger(read_raw_bigint(rdr)) elif tag == CODE: return read_code(rdr) elif tag == NIL: return nil elif tag == VAR: return read_var(rdr) elif tag == STRING: return String(read_raw_string(rdr)) elif tag == KEYWORD: return keyword(read_raw_string(rdr)) elif tag == SYMBOL: return symbol(read_raw_string(rdr)) elif tag == LINE_PROMISE: lp = LinePromise() lp._chrs = None lp._str = read_raw_string(rdr) return lp elif tag == MAP: return read_map(rdr) elif tag == TRUE: return true elif tag == FALSE: return false elif tag == NIL: return nil elif tag == VECTOR: return read_vector(rdr) elif tag == SEQ: return read_seq(rdr) elif tag == FLOAT: return read_float(rdr) elif tag == NAMESPACE: return read_namespace(rdr) elif tag == INT_STRING: return Integer(int(read_raw_string(rdr))) elif tag == BIGINT_STRING: return BigInteger(rbigint.fromstr(str(read_raw_string(rdr)))) elif tag == NEW_CACHED_OBJ: return rdr.read_and_cache() elif tag == CACHED_OBJ: return rdr.read_cached_obj() elif tag == EOF: from pixie.vm.reader import eof return eof elif tag == CODE_INFO: return read_interpreter_code_info(rdr) elif tag == TAGGED: tp_name = read_raw_string(rdr) tp = get_type_by_name(tp_name) handler = read_handlers.get(tp, None) if handler is None: runtime_error(u"No type handler for " + tp_name) obj = read_obj(rdr) return handler.invoke([obj]) else: runtime_error(u"No dispatch for bytecode: " + unicode(tag_name[tag])) return nil
def number(p): number_str = filtered_str(p[0].getstr()) number_int = rbigint.fromstr(number_str) return datatype.Number(number_int)
def entry_point(argv): """ All benchmarks are run using --opt=2 and minimark gc (default). Benchmark changes: 2**N is a VERY heavy operation in default pypy, default to 10 million instead of 500,000 used like an hour to finish. A cutout with some benchmarks. Pypy default: mod by 2: 7.978181 mod by 10000: 4.016121 mod by 1024 (power of two): 3.966439 Div huge number by 2**128: 2.906821 rshift: 2.444589 lshift: 2.500746 Floordiv by 2: 4.431134 Floordiv by 3 (not power of two): 4.404396 2**500000: 23.206724 (2**N)**5000000 (power of two): 13.886118 10000 ** BIGNUM % 100 8.464378 i = i * i: 10.121505 n**10000 (not power of two): 16.296989 Power of two ** power of two: 2.224125 v = v * power of two 12.228391 v = v * v 17.119933 v = v + v 6.489957 Sum: 142.686547 Pypy with improvements: mod by 2: 0.007059 mod by 10000: 3.204295 mod by 1024 (power of two): 0.009401 Div huge number by 2**128: 1.368511 rshift: 2.345295 lshift: 1.339761 Floordiv by 2: 1.532028 Floordiv by 3 (not power of two): 4.005607 2**500000: 0.033466 (2**N)**5000000 (power of two): 0.047093 10000 ** BIGNUM % 100 1.207310 i = i * i: 3.998161 n**10000 (not power of two): 6.323250 Power of two ** power of two: 0.013258 v = v * power of two 3.567459 v = v * v 6.316683 v = v + v 2.757308 Sum: 38.075946 # Notice: This is slightly old! With SUPPORT_INT128 set to False mod by 2: 0.004103 mod by 10000: 3.237434 mod by 1024 (power of two): 0.016363 Div huge number by 2**128: 2.836237 rshift: 2.343860 lshift: 1.172665 Floordiv by 2: 1.537474 Floordiv by 3 (not power of two): 3.796015 2**500000: 0.327269 (2**N)**5000000 (power of two): 0.084709 10000 ** BIGNUM % 100 2.063215 i = i * i: 8.109634 n**10000 (not power of two): 11.243292 Power of two ** power of two: 0.072559 v = v * power of two 9.753532 v = v * v 13.569841 v = v + v 5.760466 Sum: 65.928667 """ sumTime = 0.0 V2 = rbigint.fromint(2) num = rbigint.pow(rbigint.fromint(100000000), rbigint.fromint(1024)) t = time() for n in xrange(600000): rbigint.mod(num, V2) _time = time() - t sumTime += _time print "mod by 2: ", _time by = rbigint.fromint(10000) t = time() for n in xrange(300000): rbigint.mod(num, by) _time = time() - t sumTime += _time print "mod by 10000: ", _time V1024 = rbigint.fromint(1024) t = time() for n in xrange(300000): rbigint.mod(num, V1024) _time = time() - t sumTime += _time print "mod by 1024 (power of two): ", _time t = time() num = rbigint.pow(rbigint.fromint(100000000), rbigint.fromint(1024)) by = rbigint.pow(rbigint.fromint(2), rbigint.fromint(128)) for n in xrange(80000): rbigint.divmod(num, by) _time = time() - t sumTime += _time print "Div huge number by 2**128:", _time t = time() num = rbigint.fromint(1000000000) for n in xrange(160000000): rbigint.rshift(num, 16) _time = time() - t sumTime += _time print "rshift:", _time t = time() num = rbigint.fromint(1000000000) for n in xrange(160000000): rbigint.lshift(num, 4) _time = time() - t sumTime += _time print "lshift:", _time t = time() num = rbigint.fromint(100000000) for n in xrange(80000000): rbigint.floordiv(num, V2) _time = time() - t sumTime += _time print "Floordiv by 2:", _time t = time() num = rbigint.fromint(100000000) V3 = rbigint.fromint(3) for n in xrange(80000000): rbigint.floordiv(num, V3) _time = time() - t sumTime += _time print "Floordiv by 3 (not power of two):", _time t = time() num = rbigint.fromint(500000) for n in xrange(10000): rbigint.pow(V2, num) _time = time() - t sumTime += _time print "2**500000:", _time t = time() num = rbigint.fromint(5000000) for n in xrange(31): rbigint.pow(rbigint.pow(V2, rbigint.fromint(n)), num) _time = time() - t sumTime += _time print "(2**N)**5000000 (power of two):", _time t = time() num = rbigint.pow(rbigint.fromint(10000), rbigint.fromint(2**8)) P10_4 = rbigint.fromint(10**4) V100 = rbigint.fromint(100) for n in xrange(60000): rbigint.pow(P10_4, num, V100) _time = time() - t sumTime += _time print "10000 ** BIGNUM % 100", _time t = time() i = rbigint.fromint(2**31) i2 = rbigint.fromint(2**31) for n in xrange(75000): i = i.mul(i2) _time = time() - t sumTime += _time print "i = i * i:", _time t = time() for n in xrange(10000): rbigint.pow(rbigint.fromint(n), P10_4) _time = time() - t sumTime += _time print "n**10000 (not power of two):", _time t = time() for n in xrange(100000): rbigint.pow(V1024, V1024) _time = time() - t sumTime += _time print "Power of two ** power of two:", _time t = time() v = rbigint.fromint(2) P62 = rbigint.fromint(2**62) for n in xrange(50000): v = v.mul(P62) _time = time() - t sumTime += _time print "v = v * power of two", _time t = time() v2 = rbigint.fromint(2**8) for n in xrange(28): v2 = v2.mul(v2) _time = time() - t sumTime += _time print "v = v * v", _time t = time() v3 = rbigint.fromint(2**62) for n in xrange(500000): v3 = v3.add(v3) _time = time() - t sumTime += _time print "v = v + v", _time x = rbigint.fromstr("13579246801357924680135792468013579246801") y = rbigint.fromstr( "112233445566778899112233445566778899112233445566778899") t = time() for i in range(5000): x.gcd(y) x = x.int_mul(2).int_add(1) _time = time() - t print "gcd", _time sumTime += _time print "Sum: ", sumTime return 0
def string_to_w_long(space, w_longtype, s, base=10): try: bigint = rbigint.fromstr(s, base) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg))