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

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

class u32:
@overload
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> u32: ...
Expand Down
10 changes: 10 additions & 0 deletions mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
is_uint8_rprimitive,
is_uint16_rprimitive,
is_uint32_rprimitive,
is_uint64_rprimitive,
object_rprimitive,
optional_value_type,
vec_api_by_item_type,
Expand Down Expand Up @@ -1175,6 +1176,13 @@ def emit_unbox(
self.emit_line(f"{dest} = CPyLong_AsUInt32({src});")
if not isinstance(error, AssignHandler):
self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure)
elif is_uint64_rprimitive(typ):
assert not optional
if declare_dest:
self.emit_line(f"uint64_t {dest};")
self.emit_line(f"{dest} = CPyLong_AsUInt64({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
Expand Down Expand Up @@ -1348,6 +1356,8 @@ def emit_box(
self.emit_inc_ref(dest, object_rprimitive)
elif is_uint32_rprimitive(typ):
self.emit_line(f"{declaration}{dest} = PyLong_FromUnsignedLong({src});")
elif is_uint64_rprimitive(typ):
self.emit_line(f"{declaration}{dest} = PyLong_FromUnsignedLongLong({src});")
elif (
is_int32_rprimitive(typ)
or is_int16_rprimitive(typ)
Expand Down
44 changes: 35 additions & 9 deletions mypyc/doc/int_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ Mypyc supports these integer types:
* ``i64`` (64-bit signed integer)
* ``i32`` (32-bit signed integer)
* ``i16`` (16-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``, ``u32``, ``u16`` and ``u8`` are *native integer types* and
``i64``, ``i32``, ``i16``, ``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 @@ -37,6 +38,7 @@ Construction
* ``int(x: i64)``
* ``int(x: i32)``
* ``int(x: i16)``
* ``int(x: u64)``
* ``int(x: u32)``
* ``int(x: u16)``
* ``int(x: u8)``
Expand All @@ -51,6 +53,7 @@ Construction
* ``i64(x: i64)`` (no-op)
* ``i64(x: i32)``
* ``i64(x: i16)``
* ``i64(x: u64)``
* ``i64(x: u32)``
* ``i64(x: u16)``
* ``i64(x: u8)``
Expand All @@ -64,7 +67,8 @@ Construction
* ``i32(x: i64)`` (truncate)
* ``i32(x: i32)`` (no-op)
* ``i32(x: i16)``
* ``i32(x: u32)`` (truncate)
* ``i32(x: u64)`` (truncate)
* ``i32(x: u32)``
* ``i32(x: u16)``
* ``i32(x: u8)``
* ``i32(x: str)``
Expand All @@ -77,19 +81,35 @@ Construction
* ``i16(x: i64)`` (truncate)
* ``i16(x: i32)`` (truncate)
* ``i16(x: i16)`` (no-op)
* ``i16(x: u64)`` (truncate)
* ``i16(x: u32)`` (truncate)
* ``i16(x: u16)`` (truncate)
* ``i16(x: u16)``
* ``i16(x: u8)``
* ``i16(x: str)``
* ``i16(x: str, base: int)``

``u64`` type:

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

``u32`` type:

* ``u32(x: int)``
* ``u32(x: float)``
* ``u32(x: i64)`` (truncate)
* ``u32(x: i32)`` (truncate)
* ``u32(x: i16)`` (truncate)
* ``u32(x: i32)``
* ``u32(x: i16)``
* ``u32(x: u64)`` (truncate)
* ``u32(x: u32)`` (no-op)
* ``u32(x: u16)``
* ``u32(x: u8)``
Expand All @@ -101,9 +121,10 @@ Construction
* ``u16(x: int)``
* ``u16(x: float)``
* ``u16(x: i64)`` (truncate)
* ``u16(x: u32)`` (truncate)
* ``u16(x: i32)`` (truncate)
* ``u16(x: i16)`` (truncate)
* ``u16(x: i16)``
* ``u16(x: u64)`` (truncate)
* ``u16(x: u32)`` (truncate)
* ``u16(x: u16)`` (no-op)
* ``u16(x: u8)``
* ``u16(x: str)``
Expand All @@ -116,6 +137,8 @@ Construction
* ``u8(x: i64)`` (truncate)
* ``u8(x: i32)`` (truncate)
* ``u8(x: i16)`` (truncate)
* ``u8(x: u64)`` (truncate)
* ``u8(x: u32)`` (truncate)
* ``u8(x: u16)`` (truncate)
* ``u8(x: u8)`` (no-op)
* ``u8(x: str)``
Expand All @@ -124,8 +147,11 @@ Construction
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
fail. More generally, operations between native integer types don't
check for overflow.
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
(for example, ``u64(i32(-1))`` is ``2**64 - 1`` and ``i64(u64(2**63))``
is ``-2**63``). More generally, operations between
native integer types don't check for overflow.

Implicit conversions
--------------------
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>`)
* ``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>`)
* ``u8`` (: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),
``u32`` (32-bit unsigned integer), ``u16`` (16-bit unsigned integer), and ``u8`` (8-bit unsigned 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
arbitrary-precision ``int`` type, since they don't require overflow
Expand Down
6 changes: 3 additions & 3 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,6 @@ def __hash__(self) -> int:
error_overlap=True,
)

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

uint64_rprimitive: Final = RPrimitive(
"u64",
is_unboxed=True,
Expand Down Expand Up @@ -633,6 +630,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_uint64_rprimitive(rtype)
or is_uint32_rprimitive(rtype)
or is_uint16_rprimitive(rtype)
or is_uint8_rprimitive(rtype)
Expand Down Expand Up @@ -768,6 +766,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 == "uint64_t":
return "U8"
elif t._ctype == "uint32_t":
return "U4"
elif t._ctype == "uint16_t":
Expand Down
50 changes: 41 additions & 9 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
is_uint8_rprimitive,
is_uint16_rprimitive,
is_uint32_rprimitive,
is_uint64_rprimitive,
none_rprimitive,
object_pointer_rprimitive,
object_rprimitive,
Expand Down Expand Up @@ -172,11 +173,14 @@
int_to_int32_op,
int_to_int64_op,
int_to_uint32_op,
int_to_uint64_op,
ssize_t_to_int_op,
uint8_overflow,
uint16_overflow,
uint32_overflow,
uint32_to_int_op,
uint64_overflow,
uint64_to_int_op,
)
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 @@ -258,6 +262,24 @@
BOOL_BINARY_OPS: Final = {"&", "&=", "|", "|=", "^", "^=", "==", "!=", "<", "<=", ">", ">="}


def int_literal_value(value: Value) -> int | None:
"""Return the numeric value if value is an int literal, otherwise None.
Recognize both short int literals (Integer) and literals that are too
large for a short int (LoadLiteral of a boxed int).
"""
if isinstance(value, Integer) and is_short_int_rprimitive(value.type):
return value.numeric_value()
if (
isinstance(value, LoadLiteral)
and is_int_rprimitive(value.type)
and isinstance(value.value, int)
and not isinstance(value.value, bool)
):
return value.value
return None


class PendingKeepAlive:
__slots__ = ("value", "scope", "seq")

Expand Down Expand Up @@ -494,15 +516,12 @@ def coerce(
if (src_type.is_unboxed and target_type.is_unboxed) and not is_runtime_subtype(
src_type, target_type
):
if (
isinstance(src, Integer)
and is_short_int_rprimitive(src_type)
and is_fixed_width_rtype(target_type)
):
value = src.numeric_value()
if is_fixed_width_rtype(target_type) and (value := int_literal_value(src)) is not None:
# Integer literal (either a short int or a large one that needs a
# boxed int literal).
if not check_native_int_range(target_type, value):
self.error(f'Value {value} is out of range for "{target_type}"', line)
return Integer(src.value >> 1, target_type)
return Integer(value, target_type)
elif is_int_rprimitive(src_type) and is_fixed_width_rtype(target_type):
return self.coerce_int_to_fixed_width(src, target_type, line)
elif is_fixed_width_rtype(src_type) and is_int_rprimitive(target_type):
Expand Down Expand Up @@ -587,10 +606,10 @@ def coerce_int_to_fixed_width(self, src: Value, target_type: RType, line: int) -
self.activate_block(fast)

size = target_type.size
needs_unsigned_fast_range_check = not target_type.is_signed and size == int_rprimitive.size
needs_unsigned_fast_range_check = not target_type.is_signed and size >= int_rprimitive.size
if needs_unsigned_fast_range_check:
# A tagged short int always fits in an unsigned type of the same
# width, except for negative values. Don't send those to the slow
# width or larger, except for negative values. Don't send those to the slow
# path, which expects a tagged pointer rather than a short int.
nonnegative = self.add(ComparisonOp(src, Integer(0, src.type), ComparisonOp.SGE, line))
valid = BasicBlock()
Expand All @@ -616,6 +635,7 @@ def coerce_int_to_fixed_width(self, src: Value, target_type: RType, line: int) -
self.activate_block(slow)
if (
is_int64_rprimitive(target_type)
or is_uint64_rprimitive(target_type)
or (is_int32_rprimitive(target_type) and size == int_rprimitive.size)
or (is_uint32_rprimitive(target_type) and size == int_rprimitive.size)
):
Expand All @@ -627,6 +647,8 @@ def coerce_int_to_fixed_width(self, src: Value, target_type: RType, line: int) -
self.add(Assign(ptr2, ptr))
if is_int64_rprimitive(target_type):
conv_op = int_to_int64_op
elif is_uint64_rprimitive(target_type):
conv_op = int_to_uint64_op
elif is_uint32_rprimitive(target_type):
conv_op = int_to_uint32_op
else:
Expand Down Expand Up @@ -744,6 +766,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_uint64_rprimitive(target_type):
self.call_c(uint64_overflow, [], line)
elif is_uint32_rprimitive(target_type):
self.call_c(uint32_overflow, [], line)
elif is_uint16_rprimitive(target_type):
Expand Down Expand Up @@ -845,6 +869,8 @@ def coerce_fixed_width_to_int(self, src: Value, line: int) -> Value:
elif is_int32_rprimitive(src_type):
assert PLATFORM_SIZE == 4
conv_op = ssize_t_to_int_op
elif is_uint64_rprimitive(src_type):
conv_op = uint64_to_int_op
elif is_uint32_rprimitive(src_type):
assert PLATFORM_SIZE == 4
conv_op = uint32_to_int_op
Expand Down Expand Up @@ -2782,6 +2808,9 @@ def fixed_width_int_op(
prim = int32_divide_op
elif is_int16_rprimitive(type):
prim = int16_divide_op
elif is_uint64_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
elif is_uint32_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
Expand All @@ -2808,6 +2837,9 @@ def fixed_width_int_op(
prim = int32_mod_op
elif is_int16_rprimitive(type):
prim = int16_mod_op
elif is_uint64_rprimitive(type):
self.check_for_zero_division(rhs, type, line)
return self.int_op(type, lhs, rhs, op, line)
elif is_uint32_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 @@ -52,6 +52,7 @@
uint8_rprimitive,
uint16_rprimitive,
uint32_rprimitive,
uint64_rprimitive,
)


Expand Down Expand Up @@ -125,6 +126,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.u64":
return uint64_rprimitive
elif typ.type.fullname == "mypy_extensions.u32":
return uint32_rprimitive
elif typ.type.fullname == "mypy_extensions.u16":
Expand Down
Loading
Loading