Example #1
0
 def __init__(self, frame):
     self.frame = frame
     self.ll_tt = r_longlong(0)
     self.ll_it = r_longlong(0)
     self.callcount = 0
     self.recursivecallcount = 0
     self.recursionLevel = 0
Example #2
0
 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
Example #3
0
    def test_long_long(self):
        def f(i):
            return 4 * i

        fn = self.getcompiled(f, [r_ulonglong])
        assert fn(r_ulonglong(2147483647)) == 4 * 2147483647

        def g(i):
            return 4 * i

        gn = self.getcompiled(g, [r_longlong])
        assert gn(r_longlong(2147483647)) == 4 * 2147483647

        def g(i):
            return i << 12

        gn = self.getcompiled(g, [r_longlong])
        assert gn(r_longlong(2147483647)) == 2147483647 << 12

        def g(i):
            return i >> 12

        gn = self.getcompiled(g, [r_longlong])
        assert gn(r_longlong(-2147483647)) == (-2147483647) >> 12

        def g(i):
            return i >> 12

        gn = self.getcompiled(g, [r_ulonglong])
        assert gn(r_ulonglong(2**64 - 12345678)) == (2**64 - 12345678) >> 12
Example #4
0
def test_cast_float_to_ulonglong():
    f = 12350000000000000000.0
    py.test.raises(OverflowError, r_longlong, f)
    r_longlong(f / 2)  # does not raise OverflowError
    #
    x = llop.cast_float_to_ulonglong(lltype.UnsignedLongLong, f)
    assert x == r_ulonglong(f)
Example #5
0
def min_max_acc_method(size, signed):
    if signed:
        min = -(2**(8 * size - 1))
        max = (2**(8 * size - 1)) - 1
        if size <= native_int_size:
            accept_method = 'accept_int_arg'
            min = int(min)
            max = int(max)
        else:
            accept_method = 'accept_longlong_arg'
            min = r_longlong(min)
            max = r_longlong(max)
    else:
        min = 0
        max = (2**(8 * size)) - 1
        if size < native_int_size:
            accept_method = 'accept_int_arg'
        elif size == native_int_size:
            accept_method = 'accept_uint_arg'
            min = r_uint(min)
            max = r_uint(max)
        else:
            accept_method = 'accept_ulonglong_arg'
            min = r_ulonglong(min)
            max = r_ulonglong(max)
    return min, max, accept_method
Example #6
0
    def test_long_long(self):
        def f(i):
            return 4 * i
        fn = self.getcompiled(f, [r_ulonglong])
        assert fn(r_ulonglong(2147483647)) == 4 * 2147483647

        def g(i):
            return 4 * i
        gn = self.getcompiled(g, [r_longlong])
        assert gn(r_longlong(2147483647)) == 4 * 2147483647

        def g(i):
            return i << 12
        gn = self.getcompiled(g, [r_longlong])
        assert gn(r_longlong(2147483647)) == 2147483647 << 12

        def g(i):
            return i >> 12
        gn = self.getcompiled(g, [r_longlong])
        assert gn(r_longlong(-2147483647)) == (-2147483647) >> 12

        def g(i):
            return i >> 12
        gn = self.getcompiled(g, [r_ulonglong])
        assert gn(r_ulonglong(2 ** 64 - 12345678)) == (2 ** 64 - 12345678) >> 12
Example #7
0
 def convert_bitfield_from_object(self, cdata, w_ob):
     ctype = self.ctype
     space = ctype.space
     #
     value = misc.as_long_long(space, w_ob)
     if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned):
         is_signed = True
         fmin = -(r_longlong(1) << (self.bitsize - 1))
         fmax = (r_longlong(1) << (self.bitsize - 1)) - 1
         if fmax == 0:
             fmax = 1  # special case to let "int x:1" receive "1"
     else:
         is_signed = False
         fmin = r_longlong(0)
         fmax = r_longlong((r_ulonglong(1) << self.bitsize) - 1)
     if value < fmin or value > fmax:
         raise oefmt(
             space.w_OverflowError,
             "value %d outside the range allowed by the bit field "
             "width: %d <= x <= %d", value, fmin, fmax)
     rawmask = ((r_ulonglong(1) << self.bitsize) - 1) << self.bitshift
     rawvalue = r_ulonglong(value) << self.bitshift
     rawfielddata = misc.read_raw_unsigned_data(cdata, ctype.size)
     rawfielddata = (rawfielddata & ~rawmask) | (rawvalue & rawmask)
     if is_signed:
         misc.write_raw_signed_data(cdata, rawfielddata, ctype.size)
     else:
         misc.write_raw_unsigned_data(cdata, rawfielddata, ctype.size)
