News for hackers. Stuff that matters.
Re:Signed Integers Are Two’s Complement by phkahler on Thursday May 31, 2018 @09:30AM (#17196129)
>> Even if nobody cares about such a machine, nothing is achieved other than perhaps simplifying a spec.

No, I use 16bit values to represent angles in embedded systems all the time. I routinely expect arithmetic on these values to roll over as 2's complement and I expect to take differences of angles using 2's complement all the time. I'm fully aware that this is undefined behavior and needs to be verified on each compiler/processor combination. It has always worked and yet it's undefined behavior. It would be nice for it to be defined. There are no modern machines that would be impacted by this.

Re:Signed Integers Are Two’s Complement by kazinator on Thursday May 31, 2018 @09:58AM (#17196319)
> It would be nice for it to be defined.

Your compiler implementors can do that in their documentation; it doesn't have to be pushed into the standard.

There are reasons for it being regarded as not nice to define something like that or make it some compiler option or pragma and whatever. Overflow is in fact an error in many situations, because it can happen unexpectedly; it's useful for the compiler or machine to trap overflows.

What I was referring to in my above remark is mainly the removal of support for sign-magnitude; if you read my response more carefully you will see that I favor ways of making the behavior defined without sacrificing things.

Anyway, you can use unsigned arithmetic instead to do portable two's complement. Unsigned integers have the required roll-over behavior.

Some 28 years ago I made an emulator for the MC68000 processor. I used unsigned 32 bit integers for all the arithmetic, including the signed operations. E.g. the difference between a signed and unsigned addition was only how the flag are calculated, like Z, X and C.

Re:Signed Integers Are Two’s Complement by markrages on Thursday May 31, 2018 @10:13AM (#17196426)
Yes, it is annoying to rrad comments that assume overflow is always a programming error.

You can store the angle as a union of signed and unsigned type. Do arithmetic on the unsigned member, where overflow is defined. (Both members are equivalent angles)

Re:Signed Integers Are Two’s Complement by kazinator on Thursday May 31, 2018 @11:37AM (#17197110)
Or you could just convert from the unsigned to the signed type, rather than dragging unions into it.

This conversion doesn't have undefined behavior; it produces an implementation defined result.

C programs can simulate two's complement math using unsigned types, avoiding UB. Then rely on IB to convert between signed and unsigned.

Re:Signed Integers Are Two’s Complement by sanxiyn on Thursday May 31, 2018 @10:17AM (#17196457)
Not always, but overflows are usually bugs. This is replicated finding of dynamic overflow checking tools, over and over.
Re:Signed Integers Are Two’s Complement by kazinator on Thursday May 31, 2018 @11:39AM (#17197119)
And if overflow is well-defined, then those tools must switch your C dialect to a non-ISO-C conforming one in order to do their job.