Fix comparison of bit shifts (unchecked overflow) to arithmetic (checked overflow)

Co-authored-by: Daniel Kirchner <daniel@ekpyron.org>

Co-authored-by: Harikrishnan Mulackal <webmail.hari@gmail.com>
This commit is contained in:
William Entriken 2021-05-12 16:58:13 -04:00 committed by chriseth
parent a87998494a
commit 74d5c51303

View File

@ -66,16 +66,23 @@ Shifts
^^^^^^ ^^^^^^
The result of a shift operation has the type of the left operand, truncating the result to match the type. The result of a shift operation has the type of the left operand, truncating the result to match the type.
Right operand must be unsigned type. Trying to shift by signed type will produce a compilation error. The right operand must be of unsigned type, trying to shift by an signed type will produce a compilation error.
- For positive and negative ``x`` values, ``x << y`` is equivalent to ``x * 2**y``. Shifts can be "simulated" using multiplication by powers of two in the following way. Note that the truncation
- For positive ``x`` values, ``x >> y`` is equivalent to ``x / 2**y``. to the type of the left operand is always performed at the end, but not mentioned explicitly.
- For negative ``x`` values, ``x >> y`` is equivalent to ``(x + 1) / 2**y - 1`` (which is the same as dividing ``x`` by ``2**y`` while rounding down towards negative infinity).
- ``x << y`` is equivalent to the mathematical expression ``x * 2**y``.
- ``x >> y`` is equivalent to the mathematical expression ``x / 2**y``, rounded towards negative infinity.
.. warning:: .. warning::
Before version ``0.5.0`` a right shift ``x >> y`` for negative ``x`` was equivalent to ``x / 2**y``, Before version ``0.5.0`` a right shift ``x >> y`` for negative ``x`` was equivalent to
the mathematical expression ``x / 2**y`` rounded towards zero,
i.e., right shifts used rounding up (towards zero) instead of rounding down (towards negative infinity). i.e., right shifts used rounding up (towards zero) instead of rounding down (towards negative infinity).
.. note::
Overflow checks are never performed for shift operations as they are done for arithmetic operations.
Instead, the result is always truncated.
Addition, Subtraction and Multiplication Addition, Subtraction and Multiplication
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^