Example #1
0
def unmarshal_Long(space, u, tc):
    # XXX access internals
    from pypy.rlib.rbigint import rbigint

    lng = u.get_int()
    if lng < 0:
        sign = -1
        lng = -lng
    elif lng > 0:
        sign = 1
    else:
        sign = 0
    if long_bits != 15:
        SHIFT = 15
        result = rbigint([0], 0)
        for i in range(lng):
            shift = i * SHIFT
            result = result.add(rbigint.fromint(u.get_short()).lshift(shift))
        if lng and not result.tobool():
            raise_exception(space, "bad marshal data")
        if sign == -1:
            result = result.neg()
    else:
        digits = [0] * lng
        for i in range(lng):
            digit = u.get_int()
            if digit < 0:
                raise_exception(space, "bad marshal data")
            digits[i] = digit
        if digits[-1] == 0:
            raise_exception(space, "bad marshal data")
        result = rbigint(digits, sign)
    w_long = W_LongObject(result)
    return w_long
Example #2
0
 def test__k_lopsided_mul(self):
     digs_a = lobj.KARATSUBA_CUTOFF + 3
     digs_b = 3 * digs_a
     f1 = rbigint([lobj.MASK] * digs_a, 1)
     f2 = rbigint([lobj.MASK] * digs_b, 1)
     ret = lobj._k_lopsided_mul(f1, f2)
     assert ret.tolong() == f1.tolong() * f2.tolong()
 def test__k_lopsided_mul(self):
     digs_a = lobj.KARATSUBA_CUTOFF + 3
     digs_b = 3 * digs_a
     f1 = rbigint([lobj.MASK] * digs_a, 1)
     f2 = rbigint([lobj.MASK] * digs_b, 1)
     ret = lobj._k_lopsided_mul(f1, f2)
     assert ret.tolong() == f1.tolong() * f2.tolong()
Example #4
0
def unmarshal_Long(space, u, tc):
    # XXX access internals
    from pypy.rlib.rbigint import rbigint
    lng = u.get_int()
    if lng < 0:
        sign = -1
        lng = -lng
    elif lng > 0:
        sign = 1
    else:
        sign = 0
    if long_bits != 15:
        SHIFT = 15
        result = rbigint([0], 0)
        for i in range(lng):
            shift = i * SHIFT
            result = result.add(rbigint.fromint(u.get_short()).lshift(shift))
        if lng and not result.tobool():
            raise_exception(space, 'bad marshal data')
        if sign == -1:
            result = result.neg()
    else:
        digits = [0] * lng
        for i in range(lng):
            digit = u.get_int()
            if digit < 0:
                raise_exception(space, 'bad marshal data')
            digits[i] = digit
        if digits[-1] == 0:
            raise_exception(space, 'bad marshal data')
        result = rbigint(digits, sign)
    w_long = W_LongObject(result)
    return w_long
 def test_args_from_uint(self):
     BASE = 1 << SHIFT
     assert rbigint.fromrarith_int(r_uint(0)).eq(rbigint([0], 0))
     assert rbigint.fromrarith_int(r_uint(17)).eq(rbigint([17], 1))
     assert rbigint.fromrarith_int(r_uint(BASE-1)).eq(rbigint([BASE-1], 1))
     assert rbigint.fromrarith_int(r_uint(BASE)).eq(rbigint([0, 1], 1))
     assert rbigint.fromrarith_int(r_uint(BASE**2)).eq(rbigint([0, 0, 1], 1))
     assert rbigint.fromrarith_int(r_uint(sys.maxint)).eq(
         rbigint.fromint(sys.maxint))
     assert rbigint.fromrarith_int(r_uint(sys.maxint+1)).eq(
         rbigint.fromlong(sys.maxint+1))
     assert rbigint.fromrarith_int(r_uint(2*sys.maxint+1)).eq(
         rbigint.fromlong(2*sys.maxint+1))
