Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class SentinelValue(NamedTuple):
"mypy_extensions.i64",
"mypy_extensions.i32",
"mypy_extensions.i16",
"mypy_extensions.i8",
"mypy_extensions.u64",
"mypy_extensions.u32",
"mypy_extensions.u16",
Expand Down
37 changes: 37 additions & 0 deletions mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,43 @@ class i16:
def __eq__(self, x: object) -> bool: ...
def __bool__(self) -> bool: ...

class i8:
@overload
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> i8: ...
@overload
def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> i8: ...

def __add__(self, x: i8) -> i8: ...
def __radd__(self, x: i8) -> i8: ...
def __sub__(self, x: i8) -> i8: ...
def __rsub__(self, x: i8) -> i8: ...
def __mul__(self, x: i8) -> i8: ...
def __rmul__(self, x: i8) -> i8: ...
def __floordiv__(self, x: i8) -> i8: ...
def __rfloordiv__(self, x: i8) -> i8: ...
def __mod__(self, x: i8) -> i8: ...
def __rmod__(self, x: i8) -> i8: ...
def __and__(self, x: i8) -> i8: ...
def __rand__(self, x: i8) -> i8: ...
def __or__(self, x: i8) -> i8: ...
def __ror__(self, x: i8) -> i8: ...
def __xor__(self, x: i8) -> i8: ...
def __rxor__(self, x: i8) -> i8: ...
def __lshift__(self, x: i8) -> i8: ...
def __rlshift__(self, x: i8) -> i8: ...
def __rshift__(self, x: i8) -> i8: ...
def __rrshift__(self, x: i8) -> i8: ...
def __neg__(self) -> i8: ...
def __invert__(self) -> i8: ...
def __pos__(self) -> i8: ...
def __lt__(self, x: i8) -> bool: ...
def __le__(self, x: i8) -> bool: ...
def __ge__(self, x: i8) -> bool: ...
def __gt__(self, x: i8) -> bool: ...
def __index__(self) -> int: ...
def __eq__(self, x: object) -> bool: ...
def __bool__(self) -> bool: ...

