我正在尝试计算 位奇偶校验 大量的 uint64。位奇偶校验是指接受 uint64 并在设置位数为偶数时输出 0,否则输出 1 的函数。
目前我正在使用以下函数(@Troyseph,发现 here):
uint parity64(uint64 n){
n ^= n >> 1;
n ^= n >> 2;
n = (n & 0x1111111111111111) * 0x1111111111111111;
return (n >> 60) & 1;
}
相同的 SO 页面具有以下汇编例程(@papadp):
.code
; bool CheckParity(size_t Result)
CheckParity PROC
mov rax, 0
add rcx, 0
jnp jmp_over
mov rax, 1
jmp_over:
ret
CheckParity ENDP
END
它利用了机器的parity flag .但是我不能让它与我的 C 程序一起工作(我知道几乎没有程序集)。
问题 .如何在我的 C 源文件中包含上述(或类似的)代码作为内联汇编,以便
parity64()
函数运行那个?(我在 Intel Xeon Haswell 上使用 GCC 和 64 位 Ubuntu 14)
如果有任何帮助,
parity64()
函数在以下例程中调用:uint bindot(uint64* a, uint64* b, uint64 entries){
uint parity = 0;
for(uint i=0; i<entries; ++i)
parity ^= parity64(a[i] & b[i]); // Running sum!
return parity;
}
(这应该是 Z/2Z 域上两个 vector 的“点积”,也就是 GF(2)。)
最佳答案
因为 C 在处理位操作时很烂,我建议使用 gcc 内置函数,在这种情况下是 __builtin_parityl()。看:
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
关于c - 在 C 中使用内联汇编进行位奇偶校验?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43883473/