, , XOR 32- arg. .
(and $1,%eax), , .
, xor %edx,%eax : xor %edx %eax. .
shr , x . 32 , , , , x 0. (- , XOR, XOR 0 , .)
, , C / C. , y ^= (x>>=1); , x, .
, C, - , ( , x ,). , y ^= x, x>>=1; .
, , ;.
int f1(unsigned x) {
int y = 0;
while(x != 0) {
y ^= x; x>>=1;
}
return y & 1;
}
, , gcc5.3 -O3 Godbolt. - xor-zeroing mov $0, %eax gcc ret. (, , gcc, .)
: :
O (n) ( n - x). O (log2 (n)) x86, .
, . ( xorw, 16- xor .)
parity:
xor %eax,%eax
movzwl 4(%esp), %edx
xorw 6(%esp), %dx
xor %dh, %dl
setnp %al
ret
, , x86 (PF), 8 " ", xor.
np, PF= 1 : xor = 0. 0 .
, SIMD, , , 32 8 .
Zeroing eax ( xor) , , , set-flags/ setp %al/movzbl %al, %eax, x86: xor, mov ?.
, @EOF, CPUID POPCNT, popcnt , , . ( : xor - add-without-carry, , , ).
GNU C __builtin_parity __builtin_popcnt, , , ( -march=... -mpopcnt), . Intel , , -mpopcnt.
, gcc pure-C . (, clang , , gcc) popcount POPCNT, .: (
godbolt.
int parity_gnuc(unsigned x) {
return __builtin_parity(x);
}
int parity_popcnt(unsigned x) {
return _mm_popcnt_u32(x) & 1;
}
popcnt 4(%esp), %eax
and $1, %eax
ret
. x86 wiki.