Typical optimization principles and methods of TCP/IP protocol

With the development of embedded technology, people are increasingly demanding the intelligence and miniaturization of the system. ARM-based microprocessors are widely used in a variety of electronic products due to their high performance, low power consumption, low price, etc., especially in some high-end embedded control applications, such as mobile phones, industrial control, and networks. Communication and other aspects. ARM technology has excellent performance and efficiency, and its partners include many of the world's top semiconductor companies. It can be said that ARM technology is almost everywhere.

The TCP/IP Internet Protocol family has become an open system interconnect protocol worldwide, providing excellent interoperability and compatibility with a variety of network technologies. The combination of embedded technology and TCP/IP technology has shown strong momentum and huge market potential. How to develop efficient code for ARM, especially to improve the execution efficiency of basic software modules like TCP/IP protocol stack has become a problem that every ARM-based embedded system developer must consider.

Program optimization for ARM

There are many aspects to developing an efficient program, including excellent algorithm implementation, good programming style, and program optimization for the target. Program optimization refers to the process of adjusting and improving the program code by using software development tools after the software programming is basically finished, so that the program can make full use of limited software and hardware resources, reduce the code size, and improve the operation efficiency.

In the actual programming process, the two goals of program optimization (run speed and code size) are often contradictory. In order to improve the efficiency of the program, it is necessary to sacrifice storage space and increase the amount of code. In order to reduce the amount of program code and compress the memory space, it may be necessary to reduce the efficiency of program operation. According to the optimization focus, program optimization can be divided into running speed optimization and code size optimization. With the continuous development of microelectronics technology, storage space is no longer the main factor restricting system integration. Program optimization for ARM is mainly to discuss how to write C language programs that can run efficiently based on understanding assembly language and compilation rules.

As a high-performance, low-power RISC chip, ARM's C compiler is very mature. However, when writing C source programs for ARM, the necessary optimization of the program is still an effective way to improve the efficiency of the program. The following are some of the more typical optimization principles and methods used in implementing the TCP/IP protocol. These techniques are also applicable to other RISC instruction set microprocessors.

Variable definitions

The 32-bit ARM processor's instruction set supports signed/unsigned 8-bit, 16-bit, 32-bit integer and floating-point variable types, which not only saves code, but also improves code efficiency. Depending on the scope of action, variables in C can be divided into global variables and local variables. The ARM compiler typically locates global variables in memory and local variables are allocated to general purpose registers.

In the global variable declaration, you need to consider the optimal memory layout, so that various types of variables can be aligned with a 32-bit space bit reference, thereby reducing unnecessary waste of storage space and improving operational efficiency. Such as:

Typical optimization principles and methods of TCP/IP protocol

The four variables defined here are of the same form, but differ in order, resulting in different data layouts in the final image, as shown in Figure 1. Obviously the second way saves more memory space.

Typical optimization principles and methods of TCP/IP protocol

For local variables, try not to use variable types other than 32 bits. When the number of local variables in a function is small, the compiler allocates local variables to internal registers, each of which occupies a 32-bit register. Such short and char type variables not only do not save space, but will consume more instruction cycles to complete short and char access operations. The C language code and its compilation results are as follows:

Typical optimization principles and methods of TCP/IP protocol

Conditional execution

Conditional execution is an essential operation in the program. A typical conditional execution code sequence begins with a comparison instruction followed by a series of related execution statements. Conditional execution in ARM is achieved by judging the flag of the operation result. In some operation results with flag bits, the result of the N and Z flag bits is the same as the result of the comparison statement. Although there is no instruction with a flag in the C language, in the C language program for ARM, if the operation result is compared with 0, the compiler will remove the comparison instruction and implement the operation and judgment through a flag bit instruction. E.g:

Typical optimization principles and methods of TCP/IP protocol

Therefore, the conditional judgment of the C language programming for ARM should try to adopt the form of "compared with 0". In C language, most of the conditional execution statements are applied in the if conditional judgment, and also in complex relational operations (etc.) and bitwise operations (&&, !, and etc.). For the C language programming for ARM, the signed variables should try to take the relational operation of x<0, x>=0, x==0, x!=0; for the unsigned variable, x== 0, x! = 0 (or x > 0) relational operators. The compiler can optimize conditional execution.

For conditional statements in programming, the if and else criteria should be simplified as much as possible. Different from the traditional C language programming, in the C language programming for ARM, similar conditions in the relational expression should be concentrated, so that the compiler can optimize the judgment conditions.

Computer Power Supply

Easy Electronic Technology Co.,Ltd , https://www.yxpcelectronicgroups.com

Posted on