To optimize the code, first identify the bottleneck of the program, that is, the code that consumes a lot of CPU time, and then perform targeted Optimization on it. Using the code profiling tool profile provided by CCS, you can calculate and display the running time of each important segment and function in the program, locate the program segment with a large amount of computing, and optimize these segments, it has a huge impact on improving algorithm performance.
(1) joint use of-PM and-03 compilation options to optimize the code at the project level: CCS provides powerful compilation options, from-O0 to-O3. The-O3 compilation option enables software flow and other optimization methods. The-PM option combines all source programs of the entire project from the perspective of program code and serves as a module for processing. The-PM and-03 options can be used in combination to perform a series of optimizations, and the code size is much smaller.
(2) Use the const and restrict keywords to modify the pointer: const indicates that the content pointed to by the Pointer Modified by the compiler cannot be modified; restrict indicates that the Pointer Modified by the compiler will not overwrite the content pointed by other pointers, this information prevents two pointers from accessing the same memory address and eliminates the correlation between the memory. This allows you to execute multiple data reads and operations in parallel, so that the code runs at a high efficiency.
(3) use a wide-length memory for short-character long data access (data packaging): When the CPU executes a series of short data (such as 16-bit data) operations, you can set the data type to 32-bit int type, so that you can access two short data types at a time, and then use the C6000 command to perform two data operations at the same time, reducing access to the memory, this saves half the time compared to the 16-bit short type.
(4) Expand the loop, open the loop in the C language, change multiple cycles to fewer loops, reduce loop nesting, and increase the number of parallel commands. This improves the Software Workflow arrangement and code performance.
Explain reduces the call of C functions. Try to use the system-provided inline functions (intrinsics functions) instead of C functions. The C6000 compiler provides many intrinsics, which are online functions directly mapped to C6000 Assembly commands, the C code can be quickly optimized to reduce unnecessary operations and increase the code operation speed.
Pipeline uses the software flow technology, which is a kind of technology for scheduling optimization of cyclic commands. It can generate very compact circular code by using the software flow. When the-O2 or-O3-level optimization option is used during compilation, the compiler will execute software flow on the loop in the program. Through the optimization of software flow, the efficiency of cyclic code can be greatly improved, and the concurrency of commands can be greatly realized.