Example #8
0
def test_direct(space):
    w5 = space.wrap(r_longlong(5))
    assert isinstance(w5, W_SmallLongObject)
    wlarge = space.wrap(r_longlong(0x123456789ABCDEFL))
    #
    assert space.int_w(w5) == 5
    if sys.maxint < 0x123456789ABCDEFL:
        with pytest.raises(OperationError):
            space.int_w(wlarge)
    else:
        assert space.int_w(wlarge) == 0x123456789ABCDEF
    #
    assert space.pos(w5) is w5
    assert space.abs(w5) is w5
    wm5 = space.wrap(r_longlong(-5))
    assert space.int_w(space.abs(wm5)) == 5
    assert space.int_w(space.neg(w5)) == -5
    assert space.is_true(w5) is True
    assert space.is_true(wm5) is True
    w0 = space.wrap(r_longlong(0))
    assert space.is_true(w0) is False
    #
    w14000000000000 = space.wrap(r_longlong(0x14000000000000L))
    assert space.is_true(space.eq(
        space.lshift(w5, space.wrap(49)), w14000000000000)) is False
    assert space.is_true(space.eq(
        space.lshift(w5, space.wrap(50)), w14000000000000)) is True
    #
    w_huge = space.sub(space.lshift(w5, space.wrap(150)), space.wrap(1))
    wx = space.and_(w14000000000000, w_huge)
    assert space.is_true(space.eq(wx, w14000000000000))

    w_obj = W_SmallLongObject.fromint(42)
    assert space.unwrap(w_obj) == 42
Example #9
0
 def convert_bitfield_from_object(self, cdata, w_ob):
     ctype = self.ctype
     space = ctype.space
     #
     value = misc.as_long_long(space, w_ob)
     if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned):
         is_signed = True
         fmin = -(r_longlong(1) << (self.bitsize - 1))
         fmax = (r_longlong(1) << (self.bitsize - 1)) - 1
         if fmax == 0:
             fmax = 1      # special case to let "int x:1" receive "1"
     else:
         is_signed = False
         fmin = r_longlong(0)
         fmax = r_longlong((r_ulonglong(1) << self.bitsize) - 1)
     if value < fmin or value > fmax:
         raise oefmt(space.w_OverflowError,
                     "value %d outside the range allowed by the bit field "
                     "width: %d <= x <= %d", value, fmin, fmax)
     rawmask = ((r_ulonglong(1) << self.bitsize) - 1) << self.bitshift
     rawvalue = r_ulonglong(value) << self.bitshift
     rawfielddata = misc.read_raw_unsigned_data(cdata, ctype.size)
     rawfielddata = (rawfielddata & ~rawmask) | (rawvalue & rawmask)
     if is_signed:
         misc.write_raw_signed_data(cdata, rawfielddata, ctype.size)
     else:
         misc.write_raw_unsigned_data(cdata, rawfielddata, ctype.size)
Example #10
0
 def __init__(self, frame):
     self.frame = frame
     self.ll_tt = r_longlong(0)
     self.ll_it = r_longlong(0)
     self.callcount = 0
     self.recursivecallcount = 0
     self.recursionLevel = 0
Example #11
0
def _divmod_ovf2small(space, x, y):
    from pypy.objspace.std.smalllongobject import W_SmallLongObject
    a = r_longlong(x)
    b = r_longlong(y)
    return space.newtuple(
        [W_SmallLongObject(a // b),
         W_SmallLongObject(a % b)])
Example #12
0
 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
Example #13
0
 def f(n1, n2):
     # n == -30000000000000
     n = (r_longlong(n1) << 32) | r_longlong(n2)
     compare(n < n,  0, 0)
     compare(n <= n, 0, 1)
     compare(n == n, 0, 1)
     compare(n != n, 0, 0)
     compare(n >  n, 0, 0)
     compare(n >= n, 0, 1)
     o = n + 2000000000
     compare(o, -6985, -1948404736)
     compare(n <  o, 0, 1)     # low word differs
     compare(n <= o, 0, 1)
     compare(o <  n, 0, 0)
     compare(o <= n, 0, 0)
     compare(n >  o, 0, 0)
     compare(n >= o, 0, 0)
     compare(o >  n, 0, 1)
     compare(o >= n, 0, 1)
     compare(n == o, 0, 0)
     compare(n != o, 0, 1)
     p = -o
     compare(n <  p, 0, 1)     # high word differs
     compare(n <= p, 0, 1)
     compare(p <  n, 0, 0)
     compare(p <= n, 0, 0)
     compare(n >  p, 0, 0)
     compare(n >= p, 0, 0)
     compare(p >  n, 0, 1)
     compare(p >= n, 0, 1)
     compare(n == p, 0, 0)
     compare(n != p, 0, 1)
     return 1
Example #14
0
def test_cast_float_to_ulonglong():
    f = 12350000000000000000.0
    py.test.raises(OverflowError, r_longlong, f)
    r_longlong(f / 2)   # does not raise OverflowError
    #
    x = llop.cast_float_to_ulonglong(lltype.UnsignedLongLong, f)
    assert x == r_ulonglong(f)
def _impl_pow(space, iv, w_int2, iz=r_longlong(0)):
    iw = w_int2.intval
    if iw < 0:
        if iz != 0:
            raise OperationError(space.w_TypeError,
                             space.wrap("pow() 2nd argument "
                 "cannot be negative when 3rd argument specified"))
        ## bounce it, since it always returns float
        raise FailedToImplementArgs(space.w_ValueError,
                                space.wrap("integer exponentiation"))
    temp = iv
    ix = r_longlong(1)
    try:
        while iw > 0:
            if iw & 1:
                ix = llong_mul_ovf(ix, temp)
            iw >>= 1   #/* Shift exponent down by 1 bit */
            if iw==0:
                break
            temp = llong_mul_ovf(temp, temp) #/* Square the value of temp */
            if iz:
                #/* If we did a multiplication, perform a modulo */
                ix = ix % iz
                temp = temp % iz
        if iz:
            ix = ix % iz
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                space.wrap("integer exponentiation"))
    return W_SmallLongObject(ix)