Example #6
0
 def test_args_from_uint(self):
     BASE = 1 << SHIFT
     assert rbigint.fromrarith_int(r_uint(0)).eq(rbigint([0], 0))
     assert rbigint.fromrarith_int(r_uint(17)).eq(rbigint([17], 1))
     assert rbigint.fromrarith_int(r_uint(BASE-1)).eq(rbigint([BASE-1], 1))
     assert rbigint.fromrarith_int(r_uint(BASE)).eq(rbigint([0, 1], 1))
     assert rbigint.fromrarith_int(r_uint(BASE**2)).eq(rbigint([0, 0, 1], 1))
     assert rbigint.fromrarith_int(r_uint(sys.maxint)).eq(
         rbigint.fromint(sys.maxint))
     assert rbigint.fromrarith_int(r_uint(sys.maxint+1)).eq(
         rbigint.fromlong(sys.maxint+1))
     assert rbigint.fromrarith_int(r_uint(2*sys.maxint+1)).eq(
         rbigint.fromlong(2*sys.maxint+1))
Example #7
0
 def test_args_from_int(self):
     BASE = 1 << SHIFT
     assert rbigint.fromrarith_int(0).eq(rbigint([0], 0))
     assert rbigint.fromrarith_int(17).eq(rbigint([17], 1))
     assert rbigint.fromrarith_int(BASE - 1).eq(rbigint([BASE - 1], 1))
     assert rbigint.fromrarith_int(BASE).eq(rbigint([0, 1], 1))
     assert rbigint.fromrarith_int(BASE**2).eq(rbigint([0, 0, 1], 1))
     assert rbigint.fromrarith_int(-17).eq(rbigint([17], -1))
     assert rbigint.fromrarith_int(-(BASE - 1)).eq(rbigint([BASE - 1], -1))
     assert rbigint.fromrarith_int(-BASE).eq(rbigint([0, 1], -1))
     assert rbigint.fromrarith_int(-(BASE**2)).eq(rbigint([0, 0, 1], -1))
 def test_args_from_int(self):
     BASE = 1 << SHIFT
     assert rbigint.fromrarith_int(0).eq(rbigint([0], 0))
     assert rbigint.fromrarith_int(17).eq(rbigint([17], 1))
     assert rbigint.fromrarith_int(BASE-1).eq(rbigint([BASE-1], 1))
     assert rbigint.fromrarith_int(BASE).eq(rbigint([0, 1], 1))
     assert rbigint.fromrarith_int(BASE**2).eq(rbigint([0, 0, 1], 1))
     assert rbigint.fromrarith_int(-17).eq(rbigint([17], -1))
     assert rbigint.fromrarith_int(-(BASE-1)).eq(rbigint([BASE-1], -1))
     assert rbigint.fromrarith_int(-BASE).eq(rbigint([0, 1], -1))
     assert rbigint.fromrarith_int(-(BASE**2)).eq(rbigint([0, 0, 1], -1))
Example #9
0
 def test_args_from_long(self):
     BASE = 1 << SHIFT
     assert rbigint.fromlong(0).eq(rbigint([0], 0))
     assert rbigint.fromlong(17).eq(rbigint([17], 1))
     assert rbigint.fromlong(BASE-1).eq(rbigint([intmask(BASE-1)], 1))
     assert rbigint.fromlong(BASE).eq(rbigint([0, 1], 1))
     assert rbigint.fromlong(BASE**2).eq(rbigint([0, 0, 1], 1))
     assert rbigint.fromlong(-17).eq(rbigint([17], -1))
     assert rbigint.fromlong(-(BASE-1)).eq(rbigint([intmask(BASE-1)], -1))
     assert rbigint.fromlong(-BASE).eq(rbigint([0, 1], -1))
     assert rbigint.fromlong(-(BASE**2)).eq(rbigint([0, 0, 1], -1))
