@@ -125,10 +125,14 @@ The error conditions that can arise, and their defined results, are as
125
125
follows. The intention is that the defined results are the same as the
126
126
defined results today. The only change is that now a panic may result.
127
127
128
- - The operations ` + ` , ` - ` , ` * ` , ` / ` , ` % ` can underflow and
129
- overflow.
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 ` / ` , ` % ` are nonsensical for the arguments ` INT_MIN ` and ` -1 ` .
132
+ When this occurs there is an unconditional panic.
130
133
- Shift operations (` << ` , ` >> ` ) can shift a value of width ` N ` by more
131
- than ` N ` bits.
134
+ than ` N ` bits. This is prevented by unconditionally masking the bits
135
+ of the right-hand-side to wrap modulo ` N ` .
132
136
133
137
## Enabling overflow checking
134
138
@@ -145,7 +149,7 @@ potential overflow (and, in particular, for code where overflow is
145
149
expected and normal, they will be immediately guided to use the
146
150
wrapping methods introduced below). However, because these checks will
147
151
be compiled out whenever an optimized build is produced, final code
148
- wilil not pay a performance penalty.
152
+ will not pay a performance penalty.
149
153
150
154
In the future, we may add additional means to control when overflow is
151
155
checked, such as scoped attributes or a global, independent
@@ -451,17 +455,7 @@ were:
451
455
452
456
# Unresolved questions
453
457
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.
458
+ None today (see Updates section below).
465
459
466
460
# Future work
467
461
@@ -491,6 +485,10 @@ Since it was accepted, the RFC has been updated as follows:
491
485
2 . ` as ` was changed to restore the behavior before the RFC (that is,
492
486
it truncates to the target bitwidth and reinterprets the highest
493
487
order bit, a.k.a. sign-bit, as necessary, as a C cast would).
488
+ 3 . Shifts were specified to mask off the bits of over-long shifts.
489
+ 4 . Overflow was specified to be two's complement wrapping (this was mostly
490
+ a clarification).
491
+ 5 . ` INT_MIN / -1 ` and ` INT_MIN % -1 ` panics.
494
492
495
493
# Acknowledgements and further reading
496
494
0 commit comments