[comp.sys.sgi] Mixed mode Arithmatic

SOFPJF@VM.UOGUELPH.CA (Peter Jaspers-Fayer) (02/21/91)

Seems that SGI C is different from any other C that we've seen when it
comes to adding pointers and long ints in various combinations.

#include <stdio.h>
main(){
long a, b, *P;
char s[2000];
 a = 2;
 b = 5;
 P = (long*)s;
 printf("Sizes: a P %d %d\n",sizeof(a),sizeof(P));
 printf("s,P:       %ld %ld\n", s,P);
 printf("a,b:       %ld %ld\n", a,b);
 printf("a+P+b:     %ld\n", a+P+b);
 printf("a+b+P:     %ld\n", a+b+P);
 printf("a+(P+b):   %ld\n", a+(P+b));
 printf("a+(b+P):   %ld\n", a+(b+P)); /* Watch this one */

}

Sequent and others:
Sizes: a P      4 4
s,P:            1073735252 1073735252
a,b:            2 5
a+P+b:          1073735280
a+b+P:          1073735280
a+(P+b):        1073735280
a+(b+P):        1073735280

SGI PI:
Sizes: a P      4 4
s,P:            2147467180 2147467180
a,b:            2 5
a+P+b:          2147467208
a+b+P:          2147467208
a+(P+b):        2147467208
a+(b+P):        2147467292   [!]


    As there does not appear to be a fixed standard, I can't say who's
"right", but this note will (I hope) serve as a warning to others that
may be surprised at how litterally SGI's C takes these sort of
expressions.

In every other compiler, I would guess that the ints are added together
first, then the result used to bump the pointer, regardless of the order
or punctuation of the expression.

 /PJ                                                SofPJF@VM.UoGuelph.Ca
         --------------------------------------
An ounce of application is worth a ton of abstraction.

davea@quasar.wpd.sgi.com (David B.Anderson) (02/22/91)

In article <9102211136.aa10474@VGR.BRL.MIL>, SOFPJF@VM.UOGUELPH.CA (Peter Jaspers-Fayer) writes:
[stuff deleted]
> #include <stdio.h>
> main(){
> long a, b, *P;
> char s[2000];
>  a = 2;
>  b = 5;
>  P = (long*)s;
>  printf("Sizes: a P %d %d\n",sizeof(a),sizeof(P));
>  printf("s,P:       %ld %ld\n", s,P);
>  printf("a,b:       %ld %ld\n", a,b);
>  printf("a+P+b:     %ld\n", a+P+b);
>  printf("a+b+P:     %ld\n", a+b+P);
>  printf("a+(P+b):   %ld\n", a+(P+b));
>  printf("a+(b+P):   %ld\n", a+(b+P)); /* Watch this one */
		The compiler generates incorrect code for this.
> }
[stuff deleted ]
> 
> SGI PI:
> Sizes: a P      4 4
> s,P:            2147467180 2147467180
> a,b:            2 5
> a+P+b:          2147467208
> a+b+P:          2147467208
> a+(P+b):        2147467208
> a+(b+P):        2147467292   [!]
	This last answer is wrong.
>     As there does not appear to be a fixed standard, I can't say who's
> "right", but this note will (I hope) serve as a warning to others that

This is simply a bug.     The (previously unknown) bug has been around a 
long time.

I have a fix and it will be in the next release (4.0).

Thanks to Peter Jaspers-Fayer for providing a nice test case with the
bug report.

Apologies for any inconvenience.....
[ David B. Anderson  Silicon Graphics  (415)335-1548  davea@sgi.com ]
[``What can go wrong?''                           --Calvin to Hobbes]