C&ES Module-2 Chapter-2
C&ES Module-2 Chapter-2
Chapter-2
For local variables held in registers, don’t use a char or short type unless 8-bit or 16-bit
modular arithmetic is necessary. Use the signed or unsigned int types instead. Unsigned
types are faster when you use divisions
For array entries and global variables held in main memory, use the type with the
smallest size possible to hold the required data. This saves memory footprint. Avoid
using offsets from the base of the array with short type arrays, as LDRH does not
support this.
Use explicit casts when reading array entries or global variables into local variables, or
writing local variables out to array entries.
Avoid implicit or explicit narrowing casts in expressions because they usually cost extra
cycles
Avoid char and short types for function arguments or return values. Instead use the int
type even if the range of the parameter is smaller. This prevents the compiler
performing unnecessary casts.
The SUBS and BNE instructions implement the loop. Our checksum example now has the
minimum number of four instructions per loop. This is much better than six for checksum_v1
and eight for checksum_v3.
3. Loop Unrolling
Each loop iteration costs two instructions in addition to the body of the loop: a subtract to
decrement the loop count and a conditional branch.
■ Define small functions in the same source file and before the functions that call them. The
compiler can then optimize the function call or inline the small function