Пример #1
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
Пример #2
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
Пример #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
Пример #4
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)
Пример #5
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)
Пример #6
0
 def f(n1, n2):
     # n == 30002000000000
     n = (r_ulonglong(n1) << 32) | r_ulonglong(n2)
     compare(n, 6985, 1653437440)
     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 = (r_ulonglong(n1) << 32) | r_ulonglong(r_uint(n2) + 1000000000)
     compare(o, 6985, -1641529856)
     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 = ~n
     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
Пример #7
0
 def f(n1, n2):
     # n == 30002000000000
     n = (r_ulonglong(n1) << 32) | r_ulonglong(n2)
     compare(n, 6985, 1653437440)
     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 = (r_ulonglong(n1) << 32) | r_ulonglong(r_uint(n2) + 1000000000)
     compare(o, 6985, -1641529856)
     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 = ~n
     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
Пример #8
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
Пример #9
0
 def f(x):
     if x == r_ulonglong(3):
         return 9
     elif x == r_ulonglong(9):
         return 27
     elif x == r_ulonglong(27):
         return 3
     return 0
Пример #10
0
 def test_ulonglongmask(self):
     assert rbigint.fromlong(-1).ulonglongmask() == r_ulonglong(-1)
     assert rbigint.fromlong(0).ulonglongmask() == r_ulonglong(0)
     assert (rbigint.fromlong(sys.maxint).ulonglongmask() == r_ulonglong(
         sys.maxint))
     assert (rbigint.fromlong(9**50).ulonglongmask() == r_ulonglong(9**50))
     assert (
         rbigint.fromlong(-9**50).ulonglongmask() == r_ulonglong(-9**50))
Пример #11
0
 def f(x):
     if x == r_ulonglong(3):
         return 9
     elif x == r_ulonglong(9):
         return 27
     elif x == r_ulonglong(27):
         return 3
     return 0
Пример #12
0
def float_pack80(x, size):
    """Convert a Python float or longfloat x into two 64-bit unsigned integers
    with 80 bit extended representation."""
    x = float(x)  # longfloat not really supported
    if size == 10 or size == 12 or size == 16:
        MIN_EXP = -16381
        MAX_EXP = 16384
        MANT_DIG = 64
        BITS = 80
    else:
        raise ValueError("invalid size value")

    sign = rfloat.copysign(1.0, x) < 0.0
    if not rfloat.isfinite(x):
        if rfloat.isinf(x):
            mant = r_ulonglong(0)
            exp = MAX_EXP - MIN_EXP + 2
        else:  # rfloat.isnan(x):
            mant = (r_ulonglong(1) <<
                    (MANT_DIG - 2)) - 1  # other values possible
            exp = MAX_EXP - MIN_EXP + 2
    elif x == 0.0:
        mant = r_ulonglong(0)
        exp = 0
    else:
        m, e = math.frexp(abs(x))  # abs(x) == m * 2**e
        exp = e - (MIN_EXP - 1)
        if exp > 0:
            # Normal case. Avoid uint64 overflow by using MANT_DIG-1
            mant = round_to_nearest(m * (r_ulonglong(1) << MANT_DIG - 1))
        else:
            # Subnormal case.
            if exp + MANT_DIG - 1 >= 0:
                mant = round_to_nearest(m *
                                        (r_ulonglong(1) << exp + MANT_DIG - 1))
            else:
                mant = r_ulonglong(0)
            exp = 0

        # Special case: rounding produced a MANT_DIG-bit mantissa.
        if mant == r_ulonglong(1) << MANT_DIG - 1:
            mant = r_ulonglong(0)
            exp += 1

        # Raise on overflow (in some circumstances, may want to return
        # infinity instead).
        if exp >= MAX_EXP - MIN_EXP + 2:
            raise OverflowError("float too large to pack in this format")

    # check constraints
    if not objectmodel.we_are_translated():
        assert 0 <= mant < 1 << MANT_DIG - 1
        assert 0 <= exp <= MAX_EXP - MIN_EXP + 2
        assert 0 <= sign <= 1
    mant = mant << 1
    exp = r_ulonglong(exp)
    sign = r_ulonglong(sign)
    return (mant, (sign << BITS - MANT_DIG - 1) | exp)
