def int_c_mod(x, y): """Return the result of the C-style 'x % y'. This differs from the Python-style division if (x < 0 xor y < 0). """ from rpython.rtyper.lltypesystem import lltype from rpython.rtyper.lltypesystem.lloperation import llop return llop.int_mod(lltype.Signed, x, y)
def int_mod_zer(x, y): '''#define OP_INT_MOD_ZER(x,y,r,err) \ if ((y)) { OP_INT_MOD(x,y,r,err); } \ else FAIL_ZER(err, "integer modulo") ''' if y: return llop.int_mod(Signed, x, y) else: raise ZeroDivisionError("integer modulo")
def llong_mod_zer(x, y): '''#define OP_LLONG_MOD_ZER(x,y,r) \ if ((y)) { OP_LLONG_MOD(x,y,r); } \ else FAIL_ZER("integer modulo") ''' if y: return llop.int_mod(SignedLongLong, x, y) else: raise ZeroDivisionError("integer modulo")
def int_mod_ovf(x, y): '''#define OP_INT_MOD_OVF(x,y,r,err) \ if ((y) == -1 && (x) < 0 && ((unsigned long)(x) << 1) == 0) \ FAIL_OVF(err, "integer modulo"); \ OP_INT_MOD(x,y,r,err) ''' if y == -1 and x < 0 and (r_uint(x) << 1) == 0: raise OverflowError("integer modulo") else: return llop.int_mod(Signed, x, y)
def test_int_floordiv_mod(): from rpython.rtyper.lltypesystem.lloperation import llop from rpython.jit.codewriter.support import _ll_2_int_floordiv, _ll_2_int_mod for x in range(-6, 7): for y in range(-3, 4): if y != 0: assert (_ll_2_int_floordiv(x, y) == llop.int_floordiv(lltype.Signed, x, y)) assert (_ll_2_int_mod(x, y) == llop.int_mod(lltype.Signed, x, y))
def test_int_floordiv_mod(): from rpython.rtyper.lltypesystem.lloperation import llop from rpython.jit.codewriter.support import _ll_2_int_floordiv, _ll_2_int_mod for x in range(-6, 7): for y in range(-3, 4): if y != 0: assert (_ll_2_int_floordiv(x, y) == llop.int_floordiv( lltype.Signed, x, y)) assert (_ll_2_int_mod(x, y) == llop.int_mod(lltype.Signed, x, y))
def prim_remainder(self, right, universe): if isinstance(right, BigInteger): d, r = _divrem(rbigint.fromint(self._embedded_integer), right.get_embedded_biginteger()) return universe.new_biginteger(r) elif isinstance(right, Double): return self._to_double(universe).prim_remainder(right, universe) else: l = self._embedded_integer r = right.get_embedded_integer() return universe.new_integer(llop.int_mod(lltype.Signed, l, r))
def _ll_2_int_mod_ovf(x, y): if x == -sys.maxint - 1 and y == -1: raise OverflowError return llop.int_mod(lltype.Signed, x, y)
def ll_int_py_mod_nonnegargs(x, y): from rpython.rlib.debug import ll_assert r = llop.int_mod(Signed, x, y) # <= truncates like in C ll_assert(r >= 0, "int_py_mod_nonnegargs(): one arg is negative") return r
def int_mod(space, n, m): return space.wrap(llop.int_mod(lltype.Signed, n, m))
def ll_int_py_mod(x, y): r = llop.int_mod(Signed, x, y) # <= truncates like in C if y < 0: u = -r else: u = r return r + (y & (u >> INT_BITS_1))
def _ll_2_int_mod_ovf(x, y): #see comment in _ll_2_int_floordiv_ovf if (x == -sys.maxint - 1) & (y == -1): raise OverflowError return llop.int_mod(lltype.Signed, x, y)
def _ll_2_int_mod_zer(x, y): if y == 0: raise ZeroDivisionError return llop.int_mod(lltype.Signed, x, y)
def _ll_2_int_mod_ovf_zer(x, y): if y == 0: raise ZeroDivisionError if x == -sys.maxint - 1 and y == -1: raise OverflowError return llop.int_mod(lltype.Signed, x, y)