Example #10
0
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)
Example #11
0
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)
Example #12
0
 def test__kmul_split(self):
     split = 5
     diglo = [0] * split
     dighi = [lobj.MASK] * split
     f1 = rbigint(diglo + dighi, 1)
     hi, lo = lobj._kmul_split(f1, split)
     assert lo.digits == [0]
     assert hi.digits == dighi
 def test__kmul_split(self):
     split = 5
     diglo = [0] * split
     dighi = [lobj.MASK] * split
     f1 = rbigint(diglo + dighi, 1)
     hi, lo = lobj._kmul_split(f1, split)
     assert lo.digits == [0]
     assert hi.digits == dighi
Example #14
0
 def test_tofloat(self):
     x = 12345678901234567890L ** 10
     f1 = rbigint.fromlong(x)
     d = f1.tofloat()
     assert d == float(x)
     x = x ** 100
     f1 = rbigint.fromlong(x)
     assert raises(OverflowError, f1.tofloat)
     f2 = rbigint([0, 2097152], 1)
     d = f2.tofloat()
     assert d == float(2097152 << SHIFT)
Example #15
0
 def bigint_w(self, w_obj):
     if self.is_true(self.isinstance(w_obj, self.w_long)):
         sign = _PyLong_Sign(w_obj)
         #XXX Can throw exception if long larger than size_t bits
         numbits = _PyLong_NumBits(w_obj)
         numbytes = (numbits+1) / 8 # +1 sign bit cpython always adds
         if (numbits+1) % 8 != 0:
             numbytes += 1
         ByteArray = c_ubyte * numbytes
         cbytes = ByteArray()
         _PyLong_AsByteArray(w_obj, cbytes, numbytes, 1, 1) # little endian, signed
         rdigits = _cpylongarray_to_bigintarray(cbytes, numbytes, numbits, sign)
         return rbigint(rdigits, sign)
     elif self.is_true(self.isinstance(w_obj, self.w_int)):
         value = self.int_w(w_obj)
         return rbigint.fromint(value)
     else:
         raise OperationError(self.w_TypeError, self.wrap("Expected type int or long"))
Example #16
0
def unmarshal_Long(space, u, tc):
    from pypy.rlib import rbigint
    lng = u.get_int()
    if lng < 0:
        sign = -1
        lng = -lng
    elif lng > 0:
        sign = 1
    else:
        sign = 0
    digits = [0] * lng
    i = 0
    while i < lng:
        digit = u.get_short()
        if digit < 0:
            raise_exception(space, 'bad marshal data')
        digits[i] = digit
        i += 1
    # XXX poking at internals
    w_long = W_LongObject(rbigint.rbigint(digits, sign))
    w_long.num._normalize()
    return w_long
Example #17
0
def unmarshal_Long(space, u, tc):
    from pypy.rlib import rbigint
    lng = u.get_int()
    if lng < 0:
        sign = -1
        lng = -lng
    elif lng > 0:
        sign = 1
    else:
        sign = 0
    digits = [0] * lng
    i = 0
    while i < lng:
        digit = u.get_short()
        if digit < 0:
            raise_exception(space, 'bad marshal data')
        digits[i] = digit
        i += 1
    # XXX poking at internals
    w_long = W_LongObject(rbigint.rbigint(digits, sign))
    w_long.num._normalize()
    return w_long
Example #18
0
    def fn(*args):
        if NonConstant(True):
            return a
        else:
            return b

    return fn


int_dummy = make_dummy(42, 43)
float_dummy = make_dummy(42.0, 42.1)
uint_dummy = make_dummy(r_uint(42), r_uint(43))
str_dummy = make_dummy('foo', 'bar')
bool_dummy = make_dummy(True, False)
unicode_dummy = make_dummy(u'abc', u'cde')
bigint_dummy = make_dummy(rbigint([0]), rbigint([1]))


class FakeObjSpace(ObjSpace):
    w_None = W_Object()
    w_False = W_Object()
    w_True = W_Object()
    w_Ellipsis = W_Object()
    w_NotImplemented = W_Object()
    w_int = W_Object()
    w_dict = W_Object()
    w_float = W_Object()
    w_long = W_Object()
    w_tuple = W_Object()
    w_str = W_Object()
    w_basestring = W_Object()