Пример #13
0
 def test_ulonglongmask(self):
     assert rbigint.fromlong(-1).ulonglongmask() == r_ulonglong(-1)
     assert rbigint.fromlong(0).ulonglongmask() == r_ulonglong(0)
     assert (rbigint.fromlong(sys.maxint).ulonglongmask() ==
             r_ulonglong(sys.maxint))
     assert (rbigint.fromlong(9**50).ulonglongmask() ==
             r_ulonglong(9**50))
     assert (rbigint.fromlong(-9**50).ulonglongmask() ==
             r_ulonglong(-9**50))
Пример #14
0
def unpack_float80(s, be):
    QQ = [r_ulonglong(0), r_ulonglong(0)]
    for i in range(8):
        c = ord(s[-i - 1 if be else i])
        QQ[0] |= r_ulonglong(c) << (i * 8)
    for i in range(8, 10):
        c = ord(s[-i - 1 if be else i])
        QQ[1] |= r_ulonglong(c) << ((i - 8) * 8)
    return float_unpack80(QQ, len(s))
Пример #15
0
 def wrap_nlonglong(self, val):
     if self.w_LargeNegativeInteger is None:
         raise WrappingError
     assert val < 0 and not is_valid_int(val)
     try:
         r_val = r_ulonglong(-val)
     except OverflowError:
         # this is a negative max-bit r_int64, mask by simple coercion
         r_val = r_ulonglong(val)
     w_class = self.w_LargeNegativeInteger
     return self.wrap_large_number(r_val, w_class)
Пример #16
0
 def f(n1, n2, ii):
     # n == 30002000000000, ii == 42
     n = (r_ulonglong(n1) << 32) | r_ulonglong(n2)
     compare(n << 1, 13970, -988092416)
     compare(r_ulonglong(5) << ii, 5120, 0)
     compare(n >> 1, 3492, -1320764928)
     compare(n >> 42, 0, 6)
     p = ~n
     compare(p >> 1, 2147480155, 1320764927)
     compare(p >> 42, 0, 4194297)
     return 1
Пример #17
0
 def f(n1, n2, ii):
     # n == 30002000000000, ii == 42
     n = (r_ulonglong(n1) << 32) | r_ulonglong(n2)
     compare(n << 1, 13970, -988092416)
     compare(r_ulonglong(5) << ii, 5120, 0)
     compare(n >> 1, 3492, -1320764928)
     compare(n >> 42, 0, 6)
     p = ~n
     compare(p >> 1, 2147480155, 1320764927)
     compare(p >> 42, 0, 4194297)
     return 1
Пример #18
0
 def test_ulonglong_switch(self):
     def f(x):
         if x == r_ulonglong(3):
             return 9
         elif x == r_ulonglong(9):
             return 27
         elif x == r_ulonglong(27):
             return 3
         return 0
     fn = self.getcompiled(f, [r_ulonglong])
     for x in (0,1,2,3,9,27,48, r_ulonglong(-9)):
         assert fn(r_ulonglong(x)) == f(r_ulonglong(x))
Пример #19
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,))
Пример #20
0
    def test_ulonglong_switch(self):
        def f(x):
            if x == r_ulonglong(3):
                return 9
            elif x == r_ulonglong(9):
                return 27
            elif x == r_ulonglong(27):
                return 3
            return 0

        fn = self.getcompiled(f, [r_ulonglong])
        for x in (0, 1, 2, 3, 9, 27, 48, r_ulonglong(-9)):
            assert fn(r_ulonglong(x)) == f(r_ulonglong(x))
Пример #21
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, ))
Пример #22
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
Пример #23
0
def BIT_MASK(x, ll_t):
    if ll_t is lltype.SignedLongLong or ll_t is lltype.UnsignedLongLong:
        one = r_ulonglong(1)
    else:
        one = r_uint(1)
    # to avoid left shift by x == sizeof(ll_t)
    return (((one << (x - 1)) - 1) << 1) + 1
