outsmarted by the compiler again. (bug?)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

outsmarted by the compiler again. (bug?)

681 Views
MrBean
Contributor I
static unsigned long ArrayInIdx  =0;static unsigned long ArrayOutIdx =0;void test1(void) {   unsigned long idx = ArrayInIdx;      // get array index   if( -16L+idx-ArrayOutIdx >= 0 )      // not full ? (less than 16)      return;   ArrayInIdx++;                        // allocate array element}void test2(void) {   unsigned long idx = ArrayInIdx;      // get array index   if( idx-ArrayOutIdx >= 16L )         // not full ? (less than 16)      return;   ArrayInIdx++;                        // allocate array element}static signed long sArrayInIdx  =0;static signed long sArrayOutIdx =0;void test3(void) {   signed long idx = sArrayInIdx;        // get array index   if( -16L+idx-sArrayOutIdx >= 0 )      // not full ? (less than 16)      return;   sArrayInIdx++;                        // allocate array element}

 

 

 

The test1() function compiles to a single rts, ie. the compiler thinks the condition is always true.

The other two compile as intended.

 

Am i wrong to assume that the argument of the 'if' always is signed ?

Is the compiler messing up ?

 

 

( PS: CW7.2se )

Labels (1)
0 Kudos
2 Replies

551 Views
vier_kuifjes
Senior Contributor I

Seems logical to me. An unsigned long can only be >= 0, so the outcome of the if must always be true...

0 Kudos

551 Views
MrBean
Contributor I

It seems i assumed promotion rules wrongly.

Mixing signed and unsigned in the if argument promotes to unsigned, i assumed signed.

Didnt know, didnt seem logical to me.

0 Kudos