This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Do C++ signed types have modulo semantics?


On Mon, Jun 27, 2005 at 08:05:48PM -0700, Mark Mitchell wrote:
> Michael Veksler wrote:
> 
> >>Most programmers "know" that arithmetic is modulo wordsize.  And those few
> >>who know the right answer (only unsigned arithmetic is modulo) will
> >>from time to time slip up and omit the "unsigned" keyword in their
> >>declarations.
> 
> I agree.
> 
> Although the standard clearly makes signed overflow undefined, I think 
> it would be better if GCC defined it to be modulo arithmetic.  The 
> degree to which that would result in inferior code seems likely to be 
> somewhat small, and the amount of user confusion we would eliminate, and 
> the number of programs that would not break with GCC, even though they 
> work with other compilers, seems likely to be high.

I strongly object.

I don't think we should give the user any such promise, and if we do
give such a promise, we will never catch icc.  The main problem is that
we will no longer be able to optimize many loops.

Here's a simple example.

int blah(int);

int func(int a, int b) {
    if (b >= 0) {
	int c = a + b;
	int count = 0;
	for (int i = a; i <= c; i++)
	    count++;
	blah(count);
    }
}

Here, if integer overflow is undefined, this is just

int func(int a, int b) {
    if (b >= 0) {
        blah(b+1);
    }
}

But if integer overflow is defined to wrap, then if a+b overflows,
count will be 0.  We could still simplify the loop, but we are forced
to emit worse code.

That said, it is certainly true that if we just have

int add(int a, int b) {
    return a + b;
}

and the add function is compiled separately, and the target's integer
addition instruction wraps, we would get modulo.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]