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
6 changes: 6 additions & 0 deletions mypyc/doc/differences_from_python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <native-ints>` 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
Expand Down
11 changes: 5 additions & 6 deletions mypyc/doc/float_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand All @@ -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)``
Expand Down
96 changes: 73 additions & 23 deletions mypyc/doc/int_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
------------

Expand Down Expand Up @@ -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
--------------------
Expand Down Expand Up @@ -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
---------
Expand Down
27 changes: 19 additions & 8 deletions mypyc/doc/using_type_annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand All @@ -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:

Expand All @@ -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
Expand Down
Loading