What is the purpose of the Parity Flag on a CPU?

后端 未结 3 1462
无人及你
无人及你 2021-02-05 04:27

Some CPUs (notably x86 CPUs) feature a parity flag on their status register. This flag indicates whether the number of bits of the result of an operation is odd or even.

相关标签:
3条回答
  • 2021-02-05 04:47

    Back in the "old days" when performance was always a concern, it made more sense. It was used in communication to verify integrity (do error checking) and a substantial portion of communication was serial, which makes more use of parity than parallel communications. In any case, it was trivial for the CPU to compute it using just 8 XOR gates, but otherwise was rather hard to compute without CPU support. Without hardware support it took an actual loop (possibly unrolled) or a lookup table, both of which were very time consuming, so the benefits outweighed the costs. Now though, it is more like a vestige.

    0 讨论(0)
  • 2021-02-05 04:48

    The Parity Flag is a relic from the old days to do parity checking in software.

    TL;DR

    What is parity

    As Randall Hyde put it in The Art of Assembly Language, 2nd Edition:

    Parity is a very simple error-detection scheme originally employed by telegraphs and other serial communication protocols. The idea was to count number of set bits in a character and include an extra bit in the transmission to indicate whether that character contained an even or odd number of set bits. The receiving end of the transmission would also count the bits and verify that the extra "parity" bit indicated a successful transmission.

    Why Parity Flag was added to CPU architecture

    In the old days there was serial communication hardware (UART) that lacked the ability to do parity checking on transmitted data, so programmers had to do it in software. Also some really old devices like paper tape punches and readers, used 7 data bits and a parity bit, and programmers had to do the parity checking in software to verify data integrity. In order to be able to use parity bit for error detection communicating parties would have to agree in advance on whether every transmitted byte should have odd or even parity (part of a communication protocol).

    The primary methods to do parity checking in software without CPU support are bit counting or using a lookup table. Both are very expensive compared to having a Parity Flag in a CPU computed by a single instruction. For that reason in April 1972 Intel introduced the Parity Flag into their 8008 8-bit CPU. Here is an example of how each byte could be tested for integrity on the receiving end since then.

    mov        al,<byte to be tested>
    test       al,al
    jp         <somewhere>         ; byte has even parity
                                   ; byte has odd parity 
    

    Then a program could perform all sorts of conditional logic based on the value of the Parity Flag.

    Evolution of conditional parity instructions in Intel CPUs

    • 1972 - the Parity Flag is first introduced with Intel 8008. There are conditional instructions for jumps (JPO, JPE), calls (CPO, CPE) and returns (RPO, RPE).
    • 1978 - Intel 8086 drops everything except for conditional jumps (JNP/JPO, JP/JPE).
    • 1985 - Conditional set instructions SETPE/SETP and SETPO/SETNP are added with Intel 80386.
    • 1995 - Conditional move instructions CMOVP/CMOVPE, CMOVNP/CMOVPO are added with Pentium Pro.

    This set of instructions which make use of the Parity Flag remained fixed since then.

    Nowadays the primary purpose of this flag has been taken over by hardware. To quote Randall Hyde in The Art of Assembly Language, 2nd Edition:

    Serial communications chips and other communications hardware that use parity for error checking normally compute the parity in hardware; you don't have to use software for this purpose.

    The antiquity of the Parity Flag is proved by the fact that it works on low 8 bits only, so it's of limited use. According to Intel® 64 and IA-32 Architectures Software Developer Manuals the Parity Flag is:

    Set if the least-significant byte of the result contains an even number of 1 bits; cleared otherwise.

    Interesting fact: By his own words a networking engineer Wolfgang Kern scanned all code he had written at some point (~14 GB) for JPE and JPO instructions and found it only in an RS232 driver module and in an very old 8-bit calculation.

    Sources

    • The Intel 8008 support page
    • Intel 8080 Assembly Programming Manual
    • Complete 8086 Instruction Set
    • Wikipedia x86 instruction listings
    • Intel® 64 and IA-32 Architectures Software Developer Manuals
    • The Art of Assembly Language, 2nd Edition by Randall Hyde
    0 讨论(0)
  • 2021-02-05 04:53

    There's one practical micro-optimization achievable with parity -- that's bit swapping as used eg in fourier transform address generation using the butterfly kernel.

    To swap bits 7 and 0, one can exploit parity of (a&0x81) followed by conditional (a^=0x81). Repeat for bits 6/1, 5/2 and 4/3.

    0 讨论(0)
提交回复
热议问题