Example #16
0
 def f(n1, n2):
     # n == -30000000000000
     n = (r_longlong(n1) << 32) | r_longlong(n2)
     compare(n < n,  0, 0)
     compare(n <= n, 0, 1)
     compare(n == n, 0, 1)
     compare(n != n, 0, 0)
     compare(n >  n, 0, 0)
     compare(n >= n, 0, 1)
     o = n + 2000000000
     compare(o, -6985, -1948404736)
     compare(n <  o, 0, 1)     # low word differs
     compare(n <= o, 0, 1)
     compare(o <  n, 0, 0)
     compare(o <= n, 0, 0)
     compare(n >  o, 0, 0)
     compare(n >= o, 0, 0)
     compare(o >  n, 0, 1)
     compare(o >= n, 0, 1)
     compare(n == o, 0, 0)
     compare(n != o, 0, 1)
     p = -o
     compare(n <  p, 0, 1)     # high word differs
     compare(n <= p, 0, 1)
     compare(p <  n, 0, 0)
     compare(p <= n, 0, 0)
     compare(n >  p, 0, 0)
     compare(n >= p, 0, 0)
     compare(p >  n, 0, 1)
     compare(p >= n, 0, 1)
     compare(n == p, 0, 0)
     compare(n != p, 0, 1)
     return 1
Example #17
0
def min_max_acc_method(size, signed):
    if signed:
        min = -(2 ** (8*size-1))
        max = (2 ** (8*size-1)) - 1
        if size <= native_int_size:
            accept_method = 'accept_int_arg'
            min = int(min)
            max = int(max)
        else:
            accept_method = 'accept_longlong_arg'
            min = r_longlong(min)
            max = r_longlong(max)
    else:
        min = 0
        max = (2 ** (8*size)) - 1
        if size < native_int_size:
            accept_method = 'accept_int_arg'
        elif size == native_int_size:
            accept_method = 'accept_uint_arg'
            min = r_uint(min)
            max = r_uint(max)
        else:
            accept_method = 'accept_ulonglong_arg'
            min = r_ulonglong(min)
            max = r_ulonglong(max)
    return min, max, accept_method
Example #18
0
def unmarshal_Int64(space, u, tc):
    lo = u.get_int()    # get the first 32 bits
    hi = u.get_int()    # get the next 32 bits
    if LONG_BIT >= 64:
        x = (hi << 32) | (lo & (2**32-1))    # result fits in an int
    else:
        x = (r_longlong(hi) << 32) | r_longlong(r_uint(lo))  # get a r_longlong
    return space.wrap(x)