Example #19
0
 def test_normalize(self):
     f1 = rbigint([1, 0], 1)
     f1._normalize()
     assert len(f1.digits) == 1
     f0 = rbigint([0], 0)
     assert f1.sub(f1).eq(f0)
Example #20
0
    def fn(*args):
        if NonConstant(True):
            return a
        else:
            return b

    return fn


int_dummy = make_dummy(42, 43)
float_dummy = make_dummy(42.0, 42.1)
uint_dummy = make_dummy(r_uint(42), r_uint(43))
str_dummy = make_dummy("foo", "bar")
bool_dummy = make_dummy(True, False)
unicode_dummy = make_dummy(u"abc", u"cde")
bigint_dummy = make_dummy(rbigint([0]), rbigint([1]))


class FakeObjSpace(ObjSpace):
    w_None = W_Object()
    w_False = W_Object()
    w_True = W_Object()
    w_Ellipsis = W_Object()
    w_NotImplemented = W_Object()
    w_int = W_Object()
    w_dict = W_Object()
    w_float = W_Object()
    w_long = W_Object()
    w_tuple = W_Object()
    w_str = W_Object()
    w_basestring = W_Object()
 def test__k_mul(self):
     digs= lobj.KARATSUBA_CUTOFF * 5
     f1 = rbigint([lobj.MASK] * digs, 1)
     f2 = lobj._x_add(f1,rbigint([1], 1))
     ret = lobj._k_mul(f1, f2)
     assert ret.tolong() == f1.tolong() * f2.tolong()
Example #22
0
 def test__v_iadd(self):
     f1 = rbigint([lobj.MASK] * 10, 1)
     f2 = rbigint([1], 1)
     carry = lobj._v_iadd(f1.digits, 1, len(f1.digits) - 1, f2.digits, 1)
     assert carry == 1
     assert f1.tolong() == lobj.MASK
 def test__v_isub(self):
     f1 = rbigint([lobj.MASK] + [0] * 9 + [1], 1)
     f2 = rbigint([1], 1)
     borrow = lobj._v_isub(f1.digits, 1, len(f1.digits)-1, f2.digits, 1)
     assert borrow == 0
     assert f1.tolong() == (1 << lobj.SHIFT) ** 10 - 1
Example #24
0
 def test__v_isub(self):
     f1 = rbigint([lobj.MASK] + [0] * 9 + [1], 1)
     f2 = rbigint([1], 1)
     borrow = lobj._v_isub(f1.digits, 1, len(f1.digits) - 1, f2.digits, 1)
     assert borrow == 0
     assert f1.tolong() == (1 << lobj.SHIFT)**10 - 1
 def test__v_iadd(self):
     f1 = rbigint([lobj.MASK] * 10, 1)
     f2 = rbigint([1], 1)
     carry = lobj._v_iadd(f1.digits, 1, len(f1.digits)-1, f2.digits, 1)
     assert carry == 1
     assert f1.tolong() == lobj.MASK
 def test_normalize(self):
     f1 = rbigint([1, 0], 1)
     f1._normalize()
     assert len(f1.digits) == 1
     f0 = rbigint([0], 0)
     assert f1.sub(f1).eq(f0)
Example #27
0
 def test__k_mul(self):
     digs = lobj.KARATSUBA_CUTOFF * 5
     f1 = rbigint([lobj.MASK] * digs, 1)
     f2 = lobj._x_add(f1, rbigint([1], 1))
     ret = lobj._k_mul(f1, f2)
     assert ret.tolong() == f1.tolong() * f2.tolong()
Example #28
0
def bigint(lst, sign):
    for digit in lst:
        assert digit & MASK == digit    # wrongly written test!
    return rbigint(map(_store_digit, lst), sign)
Example #29
0
 def test():
     x = rbigint([1410065408, 4], 1)
     y = x.mul(x)
     return y.str()