| 1 | from pypy.interpreter.gateway import unwrap_spec |
| 2 | from pypy.interpreter.error import oefmt |
| 3 | from rpython.rtyper.lltypesystem import lltype |
| 4 | from rpython.rtyper.lltypesystem.lloperation import llop |
| 5 | from rpython.rlib.rarithmetic import r_uint, intmask |
| 6 | from rpython.rlib.rarithmetic import int_c_div, int_c_mod, mulmod |
| 7 | from rpython.rlib import jit |
| 8 | |
| 9 | |
| 10 | @unwrap_spec(n=int, m=int) |
| 11 | def int_add(space, n, m): |
| 12 | return space.newint(llop.int_add(lltype.Signed, n, m)) |
| 13 | |
| 14 | @unwrap_spec(n=int, m=int) |
| 15 | def int_sub(space, n, m): |
| 16 | return space.newint(llop.int_sub(lltype.Signed, n, m)) |
| 17 | |
| 18 | @unwrap_spec(n=int, m=int) |
| 19 | def int_mul(space, n, m): |
| 20 | return space.newint(llop.int_mul(lltype.Signed, n, m)) |
| 21 | |
| 22 | @unwrap_spec(n=int, m=int) |
| 23 | def int_floordiv(space, n, m): |
| 24 | if m == 0: |
| 25 | raise oefmt(space.w_ZeroDivisionError, "division by zero") |
| 26 | return space.newint(int_c_div(n, m)) |
| 27 | |
| 28 | @unwrap_spec(n=int, m=int) |
| 29 | def int_mod(space, n, m): |
| 30 | if m == 0: |
| 31 | raise oefmt(space.w_ZeroDivisionError, "division by zero") |
| 32 | return space.newint(int_c_mod(n, m)) |
| 33 | |
| 34 | @unwrap_spec(n=int, m=int) |
| 35 | def int_lshift(space, n, m): |
| 36 | return space.newint(llop.int_lshift(lltype.Signed, n, m)) |
| 37 | |
| 38 | @unwrap_spec(n=int, m=int) |
| 39 | def int_rshift(space, n, m): |
| 40 | return space.newint(llop.int_rshift(lltype.Signed, n, m)) |
| 41 | |
| 42 | @unwrap_spec(n=int, m=int) |
| 43 | def uint_rshift(space, n, m): |
| 44 | n = r_uint(n) |
| 45 | x = llop.uint_rshift(lltype.Unsigned, n, m) |
| 46 | return space.newint(intmask(x)) |
| 47 | |
| 48 | @unwrap_spec(a=int, b=int, c=int) |
| 49 | def int_mulmod(space, a, b, c): |
| 50 | if c <= 0: |
| 51 | raise oefmt(space.w_ValueError, "'c' must be greater than 0") |
| 52 | return space.newint(mulmod(a, b, c)) |