Example #19
0
 def g(n, m, o, p):
     # On 64-bit platforms, long longs == longs.  On 32-bit platforms,
     # this function should be either completely marked as residual
     # (with supports_longlong==False), or be compiled as a
     # sequence of residual calls (with long long arguments).
     n = r_longlong(n)
     m = r_longlong(m)
     return intmask((n*m + p) // o)
Example #20
0
 def f(x):
     if x == r_longlong(3):
         return 9
     elif x == r_longlong(9):
         return 27
     elif x == r_longlong(27):
         return 3
     return 0
Example #21
0
def unmarshal_int64(space, u, tc):
    lo = u.get_int()  # get the first 32 bits
    hi = u.get_int()  # get the next 32 bits
    if LONG_BIT >= 64:
        x = (hi << 32) | (lo & (2**32 - 1))  # result fits in an int
    else:
        x = (r_longlong(hi) << 32) | r_longlong(r_uint(lo))  # get a r_longlong
    return space.newint(x)
Example #22
0
 def g(n, m, o, p):
     # On 64-bit platforms, long longs == longs.  On 32-bit platforms,
     # this function should be either completely marked as residual
     # (with supports_longlong==False), or be compiled as a
     # sequence of residual calls (with long long arguments).
     n = r_longlong(n)
     m = r_longlong(m)
     return intmask((n*m + p) // o)
Example #23
0
 def f(x):
     if x == r_longlong(3):
         return 9
     elif x == r_longlong(9):
         return 27
     elif x == r_longlong(27):
         return 3
     return 0
Example #24
0
    def test_cast_to_bool_1_longlong(self):
        def f(n):
            return cast_primitive(Bool, n)

        fn = self.getcompiled(f, [r_longlong])
        assert fn(r_longlong(0)) == False
        assert fn(r_longlong(1)) == True
        assert fn(r_longlong(256)) == True
        assert fn(r_longlong(2**32)) == True
Example #25
0
    def test_cast_to_bool_2_longlong(self):
        def f(n):
            return rffi.cast(Bool, n)

        fn = self.getcompiled(f, [r_longlong])
        assert fn(r_longlong(0)) == False
        assert fn(r_longlong(1)) == True
        assert fn(r_longlong(256)) == True
        assert fn(r_longlong(2**32)) == True
Example #26
0
def test_wrap_int():
    for num in [-10, 1, 15, 0x3fffffff]:
        assert space.wrap_int(num).value == num

    sbit = (constants.LONG_BIT-1)
    for num in [r_longlong(2**sbit - 1), r_longlong(-(2**sbit))]:
        assert space.wrap_int(num).unwrap_long_untranslated(space) == num
    for num in [-(2**sbit + 1)]:
        with py.test.raises(error.WrappingError):
            space.wrap_int(num)
Example #27
0
def _debug_stop(category, timestamp):
    c = int(time.clock() * 100)
    print >> sys.stderr, '%s[%x] %s}%s' % (_start_colors_2, c, category,
                                           _stop_colors)
    if _log is not None:
        _log.debug_stop(category)

    if timestamp:
        return r_longlong(c)
    return r_longlong(-42)  # random undefined value
Example #28
0
def test_wrap_int():
    for num in [-10, 1, 15, 0x3fffffff]:
        assert space.wrap_int(num).value == num

    sbit = (constants.LONG_BIT - 1)
    for num in [r_longlong(2**sbit - 1), r_longlong(-(2**sbit))]:
        assert space.wrap_int(num).unwrap_long_untranslated(space) == num
    for num in [-(2**sbit + 1)]:
        with py.test.raises(error.WrappingError):
            space.wrap_int(num)
Example #29
0
 def f(n):
     n = r_longlong(n)
     if n == r_longlong(-5):
         return 12
     elif n == r_longlong(2):
         return 51
     elif n == r_longlong(7):
         return 1212
     else:
         return 42
Example #30
0
 def test_simple_call_longlong(self, **kwds):
     kwds.setdefault('supports_longlong', True)
     if is_64_bit:
         kwds['expected_call_release_gil_i'] = kwds.pop('expected_call_release_gil', 1)
     else:
         kwds['expected_call_release_gil_f'] = kwds.pop('expected_call_release_gil', 1)
         kwds['expected_call_release_gil_i'] = 0
     maxint32 = 2147483647
     a = r_longlong(maxint32) + 1
     b = r_longlong(maxint32) + 2
     self._run([types.slonglong] * 2, types.slonglong, [a, b], a, **kwds)
Example #31
0
 def test_simple_call_longlong(self, **kwds):
     kwds.setdefault('supports_longlong', True)
     if is_64_bit:
         kwds['expected_call_release_gil_i'] = kwds.pop('expected_call_release_gil', 1)
     else:
         kwds['expected_call_release_gil_f'] = kwds.pop('expected_call_release_gil', 1)
         kwds['expected_call_release_gil_i'] = 0
     maxint32 = 2147483647
     a = r_longlong(maxint32) + 1
     b = r_longlong(maxint32) + 2
     self._run([types.slonglong] * 2, types.slonglong, [a, b], a, **kwds)
Example #32
0
 def unwrap_longlong(self, space):
     # TODO: Completely untested! This failed translation bigtime...
     # XXX Probably we want to allow all subclasses
     if not self.getclass(space).is_same_object(space.w_LargePositiveInteger):
         raise error.UnwrappingError("Failed to convert bytes to word")
     if self.size() > 8:
         raise error.UnwrappingError("Too large to convert bytes to word")
     word = r_longlong(0)
     for i in range(self.size()):
         word += r_longlong(ord(self.getchar(i))) << 8*i
     return word
Example #33
0
 def f(n1, n2, m1, m2, ii):
     # n == -30000000000000, m == -20000000000, ii == 42
     n = (r_longlong(n1) << 32) | r_longlong(n2)
     m = (r_longlong(m1) << 32) | r_longlong(m2)
     compare(n & m, -6989, 346562560)
     compare(n | m, -1, 1474836480)
     compare(n ^ m, 6988, 1128273920)
     compare(n << 1, -13970, 693125120)
     compare(r_longlong(5) << ii, 5120, 0)
     compare(n >> 1, -3493, -1974202368)
     compare(n >> 42, -1, -7)
     return 1
Example #34
0
File: hook.py Project: sota/pypy
 def fix_annotation(self):
     # the annotation of the class and its attributes must be completed
     # BEFORE we do the gc transform; this makes sure that everything is
     # annotated with the correct types
     if NonConstant(False):
         self.count = NonConstant(-42)
         self.duration = NonConstant(r_longlong(-42))
         self.duration_min = NonConstant(r_longlong(-42))
         self.duration_max = NonConstant(r_longlong(-42))
         self.oldstate = NonConstant(-42)
         self.newstate = NonConstant(-42)
         self.fire()
Example #35
0
 def test_longlong_switch(self):
     def f(x):
         if x == r_longlong(3):
             return 9
         elif x == r_longlong(9):
             return 27
         elif x == r_longlong(27):
             return 3
         return 0
     fn = self.getcompiled(f, [r_longlong])
     for x in (0,1,2,3,9,27,48, -9):
         assert fn(r_longlong(x)) == f(r_longlong(x))
Example #36
0
def unmarshal_int64(space, u, tc):
    from rpython.rlib.rbigint import rbigint
    # no longer generated, but we still support unmarshalling
    lo = u.get_int()  # get the first 32 bits
    hi = u.get_int()  # get the next 32 bits
    if LONG_BIT >= 64:
        x = (hi << 32) | (lo & (2**32 - 1))  # result fits in an int
        return space.newint(x)
    else:
        x = (r_longlong(hi) << 32) | r_longlong(r_uint(lo))  # get a r_longlong
        result = rbigint.fromrarith_int(x)
        return space.newlong_from_rbigint(result)
Example #37
0
 def f(n1, n2, m1, m2, ii):
     # n == -30000000000000, m == -20000000000, ii == 42
     n = (r_longlong(n1) << 32) | r_longlong(n2)
     m = (r_longlong(m1) << 32) | r_longlong(m2)
     compare(n & m, -6989, 346562560)
     compare(n | m, -1, 1474836480)
     compare(n ^ m, 6988, 1128273920)
     compare(n << 1, -13970, 693125120)
     compare(r_longlong(5) << ii, 5120, 0)
     compare(n >> 1, -3493, -1974202368)
     compare(n >> 42, -1, -7)
     return 1
Example #38
0
 def test_pack_int(self):
     self.check('b', 42)
     self.check('B', 242)
     self.check('h', 32767)
     self.check('H', 32768)
     self.check("i", 0x41424344)
     self.check("i", -3)
     self.check("i", -2147483648)
     self.check("I", r_uint(0x81424344))
     self.check("q", r_longlong(0x4142434445464748))
     self.check("q", r_longlong(-0x41B2B3B4B5B6B7B8))
     self.check("Q", r_ulonglong(0x8142434445464748))
Example #39
0
def _pow_ovf2long(space, iv, iw, w_modulus):
    if space.is_none(w_modulus) and _recover_with_smalllong(space):
        from pypy.objspace.std.smalllongobject import _pow as _pow_small
        try:
            # XXX: shouldn't have to pass r_longlong(0) here (see
            # 4fa4c6b93a84)
            return _pow_small(space, r_longlong(iv), iw, r_longlong(0))
        except (OverflowError, ValueError):
            pass
    from pypy.objspace.std.longobject import W_LongObject
    w_iv = W_LongObject.fromint(space, iv)
    w_iw = W_LongObject.fromint(space, iw)
    return w_iv.descr_pow(space, w_iw, w_modulus)
Example #40
0
    def test_longlong_switch(self):
        def f(x):
            if x == r_longlong(3):
                return 9
            elif x == r_longlong(9):
                return 27
            elif x == r_longlong(27):
                return 3
            return 0

        fn = self.getcompiled(f, [r_longlong])
        for x in (0, 1, 2, 3, 9, 27, 48, -9):
            assert fn(r_longlong(x)) == f(r_longlong(x))
Example #41
0
def expose_value_as_rpython(value):
    if intmask(value) == value:
        return value
    if r_uint(value) == value:
        return r_uint(value)
    try:
        if r_longlong(value) == value:
            return r_longlong(value)
    except OverflowError:
        pass
    if r_ulonglong(value) == value:
        return r_ulonglong(value)
    raise OverflowError("value %d does not fit into any RPython integer type" % (value,))
Example #42
0
def _pow_ovf2long(space, iv, iw, w_modulus):
    if space.is_none(w_modulus) and _recover_with_smalllong(space):
        from pypy.objspace.std.smalllongobject import _pow as _pow_small
        try:
            # XXX: shouldn't have to pass r_longlong(0) here (see
            # 4fa4c6b93a84)
            return _pow_small(space, r_longlong(iv), iw, r_longlong(0))
        except (OverflowError, ValueError):
            pass
    from pypy.objspace.std.longobject import W_LongObject
    w_iv = W_LongObject.fromint(space, iv)
    w_iw = W_LongObject.fromint(space, iw)
    return w_iv.descr_pow(space, w_iw, w_modulus)
Example #43
0
def expose_value_as_rpython(value):
    if intmask(value) == value:
        return value
    if r_uint(value) == value:
        return r_uint(value)
    try:
        if r_longlong(value) == value:
            return r_longlong(value)
    except OverflowError:
        pass
    if r_ulonglong(value) == value:
        return r_ulonglong(value)
    raise OverflowError("value %d does not fit into any RPython integer type" %
                        (value, ))
Example #44
0
def test_mov_ri_64():
    s = CodeBuilder64()
    s.MOV_ri(ecx, -2)
    s.MOV_ri(r15, -3)
    s.MOV_ri(ebx, -0x80000003)
    s.MOV_ri(r13, -0x80000002)
    s.MOV_ri(ecx, 42)
    s.MOV_ri(r12, r_longlong(0x80000042))
    s.MOV_ri(r12, r_longlong(0x100000007))
    assert s.getvalue() == ('\x48\xC7\xC1\xFE\xFF\xFF\xFF' +
                            '\x49\xC7\xC7\xFD\xFF\xFF\xFF' +
                            '\x48\xBB\xFD\xFF\xFF\x7F\xFF\xFF\xFF\xFF' +
                            '\x49\xBD\xFE\xFF\xFF\x7F\xFF\xFF\xFF\xFF' +
                            '\xB9\x2A\x00\x00\x00' +
                            '\x41\xBC\x42\x00\x00\x80' +
                            '\x49\xBC\x07\x00\x00\x00\x01\x00\x00\x00')
Example #45
0
 def appendobj(self, value):
     # CPython tries hard to return int objects whenever it can, but
     # space.newint returns a long if we pass a r_uint, r_ulonglong or
     # r_longlong. So, we need special care in those cases.
     is_unsigned = (isinstance(value, r_uint)
                    or isinstance(value, r_ulonglong))
     if is_unsigned:
         if value <= maxint:
             w_value = self.space.newint(intmask(value))
         else:
             w_value = self.space.newint(value)
     elif isinstance(value, r_longlong):
         if value == r_longlong(intmask(value)):
             w_value = self.space.newint(intmask(value))
         else:
             w_value = self.space.newint(value)
     elif isinstance(value, bool):
         w_value = self.space.newbool(value)
     elif isinstance(value, int):
         w_value = self.space.newint(value)
     elif isinstance(value, float):
         w_value = self.space.newfloat(value)
     elif isinstance(value, str):
         w_value = self.space.newbytes(value)
     elif isinstance(value, unicode):
         w_value = self.space.newutf8(value.decode('utf-8'), len(value))
     else:
         assert 0, "unreachable"
     self.result_w.append(w_value)
Example #46
0
    def ovf2long(space, x, y):
        """Handle overflowing to smalllong or long"""
        if _recover_with_smalllong(space):
            if ovf2small:
                return ovf2small(space, x, y)
            # Assume a generic operation without an explicit ovf2small
            # handler
            from pypy.objspace.std.smalllongobject import W_SmallLongObject
            a = r_longlong(x)
            b = r_longlong(y)
            return W_SmallLongObject(op(a, b))

        from pypy.objspace.std.longobject import W_LongObject
        w_x = W_LongObject.fromint(space, x)
        w_y = W_LongObject.fromint(space, y)
        return getattr(w_x, 'descr_' + opname)(space, w_y)
Example #47
0
    def test_float_constant_conversions(self):
        DIV = r_longlong(10 ** 10)
        def fn():
            return 420000000000.0 / DIV

        res = self.interpret(fn, [])
        assert self.float_eq(res, 42.0)
Example #48
0
 def f(n1, n2, m1, m2):
     # n == -30000000000000, m == -20000000000
     n = (r_longlong(n1) << 32) | r_longlong(n2)
     m = (r_longlong(m1) << 32) | r_longlong(m2)
     compare(n, -6985, 346562560)
     compare(m, -5, 1474836480)
     if not n: raise WrongResult
     if not r_longlong(m2): raise WrongResult
     if n-n: raise WrongResult
     compare(-n, 6984, -346562560)
     compare(~n, 6984, -346562561)
     compare(n + m, -6990, 1821399040)
     compare(n - m, -6981, -1128273920)
     compare(n * (-3), 20954, -1039687680)
     compare((-4) * m, 18, -1604378624)
     return 1
Example #49
0
def dump_int(buf, x):
    # only use TYPE_INT on 32-bit platforms
    if LONG_BIT > 32:
        dump_longlong(buf, r_longlong(x))
    else:
        buf.append(TYPE_INT)
        w_long(buf, x)
Example #50
0
 def f(n1, n2, m1, m2):
     # n == -30000000000000, m == -20000000000
     n = (r_longlong(n1) << 32) | r_longlong(n2)
     m = (r_longlong(m1) << 32) | r_longlong(m2)
     compare(n, -6985, 346562560)
     compare(m, -5, 1474836480)
     if not n: raise WrongResult
     if not r_longlong(m2): raise WrongResult
     if n-n: raise WrongResult
     compare(-n, 6984, -346562560)
     compare(~n, 6984, -346562561)
     compare(n + m, -6990, 1821399040)
     compare(n - m, -6981, -1128273920)
     compare(n * (-3), 20954, -1039687680)
     compare((-4) * m, 18, -1604378624)
     return 1
Example #51
0
def test_contains_unsupported_variable_type():
    def f(x):
        return x
    graph = support.getgraph(f, [5])
    for sf in [False, True]:
        for sll in [False, True]:
            for ssf in [False, True]:
                assert not contains_unsupported_variable_type(graph, sf,
                                                              sll, ssf)
    #
    graph = support.getgraph(f, [5.5])
    for sf in [False, True]:
        for sll in [False, True]:
            for ssf in [False, True]:
                res = contains_unsupported_variable_type(graph, sf, sll, ssf)
                assert res is not sf
    #
    graph = support.getgraph(f, [r_singlefloat(5.5)])
    for sf in [False, True]:
        for sll in [False, True]:
            for ssf in [False, True]:
                res = contains_unsupported_variable_type(graph, sf, sll, ssf)
                assert res == (not ssf)
    #
    graph = support.getgraph(f, [r_longlong(5)])
    for sf in [False, True]:
        for sll in [False, True]:
            for ssf in [False, True]:
                res = contains_unsupported_variable_type(graph, sf, sll, ssf)
                assert res == (sys.maxint == 2147483647 and not sll)
Example #52
0
    def test_interp2app_unwrap_spec_c_int(self):
        from rpython.rlib.rarithmetic import r_longlong

        space = self.space
        w = space.wrap

        def g(space, x):
            return space.wrap(x + 6)

        app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, "c_int"])
        app_ug = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, "c_uint"])
        app_ng = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, "c_nonnegint"])
        assert app_ug is not app_g
        w_app_g = space.wrap(app_g)
        w_app_ug = space.wrap(app_ug)
        w_app_ng = space.wrap(app_ng)
        #
        assert self.space.eq_w(space.call_function(w_app_g, space.wrap(7)), space.wrap(13))
        space.raises_w(space.w_OverflowError, space.call_function, w_app_g, space.wrap(r_longlong(0x80000000)))
        space.raises_w(space.w_OverflowError, space.call_function, w_app_g, space.wrap(r_longlong(-0x80000001)))
        #
        assert self.space.eq_w(space.call_function(w_app_ug, space.wrap(7)), space.wrap(13))
        assert self.space.eq_w(
            space.call_function(w_app_ug, space.wrap(0x7FFFFFFF)), space.wrap(r_longlong(0x7FFFFFFF + 6))
        )
        space.raises_w(space.w_ValueError, space.call_function, w_app_ug, space.wrap(-1))
        space.raises_w(space.w_OverflowError, space.call_function, w_app_ug, space.wrap(r_longlong(0x100000000)))
        #
        assert self.space.eq_w(space.call_function(w_app_ng, space.wrap(7)), space.wrap(13))
        space.raises_w(space.w_OverflowError, space.call_function, w_app_ng, space.wrap(r_longlong(0x80000000)))
        space.raises_w(space.w_ValueError, space.call_function, w_app_ng, space.wrap(-1))
