diff --git a/mypy/types.py b/mypy/types.py index 0628f3e20b2ed..f46e7b5491611 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -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", diff --git a/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi b/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi index a982db1463568..18944e49c5b70 100644 --- a/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi +++ b/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi @@ -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: ... diff --git a/mypyc/codegen/emit.py b/mypyc/codegen/emit.py index 82f4641b09cf8..3fd0f44acead2 100644 --- a/mypyc/codegen/emit.py +++ b/mypyc/codegen/emit.py @@ -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, @@ -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: @@ -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) ): diff --git a/mypyc/doc/float_operations.rst b/mypyc/doc/float_operations.rst index 64cba65c908e0..f1da294f3c2f1 100644 --- a/mypyc/doc/float_operations.rst +++ b/mypyc/doc/float_operations.rst @@ -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)`` @@ -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)`` diff --git a/mypyc/doc/int_operations.rst b/mypyc/doc/int_operations.rst index dfa5c5b270c7f..0e51d4ed0ce2b 100644 --- a/mypyc/doc/int_operations.rst +++ b/mypyc/doc/int_operations.rst @@ -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. @@ -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)`` @@ -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)`` @@ -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)`` @@ -81,6 +85,7 @@ 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)`` @@ -88,6 +93,21 @@ Construction * ``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)`` @@ -95,6 +115,7 @@ Construction * ``u64(x: i64)`` * ``u64(x: i32)`` * ``u64(x: i16)`` +* ``u64(x: i8)`` * ``u64(x: u64)`` (no-op) * ``u64(x: u32)`` * ``u64(x: u16)`` @@ -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)`` @@ -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) @@ -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) @@ -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 @@ -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:: diff --git a/mypyc/doc/using_type_annotations.rst b/mypyc/doc/using_type_annotations.rst index 1a9d38654963d..336d990805040 100644 --- a/mypyc/doc/using_type_annotations.rst +++ b/mypyc/doc/using_type_annotations.rst @@ -33,6 +33,7 @@ implementations: * ``i64`` (:ref:`documentation `, :ref:`native operations `) * ``i32`` (:ref:`documentation `, :ref:`native operations `) * ``i16`` (:ref:`documentation `, :ref:`native operations `) +* ``i8`` (:ref:`documentation `, :ref:`native operations `) * ``u64`` (:ref:`documentation `, :ref:`native operations `) * ``u32`` (:ref:`documentation `, :ref:`native operations `) * ``u16`` (:ref:`documentation `, :ref:`native operations `) @@ -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 diff --git a/mypyc/ir/rtypes.py b/mypyc/ir/rtypes.py index 918766579a0e0..18b37f0fcb641 100644 --- a/mypyc/ir/rtypes.py +++ b/mypyc/ir/rtypes.py @@ -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" @@ -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, @@ -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" @@ -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) @@ -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": diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 08bebbe8aab7c..73dcb597ef437 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -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, @@ -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, @@ -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): @@ -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) @@ -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) @@ -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) diff --git a/mypyc/irbuild/mapper.py b/mypyc/irbuild/mapper.py index caa11f5d87460..aacfc7931f4db 100644 --- a/mypyc/irbuild/mapper.py +++ b/mypyc/irbuild/mapper.py @@ -38,6 +38,7 @@ dict_rprimitive, float_rprimitive, frozenset_rprimitive, + int8_rprimitive, int16_rprimitive, int32_rprimitive, int64_rprimitive, @@ -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": diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index 69cb21a002131..cb34a323ab992 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -64,6 +64,7 @@ bytes_writer_rprimitive, c_int_rprimitive, dict_rprimitive, + int8_rprimitive, int16_rprimitive, int32_rprimitive, int64_rprimitive, @@ -74,6 +75,7 @@ is_dict_rprimitive, is_fixed_width_rtype, is_float_rprimitive, + is_int8_rprimitive, is_int16_rprimitive, is_int32_rprimitive, is_int64_rprimitive, @@ -326,6 +328,7 @@ def translate_globals(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Va @specialize_function("mypy_extensions.i64") @specialize_function("mypy_extensions.i32") @specialize_function("mypy_extensions.i16") +@specialize_function("mypy_extensions.i8") @specialize_function("mypy_extensions.u64") @specialize_function("mypy_extensions.u32") @specialize_function("mypy_extensions.u16") @@ -341,7 +344,7 @@ def translate_builtins_with_unary_dunder( arg = expr.args[0] arg_typ = builder.node_type(arg) shortname = callee.fullname.split(".")[1] - if shortname in ("i64", "i32", "i16", "u64", "u32", "u16", "u8"): + if shortname in ("i64", "i32", "i16", "i8", "u64", "u32", "u16", "u8"): method = "__int__" else: method = f"__{shortname}__" @@ -1182,7 +1185,11 @@ def translate_i64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value arg_type = builder.node_type(arg) if is_int64_rprimitive(arg_type): return builder.accept(arg) - elif is_int32_rprimitive(arg_type) or is_int16_rprimitive(arg_type): + elif ( + is_int32_rprimitive(arg_type) + or is_int16_rprimitive(arg_type) + or is_int8_rprimitive(arg_type) + ): val = builder.accept(arg) return builder.add(Extend(val, int64_rprimitive, signed=True, line=expr.line)) elif is_uint64_rprimitive(arg_type): @@ -1215,7 +1222,7 @@ def translate_i32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value elif is_int64_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Truncate(val, int32_rprimitive, line=expr.line)) - elif is_int16_rprimitive(arg_type): + elif is_int16_rprimitive(arg_type) or is_int8_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int32_rprimitive, signed=True, line=expr.line)) elif is_uint64_rprimitive(arg_type): @@ -1260,6 +1267,9 @@ def translate_i16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value elif is_uint8_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int16_rprimitive, signed=False, line=expr.line)) + elif is_int8_rprimitive(arg_type): + val = builder.accept(arg) + return builder.add(Extend(val, int16_rprimitive, signed=True, line=expr.line)) elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type): val = builder.accept(arg) val = truncate_literal(val, int16_rprimitive) @@ -1267,6 +1277,36 @@ def translate_i16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value return None +@specialize_function("mypy_extensions.i8") +def translate_i8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: + if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: + return None + arg = expr.args[0] + arg_type = builder.node_type(arg) + if is_int8_rprimitive(arg_type): + return builder.accept(arg) + elif ( + is_int16_rprimitive(arg_type) + or is_int32_rprimitive(arg_type) + or is_int64_rprimitive(arg_type) + ): + val = builder.accept(arg) + return builder.add(Truncate(val, int8_rprimitive, line=expr.line)) + elif ( + is_uint64_rprimitive(arg_type) + or is_uint32_rprimitive(arg_type) + or is_uint16_rprimitive(arg_type) + or is_uint8_rprimitive(arg_type) + ): + val = builder.accept(arg) + return builder.add(Truncate(val, int8_rprimitive, line=expr.line)) + elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type): + val = builder.accept(arg) + val = truncate_literal(val, int8_rprimitive) + return builder.coerce(val, int8_rprimitive, expr.line) + return None + + @specialize_function("mypy_extensions.u64") def translate_u64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: @@ -1278,7 +1318,11 @@ def translate_u64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value elif is_int64_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Truncate(val, uint64_rprimitive, line=expr.line)) - elif is_int32_rprimitive(arg_type) or is_int16_rprimitive(arg_type): + elif ( + is_int32_rprimitive(arg_type) + or is_int16_rprimitive(arg_type) + or is_int8_rprimitive(arg_type) + ): val = builder.accept(arg) return builder.add(Extend(val, uint64_rprimitive, signed=True, line=expr.line)) elif ( @@ -1305,6 +1349,7 @@ def translate_u32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value return builder.accept(arg) elif ( is_int16_rprimitive(arg_type) + or is_int8_rprimitive(arg_type) or is_int32_rprimitive(arg_type) or is_int64_rprimitive(arg_type) or is_uint64_rprimitive(arg_type) @@ -1314,7 +1359,7 @@ def translate_u32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value val = builder.accept(arg) if is_uint16_rprimitive(arg_type) or is_uint8_rprimitive(arg_type): return builder.add(Extend(val, uint32_rprimitive, signed=False, line=expr.line)) - if is_int16_rprimitive(arg_type): + if is_int16_rprimitive(arg_type) or is_int8_rprimitive(arg_type): return builder.add(Extend(val, uint32_rprimitive, signed=True, line=expr.line)) return builder.add(Truncate(val, uint32_rprimitive, line=expr.line)) elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type): @@ -1334,6 +1379,7 @@ def translate_u16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value return builder.accept(arg) elif ( is_int16_rprimitive(arg_type) + or is_int8_rprimitive(arg_type) or is_int32_rprimitive(arg_type) or is_int64_rprimitive(arg_type) or is_uint64_rprimitive(arg_type) @@ -1343,6 +1389,8 @@ def translate_u16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value val = builder.accept(arg) if is_uint8_rprimitive(arg_type): return builder.add(Extend(val, uint16_rprimitive, signed=False, line=expr.line)) + if is_int8_rprimitive(arg_type): + return builder.add(Extend(val, uint16_rprimitive, signed=True, line=expr.line)) return builder.add(Truncate(val, uint16_rprimitive, line=expr.line)) elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type): val = builder.accept(arg) @@ -1360,7 +1408,8 @@ def translate_u8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | if is_uint8_rprimitive(arg_type): return builder.accept(arg) elif ( - is_int16_rprimitive(arg_type) + is_int8_rprimitive(arg_type) + or is_int16_rprimitive(arg_type) or is_int32_rprimitive(arg_type) or is_int64_rprimitive(arg_type) or is_uint64_rprimitive(arg_type) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 4eb54a7e23779..24f294b06f5ad 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -233,6 +233,10 @@ int32_t CPyLong_AsInt32_(PyObject *o); int32_t CPyInt32_Divide(int32_t x, int32_t y); int32_t CPyInt32_Remainder(int32_t x, int32_t y); void CPyInt32_Overflow(void); +int8_t CPyLong_AsInt8_(PyObject *o); +int8_t CPyInt8_Divide(int8_t x, int8_t y); +int8_t CPyInt8_Remainder(int8_t x, int8_t y); +void CPyInt8_Overflow(void); int16_t CPyLong_AsInt16_(PyObject *o); int16_t CPyInt16_Divide(int16_t x, int16_t y); int16_t CPyInt16_Remainder(int16_t x, int16_t y); @@ -482,6 +486,33 @@ static inline int16_t CPyLong_AsInt16(PyObject *o) { return CPyLong_AsInt16_(o); } +static inline int8_t CPyLong_AsInt8(PyObject *o) { + if (likely(PyLong_Check(o))) { + #if CPY_3_12_FEATURES + PyLongObject *lobj = (PyLongObject *)o; + size_t tag = CPY_LONG_TAG(lobj); + if (likely(tag == (1 << CPY_NON_SIZE_BITS))) { + digit x = CPY_LONG_DIGIT(lobj, 0); + if (x < 0x80) + return x; + } else if (likely(tag == CPY_SIGN_ZERO)) { + return 0; + } + #else + PyLongObject *lobj = (PyLongObject *)o; + Py_ssize_t size = lobj->ob_base.ob_size; + if (likely(size == 1)) { + digit x = lobj->ob_digit[0]; + if (x < 0x80) + return x; + } else if (likely(size == 0)) { + return 0; + } + #endif + } + return CPyLong_AsInt8_(o); +} + static inline uint32_t CPyLong_AsUInt32(PyObject *o) { if (likely(PyLong_Check(o))) { #if CPY_3_12_FEATURES diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index 7f265bf00cad4..9e85519fae338 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -533,6 +533,60 @@ void CPyInt32_Overflow() { PyErr_SetString(PyExc_ValueError, "int too large to convert to i32"); } +// i8 unboxing slow path +int8_t CPyLong_AsInt8_(PyObject *o) { + int overflow; + long result = PyLong_AsLongAndOverflow(o, &overflow); + if (result > 0x7f || result < -0x80) { + overflow = 1; + result = -1; + } + if (result == -1) { + if (PyErr_Occurred()) { + return CPY_LL_INT_ERROR; + } else if (overflow) { + PyErr_SetString(PyExc_ValueError, "int too large to convert to i8"); + return CPY_LL_INT_ERROR; + } + } + return result; +} + +int8_t CPyInt8_Divide(int8_t x, int8_t y) { + if (y == 0) { + PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); + return CPY_LL_INT_ERROR; + } + if (y == -1 && x == INT8_MIN) { + PyErr_SetString(PyExc_OverflowError, "integer division overflow"); + return CPY_LL_INT_ERROR; + } + int8_t d = x / y; + if (((x < 0) != (y < 0)) && d * y != x) { + d--; + } + return d; +} + +int8_t CPyInt8_Remainder(int8_t x, int8_t y) { + if (y == 0) { + PyErr_SetString(PyExc_ZeroDivisionError, "integer division or modulo by zero"); + return CPY_LL_INT_ERROR; + } + if (y == -1 && x == INT8_MIN) { + return 0; + } + int8_t d = x % y; + if (((x < 0) != (y < 0)) && d != 0) { + d += y; + } + return d; +} + +void CPyInt8_Overflow() { + PyErr_SetString(PyExc_ValueError, "int too large to convert to i8"); +} + // i16 unboxing slow path int16_t CPyLong_AsInt16_(PyObject *o) { int overflow; diff --git a/mypyc/primitives/int_ops.py b/mypyc/primitives/int_ops.py index e166a9b1a2403..997b19e49693b 100644 --- a/mypyc/primitives/int_ops.py +++ b/mypyc/primitives/int_ops.py @@ -24,6 +24,7 @@ bytes_rprimitive, c_pyssize_t_rprimitive, float_rprimitive, + int8_rprimitive, int16_rprimitive, int32_rprimitive, int64_rprimitive, @@ -51,6 +52,7 @@ "mypy_extensions.i64", "mypy_extensions.i32", "mypy_extensions.i16", + "mypy_extensions.i8", "mypy_extensions.u64", "mypy_extensions.u32", "mypy_extensions.u16", @@ -276,6 +278,20 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription: error_kind=ERR_MAGIC_OVERLAPPING, ) +int8_divide_op = custom_op( + arg_types=[int8_rprimitive, int8_rprimitive], + return_type=int8_rprimitive, + c_function_name="CPyInt8_Divide", + error_kind=ERR_MAGIC_OVERLAPPING, +) + +int8_mod_op = custom_op( + arg_types=[int8_rprimitive, int8_rprimitive], + return_type=int8_rprimitive, + c_function_name="CPyInt8_Remainder", + error_kind=ERR_MAGIC_OVERLAPPING, +) + # Convert tagged int (as PyObject *) to i64 int_to_int64_op = custom_op( arg_types=[object_rprimitive], @@ -350,6 +366,10 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription: error_kind=ERR_ALWAYS, ) +int8_overflow = custom_op( + arg_types=[], return_type=void_rtype, c_function_name="CPyInt8_Overflow", error_kind=ERR_ALWAYS +) + uint32_overflow = custom_op( arg_types=[], return_type=void_rtype, diff --git a/mypyc/test-data/irbuild-i8.test b/mypyc/test-data/irbuild-i8.test new file mode 100644 index 0000000000000..e7b5bbd09f919 --- /dev/null +++ b/mypyc/test-data/irbuild-i8.test @@ -0,0 +1,82 @@ +# Test cases for i8 native ints. Focus on things that are different from i16; no need to +# duplicate all i16 test cases here. + +[case testI8Operations] +from mypy_extensions import i8 + +def ops(x: i8, y: i8) -> i8: + return (x // y) + (x % y) +[out] +def ops(x, y): + x, y, r0, r1, r2 :: i8 +L0: + r0 = CPyInt8_Divide(x, y) + r1 = CPyInt8_Remainder(x, y) + r2 = r0 + r1 + return r2 + +[case testI8BoxAndUnbox] +from typing import Any +from mypy_extensions import i8 + +def f(x: Any) -> Any: + y: i8 = x + return y +[out] +def f(x): + x :: object + r0, y :: i8 + r1 :: object +L0: + r0 = unbox(i8, x) + y = r0 + r1 = box(i8, y) + return r1 + +[case testI8ConvertToInt] +from mypy_extensions import i8 + +def f(x: i8) -> int: + return int(x) +[out] +def f(x): + x :: i8 + r0 :: native_int + r1 :: int +L0: + r0 = extend signed x: i8 to native_int + r1 = r0 << 1 + return r1 + +[case testI8ConvertFromInt] +from mypy_extensions import i8 + +def f(x: int) -> i8: + return i8(x) +[out] +def f(x): + x :: int + r0 :: native_int + r1, r2, r3 :: bit + r4 :: native_int + r5, r6 :: i8 +L0: + r0 = x & 1 + r1 = r0 == 0 + if r1 goto L1 else goto L4 :: bool +L1: + r2 = x < 256 :: signed + if r2 goto L2 else goto L4 :: bool +L2: + r3 = x >= -256 :: signed + if r3 goto L3 else goto L4 :: bool +L3: + r4 = x >> 1 + r5 = truncate r4: native_int to i8 + r6 = r5 + goto L5 +L4: + CPyInt8_Overflow() + unreachable +L5: + return r6 diff --git a/mypyc/test-data/irbuild-u16.test b/mypyc/test-data/irbuild-u16.test index 65248c2cef302..37a9557c9794d 100644 --- a/mypyc/test-data/irbuild-u16.test +++ b/mypyc/test-data/irbuild-u16.test @@ -115,7 +115,7 @@ L0: return r1 [case testU16ExplicitConversionFromNativeInt] -from mypy_extensions import i64, i32, i16, u8, u16 +from mypy_extensions import i64, i32, i16, i8, u8, u16 def from_u16(x: u16) -> u16: return u16(x) @@ -131,6 +131,9 @@ def from_i32(x: i32) -> u16: def from_i64(x: i64) -> u16: return u16(x) + +def from_i8(x: i8) -> u16: + return u16(x) [out] def from_u16(x): x :: u16 @@ -160,6 +163,12 @@ def from_i64(x): L0: r0 = truncate x: i64 to u16 return r0 +def from_i8(x): + x :: i8 + r0 :: u16 +L0: + r0 = extend signed x: i8 to u16 + return r0 [case testU16ExplicitConversionToNativeInt] from mypy_extensions import i64, i32, i16, u8, u16 diff --git a/mypyc/test-data/irbuild-u32.test b/mypyc/test-data/irbuild-u32.test index 6e347722e7353..d5567172bd0a3 100644 --- a/mypyc/test-data/irbuild-u32.test +++ b/mypyc/test-data/irbuild-u32.test @@ -115,7 +115,7 @@ L0: return r1 [case testU32ExplicitConversionFromNativeInt] -from mypy_extensions import i64, i32, i16, u8, u32 +from mypy_extensions import i64, i32, i16, i8, u8, u32 def from_u32(x: u32) -> u32: return u32(x) @@ -131,6 +131,9 @@ def from_i32(x: i32) -> u32: def from_i64(x: i64) -> u32: return u32(x) + +def from_i8(x: i8) -> u32: + return u32(x) [out] def from_u32(x): x :: u32 @@ -160,6 +163,12 @@ def from_i64(x): L0: r0 = truncate x: i64 to u32 return r0 +def from_i8(x): + x :: i8 + r0 :: u32 +L0: + r0 = extend signed x: i8 to u32 + return r0 [case testU32ExplicitConversionToNativeInt] from mypy_extensions import i64, i32, i16, u8, u32 diff --git a/mypyc/test-data/irbuild-u8.test b/mypyc/test-data/irbuild-u8.test index 14f691c9451f2..7ed94757c6f18 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -349,7 +349,7 @@ L0: return 1 [case testU8ExplicitConversionFromNativeInt] -from mypy_extensions import i64, i32, i16, u8 +from mypy_extensions import i64, i32, i16, i8, u8 def from_u8(x: u8) -> u8: return u8(x) @@ -362,6 +362,9 @@ def from_i32(x: i32) -> u8: def from_i64(x: i64) -> u8: return u8(x) + +def from_i8(x: i8) -> u8: + return u8(x) [out] def from_u8(x): x :: u8 @@ -385,6 +388,12 @@ def from_i64(x): L0: r0 = truncate x: i64 to u8 return r0 +def from_i8(x): + x :: i8 + r0 :: u8 +L0: + r0 = truncate x: i8 to u8 + return r0 [case testU8ExplicitConversionToNativeInt] from mypy_extensions import i64, i32, i16, u8 diff --git a/mypyc/test-data/run-i8.test b/mypyc/test-data/run-i8.test new file mode 100644 index 0000000000000..1998821f812d4 --- /dev/null +++ b/mypyc/test-data/run-i8.test @@ -0,0 +1,64 @@ +[case testI8BasicOps] +from typing import Any + +from mypy_extensions import i8, i16, i32, i64, u8, u16, u32, u64 +from testutil import assertRaises + +def div(x: i8, y: i8) -> i8: + return x // y + +def mod(x: i8, y: i8) -> i8: + return x % y + +def to_i8(x: i16) -> i8: + return i8(x) + +def widen(x: i8) -> tuple[i16, i32, i64]: + return i16(x), i32(x), i64(x) + +def from_u8(x: u8) -> i8: + return i8(x) + +def to_unsigned(x: i8) -> tuple[u8, u16, u32, u64]: + return u8(x), u16(x), u32(x), u64(x) + +def test_box_unbox_and_divmod() -> None: + for value in range(-128, 128): + o: Any = value + x: i8 = o + assert x == value + for value in (-129, 128): + with assertRaises(ValueError, "int too large to convert to i8"): + o = value + x = o + for x in range(-128, 128): + for y in range(-128, 128): + if y == 0: + with assertRaises(ZeroDivisionError): + div(x, y) + mod(x, y) + elif x == -128 and y == -1: + with assertRaises(OverflowError, "integer division overflow"): + div(x, y) + assert mod(x, y) == 0 + else: + assert div(x, y) == x // y + assert mod(x, y) == x % y + +def test_operations() -> None: + x: i8 = 65 + int() + y: i8 = -12 + int() + assert x + y == 53 + assert x * y == -12 + assert x << 1 == -126 + assert y >> 2 == -3 + assert ~x == -66 + +def test_conversions() -> None: + assert to_i8(127) == 127 + assert to_i8(128) == -128 + assert widen(-128) == (-128, -128, -128) + assert from_u8(255) == -1 + assert to_unsigned(5) == (5, 5, 5, 5) + assert to_unsigned(-1) == (255, 2**16 - 1, 2**32 - 1, 2**64 - 1) + assert to_unsigned(-128) == (128, 2**16 - 128, 2**32 - 128, 2**64 - 128) diff --git a/mypyc/test/test_irbuild.py b/mypyc/test/test_irbuild.py index 07e0962e5d365..5e7d82e45f640 100644 --- a/mypyc/test/test_irbuild.py +++ b/mypyc/test/test_irbuild.py @@ -45,6 +45,7 @@ "irbuild-i64.test", "irbuild-i32.test", "irbuild-i16.test", + "irbuild-i8.test", "irbuild-u8.test", "irbuild-u64.test", "irbuild-u32.test", diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index e9aca6ba5d7df..016e076284522 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -49,6 +49,7 @@ "run-i64.test", "run-i32.test", "run-i16.test", + "run-i8.test", "run-u8.test", "run-u64.test", "run-u32.test", diff --git a/test-data/unit/lib-stub/mypy_extensions.pyi b/test-data/unit/lib-stub/mypy_extensions.pyi index 878b84651d960..9ef7c449430ae 100644 --- a/test-data/unit/lib-stub/mypy_extensions.pyi +++ b/test-data/unit/lib-stub/mypy_extensions.pyi @@ -50,7 +50,7 @@ class FlexibleAlias(Generic[_T, _U]): ... class __SupportsInt(Protocol[T_co]): def __int__(self) -> int: pass -_Int = Union[int, u8, u16, u32, u64, i16, i32, i64] +_Int = Union[int, u8, u16, u32, u64, i8, i16, i32, i64] class u8: def __init__(self, x: Union[_Int, str, bytes, SupportsInt], base: int = 10) -> None: ... @@ -172,6 +172,36 @@ class u64: def __ge__(self, x: u64) -> bool: ... def __gt__(self, x: u64) -> bool: ... +class i8: + def __init__(self, x: Union[_Int, str, bytes, SupportsInt], base: int = 10) -> None: ... + 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: ... + class i16: def __init__(self, x: Union[_Int, str, bytes, SupportsInt], base: int = 10) -> None: ... def __add__(self, x: i16) -> i16: ...