Efficiently computing the parity of the Hamming weight of a binary sequence

Click For Summary
The discussion revolves around efficiently determining the parity of the weight of a binary number, focusing on whether it is even or odd without calculating the actual weight. Participants explore various methods, including the use of XOR operations and lookup tables to simplify the process. One user mentions using a lookup table for fast implementation, while another highlights the efficiency of the "popcnt" instruction available in modern processors, which counts the number of 1 bits in a binary value. The conversation also touches on error-correcting codes and their relation to parity, with references to Hamming codes and finite fields. Concerns about the time complexity of different methods are raised, with some suggesting that certain approaches may be inefficient for large integers. The discussion concludes with a focus on the practical applications of these methods in research related to primitive polynomials and the factorization of cyclotomic polynomials. Overall, participants share insights on optimizing parity calculations while acknowledging the limitations of their respective hardware.
  • #31
uart said:
X86 parity flag implementation.
Based on some comments in some old threads at other forums, I think this is similar to what gcc probably does. It takes 4.4 seconds on my system (note it's 1/2 the number of iterations I used for my popcnt test).

Code:
        mov     ecx,07blackfh
        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
 
Technology news on Phys.org
  • #32
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. :smile:

note it's 1/2 the number of iterations I used for my popcnt test
Yeah I noticed that you had 2^32-1 for that previous test. At first I didn't see it, and I was seriously puzzled as to why my much slower computer seemed to be doing the tight loop at the same speed or even slightly faster than yours (about 5.6 sec). Then I noticed you were doing twice as many iterations.
 
Last edited:
  • #33
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).
 
Last edited:

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
Replies
13
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
7K
Replies
9
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
Replies
29
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
5K