2:50 PM
@usr: What I would like to see would be a set of macros that would allow code to specify what it requires (a typical implementation could simply check if the requirements were compatible with a what a compiler would do given the current command-line settings). It should be possible to implement a thing in such a fashion that almost any current compiler could be made compliant with that part of the new standard merely by adding a suitable tcsb.h
[I'd use the terms...
Testably Constrained Behavior for things that are presently UB like overflow, and Testably Specified Behavior for things that are presently IB, like storing a 1234567 to int16_t
, and use tcsb.h for macros related to both].
For some kinds of programming, tightly-trapped overflows are needed. For others, yield-arbitrary-value and even maybe-exit-loops-prematurely are needed. A type of semantics I'd like to see added would be loosely trapped overflow (have a flag like errno
, and also allow code to mark a block which the compiler would be free to have exit prematurely in case of overflow [useful on systems with hardware-trapped overflow], with the semantics...
...that if the compiler exits the block for overflow [or other similarly-handled cause] there would be no guarantee as to which statements executed or did not, and further that the compiler need not do anything in response to an overflow that does not cause loss of numeric precision. For example, given long1 = int1+int2;
a compiler could signal an overflow if the sum isn't representable as int
, and...
...would be required to signal an overflow if long1
didn't receive the arithmetically-correct value of int1+int2
, but would be not be required to signal an overflow in cases where the generated code was able to preserve arithmetic correctness. The only useful optimization I can think of that wouldn't be available with a partially-indeterminate-value semantic would be loop index replacement, and that could be allowed if code specified that early loop exit was an acceptable overflow behavior.
I think C could significantly outperform FORTRAN for some applications if it reined in some forms of Undefined Behavior to the point that programs could exploit them, especially if it defined loosely-trapped overflows.
For example, if code needed to compute the sum of many int32_t
values or report that if overflowed, having a DSP with a 40-bit accumulator compute subtotals of groups of 128 items and check after each group whether the 40-bit total was within range for an int32_t
would be faster than having it check after adding each value. A straightforward optimization under loose semantics, but not under semantics which would require that the loop exit as soon as an overflow occurs.
Code which wanted to meet the specified requirements using the present Standard would need to either compute the total using an int64_t
or check for overflow at each step; on many platforms, one or the other of those choices (if not both) would take twice as long as would the optimal code for loose overflow semantics.