Skip to content

Commit dd79587

Browse files
committed
Merge pull request #1237 from Gankro/clarify-math
clarify extreme operator behaviour
2 parents 2ab5a50 + 32ed8d4 commit dd79587

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

text/0560-integer-overflow.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,15 @@ The error conditions that can arise, and their defined results, are as
125125
follows. The intention is that the defined results are the same as the
126126
defined results today. The only change is that now a panic may result.
127127

128-
- The operations `+`, `-`, `*`, `/`, `%` can underflow and
129-
overflow.
130-
- Shift operations (`<<`, `>>`) can shift a value of width `N` by more
131-
than `N` bits.
128+
- The operations `+`, `-`, `*`, can underflow and overflow. When checking is
129+
enabled this will panic. When checking is disabled this will two's complement
130+
wrap.
131+
- The operations `/`, `%` for the arguments `INT_MIN` and `-1`
132+
will unconditionally panic. This is unconditional for legacy reasons.
133+
- Shift operations (`<<`, `>>`) on a value of with `N` can be passed a shift value
134+
>= `N`. It is unclear what behaviour should result from this, so the shift value
135+
is unconditionally masked to be modulo `N` to ensure that the argument is always
136+
in range.
132137

133138
## Enabling overflow checking
134139

@@ -145,7 +150,7 @@ potential overflow (and, in particular, for code where overflow is
145150
expected and normal, they will be immediately guided to use the
146151
wrapping methods introduced below). However, because these checks will
147152
be compiled out whenever an optimized build is produced, final code
148-
wilil not pay a performance penalty.
153+
will not pay a performance penalty.
149154

150155
In the future, we may add additional means to control when overflow is
151156
checked, such as scoped attributes or a global, independent
@@ -451,17 +456,7 @@ were:
451456

452457
# Unresolved questions
453458

454-
The C semantics of wrapping operations in some cases are undefined:
455-
456-
- `INT_MIN / -1`, `INT_MIN % -1`
457-
- Shifts by an excessive number of bits
458-
459-
This RFC takes no position on the correct semantics of these
460-
operations, simply preserving the existing semantics. However, it may
461-
be worth trying to define the wrapping semantics of these operations
462-
in a portable way, even if that implies some runtime cost. Since these
463-
are all error conditions, this is an orthogonal topic to the matter of
464-
overflow.
459+
None today (see Updates section below).
465460

466461
# Future work
467462

@@ -491,6 +486,10 @@ Since it was accepted, the RFC has been updated as follows:
491486
2. `as` was changed to restore the behavior before the RFC (that is,
492487
it truncates to the target bitwidth and reinterprets the highest
493488
order bit, a.k.a. sign-bit, as necessary, as a C cast would).
489+
3. Shifts were specified to mask off the bits of over-long shifts.
490+
4. Overflow was specified to be two's complement wrapping (this was mostly
491+
a clarification).
492+
5. `INT_MIN / -1` and `INT_MIN % -1` panics.
494493

495494
# Acknowledgements and further reading
496495

0 commit comments

Comments
 (0)