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.u16",
"mypy_extensions.u8",
)

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 u16:
@overload
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> u16: ...
@overload
def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> u16: ...

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

class u8:
@overload
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> u8: ...
Expand Down
16 changes: 15 additions & 1 deletion mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
is_str_rprimitive,
is_tuple_rprimitive,
is_uint8_rprimitive,
is_uint16_rprimitive,
object_rprimitive,
optional_value_type,
vec_api_by_item_type,
Expand Down Expand Up @@ -1166,6 +1167,14 @@ 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_uint16_rprimitive(typ):
# Whether we are borrowing or not makes no difference.
assert not optional # Not supported for overlapping error values
if declare_dest:
self.emit_line(f"uint16_t {dest};")
self.emit_line(f"{dest} = CPyLong_AsUInt16({src});")
if not isinstance(error, AssignHandler):
self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
elif is_uint8_rprimitive(typ):
# Whether we are borrowing or not makes no difference.
assert not optional # Not supported for overlapping error values
Expand Down Expand Up @@ -1329,7 +1338,12 @@ def emit_box(
self.emit_lines(f"{declaration}{dest} = Py_None;")
if not can_borrow:
self.emit_inc_ref(dest, object_rprimitive)
elif is_int32_rprimitive(typ) or is_int16_rprimitive(typ) or is_uint8_rprimitive(typ):
elif (
is_int32_rprimitive(typ)
or is_int16_rprimitive(typ)
or is_uint16_rprimitive(typ)
or is_uint8_rprimitive(typ)
):
self.emit_line(f"{declaration}{dest} = PyLong_FromLong({src});")
elif is_int64_rprimitive(typ):
self.emit_line(f"{declaration}{dest} = PyLong_FromLongLong({src});")
Expand Down
12 changes: 12 additions & 0 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
from mypyc.ir.rtypes import (
RArray,
RInstance,
RPrimitive,
RStruct,
RTuple,
RType,
Expand Down Expand Up @@ -940,6 +941,17 @@ def visit_int_op(self, op: IntOp) -> None:
# Signed right shift
lhs = self.emit_signed_int_cast(op.lhs.type) + lhs
rhs = self.emit_signed_int_cast(op.rhs.type) + rhs
elif (
op.op in (IntOp.MUL, IntOp.LEFT_SHIFT)
and isinstance(op.type, RPrimitive)
and op.type.is_native_int
and not op.type.is_signed
and op.type.size < 4
):
# C promotes unsigned types narrower than int to (signed) int, so
# e.g. u16 * u16 could overflow int, which is undefined behavior.
# Do the arithmetic using unsigned int to get predictable wraparound.
lhs = f"(unsigned int){lhs}"
self.emit_line(f"{dest} = {lhs} {op.op_str[op.op]} {rhs};")

def visit_comparison_op(self, op: ComparisonOp) -> None:
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: u16)``
* ``float(x: u8)``
* ``float(x: str)``
* ``float(x: float)`` (no-op)
Expand Down Expand Up @@ -42,6 +43,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)
* ``u16(f)`` (convert to 16-bit unsigned integer)
* ``u8(f)`` (convert to 8-bit unsigned integer)
* ``abs(f)``
* ``math.sin(f)``
Expand Down
33 changes: 31 additions & 2 deletions mypyc/doc/int_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Mypyc supports these integer types:
* ``i64`` (64-bit signed integer)
* ``i32`` (32-bit signed integer)
* ``i16`` (16-bit signed integer)
* ``u16`` (16-bit unsigned integer)
* ``u8`` (8-bit unsigned integer)

``i64``, ``i32``, ``i16`` and ``u8`` are *native integer types* and
``i64``, ``i32``, ``i16``, ``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 @@ -35,6 +36,7 @@ Construction
* ``int(x: i64)``
* ``int(x: i32)``
* ``int(x: i16)``
* ``int(x: u16)``
* ``int(x: u8)``
* ``int(x: str)``
* ``int(x: str, base: int)``
Expand All @@ -47,6 +49,7 @@ Construction
* ``i64(x: i64)`` (no-op)
* ``i64(x: i32)``
* ``i64(x: i16)``
* ``i64(x: u16)``
* ``i64(x: u8)``
* ``i64(x: str)``
* ``i64(x: str, base: int)``
Expand All @@ -58,6 +61,7 @@ Construction
* ``i32(x: i64)`` (truncate)
* ``i32(x: i32)`` (no-op)
* ``i32(x: i16)``
* ``i32(x: u16)``
* ``i32(x: u8)``
* ``i32(x: str)``
* ``i32(x: str, base: int)``
Expand All @@ -69,10 +73,35 @@ Construction
* ``i16(x: i64)`` (truncate)
* ``i16(x: i32)`` (truncate)
* ``i16(x: i16)`` (no-op)
* ``i16(x: u16)`` (truncate)
* ``i16(x: u8)``
* ``i16(x: str)``
* ``i16(x: str, base: int)``