Пример #24
0
def as_unsigned_long_long(space, w_ob, strict):
    # (possibly) convert and cast a Python object to an unsigned long long.
    # This accepts a Python int too, and does convertions from other types of
    # objects.  If 'strict', complains with OverflowError; if 'not strict',
    # mask the result and round floats.
    if space.is_w(space.type(w_ob), space.w_int):   # shortcut
        value = space.int_w(w_ob)
        if strict and value < 0:
            raise OperationError(space.w_OverflowError, space.wrap(neg_msg))
        return r_ulonglong(value)
    try:
        bigint = space.bigint_w(w_ob, allow_conversion=False)
    except OperationError as e:
        if not e.match(space, space.w_TypeError):
            raise
        if strict and _is_a_float(space, w_ob):
            raise
        bigint = space.bigint_w(space.int(w_ob), allow_conversion=False)
    if strict:
        try:
            return bigint.toulonglong()
        except ValueError:
            raise OperationError(space.w_OverflowError, space.wrap(neg_msg))
        except OverflowError:
            raise OperationError(space.w_OverflowError, space.wrap(ovf_msg))
    else:
        return bigint.ulonglongmask()
Пример #25
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)
Пример #26
0
def as_unsigned_long_long(space, w_ob, strict):
    # (possibly) convert and cast a Python object to an unsigned long long.
    # This accepts a Python int too, and does convertions from other types of
    # objects.  If 'strict', complains with OverflowError; if 'not strict',
    # mask the result and round floats.
    if space.isinstance_w(w_ob, space.w_int):  # shortcut
        value = space.int_w(w_ob)
        if strict and value < 0:
            raise OperationError(space.w_OverflowError, space.newtext(neg_msg))
        return r_ulonglong(value)
    try:
        bigint = space.bigint_w(w_ob, allow_conversion=False)
    except OperationError as e:
        if not e.match(space, space.w_TypeError):
            raise
        if strict and _is_a_float(space, w_ob):
            raise
        bigint = space.bigint_w(space.int(w_ob), allow_conversion=False)
    if strict:
        try:
            return bigint.toulonglong()
        except ValueError:
            raise OperationError(space.w_OverflowError, space.newtext(neg_msg))
        except OverflowError:
            raise OperationError(space.w_OverflowError, space.newtext(ovf_msg))
    else:
        return bigint.ulonglongmask()
Пример #27
0
def BIT_MASK(x, ll_t):
    if ll_t is lltype.SignedLongLong or ll_t is lltype.UnsignedLongLong:
        one = r_ulonglong(1)
    else:
        one = r_uint(1)
    # to avoid left shift by x == sizeof(ll_t)
    return (((one << (x - 1)) - 1) << 1) + 1
Пример #28
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)
Пример #29
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
Пример #30
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)
Пример #31
0
def init_random():
    n = r_ulonglong(time.time())
    key = []
    while n > 0:
        key.append(r_uint(n & masklower))
        n >>= 32
    if len(key) == 0:
        key.append(r_uint(0))
    random.init_by_array(key)
Пример #32
0
 def test_struct_fields_longlong(self):
     POINT = lltype.Struct('POINT', ('x', rffi.LONGLONG),
                           ('y', rffi.ULONGLONG))
     y_ofs = 8
     p = lltype.malloc(POINT, flavor='raw')
     p.x = r_longlong(123)
     p.y = r_ulonglong(456)
     addr = rffi.cast(rffi.VOIDP, p)
     assert struct_getfield_longlong(types.slonglong, addr, 0) == 123
     assert struct_getfield_longlong(types.ulonglong, addr, y_ofs) == 456
     #
     v = rffi.cast(lltype.SignedLongLong, r_ulonglong(9223372036854775808))
     struct_setfield_longlong(types.slonglong, addr, 0, v)
     struct_setfield_longlong(types.ulonglong, addr, y_ofs, r_longlong(-1))
     assert p.x == -9223372036854775808
     assert rffi.cast(lltype.UnsignedLongLong, p.y) == 18446744073709551615
     #
     lltype.free(p, flavor='raw')
Пример #33
0
def init_random():
    n = r_ulonglong(time.time())
    key = []
    while n > 0:
        key.append(r_uint(n & masklower))
        n >>= 32
    if len(key) == 0:
        key.append(r_uint(0))
    random.init_by_array(key)
Пример #34
0
    def test_longlongmask(self):
        def f(x=r_ulonglong):
            try:
                return longlongmask(x)
            except ValueError:
                return 0

        res = self.interpret(f, [r_ulonglong(5)])
        assert type(res) is r_int64 and res == 5
