| New Reply |
Efficiently computing the parity of the Hamming weight of a binary sequence |
Share Thread | Thread Tools |
| Aug17-12, 05:44 PM | #18 |
|
Recognitions:
|
Efficiently computing the parity of the Hamming weight of a binary sequencelong j = 0; long m = (1L << 31) - 1; for(long i=0; i < m; i++) { j += __builtin_popcountl(i); } cout << j << endl; |
| Aug17-12, 05:57 PM | #19 |
|
|
Cool, computing time was 29197 milliseconds, j = 33285996513. Still, addition time is proportional to the amount of digits no?
|
| Aug17-12, 05:59 PM | #20 |
|
Recognitions:
|
gcc -S -O2 example.cpp or to generate assembly source and object but not link: gcc -S -O2 -c example.cpp This will produce an assembler file called example.s. register long j = 0; long m = (1L << 31) - 1; for(long i=0; i < m; i++) { j = __builtin_popcountl(i); } cout << j << endl; |
| Aug17-12, 06:13 PM | #21 |
|
|
Ooh, this is what I get after adding the -S option for g++:
|
| Aug17-12, 06:27 PM | #22 |
|
Recognitions:
|
gcc -O2 -S -c example.cpp In spite of the errors you got, gcc may have produced an assembly source file, look for a file with a .s suffix. |
| Aug17-12, 06:41 PM | #23 |
|
|
Same thing happened with -c. I tried
|
| Aug17-12, 06:45 PM | #24 |
|
|
The .s file is not there...
|
| Aug17-12, 07:21 PM | #25 |
|
Recognitions:
|
Code:
mov ecx,0ffffffffh
test0: popcnt eax,ecx
loop test0
gcc -help to see the options. Also make sure you're using upper case S: gcc -S example.cpp I still don't understand why you want to calculate the even / odd parity when you're learning about GF(2) primitive polynomials. |
| Aug18-12, 02:56 AM | #26 |
|
|
Thanks rcgldr. |
| Aug18-12, 04:33 AM | #27 |
|
Recognitions:
|
|
| Aug18-12, 09:33 AM | #28 |
|
Recognitions:
|
|
| Aug18-12, 09:45 AM | #29 |
|
Recognitions:
|
|
| Aug18-12, 10:00 AM | #30 |
|
Recognitions:
|
Code:
var myGlobalResult : longword;
procedure testparity;
label Strt,L1,L2,L3,L4;
begin
asm
mov ecx,$7FFFFFFF
mov edx,0
Strt:
mov eax,ecx
or eax,eax
jpe L1
not edx
L1:
shr eax,08
jpe L2
not edx
L2:
shr eax,08
jpe L3
not edx
L3:
shr eax,08
jpe L4
not edx
L4:
loop Strt
and edx,01
mov myGlobalResult,edx
end; {asm}
end; {testparity}
|
| Aug18-12, 11:58 AM | #31 |
|
Recognitions:
|
Code:
mov ecx,07fffffffh
xor edx,edx ;set edx = parity
par0: mov eax,ecx
xor al,ah
jpe par1
not edx
par1: shr eax,16
xor al,ah
jpe par2
not edx
par2: loop par0
neg edx
|
| Aug18-12, 12:48 PM | #32 |
|
Recognitions:
|
Hey good idea to add the "xor al,ah" to check 16 bits at once, that nearly doubles the speed. That reduced my times from 14.7 sec down to about 8.6 seconds.
![]() |
| Aug18-12, 07:21 PM | #33 |
|
Recognitions:
|
Wasn't sure what the return value is supposed to be. At the end of the code the "neg edx", returns 0 for even parity, 1 for odd parity, to reverse this use "inc edx" instead (also the return value would normally be in eax, not edx).
|
| New Reply |
| Thread Tools | |
Similar Threads for: Efficiently computing the parity of the Hamming weight of a binary sequence
|
||||
| Thread | Forum | Replies | ||
| [C++] Efficiently counting the number of '1' bits inside blocks of a binary number | Programming & Comp Sci | 4 | ||
| reduce the sequence..or how to calculate it efficiently | General Math | 1 | ||
| Algorithm for computing conjugacy classes in subgroups of the GL(n,F_p) efficiently | Calculus & Beyond Homework | 0 | ||
| Hamming Code, Parity Matrix | Linear & Abstract Algebra | 0 | ||
| Show the following properties of Hamming weight... | Calculus & Beyond Homework | 3 | ||