Example #53
0
 def test_int_or_float_special_nan(self):
     from rpython.rlib import longlong2float, rarithmetic
     space = self.space
     ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
     specialnan = longlong2float.longlong2float(ll)
     w_l = W_ListObject(space, [space.wrap(1), space.wrap(specialnan)])
     assert isinstance(w_l.strategy, ObjectListStrategy)
Example #54
0
def dump_int(buf, x):
    # only use TYPE_INT on 32-bit platforms
    if LONG_BIT > 32:
        dump_longlong(buf, r_longlong(x))
    else:
        buf.append(TYPE_INT)
        w_long(buf, x)
Example #55
0
    def ovf2long(space, x, y):
        """Handle overflowing to smalllong or long"""
        if _recover_with_smalllong(space):
            if ovf2small:
                return ovf2small(space, x, y)
            # Assume a generic operation without an explicit ovf2small
            # handler
            from pypy.objspace.std.smalllongobject import W_SmallLongObject
            a = r_longlong(x)
            b = r_longlong(y)
            return W_SmallLongObject(op(a, b))

        from pypy.objspace.std.longobject import W_LongObject
        w_x = W_LongObject.fromint(space, x)
        w_y = W_LongObject.fromint(space, y)
        return getattr(w_x, 'descr_' + opname)(space, w_y)