Пример #35
0
 def store(self, space, n0, w_obj):
     uint = r_ulonglong(space.unwrap_uint(w_obj))
     r = float_pack(self.getvalue(), 8)
     if n0 == 0:
         r = ((r << 32) >> 32) | (uint << 32)
     else:
         assert n0 == 1
         r = ((r >> 32) << 32) | uint
     self.value = float_unpack(r, 8)
Пример #36
0
 def store(self, space, n0, w_obj):
     uint = r_ulonglong(space.unwrap_uint(w_obj))
     r = float_pack(self.getvalue(), 8)
     if n0 == 0:
         r = ((r << 32) >> 32) | (uint << 32)
     else:
         assert n0 == 1
         r = ((r >> 32) << 32) | uint
     self.value = float_unpack(r, 8)
Пример #37
0
 def process_time(space, w_info=None):
     from rpython.rlib.rposix import GetCurrentProcess, GetProcessTimes
     current_process = GetCurrentProcess()
     with lltype.scoped_alloc(rwin32.FILETIME) as creation_time, \
          lltype.scoped_alloc(rwin32.FILETIME) as exit_time, \
          lltype.scoped_alloc(rwin32.FILETIME) as kernel_time, \
          lltype.scoped_alloc(rwin32.FILETIME) as user_time:
         worked = GetProcessTimes(current_process, creation_time, exit_time,
                                  kernel_time, user_time)
         if not worked:
             raise wrap_oserror(
                 space, rwin32.lastSavedWindowsError("GetProcessTimes"))
         kernel_time2 = (kernel_time.c_dwLowDateTime
                         | r_ulonglong(kernel_time.c_dwHighDateTime) << 32)
         user_time2 = (user_time.c_dwLowDateTime
                       | r_ulonglong(user_time.c_dwHighDateTime) << 32)
     if w_info is not None:
         _setinfo(space, w_info, "GetProcessTimes()", 1e-7, True, False)
     return space.newfloat((float(kernel_time2) + float(user_time2)) * 1e-7)
Пример #38
0
 def test_ulonglong_args(self):
     """
         unsigned long long sum_xy_ulonglong(unsigned long long x,
                                             unsigned long long y)
         {
             return x+y;
         }
     """
     maxint64 = 9223372036854775807 # maxint64+1 does not fit into a
                                    # longlong, but it does into a
                                    # ulonglong
     libfoo = self.get_libfoo()
     func = (libfoo, 'sum_xy_ulonglong', [types.ulonglong, types.ulonglong],
             types.ulonglong)
     x = r_ulonglong(maxint64+1)
     y = r_ulonglong(2)
     res = self.call(func, [x, y], rffi.ULONGLONG, jitif=["longlong"])
     expected = maxint64 + 3
     assert res == expected
Пример #39
0
 def convert_bitfield_to_object(self, cdata):
     ctype = self.ctype
     space = ctype.space
     #
     if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned):
         if ctype.value_fits_long:
             value = r_uint(misc.read_raw_long_data(cdata, ctype.size))
             valuemask = (r_uint(1) << self.bitsize) - 1
             shiftforsign = r_uint(1) << (self.bitsize - 1)
             value = ((value >> self.bitshift) + shiftforsign) & valuemask
             result = intmask(value) - intmask(shiftforsign)
             return space.wrap(result)
         else:
             value = misc.read_raw_unsigned_data(cdata, ctype.size)
             valuemask = (r_ulonglong(1) << self.bitsize) - 1
             shiftforsign = r_ulonglong(1) << (self.bitsize - 1)
             value = ((value >> self.bitshift) + shiftforsign) & valuemask
             result = r_longlong(value) - r_longlong(shiftforsign)
             return space.wrap(result)
     #
     if isinstance(ctype, ctypeprim.W_CTypePrimitiveUnsigned):
         value_fits_long = ctype.value_fits_long
         value_fits_ulong = ctype.value_fits_ulong
     elif isinstance(ctype, ctypeprim.W_CTypePrimitiveCharOrUniChar):
         value_fits_long = True
         value_fits_ulong = True
     else:
         raise NotImplementedError
     #
     if value_fits_ulong:
         value = misc.read_raw_ulong_data(cdata, ctype.size)
         valuemask = (r_uint(1) << self.bitsize) - 1
         value = (value >> self.bitshift) & valuemask
         if value_fits_long:
             return space.wrap(intmask(value))
         else:
             return space.wrap(value)    # uint => wrapped long object
     else:
         value = misc.read_raw_unsigned_data(cdata, ctype.size)
         valuemask = (r_ulonglong(1) << self.bitsize) - 1
         value = (value >> self.bitshift) & valuemask
         return space.wrap(value)      # ulonglong => wrapped long object
