GBDK libraries documentation
PrevChapter 2. The Gameboy as a TargetNext

Avoiding Promotion

lcc assumes that any parameters in an expression are signed by default which can cause unnecessary promotion. Promotion is where the compiler believes that the result of an operation will overflow and so it promotes the variable up a size, performs the operation and then demotes it back to the proper size. For example in:

    UBYTE i, j = 0;
    i = j+0x80;
				
The compiler assumes that the argument is signed, and as 0x80 is greater than the biggest eight bit signed number 0x7f it is promoted to sixteen bit. The operation is performed then the result truncated.

This can be solved by explicitly telling the compiler that the argument is unsigned by adding a trailing U. A better version of the above code is:

    UBYTE i, j = 0;
    i = j+0x80U;
				

Changing the order of operations in a function can also stop promotion. ??why??


PrevHomeNext
Size of variablesUpUsing global variables