Example #56
0
 def wrap(self, x):
     "Wraps the Python value 'x' into one of the wrapper classes."
     # You might notice that this function is rather conspicuously
     # not RPython.  We can get away with this because the function
     # is specialized (see after the function body).  Also worth
     # noting is that the isinstance's involving integer types
     # behave rather differently to how you might expect during
     # annotation (see pypy/annotation/builtin.py)
     if x is None:
         return self.w_None
     if isinstance(x, OperationError):
         raise TypeError, ("attempt to wrap already wrapped exception: %s"%
                           (x,))
     if isinstance(x, int):
         if isinstance(x, bool):
             return self.newbool(x)
         else:
             return self.newint(x)
     if isinstance(x, str):
         # this hack is temporary: look at the comment in
         # test_stdstdobjspace.test_wrap_string
         try:
             unicode_x = x.decode('ascii')
         except UnicodeDecodeError:
             # poor man's x.decode('ascii', 'replace'), since it's not
             # supported by RPython
             if not we_are_translated():
                 print 'WARNING: space.wrap() called on a non-ascii byte string: %r' % x
             lst = []
             for ch in x:
                 ch = ord(ch)
                 if ch > 127:
                     lst.append(u'\ufffd')
                 else:
                     lst.append(unichr(ch))
             unicode_x = u''.join(lst)
         return wrapunicode(self, unicode_x)
     if isinstance(x, unicode):
         return wrapunicode(self, x)
     if isinstance(x, float):
         return W_FloatObject(x)
     if isinstance(x, W_Root):
         w_result = x.__spacebind__(self)
         #print 'wrapping', x, '->', w_result
         return w_result
     if isinstance(x, base_int):
         if self.config.objspace.std.withsmalllong:
             from pypy.objspace.std.smalllongobject import W_SmallLongObject
             from rpython.rlib.rarithmetic import r_longlong, r_ulonglong
             from rpython.rlib.rarithmetic import longlongmax
             if (not isinstance(x, r_ulonglong)
                 or x <= r_ulonglong(longlongmax)):
                 return W_SmallLongObject(r_longlong(x))
         x = widen(x)
         if isinstance(x, int):
             return self.newint(x)
         else:
             return W_LongObject.fromrarith_int(x)
     return self._wrap_not_rpython(x)
