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

Click For Summary

Discussion Overview

The discussion revolves around methods for efficiently computing the parity of the Hamming weight of a binary sequence, specifically whether the weight is even or odd. Participants explore various algorithms and techniques, including the use of lookup tables, intrinsic functions, and error-correcting codes.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants propose using XOR operations to determine parity without calculating the actual weight, noting that x has odd weight if exactly one of a or b has odd weight.
  • Others mention the use of lookup tables for fast implementation, particularly for smaller bit sizes, while expressing concerns about potential time complexity.
  • A participant highlights the availability of the "popcnt" instruction in modern processors, which counts the number of 1 bits, and suggests its use for determining parity.
  • There is a discussion about the efficiency of various methods, with some participants questioning the time complexity of using lookup tables and others defending their linearity.
  • Some express skepticism about the necessity of calculating the actual weight, suggesting there may be quicker methods to determine parity.
  • Participants discuss the relevance of error-correcting codes and their relation to the problem, with some questioning the need for parity calculations in certain contexts.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the most efficient method for computing parity. Multiple competing views and methods are presented, with ongoing debate about their relative efficiencies and applicability.

Contextual Notes

Some participants mention limitations related to the size of integers being processed, with concerns about the applicability of certain methods to arbitrarily large integers. There are also discussions about the assumptions underlying the use of specific algorithms and their performance characteristics.

Who May Find This Useful

This discussion may be useful for computer scientists, software developers, and engineers interested in efficient algorithms for binary operations, particularly in contexts involving error detection and correction.

  • #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
4K
Replies
13
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • 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