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

Click For Summary
SUMMARY

This discussion focuses on efficient methods for computing the parity of the Hamming weight of a binary sequence, specifically whether the weight is even or odd. Key techniques mentioned include the use of XOR operations and lookup tables for fast implementation, particularly in Pascal and C/C++ environments. The intrinsic function _popcnt() in Visual Studio 2010 and __builtin_popcount() in GCC are highlighted as hardware-accelerated methods for counting bits, providing significant performance advantages. The conversation also touches on the complexities of bitwise operations and the implications of using lookup tables versus direct computation.

PREREQUISITES
  • Understanding of binary sequences and Hamming weight
  • Familiarity with bitwise operations (XOR, AND, shifts)
  • Knowledge of intrinsic functions in C/C++ (e.g., _popcnt(), __builtin_popcount())
  • Basic concepts of error correcting codes and finite fields (GF(2))
NEXT STEPS
  • Research the implementation of lookup tables for parity calculations
  • Explore the use of intrinsic functions for bit manipulation in various programming languages
  • Study the time complexities of bitwise operations in detail
  • Investigate advanced error correcting codes and their applications in data transmission
USEFUL FOR

Software developers, computer scientists, and engineers working with binary data, error correction algorithms, or optimizing performance in low-level programming.

  • #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
8K
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 11 ·
Replies
11
Views
4K
Replies
29
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K