Пример #40
0
 def convert_bitfield_to_object(self, cdata):
     ctype = self.ctype
     space = ctype.space
     #
     if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned):
         if ctype.value_fits_long:
             value = r_uint(misc.read_raw_long_data(cdata, ctype.size))
             valuemask = (r_uint(1) << self.bitsize) - 1
             shiftforsign = r_uint(1) << (self.bitsize - 1)
             value = ((value >> self.bitshift) + shiftforsign) & valuemask
             result = intmask(value) - intmask(shiftforsign)
             return space.wrap(result)
         else:
             value = misc.read_raw_unsigned_data(cdata, ctype.size)
             valuemask = (r_ulonglong(1) << self.bitsize) - 1
             shiftforsign = r_ulonglong(1) << (self.bitsize - 1)
             value = ((value >> self.bitshift) + shiftforsign) & valuemask
             result = r_longlong(value) - r_longlong(shiftforsign)
             return space.wrap(result)
     #
     if isinstance(ctype, ctypeprim.W_CTypePrimitiveUnsigned):
         value_fits_long = ctype.value_fits_long
         value_fits_ulong = ctype.value_fits_ulong
     elif isinstance(ctype, ctypeprim.W_CTypePrimitiveCharOrUniChar):
         value_fits_long = True
         value_fits_ulong = True
     else:
         raise NotImplementedError
     #
     if value_fits_ulong:
         value = misc.read_raw_ulong_data(cdata, ctype.size)
         valuemask = (r_uint(1) << self.bitsize) - 1
         value = (value >> self.bitshift) & valuemask
         if value_fits_long:
             return space.wrap(intmask(value))
         else:
             return space.wrap(value)  # uint => wrapped long object
     else:
         value = misc.read_raw_unsigned_data(cdata, ctype.size)
         valuemask = (r_ulonglong(1) << self.bitsize) - 1
         value = (value >> self.bitshift) & valuemask
         return space.wrap(value)  # ulonglong => wrapped long object
Пример #41
0
 def immutable_unique_id(self, space):
     if self.user_overridden_class:
         return None
     from rpython.rlib.longlong2float import float2longlong
     from pypy.objspace.std.util import IDTAG_COMPLEX as tag
     real = space.float_w(space.getattr(self, space.wrap("real")))
     imag = space.float_w(space.getattr(self, space.wrap("imag")))
     real_b = rbigint.fromrarith_int(float2longlong(real))
     imag_b = rbigint.fromrarith_int(r_ulonglong(float2longlong(imag)))
     val = real_b.lshift(64).or_(imag_b).lshift(3).or_(rbigint.fromint(tag))
     return space.newlong_from_rbigint(val)
Пример #42
0
 def immutable_unique_id(self, space):
     if self.user_overridden_class:
         return None
     from rpython.rlib.longlong2float import float2longlong
     from pypy.objspace.std.util import IDTAG_COMPLEX as tag
     real = space.float_w(space.getattr(self, space.wrap("real")))
     imag = space.float_w(space.getattr(self, space.wrap("imag")))
     real_b = rbigint.fromrarith_int(float2longlong(real))
     imag_b = rbigint.fromrarith_int(r_ulonglong(float2longlong(imag)))
     val = real_b.lshift(64).or_(imag_b).lshift(3).or_(rbigint.fromint(tag))
     return space.newlong_from_rbigint(val)
Пример #43
0
 def test_ulonglong_args(self):
     """
         RPY_EXPORTED
         unsigned long long sum_xy_ulonglong(unsigned long long x,
                                             unsigned long long y)
         {
             return x+y;
         }
     """
     maxint64 = 9223372036854775807  # maxint64+1 does not fit into a
     # longlong, but it does into a
     # ulonglong
     libfoo = self.get_libfoo()
     func = (libfoo, 'sum_xy_ulonglong', [types.ulonglong,
                                          types.ulonglong], types.ulonglong)
     x = r_ulonglong(maxint64 + 1)
     y = r_ulonglong(2)
     res = self.call(func, [x, y], rffi.ULONGLONG, jitif=["longlong"])
     expected = maxint64 + 3
     assert res == expected
