N

Computer Arithmetic

An introduction to computer arithmetic algorithms and hardware, covering signed-magnitude and 2's complement addition and subtraction, Booth's algorithm, array multiplication, binary division, floating-point operations, BCD arithmetic circuits, and multi-digit decimal arithmetic.

Introduction

How does a modern processor perform billions of mathematical calculations every second using nothing more than microscopic switches turning on and off? The secret lies in computer arithmetic—the precise collection of hardware logic and step-by-step algorithms that transform raw binary logic into high-speed mathematical powerhouse performance!

In this comprehensive lesson, we will explore the internal algorithms and hardware configurations that make modern digital computing possible. By the end of this lesson, you will be able to:

  • Analyze hardware addition and subtraction algorithms for signed-magnitude and signed-2's complement representations.
  • Evaluate efficient multiplication techniques, including Booth's algorithm and combinational array multipliers.
  • Explain binary division hardware, restorable vs. nonrestoring methods, and divide-overflow prevention.
  • Synthesize floating-point operations, including mantissa alignment, exponent biasing, and normalization.
  • Design Binary-Coded Decimal (BCD) adders, subtractors, and multi-digit decimal arithmetic circuits.

Introduction to Computer Arithmetic

At its heart, an arithmetic processor is the specialized execution engine inside a CPU designed to execute data manipulation instructions. Everyday software applications rely heavily on four elementary arithmetic operations: addition, subtraction, multiplication, and division. From these four bedrock operations, numerical analysis methods can formulate virtually any higher-level mathematical function—from standard trigonometry to complex physics simulations.

Data Types and System Complexity

The architecture of an arithmetic processor depends heavily on the data types it must support. Data stored in processor registers during instruction execution can be formatted as binary or decimal, and each can be represented as either fixed-point (integers or fractions) or floating-point numbers. Furthermore, negative values require designated representation schemes such as signed-magnitude, signed-1's complement, or signed-2's complement.

If a processor only supports binary fixed-point addition, its underlying hardware remains simple. However, incorporating all four basic operations across binary and decimal data in both fixed-point and floating-point layouts creates a significantly more sophisticated hardware architecture.

Algorithmic Flowcharts

To build hardware that performs mathematics reliably, engineers rely on an algorithm—a finite sequence of well-defined procedural steps that solves a specific problem. When negative numbers use signed-2's complement representation, addition requires only a simple parallel binary adder. However, signed-magnitude representation demands a more elaborate algorithm that actively checks sign bits, compares relative magnitudes, and selects between addition and subtraction units.

To visualize these algorithmic sequences, hardware designers use flowcharts:

  • Rectangular boxes represent specific computational steps (such as register transfers or arithmetic microoperations).
  • Diamond-shaped boxes represent decision steps where execution paths branch based on condition checks (such as sign bit comparisons or carry flip-flops).

Addition and Subtraction Algorithms

Most digital computers adopt signed-2's complement representation for integer calculations because it simplifies arithmetic circuits. However, signed-magnitude representation is commonly retained for mantissas in floating-point operations. Understanding both hardware implementations reveals why complement arithmetic is so popular in modern CPU design.

Signed-Magnitude Addition and Subtraction

When adding or subtracting two signed numbers with magnitudes AA and BB, there are eight distinct conditions based on operation type and operand signs. When adding numbers with identical signs (or subtracting numbers with different signs), we simply add the magnitudes (A+BA + B) and attach the sign of AA. But when adding numbers with different signs (or subtracting numbers with identical signs), we must compare magnitudes:

  1. If A>BA > B, subtract BB from AA (A−BA - B) and keep the sign of AA.
  2. If A<BA < B, subtract AA from BB (B−AB - A) and reverse the sign of AA.
  3. If A=BA = B, subtract BB from AA to yield zero, forcing the sign to positive to avoid a negative zero (−0-0).

Hardware Implementation for Signed-Magnitude

