From 110eb3becb807e4761c15480c2cca4bc111fccee Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 24 Nov 2026 16:41:34 +0000 Subject: [PATCH] [mypyc] Updates to native integer documentation Among other things, improve consistency and explain interpreted semantics in more detail. --- mypyc/doc/differences_from_python.rst | 6 ++ mypyc/doc/float_operations.rst | 11 ++- mypyc/doc/int_operations.rst | 96 ++++++++++++++++++++------- mypyc/doc/using_type_annotations.rst | 27 +++++--- 4 files changed, 103 insertions(+), 37 deletions(-) diff --git a/mypyc/doc/differences_from_python.rst b/mypyc/doc/differences_from_python.rst index b945d92d6abed..456e3269711cf 100644 --- a/mypyc/doc/differences_from_python.rst +++ b/mypyc/doc/differences_from_python.rst @@ -119,6 +119,12 @@ is converted to the corresponding ``int`` value, which is ``1``. Note that integers still have an arbitrary precision in compiled code, similar to normal Python integers. +:ref:`Native integer types ` such as ``i64`` have a fixed +range in compiled code, unlike ``int``. When interpreted, they behave +like regular ``int`` values with arbitrary precision. Code that +overflows a native integer type may thus produce different results +when compiled and interpreted (see :ref:`native-ints-interpreted`). + Fixed-length tuples are unboxed, similar to integers. The exact type and identity of fixed-length tuples is not preserved, and you can't reliably use ``is`` checks to compare tuples that are used in compiled diff --git a/mypyc/doc/float_operations.rst b/mypyc/doc/float_operations.rst index f1da294f3c2f1..0f66ec3b64a0c 100644 --- a/mypyc/doc/float_operations.rst +++ b/mypyc/doc/float_operations.rst @@ -12,15 +12,12 @@ Construction * Float literal * ``float(x: int)`` -* ``float(x: i64)`` -* ``float(x: i32)`` -* ``float(x: i16)`` -* ``float(x: i8)`` -* ``float(x: u16)`` -* ``float(x: u8)`` * ``float(x: str)`` * ``float(x: float)`` (no-op) +Native integer arguments, such as ``i64``, are implicitly converted to +``int``, so ``float(x)`` is fast for them as well. + Operators --------- @@ -45,6 +42,8 @@ Functions * ``i32(f)`` (convert to 32-bit signed integer) * ``i16(f)`` (convert to 16-bit signed integer) * ``i8(f)`` (convert to 8-bit signed integer) +* ``u64(f)`` (convert to 64-bit unsigned integer) +* ``u32(f)`` (convert to 32-bit unsigned 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 86247217fc9ef..92f9b161cb978 100644 --- a/mypyc/doc/int_operations.rst +++ b/mypyc/doc/int_operations.rst @@ -15,10 +15,11 @@ Mypyc supports these integer types: * ``u16`` (16-bit unsigned integer) * ``u8`` (8-bit unsigned integer) -``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. +``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. See :ref:`native-ints` for an introduction. All integer types have optimized primitive operations, but the native integer types are more efficient than ``int``, since they don't @@ -29,6 +30,32 @@ implementations. Other integer operations use generic implementations that are generally slower. Some operations involving integers and other types, such as list indexing, are documented elsewhere. +.. _native-ints-interpreted: + +Behavior when interpreted +------------------------- + +Native integer types only have an effect in compiled code. When code +runs interpreted (not compiled), they behave exactly like ``int``, and +values have arbitrary precision: + +* ``i64(x)``, ``u8(x)``, etc. simply return ``int(x)``. The result is a + regular ``int`` object. + +* No range checks are performed, and values are never truncated or wrapped + around. For example, ``u8(-1)`` is ``-1`` and ``i32(2**40)`` is ``2**40``. + +* Arithmetic never overflows. For example, ``u8(255) + 1`` is ``256``. + +* ``isinstance(x, i64)`` (or any other native integer type) is true for + every ``int`` value. + +As a result, code that relies on overflow or wraparound behavior can produce +different results when compiled and interpreted. If you need identical +results, keep all values within the range of the type, or mask the +values explicitly (for example, ``(x * y) & 0xFFFF_FFFF`` with ``u32`` +operands). + Construction ------------ @@ -168,16 +195,22 @@ Construction * ``u8(x: str)`` * ``u8(x: str, base: int)`` -Conversions from ``int`` to a native integer type raise +Conversions from ``int`` or ``float`` to a native integer type raise ``ValueError`` if the value is too large or small. If the value is a constant, such as in ``u8(-1)``, this is a compile-time error instead. -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 -(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. + +Conversions between native integer types never fail: + +* Conversions from a wider native integer type to a narrower one + truncate the value. + +* Conversions between signed and unsigned types of the same or larger + size wrap around if the value is outside the target range. For + example, ``u64(i32(-1))`` is ``2**64 - 1`` and ``i64(u64(2**63))`` + is ``-2**63``. + +These conversions behave differently when interpreted (see +:ref:`native-ints-interpreted`). Implicit conversions -------------------- @@ -234,19 +267,36 @@ and the ``int`` operand is coerced to the native integer type:: b = 1 - x # Similarly, type of "b" is "i64" -If one of the above native integer arithmetic operations overflows or underflows -with signed operands, the behavior is undefined. Signed native integer -types should only be used if all possible values are small enough for -the type. For this reason, the arbitrary-precision ``int`` type is -recommended for signed values unless the performance of integer -operations is critical. +Overflow and errors +------------------- + +Operations on native integer types don't check for overflow: + +* If an arithmetic operation on signed operands (``i64``, ``i32``, + ``i16`` and ``i8``) overflows or underflows, the behavior is + undefined. Signed native integer types should only be used if all + possible values are small enough for the type. For this reason, the + arbitrary-precision ``int`` type is recommended for signed values + unless the performance of integer operations is important. + +* Operations on unsigned operands (``u64``, ``u32``, ``u16`` and ``u8``) + wrap around on overflow. For example, ``u8(255) + 1`` is ``0``. + +* Left shifts (``<<``) wrap around on overflow for both signed and + unsigned types. For example, ``i8(64) << 1`` is ``-128``. + +* Shifting by a negative count or by at least the bit width gives 0, or + -1 for a right shift of a negative value (similar to NumPy). + +Some operations raise an exception instead: + +* Division (``//``) and modulo (``%``) by zero raise ``ZeroDivisionError``. -Operations on unsigned integers (``u64``, ``u32``, ``u16`` and ``u8``) wrap -around on overflow. Left shifts (``<<``) wrap around on overflow for both signed -and unsigned types. For example, ``i8(64) << 1`` is ``-128``. +* Dividing the smallest value of a signed type by -1 (for example, + ``i64(-2**63) // -1``) raises ``OverflowError``. -Shifting by a negative count or by at least the bit width gives 0, or --1 for a right shift of a negative value (similar to NumPy). +When interpreted, none of the operations overflow, since native integers +behave like ``int`` (see :ref:`native-ints-interpreted`). Functions --------- diff --git a/mypyc/doc/using_type_annotations.rst b/mypyc/doc/using_type_annotations.rst index 336d990805040..b1c3758f95c7b 100644 --- a/mypyc/doc/using_type_annotations.rst +++ b/mypyc/doc/using_type_annotations.rst @@ -358,7 +358,10 @@ 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`` -values. The types are imported from the ``mypy_extensions`` module +values, and they don't switch to a boxed representation for large +values, unlike ``int`` (``int`` values outside the range ``-2**62`` to +``2**62 - 1`` are boxed on 64-bit platforms). +The types are imported from the ``mypy_extensions`` module (installed via ``pip install mypy_extensions``). Example:: @@ -377,13 +380,16 @@ Example:: .. note:: Since there are no overflow checks when performing native integer - arithmetic, the above function could result in an overflow or other - undefined behavior if the sum might not fit within 64 bits. + arithmetic, the above function has undefined behavior if the sum + doesn't fit within 64 bits. - The behavior when running as interpreted Python program will be - different if there are overflows. Declaring native integer types - have no effect unless code is compiled. Native integer types are - effectively equivalent to ``int`` when interpreted. +Native integer types only have an effect in compiled code. When +interpreted, they behave exactly like ``int``: values have arbitrary +precision, and there are no range checks, truncation or wraparound. +For example, ``u8(255) + 1`` evaluates to ``256`` when interpreted. +Code that overflows a native integer type may thus behave differently when +compiled and interpreted. See :ref:`native-ints-interpreted` for more +information. Native integer types have these additional properties: @@ -392,7 +398,12 @@ Native integer types have these additional properties: * Conversions between different native integer types must be explicit. A conversion to a narrower native integer type truncates the value - without a runtime overflow check. + without a runtime overflow check. A conversion from ``int`` raises + ``ValueError`` if the value is out of range. + +* Arithmetic on unsigned types wraps around on overflow, but overflow + on signed types is undefined behavior (except for ``<<``, which always + wraps around). * If a binary operation (such as ``+``) or an augmented assignment (such as ``+=``) mixes native integer and ``int`` values, the