K
Keith Thompson
Chris Torek said:Indeed, because signed arithmetic is loosely defined, one should
be able to get away with a lot of perversity (e.g., on the DS9k).
This implementation is not conforming. For instance:
unsigned int u = UINT_MAX; /* ie 0x7fffffff */
u += 3; /* must put 2 in u */
Hence, if you make the above change to a typical 32-bit machine,
you must also make changes so that unsigned arithmetic masks out
the top bit.
Quite right, thanks.
Removing the wording about trap representations doesn't help; you
still need, for example, printf("%u\n", UINT_MAX + 3) to print 2,
which means the implementation really has to do some extra work,
either to zero the padding bit or to ignore it.
I think you can still play some tricks by defining padding bits for
*signed* int, though. For example, if you change INT_MAX from 2**31-1
to 2**30-1, and document that signed int has a padding bit, and that
setting that bit creates a trap representation, I think you can still
have a conforming implementation. (A trap representation, of course,
isn't required to cause a trap; it just invokes undefined behavior --
which signed overflow does anyway.)
If Chris doesn't shoot this one down, I'll begin to suspect that I'm
right.