class u64:
@overload
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> u64: ...
Expand Down
9 changes: 9 additions & 0 deletions mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
is_fixed_width_rtype,
is_float_rprimitive,
is_frozenset_rprimitive,
is_int8_rprimitive,
is_int16_rprimitive,
is_int32_rprimitive,
is_int64_rprimitive,
Expand Down Expand Up @@ -1169,6 +1170,13 @@ def emit_unbox(
self.emit_line(f"{dest} = CPyLong_AsInt16({src});")
if not isinstance(error, AssignHandler):
self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
elif is_int8_rprimitive(typ):
assert not optional
if declare_dest:
self.emit_line(f"int8_t {dest};")
self.emit_line(f"{dest} = CPyLong_AsInt8({src});")
if not isinstance(error, AssignHandler):
self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
elif is_uint32_rprimitive(typ):
assert not optional
if declare_dest:
Expand Down Expand Up @@ -1361,6 +1369,7 @@ def emit_box(
elif (
is_int32_rprimitive(typ)
or is_int16_rprimitive(typ)
or is_int8_rprimitive(typ)
or is_uint16_rprimitive(typ)
or is_uint8_rprimitive(typ)
):
Expand Down
2 changes: 2 additions & 0 deletions mypyc/doc/float_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Construction
* ``float(x: i64)``
* ``float(x: i32)``
* ``float(x: i16)``
* ``float(x: i8)``
* ``float(x: u16)``
* ``float(x: u8)``
* ``float(x: str)``
Expand Down Expand Up @@ -43,6 +44,7 @@ Functions
* ``i64(f)`` (convert to 64-bit signed integer)
* ``i32(f)`` (convert to 32-bit signed integer)
* ``i16(f)`` (convert to 16-bit signed integer)
* ``i8(f)`` (convert to 8-bit signed integer)
* ``u16(f)`` (convert to 16-bit unsigned integer)
* ``u8(f)`` (convert to 8-bit unsigned integer)
* ``abs(f)``
Expand Down
31 changes: 28 additions & 3 deletions mypyc/doc/int_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ Mypyc supports these integer types:
* ``i64`` (64-bit signed integer)
* ``i32`` (32-bit signed integer)
* ``i16`` (16-bit signed integer)
* ``i8`` (8-bit signed integer)
* ``u64`` (64-bit unsigned integer)
* ``u32`` (32-bit unsigned integer)
* ``u16`` (16-bit unsigned integer)
* ``u8`` (8-bit unsigned integer)

``i64``, ``i32``, ``i16``, ``u64``, ``u32``, ``u16`` and ``u8`` are *native integer types* and
``i64``, ``i32``, ``i16``, ``i8``, ``u64``, ``u32``, ``u16`` and ``u8`` are *native integer types* and
are available in the ``mypy_extensions`` module. ``int`` corresponds
to the Python ``int`` type, but uses a more efficient runtime
representation (tagged pointer). Native integer types are value types.
Expand All @@ -38,6 +39,7 @@ Construction
* ``int(x: i64)``
* ``int(x: i32)``
* ``int(x: i16)``
* ``int(x: i8)``
* ``int(x: u64)``
* ``int(x: u32)``
* ``int(x: u16)``
Expand All @@ -53,6 +55,7 @@ Construction
* ``i64(x: i64)`` (no-op)
* ``i64(x: i32)``
* ``i64(x: i16)``
* ``i64(x: i8)``
* ``i64(x: u64)``
* ``i64(x: u32)``
* ``i64(x: u16)``
Expand All @@ -67,6 +70,7 @@ Construction
* ``i32(x: i64)`` (truncate)
* ``i32(x: i32)`` (no-op)
* ``i32(x: i16)``
* ``i32(x: i8)``
* ``i32(x: u64)`` (truncate)
* ``i32(x: u32)``
* ``i32(x: u16)``
Expand All @@ -81,20 +85,37 @@ Construction
* ``i16(x: i64)`` (truncate)
* ``i16(x: i32)`` (truncate)
* ``i16(x: i16)`` (no-op)
* ``i16(x: i8)``
* ``i16(x: u64)`` (truncate)
* ``i16(x: u32)`` (truncate)
* ``i16(x: u16)``
* ``i16(x: u8)``
* ``i16(x: str)``
* ``i16(x: str, base: int)``

``i8`` type:

* ``i8(x: int)``
* ``i8(x: float)``
* ``i8(x: i64)`` (truncate)
* ``i8(x: i32)`` (truncate)
* ``i8(x: i16)`` (truncate)
* ``i8(x: i8)`` (no-op)
* ``i8(x: u64)`` (truncate)
* ``i8(x: u32)`` (truncate)
* ``i8(x: u16)`` (truncate)
* ``i8(x: u8)``
* ``i8(x: str)``
* ``i8(x: str, base: int)``

``u64`` type:

* ``u64(x: int)``
* ``u64(x: float)``
* ``u64(x: i64)``
* ``u64(x: i32)``
* ``u64(x: i16)``
* ``u64(x: i8)``
* ``u64(x: u64)`` (no-op)
* ``u64(x: u32)``
* ``u64(x: u16)``
Expand All @@ -109,6 +130,7 @@ Construction
* ``u32(x: i64)`` (truncate)
* ``u32(x: i32)``
* ``u32(x: i16)``
* ``u32(x: i8)``
* ``u32(x: u64)`` (truncate)
* ``u32(x: u32)`` (no-op)
* ``u32(x: u16)``
Expand All @@ -123,6 +145,7 @@ Construction
* ``u16(x: i64)`` (truncate)
* ``u16(x: i32)`` (truncate)
* ``u16(x: i16)``
* ``u16(x: i8)``
* ``u16(x: u64)`` (truncate)
* ``u16(x: u32)`` (truncate)
* ``u16(x: u16)`` (no-op)
Expand All @@ -137,6 +160,7 @@ Construction
* ``u8(x: i64)`` (truncate)
* ``u8(x: i32)`` (truncate)
* ``u8(x: i16)`` (truncate)
* ``u8(x: i8)``
* ``u8(x: u64)`` (truncate)
* ``u8(x: u32)`` (truncate)
* ``u8(x: u16)`` (truncate)
Expand All @@ -145,7 +169,7 @@ Construction
* ``u8(x: str, base: int)``

Conversions from ``int`` to a native integer type raise
``OverflowError`` if the value is too large or small. Conversions from
``ValueError`` if the value is too large or small. Conversions from
a wider native integer type to a narrower one truncate the value and never
fail. Conversions between signed and unsigned types of the same or larger
size don't fail either: a value outside the target range wraps around
Expand Down Expand Up @@ -195,7 +219,8 @@ the type. For this reason, the arbitrary-precision ``int`` type is
recommended for signed values unless the performance of integer
operations is critical.

Operations on unsigned integers (``u32``, ``u16`` and ``u8``) wrap around on overflow.
Operations on unsigned integers (``u64``, ``u32``, ``u16`` and ``u8``) wrap
around on overflow.

It's a compile-time error to mix different native integer types in a
binary operation such as addition. An explicit conversion is required::
Expand Down
3 changes: 2 additions & 1 deletion mypyc/doc/using_type_annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ implementations:
* ``i64`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``i32`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``i16`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``i8`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``u64`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``u32`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``u16`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
Expand Down Expand Up @@ -351,7 +352,7 @@ Native integer types
--------------------

You can use the native integer types ``i64`` (64-bit signed integer),
``i32`` (32-bit signed integer), ``i16`` (16-bit signed integer),
``i32`` (32-bit signed integer), ``i16`` (16-bit signed integer), ``i8`` (8-bit signed integer),
``u64`` (64-bit unsigned integer), ``u32`` (32-bit unsigned integer), ``u16`` (16-bit unsigned integer), and ``u8`` (8-bit unsigned integer)
if you know that integer values will
always fit within fixed bounds. These types are faster than the
Expand Down
19 changes: 18 additions & 1 deletion mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def __init__(
self.dependencies = dependencies
if ctype == "CPyTagged":
self.c_undefined = "CPY_INT_TAG"
elif ctype in ("int16_t", "int32_t", "int64_t"):
elif ctype in ("int8_t", "int16_t", "int32_t", "int64_t"):
# This is basically an arbitrary value that is pretty
# unlikely to overlap with a real value.
self.c_undefined = "-113"
Expand Down Expand Up @@ -370,6 +370,16 @@ def __hash__(self) -> int:

# Low level integer types (correspond to C integer types)

int8_rprimitive: Final = RPrimitive(
"i8",
is_unboxed=True,
is_refcounted=False,
is_native_int=True,
is_signed=True,
ctype="int8_t",
size=1,
error_overlap=True,
)
int16_rprimitive: Final = RPrimitive(
"i16",
is_unboxed=True,
Expand Down Expand Up @@ -613,6 +623,10 @@ def is_int16_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return rtype is int16_rprimitive


def is_int8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return rtype is int8_rprimitive


def is_int32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return rtype is int32_rprimitive or (
rtype is c_pyssize_t_rprimitive and rtype._ctype == "int32_t"
Expand All @@ -630,6 +644,7 @@ def is_fixed_width_rtype(rtype: RType) -> TypeGuard[RPrimitive]:
is_int64_rprimitive(rtype)
or is_int32_rprimitive(rtype)
or is_int16_rprimitive(rtype)
or is_int8_rprimitive(rtype)
or is_uint64_rprimitive(rtype)
or is_uint32_rprimitive(rtype)
or is_uint16_rprimitive(rtype)
Expand Down Expand Up @@ -766,6 +781,8 @@ def visit_rprimitive(self, t: RPrimitive) -> str:
return "4" # "4 byte integer"
elif t._ctype == "int16_t":
return "2" # "2 byte integer"
elif t._ctype == "int8_t":
return "1" # "1 byte integer"
elif t._ctype == "uint64_t":
return "U8"
elif t._ctype == "uint32_t":
Expand Down
11 changes: 11 additions & 0 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
is_fixed_width_rtype,
is_float_rprimitive,
is_frozenset_rprimitive,
is_int8_rprimitive,
is_int16_rprimitive,
is_int32_rprimitive,
is_int64_rprimitive,
Expand Down Expand Up @@ -161,6 +162,9 @@
py_vectorcall_op,
)
from mypyc.primitives.int_ops import (
int8_divide_op,
int8_mod_op,
int8_overflow,
int16_divide_op,
int16_mod_op,
int16_overflow,
Expand Down Expand Up @@ -766,6 +770,8 @@ def emit_fixed_width_overflow_error(self, target_type: RType, line: int) -> None
self.call_c(int32_overflow, [], line)
elif is_int16_rprimitive(target_type):
self.call_c(int16_overflow, [], line)
elif is_int8_rprimitive(target_type):
self.call_c(int8_overflow, [], line)
elif is_uint64_rprimitive(target_type):
self.call_c(uint64_overflow, [], line)
elif is_uint32_rprimitive(target_type):
Expand Down Expand Up @@ -822,6 +828,7 @@ def coerce_fixed_width_to_int(self, src: Value, line: int) -> Value:
if (
(is_int32_rprimitive(src.type) and PLATFORM_SIZE == 8)
or is_int16_rprimitive(src.type)
or is_int8_rprimitive(src.type)
or (is_uint32_rprimitive(src.type) and PLATFORM_SIZE == 8)
or is_uint16_rprimitive(src.type)
or is_uint8_rprimitive(src.type)
Expand Down Expand Up @@ -2808,6 +2815,8 @@ def fixed_width_int_op(
prim = int32_divide_op
elif is_int16_rprimitive(type):
prim = int16_divide_op
elif is_int8_rprimitive(type):
prim = int8_divide_op
elif is_uint64_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
Expand Down Expand Up @@ -2837,6 +2846,8 @@ def fixed_width_int_op(
prim = int32_mod_op
elif is_int16_rprimitive(type):
prim = int16_mod_op
elif is_int8_rprimitive(type):
prim = int8_mod_op
elif is_uint64_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
Expand Down
3 changes: 3 additions & 0 deletions mypyc/irbuild/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
dict_rprimitive,
float_rprimitive,
frozenset_rprimitive,
int8_rprimitive,
int16_rprimitive,
int32_rprimitive,
int64_rprimitive,
Expand Down Expand Up @@ -126,6 +127,8 @@ def type_to_rtype(self, typ: Type | None) -> RType:
return int32_rprimitive
elif typ.type.fullname == "mypy_extensions.i16":
return int16_rprimitive
elif typ.type.fullname == "mypy_extensions.i8":
return int8_rprimitive
elif typ.type.fullname == "mypy_extensions.u64":
return uint64_rprimitive
elif typ.type.fullname == "mypy_extensions.u32":
Expand Down
Loading
Loading