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 and , 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 () and attach the sign of . But when adding numbers with different signs (or subtracting numbers with identical signs), we must compare magnitudes:
- If , subtract from () and keep the sign of .
- If , subtract from () and reverse the sign of .
- If , subtract from to yield zero, forcing the sign to positive to avoid a negative zero ().
Hardware Implementation for Signed-Magnitude
To construct this in hardware, we use registers and , alongside sign flip-flops and . 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 plus the 2's complement of ().
- A mode control signal directs the complementer ( passes directly for addition; feeds the 1's complement of and sets the input carry to 1 for subtraction).
- The carry output is stored in flip-flop . If , then , and contains the correct result. If , then , meaning holds the 2's complement of the true magnitude; taking the 2's complement of restores the correct value, and complementing sign bit yields the proper sign.
- Overflow during addition is captured in the add-overflow flip-flop .
Signed-2's Complement Addition and Subtraction
In signed-2's complement, the leftmost bit serves directly as the sign ( for positive, for negative). This layout simplifies hardware dramatically:
- Addition: Add and 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 () by adding its 1's complement plus 1, then add to .
- Overflow Detection (): 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 ().
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 -bit signed-magnitude numbers, hardware uses three registers (, , ), a sequence counter (), and sign logic:
- The multiplicand is stored in register , the multiplier in , and accumulator is initialized to zero.
- Sign logic sets .
- The sequence counter is set to (the number of magnitude bits).
- For each step, hardware inspects the least significant bit of the multiplier ():
- If , the multiplicand is added to ().
- If , remains unchanged.
- The combined register bit string is shifted right by one position (
shr EAQ), pushing the lowest bit of into and bringing into . - is decremented by 1. This loop continues until , leaving the final double-length product in combined registers and .
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 s in a multiplier ranging from bit weight down to can be evaluated as .
Booth's algorithm inspects two adjacent bits of the multiplier ( and an appended extra flip-flop ):
- 10: First 1 of a string encountered Subtract multiplicand ().
- 01: First 0 after a string encountered Add multiplicand ().
- 00 or 11: Inside a string of 0s or 1s No arithmetic operation required.
- Following the step, an arithmetic shift right (
ashr AC & QR) shifts and right while preserving the sign bit of .
Combinational Array Multipliers
While sequential multiply algorithms require clock cycles, an array multiplier uses a pure combinational circuit to calculate all partial product bits simultaneously. For multiplier bits and multiplicand bits, the array requires AND gates and -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 or , 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 (, , , , and ):
- The double-length dividend is loaded into combined registers and , while the divisor resides in .
- Rather than shifting the divisor right, hardware shifts register pair left by one bit (
shl EAQ). - The divisor is subtracted from by adding its 2's complement ().
- If carry flip-flop , then . The subtraction is valid, and quotient bit is set to .
- If carry flip-flop , then . The result is negative, quotient bit remains , and is restored by adding back ().
- This sequence repeats for counter steps until , leaving the quotient in and the final remainder in .
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 () is greater than or equal to the divisor ().
If , the system sets a divide-overflow flag (). 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 by raising an interrupt request, suspending execution so an error handler or subroutine can rescale the data.
Alternative Division Methods
- Restoring Method: The standard approach described above, where negative trial subtractions are canceled out by immediately adding back the divisor .
- Comparison Method: Compares and using adder look-ahead carry logic prior to subtraction, performing the subtraction only if .
- Nonrestoring Method: Avoids restoring additions. If a trial subtraction yields a negative partial remainder (), the negative remainder is shifted left () and is added in the next cycle, since .
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 and an exponent scaled by radix ().
Biased Exponents and Normalization
To simplify hardware floating-point math:
- Normalization: A floating-point number is normalized when the most significant bit () of its fractional mantissa is non-zero ( in binary). This ensures maximum bit accuracy.
- Biased Exponent: Exponents are stored as positive numbers by adding a fixed constant 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:
- Zero Check: If either operand is zero, the non-zero value is passed immediately as the result.
- Mantissa Alignment: Exponents and are compared. The mantissa associated with the smaller exponent is shifted right while its exponent is incremented until both exponents match.
- Mantissa Addition/Subtraction: The aligned mantissas are added or subtracted using standard fixed-point hardware logic.
- Normalization:
- Overflow: If mantissa addition creates a carry (), the sum is shifted right once, and exponent is incremented.
- Underflow: If subtraction creates leading zeros (), the mantissa is shifted left repeatedly while exponent is decremented until .
Floating-Point Multiplication and Division
- Multiplication: Check for zeros, add biased exponents and subtract the extra bias (), multiply mantissas (), and perform a single left shift to normalize if an underflow bit appears in .
- Division: Check for zeros (and handle divide-by-zero errors), perform dividend alignment (if , shift right once and increment exponent to prevent mantissa divide overflow), subtract exponents and add bias (), and divide mantissas to produce a normalized quotient in .
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 through 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 to .
- Binary sums from to (
0000to1001) are valid BCD digits and require no adjustment. - Binary sums from to (
1010to10011) are non-valid BCD representation.
To correct non-valid sums, hardware must add binary (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 ().
The Boolean function that detects when a correction factor of is required is: where is the raw binary carry-out, and 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 .
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 can be generated using a combinational logic circuit controlled by a mode signal :
- When , the circuit passes unchanged.
- When , the circuit outputs the 9's complement logic expressions ().
Feeding and applying an initial input carry to the lowest-stage BCD adder automatically computes plus the 10's complement of , 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 () 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:
- Parallel Adder: Uses dedicated BCD adders to add all digits simultaneously in a single clock cycle.
- Digit-Serial Bit-Parallel Adder: Uses a single BCD adder shared across all digits, shifting decimal digits through the unit sequentially over clock cycles.
- 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 ( or ), a decimal multiplier digit ranges from to . In decimal multiplication:
- The low-order multiplier digit in is tested.
- If , multiplicand is added to register , and is decremented (). This loop repeats until , effectively multiplying by digit .
- Extended digit register holds any temporary overflow digits ( to ).
- Once , register pair is shifted right by one decimal digit (
dshr AQ), bringing the next multiplier digit into . The loop repeats for all multiplier digits.
Decimal Division Algorithm
Decimal division implements restoring arithmetic across decimal digits:
- The dividend in is shifted left by one decimal digit (
dshl AQ), moving its highest digit into . - Divisor is repeatedly subtracted from using 10's complement addition, and quotient digit is incremented () for each successful subtraction.
- When subtraction yields a negative result (), is added back to restore , 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 s, while array multipliers trade hardware area for ultra-fast combinational multiplication.
- Overflow Safeguards: Divide operations must enforce check mechanisms () 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 (
0110) whenever a 4-bit binary sum exceeds (1001).