News for hackers. Stuff that matters.
Re:Signed Integers Are Two’s Complement by kazinator on Thursday May 31, 2018 @08:55AM (#17195895)
Requiring two's complement just means you can't have a sensible C language on some sign-magnitude machine.

Even if nobody cares about such a machine, nothing is achieved other than perhaps simplifying a spec.

A language spec can provide a more detailed two's complement model with certain behaviors being defined that only make sense on two's complement machines, without tossing other machines out the window.

There could be a separate spec for a detailed two's complement model. That could be an independent document. (Analogy: IEEE floating-point.) Or it could be an optional section in ISO C.

Two's complement has some nice properties, but isn't nice in other regards. Multi-precision integer libraries tend to use sign-magnitude, for good reasons.

What I suspect is going on here is that someone is unhappy with what is going on in GCC development, and thinks ISO C is the suitable babysitting tool. (Perhaps a reasonable assumption, if those people won't listen to anything that doesn't come from ISO C.)

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.
Re:Signed Integers Are Two’s Complement by mannykannot on Thursday May 31, 2018 @09:12AM (#17195988)
The worst case is that there would not be an ISO C for such machines. As they are very unusual, this does not strike me as a big deal, and definitely less of an issue than making it easier to avoid invoking undefined behavior.

I take your point about the possible motives behind this proposal, which seem quite plausible.

Re:Signed Integers Are Two’s Complement by kazinator on Thursday May 31, 2018 @10:00AM (#17196339)
But gutting support for sign-magnitude machines has nothing to do with making certain two's complement behaviors defined.

It's like saying we have to drop USB 1.0 support in an OS in order to fix missing features in the Bluetooth stack.

Re:Signed Integers Are Two’s Complement by mannykannot on Thursday May 31, 2018 @11:40AM (#17197139)
I don't think that analogy works well, because if there was a dependency in standards that led to this outcome, would it not be better to break the dependency going forward, or to have avoided it in the first place?

And not having an ISO C standard for sign-magnitude machines (which is not a necessary consequence of the proposed change, it is just the worst case, depending on how ISO chose to deal with the consequences for such machines) does not necessarily force an end to actual C support for them.