Nic*_*ick 3 c assembly bit-shift
请考虑维基百科关于平方根algorythms的文章中的以下代码
short isqrt(short num) {
short res = 0;
short bit = 1 << 14; // The second-to-top bit is set: 1 << 30 for 32 bits
// "bit" starts at the highest power of four <= the argument.
while (bit > num)
bit >>= 2;
while (bit != 0) {
if (num >= res + bit) {
num -= res + bit;
res = (res >> 1) + bit;
}
else
res >>= 1;
bit >>= 2;
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
为什么会有人这样做:
short bit = 1<< 14;
Run Code Online (Sandbox Code Playgroud)
而不是直接分配值:
short bit = 0x4000;
Run Code Online (Sandbox Code Playgroud)
一些RISC指令集可以让您按给定的数量移动,这很方便.例如,MIPS允许您提供shamt参数.其他指令集不是那么方便.MSP430(16位 - 不是扩展指令)编译器需要将其渲染为对RLA我所假设的伪指令的循环调用.
因此,在某些情况下,这样做似乎不会"伤害",但在其他情况下似乎可以.这样长途班次有没有优势呢?因为在某种意义上它似乎会降低代码的可移植性.
X86或其他CISC机器是否做了一些我不知道的神奇事物?:)
| 归档时间: |
|
| 查看次数: |
166 次 |
| 最近记录: |