outsmarted by the compiler again. (bug?)

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

outsmarted by the compiler again. (bug?)

1,493件の閲覧回数
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 )

ラベル(1)
タグ(1)
0 件の賞賛
返信
2 返答(返信)

1,363件の閲覧回数
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 件の賞賛
返信

1,363件の閲覧回数
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 件の賞賛
返信