Microchip Technology SW006021-1N Benutzerhandbuch
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 180
2012 Microchip Technology Inc.
The consequence of integral promotion as illustrated above is that operations are not
performed with char -type operands, but with int -type operands. However, there are
circumstances when the result of an operation is identical regardless of whether the
operands are of type char or int. In these cases, the compiler will not perform the
integral promotion so as to increase the code efficiency. Consider this example.
performed with char -type operands, but with int -type operands. However, there are
circumstances when the result of an operation is identical regardless of whether the
operands are of type char or int. In these cases, the compiler will not perform the
integral promotion so as to increase the code efficiency. Consider this example.
unsigned char a, b, c;
a = b + c;
Strictly speaking, this statement requires that the values of b and c should be promoted
to unsigned int, the addition performed, the result of the addition cast to the type of
a
to unsigned int, the addition performed, the result of the addition cast to the type of
a
, and then the assignment can take place. Even if the result of the unsigned int
addition of the promoted values of b and c was different to the result of the unsigned
char
char
addition of these values without promotion, after the unsigned int result was
converted back to unsigned char, the final result would be the same. If an 8-bit addi-
tion is more efficient than a 16-bit addition, the compiler will encode the former.
tion is more efficient than a 16-bit addition, the compiler will encode the former.
If, in the above example, the type of a was unsigned int, then integral promotion
would have to be performed to comply with the ANSI C standard.
would have to be performed to comply with the ANSI C standard.
5.6.2
Rotation
The C language does not specify a rotate operator; however, it does allow shifts. The
compiler will detect expressions that implement rotate operations using shift and logical
operators and compile them efficiently.
compiler will detect expressions that implement rotate operations using shift and logical
operators and compile them efficiently.
For the following code:
c = (c << 1) | (c >> 7);
if c is unsigned and non-volatile, the compiler will detect that the intended
operation is a rotate left of 1 bit and will encode the output using the PIC MCU rotate
instructions. A rotate left of 2 bits would be implemented with code like:
operation is a rotate left of 1 bit and will encode the output using the PIC MCU rotate
instructions. A rotate left of 2 bits would be implemented with code like:
c = (c << 2) | (c >> 6);
This code optimization will also work for integral types larger than a char. If the opti-
mization cannot be applied, or this code is ported to another compiler, the rotate will be
implemented, but typically with shifts and a bitwise OR operation.
mization cannot be applied, or this code is ported to another compiler, the rotate will be
implemented, but typically with shifts and a bitwise OR operation.
5.6.3
Switch Statements
The compiler may encode switch statements using one of several strategies. By
default, the compiler chooses a strategy based on the case values that are used inside
the switch statement. Each switch statement is assigned its strategy independently.
default, the compiler chooses a strategy based on the case values that are used inside
the switch statement. Each switch statement is assigned its strategy independently.
The type of strategy can be indicated by using the #pragma switch directive. See
Section 5.14.4.10 “The #pragma switch Directive”, which also lists the available
strategy types. There may be more than one strategy associated with each type.
Section 5.14.4.10 “The #pragma switch Directive”, which also lists the available
strategy types. There may be more than one strategy associated with each type.
There is information printed in the assembly list file for each switch statement detail-
ing the value being switched and the case values listed. See Section 6.6.4 “Switch
Statement Information”.
ing the value being switched and the case values listed. See Section 6.6.4 “Switch
Statement Information”.