Пример #44
0
 def test_struct_fields_longlong(self):
     POINT = lltype.Struct('POINT',
                           ('x', rffi.LONGLONG),
                           ('y', rffi.ULONGLONG)
                           )
     y_ofs = 8
     p = lltype.malloc(POINT, flavor='raw')
     p.x = r_longlong(123)
     p.y = r_ulonglong(456)
     addr = rffi.cast(rffi.VOIDP, p)
     assert struct_getfield_longlong(types.slonglong, addr, 0) == 123
     assert struct_getfield_longlong(types.ulonglong, addr, y_ofs) == 456
     #
     v = rffi.cast(lltype.SignedLongLong, r_ulonglong(9223372036854775808))
     struct_setfield_longlong(types.slonglong, addr, 0, v)
     struct_setfield_longlong(types.ulonglong, addr, y_ofs, r_longlong(-1))
     assert p.x == -9223372036854775808
     assert rffi.cast(lltype.UnsignedLongLong, p.y) == 18446744073709551615
     #
     lltype.free(p, flavor='raw')
Пример #45
0
 def newint(self, intval):
     if self.config.objspace.std.withsmalllong and isinstance(intval, base_int):
         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(intval, r_ulonglong)
             or intval <= r_ulonglong(longlongmax)):
             return W_SmallLongObject(r_longlong(intval))
     intval = widen(intval)
     if not isinstance(intval, int):
         return W_LongObject.fromrarith_int(intval)
     return wrapint(self, intval)
Пример #46
0
def PyInt_AsUnsignedLongLongMask(space, w_obj):
    """Will first attempt to cast the object to a PyIntObject or
    PyLongObject, if it is not already one, and then return its value as
    unsigned long long, without checking for overflow.
    """
    w_int = space.int(w_obj)
    if space.isinstance_w(w_int, space.w_int):
        num = space.int_w(w_int)
        return r_ulonglong(num)
    else:
        num = space.bigint_w(w_int)
        return num.ulonglongmask()
Пример #47
0
    def _init(self):
        """Set this object to an initial empty state.
        """
        self.count = r_ulonglong(0)  # total number of bytes
        self.input = ""  # pending unprocessed data, < 64 bytes
        self.uintbuffer = [r_uint(0)] * 16

        # Load magic initialization constants.
        self.A = r_uint(0x67452301L)
        self.B = r_uint(0xefcdab89L)
        self.C = r_uint(0x98badcfeL)
        self.D = r_uint(0x10325476L)
Пример #48
0
    def _init(self):
        """Set this object to an initial empty state.
        """
        self.count = r_ulonglong(0)   # total number of bytes
        self.input = ""   # pending unprocessed data, < 64 bytes
        self.uintbuffer = [r_uint(0)] * 16

        # Load magic initialization constants.
        self.A = r_uint(0x67452301L)
        self.B = r_uint(0xefcdab89L)
        self.C = r_uint(0x98badcfeL)
        self.D = r_uint(0x10325476L)
Пример #49
0
 def test_truncatedlonglong_w(self):
     space = self.space
     w_value = space.wrap(12)
     res = space.truncatedlonglong_w(w_value)
     assert res == 12
     assert type(res) is r_longlong
     #
     w_value = space.wrap(r_ulonglong(9223372036854775808))
     res = space.truncatedlonglong_w(w_value)
     assert res == -9223372036854775808
     assert type(res) is r_longlong
     #
     w_value = space.wrap(r_ulonglong(18446744073709551615))
     res = space.truncatedlonglong_w(w_value)
     assert res == -1
     assert type(res) is r_longlong
     #
     w_value = space.wrap(r_ulonglong(18446744073709551616))
     res = space.truncatedlonglong_w(w_value)
     assert res == 0
     assert type(res) is r_longlong
