如何解决用Assembly在pos n的一个字节中插入一点
与普通的C语言相比,有没有比组装C语言更快的方法了?
这是C语言中函数的原型:
uint8_t InsertBit(uint8_t input,uint16_t idx,uint8_t insbit)
该函数应返回新值。我从事嵌入式手臂皮质M的研究。 C例程大约有18条指令。
C例程:
__attribute__((optimize("O1")))
uint8_t InsertBit(uint8_t input,uint8_t insbit)
{
//Split the input into two parts,one shifted and one not
uint8_t bottom = input;
uint8_t top = (input << 1);
//insert a '0' or '1' before the shifted part
if (insbit)
top |= (1 << idx);
else
top &= (~(1 << idx));
//keep the top bits of top
top &= (-1 << idx);
//keep the bottom bits of bottom
bottom &= ~(-1 << idx);
//combine the two parts.
return (bottom | top);
}
解决方法
Erik的方法(通过添加输入的掩码版本将插入点移到插入点上方,以左移)可以表示为
class ParentModelRouter:
def db_for_write(self,model,**hints):
# some options
return 'default'
class ChildModelRouter:
def db_for_write(self,**hints):
# some options
return 'other_db'
由于可自由移动,它在ARM上基本上编译为两条指令:
uint8_t InsertBit(uint8_t input,uint16_t idx,uint8_t insbit)
{
return ((insbit + (input >> idx)) << idx) + input;
}
,
uint8_t InsertBit(uint8_t input,uint8_t insbit)
{
return (input + (input & (-1 << idx))) | (insbit << idx);
}
注意:
- x + x将x乘以2,与将其左移1一样。
- 我们想插入一点,所以我们只将x的高位添加到原始x。
abcdefgh x
abc00000 x & (-1 << 5) // idx is 5
--------(+)
abc0defgh <--- one bit inserted at position 5
-
此处
abc + abc
导致abc0
,即abc
向左移1位 -
不理会
defgh
,却保持不变 -
然后仅将
or
放在所需位置的适当位置。 -
非常简单,没有条件逻辑
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。