Performance 如何在MIPS中有效地反转16位整数的最后7位?

Performance 如何在MIPS中有效地反转16位整数的最后7位?,performance,assembly,mips,Performance,Assembly,Mips,例如,如果我有一个可以表示为01234567的整数,那么我需要将其反转并保留第一位,使其看起来像07654321。我还需要尽可能高效地完成这项工作 我已经在网上找到了: for (x=0;x<8;x++) { bit = input & 0x01; // get the low bit output |= bit; // set the low bit in output input >> 1; // shift to the right for AND mask ou

例如,如果我有一个可以表示为01234567的整数,那么我需要将其反转并保留第一位,使其看起来像07654321。我还需要尽可能高效地完成这项工作

我已经在网上找到了:

for (x=0;x<8;x++) {
bit = input & 0x01; // get the low bit
output |= bit; // set the low bit in output
input >> 1; // shift to the right for AND mask
output << 1; // move all bits to the left, low bit = 0 now
}
(在看了这里的其他问题后,我意识到我的形式与标准不同——这就是我在课堂上被教授的方式)


这段代码很有效,但我正在寻找一种更有效的方法来实现它。有人能提供如何改进的指导吗?

使用带有一点逻辑的查找表是一个非常有效的解决方案。像Gene建议的一个简单的表可以工作,或者您可以尝试使用多个表来权衡内存/速度(您可能不需要64位,因为您使用的是MIPS)

最快的可能是128字节或字查找表。屏蔽lo 7,使用此值查找位翻转索引的字节或字,或将此值放入原始值的lo 7。大约8条说明。该表很容易适应一级缓存。
    andi $7, $8, 0xC000     #$7: obtain the first two bits that we want to save
    sll $7, $7, 2           #$7: sll to make it possible to later append $7 on to the beginning

Flip:andi $4, $8, 0x03      #$4: bitwiseAND the reference with 0x03 to isolate last two bits
    or $5, $5, $4           #$5: store the bit using bitwiseOR 
    sll $5, $5, 2           #$5: sll $5 to create space for the next two bits to be loaded in 
    srl $8, $8, 2           #$2 slr $8 so the next two bits can be isolated
    addi $13, $13, -1       # decrease counter
    bne $13, $0, Flip       # if $13 is not equal to zero, then branch to start of Flip 

    or $5, $5, $7           # add the saved bit back onto the beginning 
    srl $5, $5, 2           # srl to get rid of the execess zeros at the end