Пример #50
0
 def test_truncatedlonglong_w(self):
     space = self.space
     w_value = space.wrap(12)
     res = space.truncatedlonglong_w(w_value)
     assert res == 12
     assert type(res) is r_longlong
     #
     w_value = space.wrap(r_ulonglong(9223372036854775808))
     res = space.truncatedlonglong_w(w_value)
     assert res == -9223372036854775808
     assert type(res) is r_longlong
     #
     w_value = space.wrap(r_ulonglong(18446744073709551615))
     res = space.truncatedlonglong_w(w_value)
     assert res == -1
     assert type(res) is r_longlong
     #
     w_value = space.wrap(r_ulonglong(18446744073709551616))
     res = space.truncatedlonglong_w(w_value)
     assert res == 0
     assert type(res) is r_longlong
Пример #51
0
def PyInt_AsUnsignedLongLongMask(space, w_obj):
    """Will first attempt to cast the object to a PyIntObject or
    PyLongObject, if it is not already one, and then return its value as
    unsigned long long, without checking for overflow.
    """
    w_int = space.int(w_obj)
    if space.isinstance_w(w_int, space.w_int):
        num = space.int_w(w_int)
        return r_ulonglong(num)
    else:
        num = space.bigint_w(w_int)
        return num.ulonglongmask()
Пример #52
0
    def _init(self):
        "Initialisation."
        self.count = r_ulonglong(0)   # total number of bytes
        self.input = ""   # pending unprocessed data, < 64 bytes
        self.uintbuffer = [r_uint(0)] * 80

        # Initial 160 bit message digest (5 times 32 bit).
        self.H0 = r_uint(0x67452301L)
        self.H1 = r_uint(0xEFCDAB89L)
        self.H2 = r_uint(0x98BADCFEL)
        self.H3 = r_uint(0x10325476L)
        self.H4 = r_uint(0xC3D2E1F0L)
Пример #53
0
 def test_compare_big_ullongs(self):
     bigval = r_ulonglong(9223372036854775808L)
     def fn(x):
         if x > bigval: return 1
         if x == bigval: return 0
         if x < bigval: return -1
         return -2
     
     for val in (bigval-1, bigval, bigval+1):
         expected = fn(val)
         res = self.interpret(fn, [val])
         assert res == expected
Пример #54
0
    def _init(self):
        "Initialisation."
        self.count = r_ulonglong(0)  # total number of bytes
        self.input = ""  # pending unprocessed data, < 64 bytes
        self.uintbuffer = [r_uint(0)] * 80

        # Initial 160 bit message digest (5 times 32 bit).
        self.H0 = r_uint(0x67452301L)
        self.H1 = r_uint(0xEFCDAB89L)
        self.H2 = r_uint(0x98BADCFEL)
        self.H3 = r_uint(0x10325476L)
        self.H4 = r_uint(0xC3D2E1F0L)
Пример #55
0
 def wrap_int(self, val):
     if isinstance(val, r_longlong) and not is_valid_int(val):
         if val > 0 and r_ulonglong(val) < r_uint(constants.U_MAXINT):
             return self.wrap_positive_32bit_int(intmask(val))
         else:
             raise WrappingError
     elif isinstance(val, r_uint):
         return self.wrap_positive_32bit_int(intmask(val))
     elif not is_valid_int(val):
         raise WrappingError
     # we don't do tagging
     return model.W_SmallInteger(intmask(val))
Пример #56
0
    def store(self, space, n0, w_obj):
        from rpython.rlib.rstruct.ieee import float_unpack, float_pack
        from rpython.rlib.rarithmetic import r_ulonglong

        uint = r_ulonglong(space.unwrap_uint(w_obj))
        r = float_pack(self.value, 8)
        if n0 == 0:
            r = ((r << 32) >> 32) | (uint << 32)
        else:
            assert n0 == 1
            r = ((r >> 32) << 32) | uint
        self.value = float_unpack(r, 8)
Пример #57
0
def marshal_w__Long(space, w_long, m):
    from rpython.rlib.rarithmetic import r_ulonglong
    m.start(TYPE_LONG)
    SHIFT = 15
    MASK = (1 << SHIFT) - 1
    num = w_long.asbigint()
    sign = num.sign
    num = num.abs()
    total_length = (num.bit_length() + (SHIFT - 1)) / SHIFT
    m.put_int(total_length * sign)
    bigshiftcount = r_ulonglong(0)
    for i in range(total_length):
        next = num.abs_rshift_and_mask(bigshiftcount, MASK)
        m.put_short(next)
        bigshiftcount += SHIFT