【发布时间】:2013-06-29 16:22:25
【问题描述】:
我正在尝试以正确的方式优化此代码。我所说的正确是......我想有一种通用的方法来执行这些优化,这样如果其他人查看代码,他们将能够删除优化。
C 代码示例以提高可读性...
int a = 1; // mapped to %l0
int b = 5; // mapped to %l1
int c = 0; // mapped to %l2
int d; // mapped to %l3
while( a < b ) {
c += a * b;
++a;
}
d = c * b;
SPARC 程序集版本...
mov %g0, %l2
cmp %l0, %l1
bge END_LOOP
nop
LOOP:
mov %l0, %o0
call .mul
mov %l1, %o1 ! Fill delay slot with second argument
add %l2, %o0, %l2
inc %l0
cmp %l0, %l1
bl LOOP
nop
END_LOOP:
mov %l2, %o0
call .mul ! Fill delay sot with second argument
mov %l1, %o1
mov %o0, %l3
我可以优化第一部分(不确定是否正确),但我不确定如何优化第二部分。
mov %g0, %l2
cmp %l0, %l1
bge,a END_LOOP ! Annul branch to execute if branch is taken
mov %l2, %o0 ! Instruction at target
LOOP:
mov %l0, %o0
call .mul
mov %l1, %o1 ! Fill delay slot with second argument
add %l2, %o0, %l2
inc %l0
cmp %l0, %l1
bl LOOP
nop
mov %l2, %o0 ! Move the instruction to above the target
END_LOOP:
call .mul ! Fill delay sot with second argument
mov %l1, %o1
mov %o0, %l3
非常感谢任何有关如何执行这些优化的帮助。
【问题讨论】:
-
如果您的目标是 SPARC v8 或更高版本,您可以使用
SMUL指令而不是调用.mul系统库例程。 -
我可能也应该添加它。这是一台较旧的 32 位 SPARC 机器。