4.1 Thumb Register Usage 4.2 ARM-Thumb Interworking 4.3 Other Branch Instructions 4.4 Data Processing Instructions 4.5 Single-Register Load-Store Instructions 4.6 Multiple-Register Load-Store Instructions 4.7 Stack Instructions 4.8 Software Interrupt Instruction 4.9 Summary
Chapter 4Introduction to the Thumb Instruction Set This chapter introduces the Thumb instruction set. Thumb encodes a subset of the 32-bit ARM instructions into a 16-bit instruction set space. Since Thumb has higher performance than ARM on a processor with a 16-bit data bus, but lower performance than ARM on a 32-bit data bus, use Thumb for memory-constrained systems. Thumb has higher code density—the space taken up in memory by an executable program—than ARM. For memory-constrained embedded systems, for example, mobile phones and PDAs, code density is very important. Cost pressures also limit memory size, width, and speed. On average, a Thumb implementation of the same code takes up around 30% less memory than the equivalent ARM implementation. As an example, Figure 4.1 shows the same divide code routine implemented in ARM and Thumb assembly code. Even though the Thumb implementation uses more instructions, the overall memory footprint is reduced. Code density was the main driving force for the Thumb instruction set. Because it was also designed as a compiler target, rather than for hand-written assembly code, we recommend that you write Thumb-targeted code in a high-level language like C or C++. Each Thumb instruction is related to a 32-bit ARM instruction. Figure 4.2 shows a simple Thumb ADD instruction being decoded into an equivalent ARM ADD instruction. Table 4.1 provides a complete list of Thumb instructions available in the THUMBv2 architecture used in the ARMv5TE architecture. Only the branch relative instruction can be conditionally executed. The limited space available in 16 bits causes the barrel shift operations ASR, LSL, LSR, and ROR to be separate instructions in the Thumb ISA. 87
88 Chapter 4 Introduction to the Thumb Instruction Set ARM code Thumb code ARMDivide ThumbDivide ; IN: r0(value),r1(divisor) ; IN: r0(value),r1(divisor) ; OUT: r2(MODulus),r3(DIVide) ; OUT: r2(MODulus),r3(DIVide) MOV r3,#0 MOV r3,#0 loop loop ADD r3,#1 SUBS r0,r0,r1 SUB r0,r1 ADDGE r3,r3,#1 BGE loop BGE loop SUB r3,#1 ADD r2,r0,r1 ADD r2,r0,r1 5 × 4 = 20 bytes 6 × 2 = 12 bytes Figure 4.1 Code density. Thumb 16-bit D ARM 32-bit instruction E instruction C ADD r0, #3 O ADDS r0, r0, #3 D E R cpsr = nzcvqifT_SVC Figure 4.2 Thumb instruction decoding. We only describe a subset of these instructions in this chapter since most code is compiled from a high-level language. See Appendix A for a complete list of Thumb instructions. This chapter covers Thumb register usage, ARM-Thumb interworking, branch instruc- tions, data processing instructions, load-store instructions, stack operations, and software interrupts.
4.1 Thumb Register Usage 89 Table 4.1 Thumb instruction set. Mnemonics THUMB ISA Description ADC v1 add two 32-bit values and carry ADD v1 add two 32-bit values AND v1 logical bitwise AND of two 32-bit values ASR v1 arithmetic shift right B v1 branch relative BIC v1 logical bit clear (AND NOT) of two 32-bit values BKPT v2 breakpoint instructions BL v1 relative branch with link BLX v2 branch with link and exchange BX v1 branch with exchange CMN v1 compare negative two 32-bit values CMP v1 compare two 32-bit integers EOR v1 logical exclusive OR of two 32-bit values LDM v1 load multiple 32-bit words from memory to ARM registers LDR v1 load a single value from a virtual address in memory LSL v1 logical shift left LSR v1 logical shift right MOV v1 move a 32-bit value into a register MUL v1 multiply two 32-bit values MVN v1 move the logical NOT of 32-bit value into a register NEG v1 negate a 32-bit value ORR v1 logical bitwise OR of two 32-bit values POP v1 pops multiple registers from the stack PUSH v1 pushes multiple registers to the stack ROR v1 rotate right a 32-bit value SBC v1 subtract with carry a 32-bit value STM v1 store multiple 32-bit registers to memory STR v1 store register to a virtual address in memory SUB v1 subtract two 32-bit values SWI v1 software interrupt TST v1 test bits of a 32-bit value 4.1 Thumb Register Usage In Thumb state, you do not have direct access to all registers. Only the low registers r0 to r7 are fully accessible, as shown in Table 4.2. The higher registers r8 to r12 are only accessible with MOV, ADD, or CMP instructions. CMP and all the data processing instructions that operate on low registers update the condition flags in the cpsr.
90 Chapter 4 Introduction to the Thumb Instruction Set Table 4.2 Summary of Thumb register usage. Registers Access r0–r7 fully accessible r8–r12 only accessible by MOV, ADD, and CMP r13 sp limited accessibility r14 lr limited accessibility r15 pc limited accessibility cpsr only indirect access spsr no access You may have noticed from the Thumb instruction set list and from the Thumb register usage table that there is no direct access to the cpsr or spsr. In other words, there are no MSR- and MRS-equivalent Thumb instructions. To alter the cpsr or spsr, you must switch into ARM state to use MSR and MRS. Similarly, there are no coprocessor instructions in Thumb state. You need to be in ARM state to access the coprocessor for configuring cache and memory management. 4.2 ARM-Thumb Interworking ARM-Thumb interworking is the name given to the method of linking ARM and Thumb code together for both assembly and C/C++. It handles the transition between the two states. Extra code, called a veneer, is sometimes needed to carry out the transition. ATPCS defines the ARM and Thumb procedure call standards. To call a Thumb routine from an ARM routine, the core has to change state. This state change is shown in the T bit of the cpsr. The BX and BLX branch instructions cause a switch between ARM and Thumb state while branching to a routine. The BX lr instruction returns from a routine, also with a state switch if necessary. The BLX instruction was introduced in ARMv5T. On ARMv4T cores the linker uses a veneer to switch state on a subroutine call. Instead of calling the routine directly, the linker calls the veneer, which switches to Thumb state using the BX instruction. There are two versions of the BX or BLX instructions: an ARM instruction and a Thumb equivalent. The ARM BX instruction enters Thumb state only if bit 0 of the address in Rn is set to binary 1; otherwise it enters ARM state. The Thumb BX instruction does the same. Syntax: BX Rm BLX Rm | label
4.2 ARM-Thumb Interworking 91 BX Thumb version branch exchange pc = Rn & 0xfffffffe T = Rn[0] BLX Thumb version of the branch exchange with link lr = (instruction address after the BLX) + 1 pc = label, T = 0 pc = Rm & 0xfffffffe, T = Rm[0] Unlike the ARM version, the Thumb BX instruction cannot be conditionally executed. Example This example shows a small code fragment that uses both the ARM and Thumb versions of the BX instruction. You can see that the branch address into Thumb has the lowest bit set. 4.1 This sets the T bit in the cpsr to Thumb state. The return address is not automatically preserved by the BX instruction. Rather the code sets the return address explicitly using a MOV instruction prior to the branch: ; ARM code ; word aligned CODE32 ; +1 to enter Thumb state LDR r0, =thumbCode+1 ; set the return address MOV lr, pc ; branch to Thumb code & mode BX r0 ; continue here ; halfword aligned ; Thumb code ; return to ARM code & state CODE16 thumbCode ADD r1, #1 BX lr A branch exchange instruction can also be used as an absolute branch providing bit 0 isn’t used to force a state change: ; address(thumbCode) = 0x00010000 ; cpsr = nzcvqIFt_SVC ; r0 = 0x00000000 0x00009000 LDR r0, =thumbCode+1 ; cpsr = nzcvqIFt_SVC ; r0 = 0x00010001 0x00009008 BX r0
92 Chapter 4 Introduction to the Thumb Instruction Set ; cpsr = nzcvqIFT_SVC ; r0 = 0x00010001 ; pc = 0x00010000 You can see that the least significant bit of register r0 is used to set the T bit of the cpsr. The cpsr changes from IFt, prior to the execution of the BX, to IFT, after execution. The pc is then set to point to the start address of the Thumb routine. ■ Example Replacing the BX instruction with BLX simplifies the calling of a Thumb routine since it sets 4.2 the return address in the link register lr: CODE32 ; enter Thumb state LDR r0, =thumbRoutine+1 ; jump to Thumb code BLX r0 ; continue here CODE16 r1, #1 ; return to ARM code and state ■ thumbRoutine r14 ADD BX 4.3 Other Branch Instructions There are two variations of the standard branch instruction, or B. The first is similar to the ARM version and is conditionally executed; the branch range is limited to a signed 8-bit immediate, or −256 to +254 bytes. The second version removes the conditional part of the instruction and expands the effective branch range to a signed 11-bit immediate, or −2048 to +2046 bytes. The conditional branch instruction is the only conditionally executed instruction in Thumb state. Syntax: B<cond> label B label BL label B branch pc = label BL branch with link pc = label lr = (instruction address after the BL) + 1 The BL instruction is not conditionally executed and has an approximate range of +/−4 MB. This range is possible because BL (and BLX) instructions are translated into a pair of 16-bit
4.4 Data Processing Instructions 93 Thumb instructions. The first instruction in the pair holds the high part of the branch offset, and the second the low part. These instructions must be used as a pair. The code here shows the various instructions used to return from a BL subroutine call: MOV pc, lr BX lr POP {pc} To return, we set the pc to the value in lr. The stack instruction called POP will be discussed in more detail in Section 4.7. 4.4 Data Processing Instructions The data processing instructions manipulate data within registers. They include move instructions, arithmetic instructions, shifts, logical instructions, comparison instructions, and multiply instructions. The Thumb data processing instructions are a subset of the ARM data processing instructions. Syntax: Rd, Rm <ADC|ADD|AND|BIC|EOR|MOV|MUL|MVN|NEG|ORR|SBC|SUB> <ADD|ASR|LSL|LSR|ROR|SUB> Rd, Rn #immediate <ADD|MOV|SUB> Rd,#immediate <ADD|SUB> Rd,Rn,Rm ADD Rd,pc,#immediate ADD Rd,sp,#immediate <ADD|SUB> sp, #immediate <ASR|LSL|LSR|ROR> Rd,Rs <CMN|CMP|TST> Rn,Rm CMP Rn,#immediate MOV Rd,Rn ADC add two 32-bit values and carry Rd = Rd + Rm + C flag ADD add two 32-bit values Rd = Rn + immediate Rd = Rd + immediate Rd = Rd + Rm 2) Rd = Rd + Rm Rd = (pc & 0xfffffffc) + (immediate Rd = sp + (immediate 2) sp = sp + (immediate 2)
94 Chapter 4 Introduction to the Thumb Instruction Set AND logical bitwise AND of two 32-bit values Rd = Rd & Rm ASR arithmetic shift right Rd = Rm immediate, C flag = Rm[immediate − 1] Rd = Rd Rs, C flag = Rd[Rs - 1] BIC logical bit clear (AND NOT) of two 32-bit Rd = Rd AND NOT(Rm) values CMN compare negative two 32-bit values Rn + Rm sets flags CMP compare two 32-bit integers Rn − immediate sets flags EOR logical exclusive OR of two 32-bit values Rn − Rm sets flags Rd = Rd EOR Rm LSL logical shift left Rd = Rm immediate, C flag = Rm[32 − immediate] Rd = Rd Rs, C flag = Rd[32 − Rs] LSR logical shift right Rd = Rm immediate, C flag = Rd[immediate − 1] Rd = Rd Rs, C flag = Rd[Rs − 1] MOV move a 32-bit value into a register Rd = immediate MUL multiply two 32-bit values Rd = Rn Rd = Rm Rd = (Rm ∗ Rd)[31:0] MVN move the logical NOT of a 32-bit value Rd = NOT(Rm) into a register Rd = 0 − Rm NEG negate a 32-bit value ORR logical bitwise OR of two 32-bit values Rd = Rd OR Rm ROR rotate right a 32-bit value Rd = Rd RIGHT_ROTATE Rs, C flag = Rd[Rs−1] SBC subtract with carry a 32-bit value Rd = Rd − Rm − NOT(C flag) SUB subtract two 32-bit values Rd = Rn − immediate Rd = Rd − immediate Rd = Rn − Rm 2) sp = sp − (immediate TST test bits of a 32-bit value Rn AND Rm sets flags
4.4 Data Processing Instructions 95 These instructions follow the same style as the equivalent ARM instructions. Most Thumb data processing instructions operate on low registers and update the cpsr. The exceptions are MOV Rd,Rn ADD Rd,Rm CMP Rn,Rm ADD sp, #immediate SUB sp, #immediate ADD Rd,sp,#immediate ADD Rd,pc,#immediate which can operate on the higher registers r8–r14 and the pc. These instructions, except for CMP, do not update the condition flags in the cpsr when using the higher registers. The CMP instruction, however, always updates the cpsr. Example This example shows a simple Thumb ADD instruction. It takes two low registers r1 and r2 4.3 and adds them together. The result is then placed into register r0, overwriting the original contents. The cpsr is also updated. PRE cpsr = nzcvIFT_SVC r1 = 0x80000000 r2 = 0x10000000 ADD r0, r1, r2 POST r0 = 0x90000000 ■ cpsr = NzcvIFT_SVC Example Thumb deviates from the ARM style in that the barrel shift operations (ASR, LSL, LSR, and 4.4 ROR) are separate instructions. This example shows the logical left shift (LSL) instruction to multiply register r2 by 2. PRE r2 = 0x00000002 r4 = 0x00000001 LSL r2, r4 POST r2 = 0x00000004 ■ r4 = 0x00000001 See Appendix A for a complete list of Thumb data processing instructions.
96 Chapter 4 Introduction to the Thumb Instruction Set 4.5 Single-Register Load-Store Instructions The Thumb instruction set supports load and storing registers, or LDR and STR. These instructions use two preindexed addressing modes: offset by register and offset by immediate. Syntax: <LDR|STR>{<B|H>} Rd, [Rn,#immediate] LDR{<H|SB|SH>} Rd,[Rn,Rm] STR{<B|H>} Rd,[Rn,Rm] LDR Rd,[pc,#immediate] <LDR|STR> Rd,[sp,#immediate] LDR load word into a register Rd <- mem32[address] STR save word from a register Rd -> mem32[address] LDRB load byte into a register Rd <- mem8[address] STRB save byte from a register Rd -> mem8[address] LDRH load halfword into a register Rd <- mem16[address] STRH save halfword into a register Rd -> mem16[address] LDRSB load signed byte into a register Rd <- SignExtend(mem8[address]) LDRSH load signed halfword into a register Rd <- SignExtend(mem16[address]) You can see the different addressing modes in Table 4.3. The offset by register uses a base register Rn plus the register offset Rm. The second uses the same base register Rn plus a 5-bit immediate or a value dependent on the data size. The 5-bit offset encoded in the instruction is multiplied by one for byte accesses, two for 16-bit accesses, and four for 32-bit accesses. Table 4.3 Addressing modes. Type Syntax Load/store register [Rn, Rm] Base register + offset [Rn, #immediate] Relative [pc|sp, #immediate] Example This example shows two Thumb instructions that use a preindex addressing mode. Both 4.5 use the same pre-condition.
4.6 Multiple-Register Load-Store Instructions 97 PRE mem32[0x90000] = 0x00000001 mem32[0x90004] = 0x00000002 mem32[0x90008] = 0x00000003 r0 = 0x00000000 r1 = 0x00090000 r4 = 0x00000004 LDR r0, [r1, r4] ; register POST r0 = 0x00000002 r1 = 0x00090000 r4 = 0x00000004 LDR r0, [r1, #0x4] ; immediate POST r0 = 0x00000002 Both instructions carry out the same operation. The only difference is the second LDR uses a fixed offset, whereas the first one depends on the value in register r4. ■ 4.6 Multiple-Register Load-Store Instructions The Thumb versions of the load-store multiple instructions are reduced forms of the ARM load-store multiple instructions. They only support the increment after (IA) addressing mode. Syntax : <LDM|STM>IA Rn!, {low Register list} LDMIA load multiple registers {Rd}*N <- mem32[Rn + 4 ∗N], Rn = Rn + 4 ∗N STMIA save multiple registers {Rd}*N -> mem32[Rn + 4 ∗N], Rn = Rn + 4 ∗N Here N is the number of registers in the list of registers. You can see that these instruc- tions always update the base register Rn after execution. The base register and list of registers are limited to the low registers r0 to r7. Example This example saves registers r1 to r3 to memory addresses 0x9000 to 0x900c. It also updates 4.6 base register r4. Note that the update character ! is not an option, unlike with the ARM instruction set. PRE r1 = 0x00000001 r2 = 0x00000002
98 Chapter 4 Introduction to the Thumb Instruction Set r3 = 0x00000003 r4 = 0x9000 STMIA r4!,{r1,r2,r3} POST mem32[0x9000] = 0x00000001 mem32[0x9004] = 0x00000002 mem32[0x9008] = 0x00000003 ■ r4 = 0x900c 4.7 Stack Instructions The Thumb stack operations are different from the equivalent ARM instructions because they use the more traditional POP and PUSH concept. Syntax: POP {low_register_list{, pc}} PUSH {low_register_list{, lr}} POP pop registers from the stacks Rd∗N <- mem32[sp + 4 ∗N], sp = sp + 4 ∗N PUSH push registers on to the stack Rd∗N -> mem32[sp + 4 ∗N], sp = sp − 4 ∗N The interesting point to note is that there is no stack pointer in the instruction. This is because the stack pointer is fixed as register r13 in Thumb operations and sp is automatically updated. The list of registers is limited to the low registers r0 to r7. The PUSH register list also can include the link register lr; similarly the POP register list can include the pc. This provides support for subroutine entry and exit, as shown in Example 4.7. The stack instructions only support full descending stack operations. Example In this example we use the POP and PUSH instructions. The subroutine ThumbRoutine is 4.7 called using a branch with link (BL) instruction. ; Call subroutine BL ThumbRoutine ; continue ThumbRoutine {r1, lr} ; enter subroutine PUSH r0, #2 ; return from subroutine MOV {r1, pc} POP
4.8 Software Interrupt Instruction 99 The link register lr is pushed onto the stack with register r1. Upon return, register r1 is popped off the stack, as well as the return address being loaded into the pc. This returns from the subroutine. ■ 4.8 Software Interrupt Instruction Similar to the ARM equivalent, the Thumb software interrupt (SWI) instruction causes a software interrupt exception. If any interrupt or exception flag is raised in Thumb state, the processor automatically reverts back to ARM state to handle the exception. Syntax: SWI immediate SWI software interrupt lr_svc = address of instruction following the SWI spsr_svc = cpsr pc = vectors + 0x8 cpsr mode = SVC cpsr I = 1 (mask IRQ interrupts) cpsr T = 0 (ARM state) The Thumb SWI instruction has the same effect and nearly the same syntax as the ARM equivalent. It differs in that the SWI number is limited to the range 0 to 255 and it is not conditionally executed. Example This example shows the execution of a Thumb SWI instruction. Note that the processor 4.8 goes from Thumb state to ARM state after execution. PRE cpsr = nzcVqifT_USER ; lr = r14 pc = 0x00008000 lr = 0x003fffff r0 = 0x12 0x00008000 SWI 0x45 POST cpsr = nzcVqIft_SVC spsr = nzcVqifT_USER pc = 0x00000008 ■ lr = 0x00008002 r0 = 0x12
100 Chapter 4 Introduction to the Thumb Instruction Set 4.9 Summary In this chapter we covered the Thumb instruction set. All Thumb instructions are 16 bits in length. Thumb provides approximately 30% better code density over ARM code. Most code written for Thumb is in a high-level language such as C and C++. ATPCS defines how ARM and Thumb code call each other, called ARM-Thumb interworking. Interworking uses the branch exchange (BX) instruction and branch exchange with link (BLX) instruction to change state and jump to a specific routine. In Thumb, only the branch instructions are conditionally executed. The barrel shift operations (ASR, LSL, LSR, and ROR) are separate instructions. The multiple-register load-store instructions only support the increment after (IA) addressing mode. The Thumb instruction set includes POP and PUSH instructions as stack operations. These instructions only support a full descending stack. There are no Thumb instructions to access the coprocessors, cpsr, and spsr.
This Page Intentionally Left Blank
5.1 Overview of C Compilers and Optimization 5.2 Basic C Data Types 5.2.1 Local Variable Types 5.2.2 Function Argument Types 5.2.3 Signed versus Unsigned Types 5.3 C Looping Structures 5.3.1 Loops with a Fixed Number of Iterations 5.3.2 Loops Using a Variable Number of Iterations 5.3.3 Loop Unrolling 5.4 Register Allocation 5.5 Function Calls 5.6 Pointer Aliasing 5.7 Structure Arrangement 5.8 Bit-fields 5.9 Unaligned Data and Endianness 5.10 Division 5.10.1 Repeated Unsigned Division with Remainder 5.10.2 Converting Divides into Multiplies 5.10.3 Unsigned Division by a Constant 5.10.4 Signed Division by a Constant 5.11 Floating Point 5.12 Inline Functions and Inline Assembly 5.13 Portability Issues 5.14 Summary
5C h a p t e r Efficient C Programming The aim of this chapter is to help you write C code in a style that will compile efficiently on the ARM architecture. We will look at many small examples to show how the compiler translates C source to ARM assembler. Once you have a feel for this translation process, you can distinguish fast C code from slow C code. The techniques apply equally to C++, but we will stick to plain C for these examples. We start with an overview of C compilers and optimization, which will give an idea of the problems the C compiler faces when optimizing your code. By understanding these problems you can write source code that will compile more efficiently in terms of increased speed and reduced code size. The following sections are grouped by topic. Sections 5.2 and 5.3 look at how to optimize a basic C loop. These sections use a data packet checksum as a simple example to illustrate the ideas. Sections 5.4 and 5.5 look at optimizing a whole C function body, including how the compiler allocates registers within a function and how to reduce the overhead of a function call. Sections 5.6 through 5.9 look at memory issues, including handling pointers and how to pack data and access memory efficiently. Sections 5.10 through 5.12 look at basic operations that are usually not supported directly by ARM instructions. You can add your own basic operations using inline functions and assembler. The final section summarizes problems you may face when porting C code from another architecture to the ARM architecture. 103
104 Chapter 5 Efficient C Programming 5.1 Overview of C Compilers and Optimization This chapter assumes that you are familiar with the C language and have some knowledge of assembly programming. The latter is not essential, but is useful for following the compiler output examples. See Chapter 3 or Appendix A for details of ARM assembly syntax. Optimizing code takes time and reduces source code readability. Usually, it’s only worth optimizing functions that are frequently executed and important for performance. We recommend you use a performance profiling tool, found in most ARM simulators, to find these frequently executed functions. Document nonobvious optimizations with source code comments to aid maintainability. C compilers have to translate your C function literally into assembler so that it works for all possible inputs. In practice, many of the input combinations are not possible or won’t occur. Let’s start by looking at an example of the problems the compiler faces. The memclr function clears N bytes of memory at address data. void memclr(char *data, int N) { for (; N>0; N--) { *data=0; data++; } } No matter how advanced the compiler, it does not know whether N can be 0 on input or not. Therefore the compiler needs to test for this case explicitly before the first iteration of the loop. The compiler doesn’t know whether the data array pointer is four-byte aligned or not. If it is four-byte aligned, then the compiler can clear four bytes at a time using an int store rather than a char store. Nor does it know whether N is a multiple of four or not. If N is a multiple of four, then the compiler can repeat the loop body four times or store four bytes at a time using an int store. The compiler must be conservative and assume all possible values for N and all possible alignments for data. Section 5.3 discusses these specific points in detail. To write efficient C code, you must be aware of areas where the C compiler has to be conservative, the limits of the processor architecture the C compiler is mapping to, and the limits of a specific C compiler. Most of this chapter covers the first two points above and should be applicable to any ARM C compiler. The third point will be very dependent on the compiler vendor and compiler revision. You will need to look at the compiler’s documentation or experiment with the compiler yourself.
5.2 Basic C Data Types 105 To keep our examples concrete, we have tested them using the following specific C compilers: ■ armcc from ARM Developer Suite version 1.1 (ADS1.1). You can license this compiler, or a later version, directly from ARM. ■ arm-elf-gcc version 2.95.2. This is the ARM target for the GNU C compiler, gcc, and is freely available. We have used armcc from ADS1.1 to generate the example assembler output in this book. The following short script shows you how to invoke armcc on a C file test.c. You can use this to reproduce our examples. armcc -Otime -c -o test.o test.c fromelf -text/c test.o > test.txt By default armcc has full optimizations turned on (the -O2 command line switch). The -Otime switch optimizes for execution efficiency rather than space and mainly affects the layout of for and while loops. If you are using the gcc compiler, then the following short script generates a similar assembler output listing: arm-elf-gcc -O2 -fomit-frame-pointer -c -o test.o test.c arm-elf-objdump -d test.o > test.txt Full optimizations are turned off by default for the GNU compiler. The -fomit-frame- pointer switch prevents the GNU compiler from maintaining a frame pointer register. Frame pointers assist the debug view by pointing to the local variables stored on the stack frame. However, they are inefficient to maintain and shouldn’t be used in code critical to performance. 5.2 Basic C Data Types Let’s start by looking at how ARM compilers handle the basic C data types. We will see that some of these types are more efficient to use for local variables than others. There are also differences between the addressing modes available when loading and storing data of each type. ARM processors have 32-bit registers and 32-bit data processing operations. The ARM architecture is a RISC load/store architecture. In other words you must load values from memory into registers before acting on them. There are no arithmetic or logical instructions that manipulate values in memory directly. Early versions of the ARM architecture (ARMv1 to ARMv3) provided hardware support for loading and storing unsigned 8-bit and unsigned or signed 32-bit values.
106 Chapter 5 Efficient C Programming Table 5.1 Load and store instructions by ARM architecture. Architecture Instruction Action Pre-ARMv4 LDRB load an unsigned 8-bit value ARMv4 STRB store a signed or unsigned 8-bit value LDR load a signed or unsigned 32-bit value ARMv5 STR store a signed or unsigned 32-bit value LDRSB load a signed 8-bit value LDRH load an unsigned 16-bit value LDRSH load a signed 16-bit value STRH store a signed or unsigned 16-bit value LDRD load a signed or unsigned 64-bit value STRD store a signed or unsigned 64-bit value These architectures were used on processors prior to the ARM7TDMI. Table 5.1 shows the load/store instruction classes available by ARM architecture. In Table 5.1 loads that act on 8- or 16-bit values extend the value to 32 bits before writing to an ARM register. Unsigned values are zero-extended, and signed values sign-extended. This means that the cast of a loaded value to an int type does not cost extra instructions. Similarly, a store of an 8- or 16-bit value selects the lowest 8 or 16 bits of the register. The cast of an int to smaller type does not cost extra instructions on a store. The ARMv4 architecture and above support signed 8-bit and 16-bit loads and stores directly, through new instructions. Since these instructions are a later addition, they do not support as many addressing modes as the pre-ARMv4 instructions. (See Section 3.3 for details of the different addressing modes.) We will see the effect of this in the example checksum_v3 in Section 5.2.1. Finally, ARMv5 adds instruction support for 64-bit load and stores. This is available in ARM9E and later cores. Prior to ARMv4, ARM processors were not good at handling signed 8-bit or any 16-bit values. Therefore ARM C compilers define char to be an unsigned 8-bit value, rather than a signed 8-bit value as is typical in many other compilers. Compilers armcc and gcc use the datatype mappings in Table 5.2 for an ARM target. The exceptional case for type char is worth noting as it can cause problems when you are porting code from another processor architecture. A common example is using a char type variable i as a loop counter, with loop continuation condition i ≥ 0. As i is unsigned for the ARM compilers, the loop will never terminate. Fortunately armcc produces a warning in this situation: unsigned comparison with 0. Compilers also provide an override switch to make char signed. For example, the command line option -fsigned-char will make char signed on gcc. The command line option -zc will have the same effect with armcc. For the rest of this book we assume that you are using an ARMv4 processor or above. This includes ARM7TDMI and all later processors.
5.2 Basic C Data Types 107 Table 5.2 C compiler datatype mappings. C Data Type Implementation char unsigned 8-bit byte short signed 16-bit halfword int signed 32-bit word long signed 32-bit word long long signed 64-bit double word 5.2.1 Local Variable Types ARMv4-based processors can efficiently load and store 8-, 16-, and 32-bit data. However, most ARM data processing operations are 32-bit only. For this reason, you should use a 32-bit datatype, int or long, for local variables wherever possible. Avoid using char and short as local variable types, even if you are manipulating an 8- or 16-bit value. The one exception is when you want wrap-around to occur. If you require modulo arithmetic of the form 255 + 1 = 0, then use the char type. To see the effect of local variable types, let’s consider a simple example. We’ll look in detail at a checksum function that sums the values in a data packet. Most communication protocols (such as TCP/IP) have a checksum or cyclic redundancy check (CRC) routine to check for errors in a data packet. The following code checksums a data packet containing 64 words. It shows why you should avoid using char for local variables. int checksum_v1(int *data) { char i; int sum = 0; for (i = 0; i < 64; i++) { sum += data[i]; } return sum; } At first sight it looks as though declaring i as a char is efficient. You may be thinking that a char uses less register space or less space on the ARM stack than an int. On the ARM, both these assumptions are wrong. All ARM registers are 32-bit and all stack entries are at least 32-bit. Furthermore, to implement the i++ exactly, the compiler must account for the case when i = 255. Any attempt to increment 255 should produce the answer 0.
108 Chapter 5 Efficient C Programming Consider the compiler output for this function. We’ve added labels and comments to make the assembly clear. checksum_v1 ; r2 = data MOV r2,r0 ; sum = 0 MOV r0,#0 ;i=0 MOV r1,#0 ; r3 = data[i] checksum_v1_loop ; r1 = i+1 LDR r3,[r2,r1,LSL #2] ; i = (char)r1 ADD r1,r1,#1 ; compare i, 64 AND r1,r1,#0xff ; sum += r3 CMP r1,#0x40 ; if (i<64) loop ADD r0,r3,r0 ; return sum BCC checksum_v1_loop MOV pc,r14 Now compare this to the compiler output where instead we declare i as an unsigned int. checksum_v2 ; r2 = data MOV r2,r0 ; sum = 0 MOV r0,#0 ;i=0 MOV r1,#0 ; r3 = data[i] checksum_v2_loop ; r1++ LDR r3,[r2,r1,LSL #2] ; compare i, 64 ADD r1,r1,#1 ; sum += r3 CMP r1,#0x40 ; if (i<64) goto loop ADD r0,r3,r0 ; return sum BCC checksum_v2_loop MOV pc,r14 In the first case, the compiler inserts an extra AND instruction to reduce i to the range 0 to 255 before the comparison with 64. This instruction disappears in the second case. Next, suppose the data packet contains 16-bit values and we need a 16-bit checksum. It is tempting to write the following C code: short checksum_v3(short *data) { unsigned int i; short sum = 0; for (i = 0; i < 64; i++) { sum = (short)(sum + data[i]);
5.2 Basic C Data Types 109 } return sum; } You may wonder why the for loop body doesn’t contain the code sum += data[i]; With armcc this code will produce a warning if you enable implicit narrowing cast warnings using the compiler switch -W + n. The expression sum + data[i] is an integer and so can only be assigned to a short using an (implicit or explicit) narrowing cast. As you can see in the following assembly output, the compiler must insert extra instructions to implement the narrowing cast: checksum_v3 ; r2 = data MOV r2,r0 ; sum = 0 MOV r0,#0 ;i=0 MOV r1,#0 ; r3 = &data[i] checksum_v3_loop ; r3 = data[i] ADD r3,r2,r1,LSL #1 ; i++ LDRH r3,[r3,#0] ; compare i, 64 ADD r1,r1,#1 ; r0 = sum + r3 CMP r1,#0x40 ADD r0,r3,r0 ; sum = (short)r0 MOV r0,r0,LSL #16 ; if (i<64) goto loop MOV r0,r0,ASR #16 ; return sum BCC checksum_v3_loop MOV pc,r14 The loop is now three instructions longer than the loop for example checksum_v2 earlier! There are two reasons for the extra instructions: ■ The LDRH instruction does not allow for a shifted address offset as the LDR instruction did in checksum_v2. Therefore the first ADD in the loop calculates the address of item i in the array. The LDRH loads from an address with no offset. LDRH has fewer addressing modes than LDR as it was a later addition to the ARM instruction set. (See Table 5.1.) ■ The cast reducing total + array[i] to a short requires two MOV instructions. The compiler shifts left by 16 and then right by 16 to implement a 16-bit sign extend. The shift right is a sign-extending shift so it replicates the sign bit to fill the upper 16 bits. We can avoid the second problem by using an int type variable to hold the partial sum. We only reduce the sum to a short type at the function exit.
110 Chapter 5 Efficient C Programming However, the first problem is a new issue. We can solve it by accessing the array by incrementing the pointer data rather than using an index as in data[i]. This is efficient regardless of array type size or element size. All ARM load and store instructions have a postincrement addressing mode. Example The checksum_v4 code fixes all the problems we have discussed in this section. It uses int 5.1 type local variables to avoid unnecessary casts. It increments the pointer data instead of using an index offset data[i]. short checksum_v4(short *data) { unsigned int i; int sum=0; for (i=0; i<64; i++) { sum += *(data++); } return (short)sum; } The *(data++) operation translates to a single ARM instruction that loads the data and increments the data pointer. Of course you could write sum += *data; data++; or even *data++ instead if you prefer. The compiler produces the following output. Three instruc- tions have been removed from the inside loop, saving three cycles per loop compared to checksum_v3. checksum_v4 ; sum = 0 MOV r2,#0 ;i=0 MOV r1,#0 ; r3 = *(data++) checksum_v4_loop ; i++ LDRSH r3,[r0],#2 ; compare i, 64 ADD r1,r1,#1 ; sum += r3 CMP r1,#0x40 ; if (sum<64) goto loop ADD r2,r3,r2 BCC checksum_v4_loop ; r0 = (short)sum MOV r0,r2,LSL #16 ; return r0 MOV r0,r0,ASR #16 MOV pc,r14 The compiler is still performing one cast to a 16-bit range, on the function return. You could remove this also by returning an int result as discussed in Section 5.2.2. ■
5.2 Basic C Data Types 111 5.2.2 Function Argument Types We saw in Section 5.2.1 that converting local variables from types char or short to type int increases performance and reduces code size. The same holds for function arguments. Consider the following simple function, which adds two 16-bit values, halving the second, and returns a 16-bit sum: short add_v1(short a, short b) { return a + (b >> 1); } This function is a little artificial, but it is a useful test case to illustrate the problems faced by the compiler. The input values a, b, and the return value will be passed in 32-bit ARM registers. Should the compiler assume that these 32-bit values are in the range of a short type, that is, −32,768 to +32,767? Or should the compiler force values to be in this range by sign-extending the lowest 16 bits to fill the 32-bit register? The compiler must make compatible decisions for the function caller and callee. Either the caller or callee must perform the cast to a short type. We say that function arguments are passed wide if they are not reduced to the range of the type and narrow if they are. You can tell which decision the compiler has made by looking at the assembly output for add_v1. If the compiler passes arguments wide, then the callee must reduce function arguments to the correct range. If the compiler passes arguments narrow, then the caller must reduce the range. If the compiler returns values wide, then the caller must reduce the return value to the correct range. If the compiler returns values narrow, then the callee must reduce the range before returning the value. For armcc in ADS, function arguments are passed narrow and values returned narrow. In other words, the caller casts argument values and the callee casts return values. The compiler uses the ANSI prototype of the function to determine the datatypes of the function arguments. The armcc output for add_v1 shows that the compiler casts the return value to a short type, but does not cast the input values. It assumes that the caller has already ensured that the 32-bit values r0 and r1 are in the range of the short type. This shows narrow passing of arguments and return value. add_v1 ADD r0,r0,r1,ASR #1 ; r0 = (int)a + ((int)b >> 1) MOV r0,r0,LSL #16 MOV r0,r0,ASR #16 ; r0 = (short)r0 MOV pc,r14 ; return r0 The gcc compiler we used is more cautious and makes no assumptions about the range of argument value. This version of the compiler reduces the input arguments to the range
112 Chapter 5 Efficient C Programming of a short in both the caller and the callee. It also casts the return value to a short type. Here is the compiled code for add_v1: add_v1_gcc r0, r0, LSL #16 ; r1 = (int)b >> 1 MOV r1, r1, LSL #16 ; r1 += (int)a MOV r1, r1, ASR #17 MOV r1, r1, r0, ASR #16 ; r0 = (short)r1 ADD r1, r1, LSL #16 ; return r0 MOV r0, r1, ASR #16 MOV pc, lr MOV Whatever the merits of different narrow and wide calling protocols, you can see that char or short type function arguments and return values introduce extra casts. These increase code size and decrease performance. It is more efficient to use the int type for function arguments and return values, even if you are only passing an 8-bit value. 5.2.3 Signed versus Unsigned Types The previous sections demonstrate the advantages of using int rather than a char or short type for local variables and function arguments. This section compares the efficiencies of signed int and unsigned int. If your code uses addition, subtraction, and multiplication, then there is no performance difference between signed and unsigned operations. However, there is a difference when it comes to division. Consider the following short example that averages two integers: int average_v1(int a, int b) { return (a+b)/2; } This compiles to average_v1 r0,r0,r1 ; r0 = a + b ADD r0,r0,r0,LSR #31 ; if (r0<0) r0++ ADD r0,r0,ASR #1 ; r0 = r0 >> 1 MOV pc,r14 ; return r0 MOV Notice that the compiler adds one to the sum before shifting by right if the sum is negative. In other words it replaces x/2 by the statement: (x<0) ? ((x+1) >> 1): (x >> 1)
5.3 C Looping Structures 113 It must do this because x is signed. In C on an ARM target, a divide by two is not a right shift if x is negative. For example, −3 1 = −2 but −3/2 = −1. Division rounds towards zero, but arithmetic right shift rounds towards −∞. It is more efficient to use unsigned types for divisions. The compiler converts unsigned power of two divisions directly to right shifts. For general divisions, the divide routine in the C library is faster for unsigned types. See Section 5.10 for discussion on avoiding divisions completely. Summary The Efficient Use of C Types ■ 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. The ARMv4 architecture is efficient at loading and storing all data widths provided you traverse arrays by incrementing the array pointer. 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. The casts make it clear that for fast operation you are taking a narrow width type stored in memory and expanding it to a wider type in the registers. Switch on implicit narrowing cast warnings in the compiler to detect implicit casts. ■ Avoid implicit or explicit narrowing casts in expressions because they usually cost extra cycles. Casts on loads or stores are usually free because the load or store instruction performs the cast for you. ■ 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. 5.3 C Looping Structures This section looks at the most efficient ways to code for and while loops on the ARM. We start by looking at loops with a fixed number of iterations and then move on to loops with a variable number of iterations. Finally we look at loop unrolling. 5.3.1 Loops with a Fixed Number of Iterations What is the most efficient way to write a for loop on the ARM? Let’s return to our checksum example and look at the looping structure.
114 Chapter 5 Efficient C Programming Here is the last version of the 64-word packet checksum routine we studied in Section 5.2. This shows how the compiler treats a loop with incrementing count i++. int checksum_v5(int *data) { unsigned int i; int sum=0; for (i=0; i<64; i++) { sum += *(data++); } return sum; } This compiles to checksum_v5 ; r2 = data MOV r2,r0 ; sum = 0 MOV r0,#0 ;i=0 MOV r1,#0 ; r3 = *(data++) checksum_v5_loop ; i++ LDR r3,[r2],#4 ; compare i, 64 ADD r1,r1,#1 ; sum += r3 CMP r1,#0x40 ; if (i<64) goto loop ADD r0,r3,r0 ; return sum BCC checksum_v5_loop MOV pc,r14 It takes three instructions to implement the for loop structure: ■ An ADD to increment i ■ A compare to check if i is less than 64 ■ A conditional branch to continue the loop if i < 64 This is not efficient. On the ARM, a loop should only use two instructions: ■ A subtract to decrement the loop counter, which also sets the condition code flags on the result ■ A conditional branch instruction The key point is that the loop counter should count down to zero rather than counting up to some arbitrary limit. Then the comparison with zero is free since the result is stored
5.3 C Looping Structures 115 in the condition flags. Since we are no longer using i as an array index, there is no problem in counting down rather than up. Example This example shows the improvement if we switch to a decrementing loop rather than an 5.2 incrementing loop. int checksum_v6(int *data) { unsigned int i; int sum=0; for (i=64; i!=0; i--) { sum += *(data++); } return sum; } This compiles to checksum_v6 MOV r2,r0 ; r2 = data ; sum = 0 MOV r0,#0 ; i = 64 MOV r1,#0x40 ; r3 = *(data++) ; i-- and set flags checksum_v6_loop ; sum += r3 ; if (i!=0) goto loop LDR r3,[r2],#4 ; return sum SUBS r1,r1,#1 ADD r0,r3,r0 BNE checksum_v6_loop MOV pc,r14 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. ■ For an unsigned loop counter i we can use either of the loop continuation conditions i!=0 or i>0. As i can’t be negative, they are the same condition. For a signed loop counter, it is tempting to use the condition i>0 to continue the loop. You might expect the compiler to generate the following two instructions to implement the loop: SUBS r1,r1,#1 ; compare i with 1, i=i-1 BGT loop ; if (i+1>1) goto loop
116 Chapter 5 Efficient C Programming In fact, the compiler will generate SUB r1,r1,#1 ; i-- CMP r1,#0 ; compare i with 0 BGT loop ; if (i>0) goto loop The compiler is not being inefficient. It must be careful about the case when i = -0x80000000 because the two sections of code generate different answers in this case. For the first piece of code the SUBS instruction compares i with 1 and then decrements i. Since -0x80000000 < 1, the loop terminates. For the second piece of code, we decrement i and then compare with 0. Modulo arithmetic means that i now has the value +0x7fffffff, which is greater than zero. Thus the loop continues for many iterations. Of course, in practice, i rarely takes the value -0x80000000. The compiler can’t usu- ally determine this, especially if the loop starts with a variable number of iterations (see Section 5.3.2). Therefore you should use the termination condition i!=0 for signed or unsigned loop counters. It saves one instruction over the condition i>0 for signed i. 5.3.2 Loops Using a Variable Number of Iterations Now suppose we want our checksum routine to handle packets of arbitrary size. We pass in a variable N giving the number of words in the data packet. Using the lessons from the last section we count down until N = 0 and don’t require an extra loop counter i. The checksum_v7 example shows how the compiler handles a for loop with a variable number of iterations N. int checksum_v7(int *data, unsigned int N) { int sum=0; for (; N!=0; N--) { sum += *(data++); } return sum; } This compiles to checksum_v7 r2,#0 ; sum = 0 MOV r1,#0 ; compare N, 0 CMP checksum_v7_end ; if (N==0) goto end BEQ
5.3 C Looping Structures 117 checksum_v7_loop ; r3 = *(data++) LDR r3,[r0],#4 ; N-- and set flags SUBS r1,r1,#1 ; sum += r3 ADD r2,r3,r2 ; if (N!=0) goto loop BNE checksum_v7_loop ; r0 = sum checksum_v7_end ; return r0 MOV r0,r2 MOV pc,r14 Notice that the compiler checks that N is nonzero on entry to the function. Often this check is unnecessary since you know that the array won’t be empty. In this case a do-while loop gives better performance and code density than a for loop. Example This example shows how to use a do-while loop to remove the test for N being zero that 5.3 occurs in a for loop. int checksum_v8(int *data, unsigned int N) { int sum=0; do { sum += *(data++); } while (--N!=0); return sum; } The compiler output is now checksum_v8 ; sum = 0 MOV r2,#0 ; r3 = *(data++) checksum_v8_loop ; N-- and set flags LDR r3,[r0],#4 ; sum += r3 SUBS r1,r1,#1 ; if (N!=0) goto loop ADD r2,r3,r2 ; r0 = sum BNE checksum_v8_loop ; return r0 MOV r0,r2 MOV pc,r14 Compare this with the output for checksum_v7 to see the two-cycle saving. ■ 5.3.3 Loop Unrolling We saw in Section 5.3.1 that 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.
118 Chapter 5 Efficient C Programming We call these instructions the loop overhead. On ARM7 or ARM9 processors the subtract takes one cycle and the branch three cycles, giving an overhead of four cycles per loop. You can save some of these cycles by unrolling a loop—repeating the loop body several times, and reducing the number of loop iterations by the same proportion. For example, let’s unroll our packet checksum example four times. Example The following code unrolls our packet checksum loop by four times. We assume that the 5.4 number of words in the packet N is a multiple of four. int checksum_v9(int *data, unsigned int N) { int sum=0; do { sum += *(data++); sum += *(data++); sum += *(data++); sum += *(data++); N -= 4; } while ( N!=0); return sum; } This compiles to checksum_v9 ; sum = 0 MOV r2,#0 r3,[r0],#4 ; r3 = *(data++) checksum_v9_loop r1,r1,#4 ; N -= 4 & set flags LDR r2,r3,r2 ; sum += r3 SUBS r3,[r0],#4 ; r3 = *(data++) ADD r2,r3,r2 ; sum += r3 LDR r3,[r0],#4 ; r3 = *(data++) ADD r2,r3,r2 ; sum += r3 LDR r3,[r0],#4 ; r3 = *(data++) ADD r2,r3,r2 ; sum += r3 LDR checksum_v9_loop ; if (N!=0) goto loop ADD r0,r2 ; r0 = sum BNE pc,r14 ; return r0 MOV MOV
5.3 C Looping Structures 119 We have reduced the loop overhead from 4N cycles to (4N)/4 = N cycles. On the ARM7TDMI, this accelerates the loop from 8 cycles per accumulate to 20/4 = 5 cycles per accumulate, nearly doubling the speed! For the ARM9TDMI, which has a faster load instruction, the benefit is even higher. ■ There are two questions you need to ask when unrolling a loop: ■ How many times should I unroll the loop? ■ What if the number of loop iterations is not a multiple of the unroll amount? For example, what if N is not a multiple of four in checksum_v9? To start with the first question, only unroll loops that are important for the overall performance of the application. Otherwise unrolling will increase the code size with little performance benefit. Unrolling may even reduce performance by evicting more important code from the cache. Suppose the loop is important, for example, 30% of the entire application. Suppose you unroll the loop until it is 0.5 KB in code size (128 instructions). Then the loop overhead is at most 4 cycles compared to a loop body of around 128 cycles. The loop overhead cost is 3/128, roughly 3%. Recalling that the loop is 30% of the entire application, overall the loop overhead is only 1%. Unrolling the code further gains little extra performance, but has a significant impact on the cache contents. It is usually not worth unrolling further when the gain is less than 1%. For the second question, try to arrange it so that array sizes are multiples of your unroll amount. If this isn’t possible, then you must add extra code to take care of the leftover cases. This increases the code size a little but keeps the performance high. Example This example handles the checksum of any size of data packet using a loop that has been 5.5 unrolled four times. int checksum_v10(int *data, unsigned int N) { unsigned int i; int sum=0; for (i=N/4; i!=0; i--) { sum += *(data++); sum += *(data++); sum += *(data++); sum += *(data++); } for (i=N&3; i!=0; i--) {
120 Chapter 5 Efficient C Programming sum += *(data++); } return sum; } The second for loop handles the remaining cases when N is not a multiple of four. Note that both N/4 and N&3 can be zero, so we can’t use do-while loops. ■ Summary Writing Loops Efficiently ■ Use loops that count down to zero. Then the compiler does not need to allocate a register to hold the termination value, and the comparison with zero is free. ■ Use unsigned loop counters by default and the continuation condition i!=0 rather than i>0. This will ensure that the loop overhead is only two instructions. ■ Use do-while loops rather than for loops when you know the loop will iterate at least once. This saves the compiler checking to see if the loop count is zero. ■ Unroll important loops to reduce the loop overhead. Do not overunroll. If the loop overhead is small as a proportion of the total, then unrolling will increase code size and hurt the performance of the cache. ■ Try to arrange that the number of elements in arrays are multiples of four or eight. You can then unroll loops easily by two, four, or eight times without worrying about the leftover array elements. 5.4 Register Allocation The compiler attempts to allocate a processor register to each local variable you use in a C function. It will try to use the same register for different local variables if the use of the variables do not overlap. When there are more local variables than available registers, the compiler stores the excess variables on the processor stack. These variables are called spilled or swapped out variables since they are written out to memory (in a similar way virtual memory is swapped out to disk). Spilled variables are slow to access compared to variables allocated to registers. To implement a function efficiently, you need to ■ minimize the number of spilled variables ■ ensure that the most important and frequently accessed variables are stored in registers First let’s look at the number of processor registers the ARM C compilers have avail- able for allocating variables. Table 5.3 shows the standard register names and usage when following the ARM-Thumb procedure call standard (ATPCS), which is used in code generated by C compilers.
5.4 Register Allocation 121 Table 5.3 C compiler register usage. Register Alternate ATPCS register usage number register names Argument registers. These hold the first four function r0 arguments on a function call and the return value on a r1 a1 function return. A function may corrupt these registers and r2 a2 use them as general scratch registers within the function. r3 a3 r4 a4 General variable registers. The function must preserve the callee r5 v1 values of these registers. r6 v2 r7 v3 r8 v4 v5 r9 v6 sb General variable register. The function must preserve the callee value of this register except when compiling for read-write position independence (RWPI). Then r9 holds the static base address. This is the address of the read-write data. r10 v7 sl General variable register. The function must preserve the callee value of this register except when compiling with stack limit checking. Then r10 holds the stack limit address. r11 v8 fp General variable register. The function must preserve the callee value of this register except when compiling using a frame pointer. Only old versions of armcc use a frame pointer. r12 ip A general scratch register that the function can corrupt. It is useful as a scratch register for function veneers or other intraprocedure call requirements. r13 sp The stack pointer, pointing to the full descending stack. r14 lr The link register. On a function call this holds the return address. r15 pc The program counter. Provided the compiler is not using software stack checking or a frame pointer, then the C compiler can use registers r0 to r12 and r14 to hold variables. It must save the callee values of r4 to r11 and r14 on the stack if using these registers. In theory, the C compiler can assign 14 variables to registers without spillage. In practice, some compilers use a fixed register such as r12 for intermediate scratch working and do not assign variables to this register. Also, complex expressions require intermediate working registers to evaluate. Therefore, to ensure good assignment to registers, you should try to limit the internal loop of functions to using at most 12 local variables.
122 Chapter 5 Efficient C Programming If the compiler does need to swap out variables, then it chooses which variables to swap out based on frequency of use. A variable used inside a loop counts multiple times. You can guide the compiler as to which variables are important by ensuring these variables are used within the innermost loop. The register keyword in C hints that a compiler should allocate the given variable to a register. However, different compilers treat this keyword in different ways, and different architectures have a different number of available registers (for example, Thumb and ARM). Therefore we recommend that you avoid using register and rely on the compiler’s normal register allocation routine. Summary Efficient Register Allocation ■ Try to limit the number of local variables in the internal loop of functions to 12. The compiler should be able to allocate these to ARM registers. ■ You can guide the compiler as to which variables are important by ensuring these variables are used within the innermost loop. 5.5 Function Calls The ARM Procedure Call Standard (APCS) defines how to pass function arguments and return values in ARM registers. The more recent ARM-Thumb Procedure Call Standard (ATPCS) covers ARM and Thumb interworking as well. The first four integer arguments are passed in the first four ARM registers: r0, r1, r2, and r3. Subsequent integer arguments are placed on the full descending stack, ascending in memory as in Figure 5.1. Function return integer values are passed in r0. This description covers only integer or pointer arguments. Two-word arguments such as long long or double are passed in a pair of consecutive argument registers and returned in r0, r1. The compiler may pass structures in registers or by reference according to command line compiler options. The first point to note about the procedure call standard is the four-register rule. Functions with four or fewer arguments are far more efficient to call than functions with five or more arguments. For functions with four or fewer arguments, the compiler can pass all the arguments in registers. For functions with more arguments, both the caller and callee must access the stack for some arguments. Note that for C++ the first argument to an object method is the this pointer. This argument is implicit and additional to the explicit arguments. If your C function needs more than four arguments, or your C++ method more than three explicit arguments, then it is almost always more efficient to use structures. Group related arguments into structures, and pass a structure pointer rather than mul- tiple arguments. Which arguments are related will depend on the structure of your software.
5.5 Function Calls 123 … … sp + 16 Argument 8 sp + 12 Argument 7 sp + 8 Argument 6 sp + 4 Argument 5 sp Argument 4 r3 Argument 3 r2 Argument 2 r1 Argument 1 r0 Argument 0 Return value Figure 5.1 ATPCS argument passing. The next example illustrates the benefits of using a structure pointer. First we show a typical routine to insert N bytes from array data into a queue. We implement the queue using a cyclic buffer with start address Q_start (inclusive) and end address Q_end (exclusive). char *queue_bytes_v1( char *Q_start, /* Queue buffer start address */ char *Q_end, /* Queue buffer end address */ char *Q_ptr, /* Current queue pointer position */ char *data, /* Data to insert into the queue */ unsigned int N) /* Number of bytes to insert */ { do { *(Q_ptr++) = *(data++); if (Q_ptr == Q_end) { Q_ptr = Q_start; } } while (--N); return Q_ptr; }
124 Chapter 5 Efficient C Programming This compiles to queue_bytes_v1 ; save lr on the stack STR r14,[r13,#-4]! ; r12 = N LDR r12,[r13,#4] ; r14 = *(data++) queue_v1_loop ; *(Q_ptr++) = r14 LDRB r14,[r3],#1 ; if (Q_ptr == Q_end) STRB r14,[r2],#1 ; {Q_ptr = Q_start;} CMP r2,r1 ; --N and set flags MOVEQ r2,r0 ; if (N!=0) goto loop SUBS r12,r12,#1 ; r0 = Q_ptr BNE queue_v1_loop ; return r0 MOV r0,r2 LDR pc,[r13],#4 Compare this with a more structured approach using three function arguments. Example The following code creates a Queue structure and passes this to the function to reduce the 5.6 number of function arguments. typedef struct { /* Queue buffer start address */ char *Q_start; /* Queue buffer end address */ char *Q_end; /* Current queue pointer position */ char *Q_ptr; } Queue; void queue_bytes_v2(Queue *queue, char *data, unsigned int N) { char *Q_ptr = queue->Q_ptr; char *Q_end = queue->Q_end; do { *(Q_ptr++) = *(data++); if (Q_ptr == Q_end) { Q_ptr = queue->Q_start; } } while (--N); queue->Q_ptr = Q_ptr; }
5.5 Function Calls 125 This compiles to queue_bytes_v2 ; save lr on the stack ■ STR r14,[r13,#-4]! ; r3 = queue->Q_ptr LDR r3,[r0,#8] ; r14 = queue->Q_end LDR r14,[r0,#4] ; r12 = *(data++) queue_v2_loop ; *(Q_ptr++) = r12 LDRB r12,[r1],#1 ; if (Q_ptr == Q_end) STRB r12,[r3],#1 ; Q_ptr = queue->Q_start CMP r3,r14 ; --N and set flags LDREQ r3,[r0,#0] ; if (N!=0) goto loop SUBS r2,r2,#1 ; queue->Q_ptr = r3 BNE queue_v2_loop ; return STR r3,[r0,#8] LDR pc,[r13],#4 The queue_bytes_v2 is one instruction longer than queue_bytes_v1, but it is in fact more efficient overall. The second version has only three function arguments rather than five. Each call to the function requires only three register setups. This compares with four register setups, a stack push, and a stack pull for the first version. There is a net saving of two instructions in function call overhead. There are likely further savings in the callee function, as it only needs to assign a single register to the Queue structure pointer, rather than three registers in the nonstructured case. There are other ways of reducing function call overhead if your function is very small and corrupts few registers (uses few local variables). Put the C function in the same C file as the functions that will call it. The C compiler then knows the code generated for the callee function and can make optimizations in the caller function: ■ The caller function need not preserve registers that it can see the callee doesn’t corrupt. Therefore the caller function need not save all the ATPCS corruptible registers. ■ If the callee function is very small, then the compiler can inline the code in the caller function. This removes the function call overhead completely. Example The function uint_to_hex converts a 32-bit unsigned integer into an array of eight hexa- 5.7 decimal digits. It uses a helper function nybble_to_hex, which converts a digit d in the range 0 to 15 to a hexadecimal digit. unsigned int nybble_to_hex(unsigned int d) { if (d<10) { return d + ’0’;
126 Chapter 5 Efficient C Programming } return d - 10 + ’A’; } void uint_to_hex(char *out, unsigned int in) { unsigned int i; for (i=8; i!=0; i--) { in = (in << 4) | (in >> 28); /* rotate in left by 4 bits */ *(out++) = (char)nybble_to_hex(in & 15); } } When we compile this, we see that uint_to_hex doesn’t call nybble_to_hex at all! In the following compiled code, the compiler has inlined the uint_to_hex code. This is more efficient than generating a function call. uint_to_hex ;i=8 ■ MOV r3,#8 ; in = (in << 4)|(in >> 28) uint_to_hex_loop ; r2 = in & 15 MOV r1,r1,ROR #28 ; if (r2>=10) AND r2,r1,#0xf ; r2 +=’A’-10 CMP r2,#0xa ; else r2 +=’0’ ADDCS r2,r2,#0x37 ; *(out++) = r2 ADDCC r2,r2,#0x30 ; i-- and set flags STRB r2,[r0],#1 ; if (i!=0) goto loop SUBS r3,r3,#1 ; return BNE uint_to_hex_loop MOV pc,r14 The compiler will only inline small functions. You can ask the compiler to inline a function using the __inline keyword, although this keyword is only a hint and the compiler may ignore it (see Section 5.12 for more on inline functions). Inlining large functions can lead to big increases in code size without much performance improvement. Summary Calling Functions Efficiently ■ Try to restrict functions to four arguments. This will make them more efficient to call. Use structures to group related arguments and pass structure pointers instead of multiple arguments.
5.6 Pointer Aliasing 127 ■ 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. ■ Critical functions can be inlined using the __inline keyword. 5.6 Pointer Aliasing Two pointers are said to alias when they point to the same address. If you write to one pointer, it will affect the value you read from the other pointer. In a function, the compiler often doesn’t know which pointers can alias and which pointers can’t. The compiler must be very pessimistic and assume that any write to a pointer may affect the value read from any other pointer, which can significantly reduce code efficiency. Let’s start with a very simple example. The following function increments two timer values by a step amount: void timers_v1(int *timer1, int *timer2, int *step) { *timer1 += *step; *timer2 += *step; } This compiles to timers_v1 r3,[r0,#0] ; r3 = *timer1 LDR r12,[r2,#0] ; r12 = *step LDR r3,r3,r12 ; r3 += r12 ADD r3,[r0,#0] ; *timer1 = r3 STR r0,[r1,#0] ; r0 = *timer2 LDR r2,[r2,#0] ; r2 = *step LDR r0,r0,r2 ; r0 += r2 ADD r0,[r1,#0] ; *timer2 = t0 STR pc,r14 ; return MOV Note that the compiler loads from step twice. Usually a compiler optimization called common subexpression elimination would kick in so that *step was only evaluated once, and the value reused for the second occurrence. However, the compiler can’t use this optimization here. The pointers timer1 and step might alias one another. In other words, the compiler cannot be sure that the write to timer1 doesn’t affect the read from step.
128 Chapter 5 Efficient C Programming In this case the second value of *step is different from the first and has the value *timer1. This forces the compiler to insert an extra load instruction. The same problem occurs if you use structure accesses rather than direct pointer access. The following code also compiles inefficiently: typedef struct {int step;} State; typedef struct {int timer1, timer2;} Timers; void timers_v2(State *state, Timers *timers) { timers->timer1 += state->step; timers->timer2 += state->step; } The compiler evaluates state->step twice in case state->step and timers->timer1 are at the same memory address. The fix is easy: Create a new local variable to hold the value of state->step so the compiler only performs a single load. Example In the code for timers_v3 we use a local variable step to hold the value of state->step. 5.8 Now the compiler does not need to worry that state may alias with timers. void timers_v3(State *state, Timers *timers) { int step = state->step; timers->timer1 += step; ■ timers->timer2 += step; } You must also be careful of other, less obvious situations where aliasing may occur. When you call another function, this function may alter the state of memory and so change the values of any expressions involving memory reads. The compiler will evaluate the expressions again. For example suppose you read state->step, call a function and then read state->step again. The compiler must assume that the function could change the value of state->step in memory. Therefore it will perform two reads, rather than reusing the first value it read for state->step. Another pitfall is to take the address of a local variable. Once you do this, the variable is referenced by a pointer and so aliasing can occur with other pointers. The compiler is likely to keep reading the variable from the stack in case aliasing occurs. Consider the following example, which reads and then checksums a data packet: int checksum_next_packet(void) { int *data; int N, sum=0;
5.6 Pointer Aliasing 129 data = get_next_packet(&N); do { sum += *(data++); } while (--N); return sum; } Here get_next_packet is a function returning the address and size of the next data packet. The previous code compiles to checksum_next_packet ; save r4, lr on the stack STMFD r13!,{r4,r14} ; create two stacked variables SUB r13,r13,#8 ; r0 = &N, N stacked ADD r0,r13,#4 ; sum = 0 MOV r4,#0 ; r0 = data BL get_next_packet ; r1 = *(data++) checksum_loop ; sum += r1 LDR r1,[r0],#4 ; r1 = N (read from stack) ADD r4,r1,r4 ; r1-- & set flags LDR r1,[r13,#4] ; N = r1 (write to stack) SUBS r1,r1,#1 ; if (N!=0) goto loop STR r1,[r13,#4] ; r0 = sum BNE checksum_loop ; delete stacked variables MOV r0,r4 ; return r0 ADD r13,r13,#8 LDMFD r13!,{r4,pc} Note how the compiler reads and writes N from the stack for every N--. Once you take the address of N and pass it to get_next_packet, the compiler needs to worry about aliasing because the pointers data and &N may alias. To avoid this, don’t take the address of local variables. If you must do this, then copy the value into another local variable before use. You may wonder why the compiler makes room for two stacked variables when it only uses one. This is to keep the stack eight-byte aligned, which is required for LDRD instructions available in ARMv5TE. The example above doesn’t actually use an LDRD, but the compiler does not know whether get_next_packet will use this instruction.
130 Chapter 5 Efficient C Programming Summary Avoiding Pointer Aliasing 5.7 ■ Do not rely on the compiler to eliminate common subexpressions involving memory accesses. Instead create new local variables to hold the expression. This ensures the expression is evaluated only once. ■ Avoid taking the address of local variables. The variable may be inefficient to access from then on. Structure Arrangement The way you lay out a frequently used structure can have a significant impact on its perfor- mance and code density. There are two issues concerning structures on the ARM: alignment of the structure entries and the overall size of the structure. For architectures up to and including ARMv5TE, load and store instructions are only guaranteed to load and store values with address aligned to the size of the access width. Table 5.4 summarizes these restrictions. For this reason, ARM compilers will automatically align the start address of a structure to a multiple of the largest access width used within the structure (usually four or eight bytes) and align entries within structures to their access width by inserting padding. For example, consider the structure struct { char a; int b; char c; short d; } For a little-endian memory system the compiler will lay this out adding padding to ensure that the next object is aligned to the size of that object: Address +3 +2 +1 +0 +0 pad pad pad a +4 b[31,24] b[23,16] b[15,8] b[7,0] +8 d[15,8] d[7,0] pad c Table 5.4 Load and store alignment restrictions for ARMv5TE. Transfer size Instruction Byte address 1 byte LDRB, LDRSB, STRB any byte address alignment 2 bytes LDRH, LDRSH, STRH multiple of 2 bytes 4 bytes LDR, STR multiple of 4 bytes 8 bytes LDRD, STRD multiple of 8 bytes
5.7 Structure Arrangement 131 To improve the memory usage, you should reorder the elements struct { char a; char c; short d; int b; } This reduces the structure size from 12 bytes to 8 bytes, with the following new layout: Address +3 +2 +1 +0 +0 d[15,8] d[7,0] c a +4 b[31,24] b[23,16] b[15,8] b[7,0] Therefore, it is a good idea to group structure elements of the same size, so that the structure layout doesn’t contain unnecessary padding. The armcc compiler does include a keyword __packed that removes all padding. For example, the structure __packed struct { char a; int b; char c; short d; } will be laid out in memory as Address +3 +2 +1 +0 +0 b[23,16] b[15,8] b[7,0] a +4 d[15,8] d[7,0] c b[31,24] However, packed structures are slow and inefficient to access. The compiler emulates unaligned load and store operations by using several aligned accesses with data operations to merge the results. Only use the __packed keyword where space is far more important than speed and you can’t reduce padding by rearragement. Also use it for porting code that assumes a certain structure layout in memory. The exact layout of a structure in memory may depend on the compiler vendor and compiler version you use. In API (Application Programmer Interface) definitions it is often
132 Chapter 5 Efficient C Programming a good idea to insert any padding that you cannot get rid of into the structure manually. This way the structure layout is not ambiguous. It is easier to link code between compiler versions and compiler vendors if you stick to unambiguous structures. Another point of ambiguity is enum. Different compilers use different sizes for an enu- merated type, depending on the range of the enumeration. For example, consider the type typedef enum { FALSE, TRUE } Bool; The armcc in ADS1.1 will treat Bool as a one-byte type as it only uses the values 0 and 1. Bool will only take up 8 bits of space in a structure. However, gcc will treat Bool as a word and take up 32 bits of space in a structure. To avoid ambiguity it is best to avoid using enum types in structures used in the API to your code. Another consideration is the size of the structure and the offsets of elements within the structure. This problem is most acute when you are compiling for the Thumb instruction set. Thumb instructions are only 16 bits wide and so only allow for small element offsets from a structure base pointer. Table 5.5 shows the load and store base register offsets available in Thumb. Therefore the compiler can only access an 8-bit structure element with a single instruc- tion if it appears within the first 32 bytes of the structure. Similarly, single instructions can only access 16-bit values in the first 64 bytes and 32-bit values in the first 128 bytes. Once you exceed these limits, structure accesses become inefficient. The following rules generate a structure with the elements packed for maximum efficiency: ■ Place all 8-bit elements at the start of the structure. ■ Place all 16-bit elements next, then 32-bit, then 64-bit. ■ Place all arrays and larger elements at the end of the structure. ■ If the structure is too big for a single instruction to access all the elements, then group the elements into substructures. The compiler can maintain pointers to the individual substructures. Table 5.5 Thumb load and store offsets. Instructions Offset available from the base register LDRB, LDRSB, STRB 0 to 31 bytes LDRH, LDRSH, STRH 0 to 31 halfwords (0 to 62 bytes) LDR, STR 0 to 31 words (0 to 124 bytes)
5.8 Bit-fields 133 Summary Efficient Structure Arrangement ■ Lay structures out in order of increasing element size. Start the structure with the smallest elements and finish with the largest. ■ Avoid very large structures. Instead use a hierarchy of smaller structures. ■ For portability, manually add padding (that would appear implicitly) into API structures so that the layout of the structure does not depend on the compiler. ■ Beware of using enum types in API structures. The size of an enum type is compiler dependent. 5.8 Bit-fields Bit-fields are probably the least standardized part of the ANSI C specification. The compiler can choose how bits are allocated within the bit-field container. For this reason alone, avoid using bit-fields inside a union or in an API structure definition. Different compilers can assign the same bit-field different bit positions in the container. It is also a good idea to avoid bit-fields for efficiency. Bit-fields are structure ele- ments and usually accessed using structure pointers; consequently, they suffer from the pointer aliasing problems described in Section 5.6. Every bit-field access is really a memory access. Possible pointer aliasing often forces the compiler to reload the bit-field several times. The following example, dostages_v1, illustrates this problem. It also shows that compilers do not tend to optimize bit-field testing very well. void dostageA(void); void dostageB(void); void dostageC(void); typedef struct { unsigned int stageA : 1; unsigned int stageB : 1; unsigned int stageC : 1; } Stages_v1; void dostages_v1(Stages_v1 *stages) { if (stages->stageA) { dostageA(); }
134 Chapter 5 Efficient C Programming if (stages->stageB) { dostageB(); } if (stages->stageC) { dostageC(); } } Here, we use three bit-field flags to enable three possible stages of processing. The example compiles to dostages_v1 r13!,{r4,r14} ; stack r4, lr STMFD r4,r0 ; move stages to r4 MOV r0,[r0,#0] ; r0 = stages bitfield LDR r0,#1 ; if (stages->stageA) TST dostageA ; {dostageA();} BLNE r0,[r4,#0] ; r0 = stages bitfield LDR r0,r0,LSL #30 ; shift bit 1 to bit 31 MOV r0,#0 ; if (bit31) CMP dostageB ; {dostageB();} BLLT r0,[r4,#0] ; r0 = stages bitfield LDR r0,r0,LSL #29 ; shift bit 2 to bit 31 MOV r0,#0 ; if (!bit31) CMP r13!,{r4,r14} ; return LDMLTFD dostageC ; dostageC(); BLT r13!,{r4,pc} ; return LDMFD Note that the compiler accesses the memory location containing the bit-field three times. Because the bit-field is stored in memory, the dostage functions could change the value. Also, the compiler uses two instructions to test bit 1 and bit 2 of the bit-field, rather than a single instruction. You can generate far more efficient code by using an integer rather than a bit-field. Use enum or #define masks to divide the integer type into different fields. Example The following code implements the dostages function using logical operations rather than 5.9 bit-fields: typedef unsigned long Stages_v2; #define STAGEA (1ul << 0)
5.8 Bit-fields 135 #define STAGEB (1ul << 1) #define STAGEC (1ul << 2) void dostages_v2(Stages_v2 *stages_v2) { Stages_v2 stages = *stages_v2; if (stages & STAGEA) { dostageA(); } if (stages & STAGEB) { dostageB(); } if (stages & STAGEC) { dostageC(); } } Now that a single unsigned long type contains all the bit-fields, we can keep a copy of their values in a single local variable stages, which removes the memory aliasing problem discussed in Section 5.6. In other words, the compiler must assume that the dostageX (where X is A, B, or C) functions could change the value of *stages_v2. The compiler generates the following code giving a saving of 33% over the previous version using ANSI bit-fields: dostages_v2 r13!,{r4,r14} ; stack r4, lr ■ STMFD r4,[r0,#0] ; stages = *stages_v2 LDR r4,#1 ; if (stage & STAGEA) TST dostageA ; {dostageA();} BLNE r4,#2 ; if (stage & STAGEB) TST dostageB ; {dostageB();} BLNE r4,#4 ; if (!(stage & STAGEC)) TST r13!,{r4,r14} ; return; LDMNEFD dostageC ; dostageC(); BNE r13!,{r4,pc} ; return LDMFD You can also use the masks to set and clear the bit-fields, just as easily as for testing them. The following code shows how to set, clear, or toggle bits using the STAGE masks: stages |= STAGEA; /* enable stage A */
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645
- 646
- 647
- 648
- 649
- 650
- 651
- 652
- 653
- 654
- 655
- 656
- 657
- 658
- 659
- 660
- 661
- 662
- 663
- 664
- 665
- 666
- 667
- 668
- 669
- 670
- 671
- 672
- 673
- 674
- 675
- 676
- 677
- 678
- 679
- 680
- 681
- 682
- 683
- 684
- 685
- 686
- 687
- 688
- 689
- 690
- 691
- 692
- 693
- 694
- 695
- 696
- 697
- 698
- 699
- 700
- 701
- 702
- 703
- 704
- 705
- 706
- 707
- 708
- 709
- 710
- 1 - 50
- 51 - 100
- 101 - 150
- 151 - 200
- 201 - 250
- 251 - 300
- 301 - 350
- 351 - 400
- 401 - 450
- 451 - 500
- 501 - 550
- 551 - 600
- 601 - 650
- 651 - 700
- 701 - 710
Pages: