当前位置:网站首页>Embedded C language loop deployment
Embedded C language loop deployment
2022-07-27 19:38:00 【WangLanguager】
1、 analysis :
During each cycle , Two instructions are added to the loop body , A subtraction instruction (1 Machine cycles ), A branch instruction (3 Machine cycles ), common 4 Machine cycles , This is the overhead of the system .
2、 improvement :
(1) Repeat the circulatory body many times , Reduce overhead ratio .
(2) The code in the loop body increases , Fewer cycles .
3、 Examples of loop expansion :
int checksum_v9(int *data, unsigned int N)
{
int sum = 0;
do
{
sum += *(data++);
sum += *(data++);
sum += *(data++);
sum += *(data++);
N-=4; // Suppose the number of data to be accumulated is 4 Multiple
} while (N!=0);
return sum;
}
The assembly code of the above program is :
checksum_v9_s
MOV r2,#0 ;sum = 0
checksum_v9_loop
LDR r3,[r0] #4 ;r3 = *(data++)
SUBS r1,r1,#4 ;N-=4 and set flags
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 r3,[r0] #4 ;r3 = *(data++)
ADD r2,r3,r2 ;sum += r3
BNE checksum_v9_loop ;if(N != 0) goto loop
MOV r0,#r2 ;r0 = sum
MOV pc,r14 ;return r0
4、 Discuss
After improvement : The total cycle overhead is from 4N Machine cycles are reduced to N Machine cycles ( Each cycle requires 4 Cycle cost of machine cycles , Reduce to the original 1/4). If the circulation body is smaller , The more obvious the effect of this method , It can even improve efficiency nearly 1 times .
hypothesis N=20, Execute before optimization 83 statement , Then execute after optimization 53 statement .
5、 problem
(1) How much should be expanded ?
(2) The number of expansions is not 4 Multiple ?
6、 The idea of circular expansion
(1) Disadvantages of loop expansion
① Code increase , Take up more memory
②Cache Space occupation problem
(2) So we need specific analysis of specific problems , Find the balance
7、 give an example :
for example : The execution of the loop body in the program requires 128 Machine cycles , The cycle overhead is generally 4 Machine cycles ,
Occupy 3%, If the loop body accounts for 30%, Then the cycle cost accounts for about of the total program 1%, This is the moment to unfold
loop , Performance improvement is limited .
The expansion of the loop , It's possible to destroy cache The content in , Causes jitter , Make the program performance drop sharply .
8、 Suppose the number of data to be accumulated is not 4 Integer multiple
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--)
{
sum += *(data++); // Suppose the number of data to be accumulated is not 4 Multiple
}
return sum;
}
9、 Conclusion
(1) The cycle count value should be decreased , When the counter adopts unsigned number , Terminate with (i!=0), Do not use (i >= 0)
(2) If it is determined that the number of cycles is greater than 1, Then use do{}while Loop structure
(3) For small loop , Cycle expansion can be carried out , Reduce system overhead
(4) Try to make the size of the array as expansion coefficient N Multiple
边栏推荐
- Subscription and use of Alibaba cloud video on demand service
- 编程式跳转
- 2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions
- kettle引用外部脚本完成电话号码清洗、去重缩进
- 嵌入式C语言结构体
- C language case: password setting and login > clear solution getchar and scanf
- 查看宝塔PHP扩展目录
- Basic concepts of Nacos and single machine deployment
- 200行代码快速入门文档型数据库MonogoDB
- c语言:c语言代码风格
猜你喜欢

Definition of graph traversal and depth first search and breadth first search (2)

golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码

Analysis of Eureka server

Make your chat bubbles colorful

成本高、落地难、见效慢,开源安全怎么办?

S32K系列芯片--简介

kettle入门级操作第一篇(读取excel、输出excel)

Webmagic+selenium+chromedriver+jdbc垂直抓取数据。
技术实践干货 | 初探大规模 GBDT 训练

Low code implementation exploration (45) business parameters
随机推荐
Can set be used to define local variables in OPDS SQL
[cloud picture theory] the first time to know Huawei cloud micro service engine CSE in issue 250
kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
VMware: set up SSH
kettle 分列、合并记录
c语言:8、makeFile编写
C language: 10. Input stream, output stream, error stream
C language printing diamond
2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management
Big guys, Oracle CDC, local operation, always encounter this an exception occurred in
嵌入式C语言对次数固定的循环的优化
Web3.0热浪之下 门萨Mensa生态震撼来袭
MFC高级控件之Tab控件( CTabCtrl )
Webmagic+selenium+chromedriver+jdbc垂直抓取数据。
Kettle switch / case control to realize classification processing
Hardware acceleration of zero knowledge proof
Using vscode to build u-boot development environment
浅谈基本的网络基本故障和问题排查
C language preprocessing instruction
原厂PW4203降压型1-3节锂电池充电芯片