``u16`` type:

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

``u8`` type:

* ``u8(x: int)``
* ``u8(x: float)``
* ``u8(x: i64)`` (truncate)
* ``u8(x: i32)`` (truncate)
* ``u8(x: i16)`` (truncate)
* ``u8(x: u16)`` (truncate)
* ``u8(x: u8)`` (no-op)
* ``u8(x: str)``
* ``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
a wider native integer type to a narrower one truncate the value and never
Expand Down Expand Up @@ -121,7 +150,7 @@ 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 (``u8``) wrap around on overflow.
Operations on unsigned integers (``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
6 changes: 4 additions & 2 deletions 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>`)
* ``u16`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``u8`` (:ref:`documentation <native-ints>`, :ref:`native operations <int-ops>`)
* ``float`` (:ref:`native operations <float-ops>`)
* ``bool`` (:ref:`native operations <bool-ops>`)
Expand Down Expand Up @@ -348,8 +349,9 @@ 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), and
``u8`` (8-bit unsigned integer) if you know that integer values will
``i32`` (32-bit signed integer), ``i16`` (16-bit signed 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
arbitrary-precision ``int`` type, since they don't require overflow
checks on operations. They may also use less memory than ``int``
Expand Down
29 changes: 18 additions & 11 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ def __hash__(self) -> int:
size=8,
error_overlap=True,
)
uint16_rprimitive: Final = RPrimitive(
"u16",
is_unboxed=True,
is_refcounted=False,
is_native_int=True,
is_signed=False,
ctype="uint16_t",
size=2,
error_overlap=True,
)
uint8_rprimitive: Final = RPrimitive(
"u8",
is_unboxed=True,
Expand All @@ -411,19 +421,9 @@ def __hash__(self) -> int:
error_overlap=True,
)

# The following unsigned native int types (u16, u32, u64) are not
# The following unsigned native int types (u32, u64) are not
# exposed to the user. They are for internal use within mypyc only.

u16_rprimitive: Final = RPrimitive(
"u16",
is_unboxed=True,
is_refcounted=False,
is_native_int=True,
is_signed=False,
ctype="uint16_t",
size=2,
error_overlap=True,
)
uint32_rprimitive: Final = RPrimitive(
"u32",
is_unboxed=True,
Expand Down Expand Up @@ -633,10 +633,15 @@ 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_uint16_rprimitive(rtype)
or is_uint8_rprimitive(rtype)
)


def is_uint16_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return rtype is uint16_rprimitive


def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return rtype is uint8_rprimitive

Expand Down Expand Up @@ -762,6 +767,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 == "uint16_t":
return "U2" # "2 byte unsigned integer"
elif t._ctype == "uint8_t":
return "U1" # "1 byte unsigned integer"
elif t._ctype == "double":
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 @@ -122,6 +122,7 @@
is_tagged,
is_tuple_rprimitive,
is_uint8_rprimitive,
is_uint16_rprimitive,
none_rprimitive,
object_pointer_rprimitive,
object_rprimitive,
Expand Down Expand Up @@ -171,6 +172,7 @@
int_to_int64_op,
ssize_t_to_int_op,
uint8_overflow,
uint16_overflow,
)
from mypyc.primitives.list_ops import list_build_op, list_extend_op, list_items, new_list_op
from mypyc.primitives.misc_ops import (
Expand Down Expand Up @@ -721,6 +723,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_uint16_rprimitive(target_type):
self.call_c(uint16_overflow, [], line)
elif is_uint8_rprimitive(target_type):
self.call_c(uint8_overflow, [], line)
else:
Expand Down Expand Up @@ -758,6 +762,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_uint16_rprimitive(src.type)
or is_uint8_rprimitive(src.type)
):
# Simple case -- just sign extend and shift.
Expand Down Expand Up @@ -2726,6 +2731,9 @@ def fixed_width_int_op(
prim = int32_divide_op
elif is_int16_rprimitive(type):
prim = int16_divide_op
elif is_uint16_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
elif is_uint8_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
Expand All @@ -2746,6 +2754,9 @@ def fixed_width_int_op(
prim = int32_mod_op
elif is_int16_rprimitive(type):
prim = int16_mod_op
elif is_uint16_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
elif is_uint8_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 @@ -50,6 +50,7 @@
str_rprimitive,
tuple_rprimitive,
uint8_rprimitive,
uint16_rprimitive,
)


Expand Down Expand Up @@ -123,6 +124,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.u16":
return uint16_rprimitive
elif typ.type.fullname == "mypy_extensions.u8":
return uint8_rprimitive
elif typ.type.fullname == "librt.vecs.vec":
Expand Down
Loading
Loading