求助:一段英文注释的解释,和一段代码的含义
Compute the sign of an integer
int v; // we want to find the sign of v
int sign; // the result goes here
// CHAR_BIT is the number of bits per byte (normally 8).
sign = -(v < 0); // if v < 0 then -1, else 0.
// or, to avoid branching on CPUs with flag registers (IA32):
sign = -(int)((unsigned int)((int)v) >> (sizeof(int) * CHAR_BIT - 1));
// or, for one less instruction (but not portable):
sign = v >> (sizeof(int) * CHAR_BIT - 1);
The last expression above evaluates to sign = v >> 31 for 32-bit integers. This is one operation faster than the obvious way, sign = -(v < 0). This trick works because when signed integers are shifted right, the value of the far left bit is copied to the other bits(算术右移). The far left bit is 1 when the value is negative and 0 otherwise; all 1 bits gives -1. Unfortunately, this behavior is architecture-specific.
文中红色字体的注释含义和代码中的(int)((unsigned int)((int)v)这么做类型转换的意图,请大家帮忙解释一下。多谢。
[解决办法]
[解决办法]
[解决办法]
[解决办法]
[解决办法]
int sign = -(v < 0);
00E412C0 xor eax,eax
00E412C2 cmp dword ptr [v],0 //cmp一般用于分支语句中,比较结果保存在eax中
00E412C6 setl al //这就是所谓的cpu branch,这种语句往往会影响cpu指令预编译
00E412C9 neg eax
00E412CB mov dword ptr [sign],eax
sign = -(int)((unsigned int)(int)v >> (sizeof(int)*8-1));
00E412CE mov eax,dword ptr [v]
00E412D1 shr eax,1Fh //确保执行逻辑右移,效率会更高
00E412D4 neg eax
00E412D6 mov dword ptr [sign],eax
[解决办法]
to avoid branching on CPUs with flag registers (IA32):
为了避免在“有多个具有32位标志寄存器的CPU”上产生歧义(不同的结果)?
[解决办法]
不错 很好~~~~~~~~~~~~~~~~
[解决办法]
不懂耶,,,~
[解决办法]
dafdafsadfadasfdsads
[解决办法]

[解决办法]

[解决办法]
对学习编程者的忠告:
眼过千遍不如手过一遍!
书看千行不如手敲一行!
手敲千行不如单步一行!
单步源代码千行不如单步对应汇编一行!
[解决办法]