Example #57
0
 def test_acquire_timed_huge_timeout(self):
     t = r_longlong(2 ** 61)
     def f():
         l = allocate_lock()
         return l.acquire_timed(t)
     fn = self.getcompiled(f, [])
     res = fn()
     assert res == 1       # RPY_LOCK_ACQUIRED
Example #58
0
 def uint_w(self, space):
     a = self.longlong
     if a < 0:
         raise oefmt(space.w_ValueError, "cannot convert negative integer to unsigned int")
     b = r_uint(a)
     if r_longlong(b) == a:
         return b
     raise oefmt(space.w_OverflowError, "long int too large to convert to unsigned int")
Example #59
0
def test_direct():
    space = gettestobjspace(**{"objspace.std.withsmalllong": True})
    w5 = space.wrap(r_longlong(5))
    assert isinstance(w5, W_SmallLongObject)
    wlarge = space.wrap(r_longlong(0x123456789ABCDEFL))
    #
    assert space.int_w(w5) == 5
    if sys.maxint < 0x123456789ABCDEFL:
        py.test.raises(OperationError, space.int_w, wlarge)
    else:
        assert space.int_w(wlarge) == 0x123456789ABCDEF
    #
    assert space.pos(w5) is w5
    assert space.abs(w5) is w5
    wm5 = space.wrap(r_longlong(-5))
    assert space.int_w(space.abs(wm5)) == 5
    assert space.int_w(space.neg(w5)) == -5
    assert space.is_true(w5) is True
    assert space.is_true(wm5) is True
    w0 = space.wrap(r_longlong(0))
    assert space.is_true(w0) is False
    #
    w14000000000000 = space.wrap(r_longlong(0x14000000000000L))
    assert space.is_true(space.eq(
        space.lshift(w5, space.wrap(49)), w14000000000000)) is False
    assert space.is_true(space.eq(
        space.lshift(w5, space.wrap(50)), w14000000000000)) is True
    #
    w_huge = space.sub(space.lshift(w5, space.wrap(150)), space.wrap(1))
    wx = space.and_(w14000000000000, w_huge)
    assert space.is_true(space.eq(wx, w14000000000000))

    w_obj = W_SmallLongObject.fromint(42)
    assert space.unwrap(w_obj) == 42