To construct this in hardware, we use registers AA and BB, alongside sign flip-flops AsA_s and BsB_s. Rather than using separate circuits for magnitude comparison, addition, and subtraction, an elegant design employs a single parallel adder and a complementer:

  • Subtraction is executed as AA plus the 2's complement of BB (A+Bˉ+1A + \bar{B} + 1).
  • A mode control signal MM directs the complementer (M=0M=0 passes BB directly for addition; M=1M=1 feeds the 1's complement of BB and sets the input carry to 1 for subtraction).
  • The carry output is stored in flip-flop EE. If E=1E=1, then A≥BA \ge B, and AA contains the correct result. If E=0E=0, then A<BA < B, meaning AA holds the 2's complement of the true magnitude; taking the 2's complement of AA restores the correct value, and complementing sign bit AsA_s yields the proper sign.
  • Overflow during addition is captured in the add-overflow flip-flop AVFAVF.

Signed-2's Complement Addition and Subtraction

In signed-2's complement, the leftmost bit serves directly as the sign (00 for positive, 11 for negative). This layout simplifies hardware dramatically:

  • Addition: Add ACAC and BRBR directly across all bits (including sign bits). Any carry-out of the sign position is simply discarded.
  • Subtraction: Take the 2's complement of the subtrahend (BRBR) by adding its 1's complement plus 1, then add to ACAC.
  • Overflow Detection (VV): An overflow occurs if two numbers of the same sign yield a result with the opposite sign. Hardware detects this by taking the Exclusive-OR of the carries into and out of the sign-bit position (V=Cin⊕CoutV = C_{\text{in}} \oplus C_{\text{out}}).

Multiplication Algorithms

Multiplication in binary hardware follows the familiar pencil-and-paper technique of forming partial products and summing them. However, digital hardware optimizes this process using shift-and-accumulate microoperations.

Signed-Magnitude Hardware Multiplication

To multiply two nn-bit signed-magnitude numbers, hardware uses three registers (AA, BB, QQ), a sequence counter (SCSC), and sign logic:

  • The multiplicand is stored in register BB, the multiplier in QQ, and accumulator AA is initialized to zero.
  • Sign logic sets As=Qs⊕BsA_s = Q_s \oplus B_s.
  • The sequence counter SCSC is set to n−1n-1 (the number of magnitude bits).
  • For each step, hardware inspects the least significant bit of the multiplier (QnQ_n):
    • If Qn=1Q_n = 1, the multiplicand BB is added to AA (EA←A+BEA \leftarrow A + B).
    • If Qn=0Q_n = 0, AA remains unchanged.
  • The combined register bit string EAQEAQ is shifted right by one position (shr EAQ), pushing the lowest bit of AA into QQ and bringing EE into AA.
  • SCSC is decremented by 1. This loop continues until SC=0SC = 0, leaving the final double-length product in combined registers AA and QQ.

Booth Multiplication Algorithm

When numbers are represented in signed-2's complement, Booth's algorithm provides an optimized multiplication technique. It relies on the principle that a sequence of adjacent 11s in a multiplier ranging from bit weight 2k2^k down to 2m2^m can be evaluated as 2k+1−2m2^{k+1} - 2^m.

Booth's algorithm inspects two adjacent bits of the multiplier (QnQ_n and an appended extra flip-flop Qn+1Q_{n+1}):

  • 10: First 1 of a string encountered →\rightarrow Subtract multiplicand (AC←AC−BRAC \leftarrow AC - BR).
  • 01: First 0 after a string encountered →\rightarrow Add multiplicand (AC←AC+BRAC \leftarrow AC + BR).
  • 00 or 11: Inside a string of 0s or 1s →\rightarrow No arithmetic operation required.
  • Following the step, an arithmetic shift right (ashr AC & QR) shifts ACAC and QRQR right while preserving the sign bit of ACAC.

Combinational Array Multipliers

While sequential multiply algorithms require nn clock cycles, an array multiplier uses a pure combinational circuit to calculate all partial product bits simultaneously. For jj multiplier bits and kk multiplicand bits, the array requires j×kj \times k AND gates and (j−1)(j - 1) kk-bit parallel adders, yielding ultra-fast multiplication bounded only by gate propagation delays.

Division Algorithms

Division in fixed-point binary is structured as a sequence of compare, shift, and subtract microoperations. Because binary quotient digits are strictly 00 or 11, hardware division avoids the trial-and-error estimation required in decimal division.

Signed-Magnitude Division Hardware

Division uses the same core register setup as multiplication (AA, BB, QQ, EE, and SCSC):

  • The double-length dividend is loaded into combined registers AA and QQ, while the divisor resides in BB.
  • Rather than shifting the divisor right, hardware shifts register pair EAQEAQ left by one bit (shl EAQ).
  • The divisor BB is subtracted from AA by adding its 2's complement (EA←A+Bˉ+1EA \leftarrow A + \bar{B} + 1).
    • If carry flip-flop E=1E = 1, then A≥BA \ge B. The subtraction is valid, and quotient bit QnQ_n is set to 11.
    • If carry flip-flop E=0E = 0, then A<BA < B. The result is negative, quotient bit QnQ_n remains 00, and AA is restored by adding BB back (A←A+BA \leftarrow A + B).
  • This sequence repeats for n−1n-1 counter steps until SC=0SC = 0, leaving the quotient in QQ and the final remainder in AA.

Divide Overflow and Safety Mechanisms

Because processor registers have fixed bit widths, division can cause a divide overflow if the resulting quotient exceeds the register capacity. When a double-length dividend is divided by a single-length divisor, an overflow occurs if the high-order half of the dividend (AA) is greater than or equal to the divisor (BB).

If A≥BA \ge B , the system sets a divide-overflow flag (DVF=1DVF = 1). Divide overflow also automatically catches attempts to divide by zero, as any non-zero dividend is greater than or equal to zero. Modern processors handle DVFDVF by raising an interrupt request, suspending execution so an error handler or subroutine can rescale the data.

Alternative Division Methods

  1. Restoring Method: The standard approach described above, where negative trial subtractions are canceled out by immediately adding back the divisor BB.
  2. Comparison Method: Compares AA and BB using adder look-ahead carry logic prior to subtraction, performing the subtraction only if A≥BA \ge B.
  3. Nonrestoring Method: Avoids restoring additions. If a trial subtraction yields a negative partial remainder (E=0E = 0), the negative remainder is shifted left (2A2A) and BB is added in the next cycle, since 2(A−B)+B=2A−B2(A - B) + B = 2A - B.

Floating-Point Arithmetic Operations

Fixed-point numbers struggle to represent extremely large or tiny numbers efficiently. Floating-point representation overcomes this limitation by expressing values as a mantissa mm and an exponent ee scaled by radix rr (m×rem \times r^e).

Biased Exponents and Normalization

To simplify hardware floating-point math:

  • Normalization: A floating-point number is normalized when the most significant bit (A1A_1) of its fractional mantissa is non-zero (11 in binary). This ensures maximum bit accuracy.
  • Biased Exponent: Exponents are stored as positive numbers by adding a fixed constant bias (estored=eactual+biase_{\text{stored}} = e_{\text{actual}} + \text{bias}). Biasing removes explicit sign bits from exponent registers, allowing simple hardware magnitude comparators to check exponent sizes directly.

Floating-Point Addition and Subtraction

Adding or subtracting floating-point values requires four distinct execution phases:

  1. Zero Check: If either operand is zero, the non-zero value is passed immediately as the result.
  2. Mantissa Alignment: Exponents aa and bb are compared. The mantissa associated with the smaller exponent is shifted right while its exponent is incremented until both exponents match.
  3. Mantissa Addition/Subtraction: The aligned mantissas are added or subtracted using standard fixed-point hardware logic.
  4. Normalization:
    • Overflow: If mantissa addition creates a carry (E=1E=1), the sum is shifted right once, and exponent aa is incremented.
    • Underflow: If subtraction creates leading zeros (A1=0A_1 = 0), the mantissa is shifted left repeatedly while exponent aa is decremented until A1=1A_1 = 1.

Floating-Point Multiplication and Division

  • Multiplication: Check for zeros, add biased exponents and subtract the extra bias (a←a+b−biasa \leftarrow a + b - \text{bias}), multiply mantissas (A×BA \times B), and perform a single left shift to normalize if an underflow bit appears in A1A_1.
  • Division: Check for zeros (and handle divide-by-zero errors), perform dividend alignment (if A≥BA \ge B, shift AA right once and increment exponent aa to prevent mantissa divide overflow), subtract exponents and add bias (q←a−b+biasq \leftarrow a - b + \text{bias}), and divide mantissas to produce a normalized quotient in QRQR.

Decimal Arithmetic Unit

In application domains with heavy input-output activity (such as financial calculators or database systems), converting decimal inputs to binary and back introduces noticeable latency. For these machines, processing data directly in Binary-Coded Decimal (BCD) format using a dedicated decimal arithmetic unit is much more efficient.

BCD Adder Design

In BCD, decimal digits 00 through 99 are encoded using 4-bit binary values (0000 to 1001). When two BCD digits plus an input carry are added using a standard 4-bit binary adder, the output ranges from 00 to 1919.

  • Binary sums from 00 to 99 (0000 to 1001) are valid BCD digits and require no adjustment.
  • Binary sums from 1010 to 1919 (1010 to 10011) are non-valid BCD representation.

To correct non-valid sums, hardware must add binary 66 (0110) to the binary sum. This shifts the result across the six unused 4-bit combinations (1010 through 1111), producing the correct BCD digit output and generating a true output carry bit (CC).

The Boolean function that detects when a correction factor of +6+6 is required is: C=K+Z8Z4+Z8Z2C = K + Z_8 Z_4 + Z_8 Z_2 where KK is the raw binary carry-out, and Z8,Z4,Z2,Z1Z_8, Z_4, Z_2, Z_1 represent the outputs of the primary 4-bit binary adder. A complete BCD adder uses two cascaded 4-bit binary adders: the first calculates the binary sum, and the second conditionally adds 0110 whenever C=1C = 1.

BCD Subtractor Logic

To perform BCD subtraction, hardware adds the 9's or 10's complement of the subtrahend to the minuend.

The 9's complement of a 4-bit BCD digit BB can be generated using a combinational logic circuit controlled by a mode signal MM:

  • When M=0M = 0, the circuit passes BB unchanged.
  • When M=1M = 1, the circuit outputs the 9's complement logic expressions (x1,x2,x4,x8x_1, x_2, x_4, x_8).

Feeding M=1M = 1 and applying an initial input carry C1=1C_1 = 1 to the lowest-stage BCD adder automatically computes AA plus the 10's complement of BB, achieving seamless BCD subtraction.

Decimal Arithmetic Operations

Executing full-word multi-digit decimal operations requires specialized shift and register controls that manage data in 4-bit digit blocks.

Register Configurations and Decimal Shifts

Decimal arithmetic registers (A,B,QA, B, Q) are grouped into 4-bit nibbles. A decimal shift right (dshr A) or decimal shift left (dshl A) moves entire 4-bit digits simultaneously.

Multi-digit decimal addition can be implemented in three hardware configurations:

  1. Parallel Adder: Uses nn dedicated BCD adders to add all digits simultaneously in a single clock cycle.
  2. Digit-Serial Bit-Parallel Adder: Uses a single BCD adder shared across all digits, shifting decimal digits through the unit sequentially over nn clock cycles.
  3. All-Serial Adder: Shifts individual bits through a single full adder, applying correction logic after every 4 bits.

Decimal Multiplication Algorithm

Unlike binary multiplication where partial products are formed by simple bit checks (00 or 11), a decimal multiplier digit ranges from 00 to 99. In decimal multiplication:

  • The low-order multiplier digit in QLQ_L is tested.
  • If QL≠0Q_L \neq 0, multiplicand BB is added to register AA, and QLQ_L is decremented (QL←QL−1Q_L \leftarrow Q_L - 1). This loop repeats until QL=0Q_L = 0, effectively multiplying BB by digit QLQ_L.
  • Extended digit register AeA_e holds any temporary overflow digits (00 to 99).
  • Once QL=0Q_L = 0, register pair AQAQ is shifted right by one decimal digit (dshr AQ), bringing the next multiplier digit into QLQ_L. The loop repeats for all kk multiplier digits.

Decimal Division Algorithm

Decimal division implements restoring arithmetic across decimal digits:

  • The dividend in AQAQ is shifted left by one decimal digit (dshl AQ), moving its highest digit into AeA_e.
  • Divisor BB is repeatedly subtracted from AA using 10's complement addition, and quotient digit QLQ_L is incremented (QL←QL+1Q_L \leftarrow Q_L + 1) for each successful subtraction.
  • When subtraction yields a negative result (E=0E = 0), BB is added back to restore AA, QLQ_L is not incremented, and the restored partial remainder is shifted left to process the next quotient digit.

Summary

Computer arithmetic forms the foundational gateway between physical digital logic gates and complex abstract numerical processing. Whether optimizing integer operations via Booth's algorithm, maintaining precise scientific data using floating-point normalization pipelines, or processing decimal financial calculations directly with BCD adders, hardware arithmetic algorithms balance circuit complexity, execution speed, and numerical precision.

Key Takeaways

  • Signed-2's Complement Superiority: Signed-2's complement representation simplifies arithmetic unit design by enabling addition and subtraction across sign bits without magnitude comparators.
  • Optimized Multiplication: Booth's algorithm reduces multiplication steps for signed-2's complement numbers by skipping bit strings of 11s, while array multipliers trade hardware area for ultra-fast combinational multiplication.
  • Overflow Safeguards: Divide operations must enforce check mechanisms (A≥BA \ge B) to prevent register overflow and handle division-by-zero safely via system interrupts.
  • Floating-Point Mechanics: Floating-point engines align mantissas by shifting smaller exponents right, compute results, and dynamically re-normalize mantissas while adjusting biased exponents.
  • BCD Corrections: Native BCD decimal adders use primary binary adders and automatically add binary 66 (0110) whenever a 4-bit binary sum exceeds 99 (1001).