Faster way for IF() and Adding?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Faster way for IF() and Adding?

1,278 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave3891 on Sun Jun 19 09:58:24 MST 2011
Is there a faster way to do this?
[SIZE=2]
[LEFT]error2 -= cp2;
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](error2 < 0 ){
error2 += cp1;
LPC_GPIO2->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MASKED_ACCESS[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][(1<<4)] = (1<<4);
position2++;[/LEFT]
}
[/SIZE]

It takes about 70 clock cycles to do this operation and I need to use 6 of these within a timer interrupt, and I cant speed up my timer freq without speeding up this operation.


Thanks
Dave
0 项奖励
回复
7 回复数

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave3891 on Tue Jun 21 21:24:32 MST 2011
Thanks for the tip, changing it to a uint32 got it down 5 more cycles to 41 each.
0 项奖励
回复

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by igorsk on Tue Jun 21 04:15:37 MST 2011
Use int or uin32_t variables. With uint16_t, the compiler has to do extra work to trim the range after each operation.
0 项奖励
回复

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave3891 on Mon Jun 20 23:09:17 MST 2011
Well I changed it to
[SIZE=2]
[LEFT]error2 -= cp2;
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](error2 < 0 ){
error2 += cp1;
mask2 |= (1<<4);
position2++;[/LEFT]
}
[/SIZE]

with a single mask access at the end of the 6 sets and used max optimization and it looks like its down to about 46 cycles each.

Does that sound about right?
0 项奖励
回复

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Jun 20 15:32:36 MST 2011

Quote:

...Release build?


... was a little hint to use optimization :)

http://support.code-red-tech.com/CodeRedWiki/CompilerOptimization

Quote:

How would I view what is being done by the compiler in assembly code?


http://support.code-red-tech.com/CodeRedWiki/DebugViewAsm?highlight=%28assembly%29
0 项奖励
回复

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave3891 on Mon Jun 20 15:09:39 MST 2011
They are all uint16_t and it is a debug build.
70 cycles seems like alot for 2 add's a subtract and a IF.
How would I view what is being done by the compiler in assembly code?
0 项奖励
回复

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by igorsk on Mon Jun 20 04:26:27 MST 2011
Are you using Release build? Are any of those variable floating-point?
0 项奖励
回复

1,269 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Sun Jun 19 12:31:58 MST 2011
As the code is trivial, the only operation that could take some significant time is the GPIO access. If you need to set several IO pins in a single port, make a mask variable, OR it with all active bit masks and then do:
[SIZE=2]LPC_GPIO2->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MASKED_ACCESS[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][mask] = mask;

Actually [/SIZE][SIZE=2]LPC_GPIO2->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MASKED_ACCESS[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][mask] = 0xffffffff; would do the same
[/SIZE]
0 项奖励
回复