Lesson 39 +40 XP

Division Algorithms & Floating-Point Arithmetic Operations

Division Algorithms & Floating-Point Arithmetic Operations

Division is the reverse of multiplication: instead of adding and shifting right, we subtract and shift left. This lesson covers Mano's restoring division of signed-magnitude numbers, divide overflow, the faster non-restoring method, and then the four floating-point operations.

1. Restoring Division (Signed Magnitude)

By hand, binary division compares the divisor with the leading bits of the dividend: if the divisor fits, the quotient bit is 1 and the divisor is subtracted; if not, the quotient bit is 0. The hardware is the same as for multiplication (lesson 38): registers B, A, Q, the carry E and the counter SC.

  • The dividend (2n bits) is in A and Q; the divisor (n bits) is in B.
  • Each step shifts EAQ left and tries A - B (done as A + overline{B} + 1).
  • The carry E tells the result: E = 1 means A ≥q B (quotient bit 1), E = 0 means A < B, so B is added back ("restored") and the quotient bit is 0.
  • At the end, Q holds the quotient and A the remainder. The sign of the quotient is Qs = As oplus Bs; the remainder has the sign of the dividend.

Flowchart for the divide operationFlowchart for the divide operation

Divide Overflow

If the high half of the dividend (A) is already greater than or equal to the divisor B, the quotient needs more than n bits and cannot fit in Q. The algorithm checks this first by subtracting B from A: if E = 1 (A ≥q B), the divide-overflow flip-flop DVF is set and the operation stops. Dividing by zero is a special case of overflow, since every A is ≥q 0.

⚠️ Warning

To avoid overflow, the dividend's high half must be smaller than the divisor. Floating-point division avoids it by shifting the dividend's mantissa right first (Section 4).

SolvedExample 1 · 448 ÷ 17 with 5-bit registers (Mano)

Dividend A Q = 01110 00000 (448), divisor B = 10001 (17), so overline{B} + 1 = 01111. Check overflow: A − B = 01110 + 01111 = 0 11101, E = 0 so A < B and there is no overflow (A is restored).

StepOperationEAQSC
initial01110000005
1shl EAQ01110000000
add overline{B} + 1101011
E = 1: Qn ← 1101011000014
2shl EAQ01011000010
add overline{B} + 1100101
E = 1: Qn ← 1100101000113
3shl EAQ00101000110
add overline{B} + 1011001
E = 0: restore (add B), Qn = 0101010001102
4shl EAQ01010001100
add overline{B} + 1100011
E = 1: Qn ← 1100011011011
5shl EAQ00011011010
add overline{B} + 1010101
E = 0: restore, Qn = 0100110110100

Check: 17 × 26 + 6 = 448.

Answerquotient Q = 11010 (26), remainder A = 00110 (6)

The divider below makes one quotient bit per clock. Its "try A − B, keep it if E = 1 or there is no borrow" step is the flowchart's two branches combined:

VERILOG
// Restoring division of signed-magnitude numbers (Mano): the 2n-bit
// dividend is in A & Q, the n-bit divisor in B. One quotient bit per clock.
module divider #(parameter N = 5) (
    input clk,
    input start,
    input [N-1:0] b,           // divisor
    input [N-1:0] a_in,        // high half of dividend
    input [N-1:0] q_in,        // low half of dividend
    output reg [N-1:0] a,      // remainder at the end
    output reg [N-1:0] q,      // quotient at the end
    output reg [3:0] sc,
    output reg dvf             // divide overflow
);
    wire [N:0] shifted = {a, q[N-1]};      // E A after shl EAQ
    wire [N:0] diff = {1'b0, shifted[N-1:0]} + {1'b0, ~b} + 1'b1;
    always @(posedge clk)
        if (start) begin
            a <= a_in;
            q <= q_in;
            sc <= N;
            dvf <= (a_in >= b);            // quotient would need n+1 bits
        end else if (sc != 0 && !dvf) begin
            // shl EAQ, then try A - B: keep it if E = 1 or A >= B
            if (shifted[N] || diff[N]) begin
                a <= diff[N-1:0];
                q <= {q[N-2:0], 1'b1};     // Qn <- 1
            end else begin
                a <= shifted[N-1:0];       // restore: A unchanged
                q <= {q[N-2:0], 1'b0};     // Qn <- 0
            end
            sc <= sc - 1'b1;
        end
endmodule

module tb;
    reg clk = 0, start = 1;
    wire [4:0] a, q;
    wire [3:0] sc;
    wire dvf;
    // 448 / 17: dividend 01110 00000, divisor 10001
    divider u (clk, start, 5'b10001, 5'b01110, 5'b00000, a, q, sc, dvf);
    always #5 clk = ~clk;
    initial begin
        @(posedge clk);
        #1 start = 0;
        $display("initial  A=%b Q=%b SC=%0d DVF=%b", a, q, sc, dvf);
        while (sc != 0) begin
            @(posedge clk);
            #1 $display("Qn = %b   A=%b Q=%b SC=%0d", q[0], a, q, sc);
        end
        $display("quotient = %0d, remainder = %0d", q, a);
        $finish;
    end
endmodule

The A and Q columns match the table after each "Qn" decision:

OUTPUT
initial  A=01110 Q=00000 SC=5 DVF=0
Qn = 1   A=01011 Q=00001 SC=4
Qn = 1   A=00101 Q=00011 SC=3
Qn = 0   A=01010 Q=00110 SC=2
Qn = 1   A=00011 Q=01101 SC=1
Qn = 0   A=00110 Q=11010 SC=0
quotient = 26, remainder = 6

2. Non-Restoring Division

Restoring wastes an addition every time a subtraction fails. Non-restoring division skips the restore: if A went negative, the next step adds M instead of subtracting it. This works because restoring then shifting then subtracting gives 2(A + M) - M = 2A + M, the same as shifting the negative A and adding M.

For an n-bit dividend Q and divisor M, with A one bit wider (to hold a sign):

  1. Clear A, then repeat the next two steps n times.
  2. Shift A Q left one place. If A was ≥q 0, do A ← A - M; otherwise do A ← A + M.
  3. Look at the new A: if it is ≥q 0, set q0 = 1, otherwise set q0 = 0.
  4. At the end, if A < 0, add M to A once to correct the remainder.
SolvedExample 2 · 11 ÷ 3 by restoring and non-restoring division

Q = 1011, M = 00011, −M = 11101 (A has 5 bits).

Restoring:

Stepshl A QA − MResultAQ
100001 011_11110 (< 0)restore, q0 = 0000010110
200010 110_11111 (< 0)restore, q0 = 0000101100
300101 100_00010 (≥ 0)q0 = 1000101001
400101 001_00010 (≥ 0)q0 = 1000100011

Non-restoring:

Stepshl A QOperationNew Aq0Q
100001 011_A ≥ 0: − M1111000110
211100 110_A < 0: + M1111101100
311111 100_A < 0: + M0001011001
400101 001_A ≥ 0: − M0001010011

A is not negative at the end, so no correction is needed. Both give the same result; non-restoring used 4 additions, restoring used 6.

Answerquotient 0011 (3), remainder 00010 (2)

The non-restoring divider checks Example 2, choosing +M or −M from the sign of A:

VERILOG
// Non-restoring division of unsigned numbers: A is one bit wider than Q
// so that it can hold a negative partial remainder.
module nonrestoring #(parameter N = 4) (
    input clk,
    input start,
    input [N-1:0] dividend,
    input [N-1:0] divisor,
    output reg [N:0] a,
    output reg [N-1:0] q,
    output reg [3:0] sc
);
    wire [N:0] m = {1'b0, divisor};
    wire [N:0] sh = {a[N-1:0], q[N-1]};    // shl A & Q
    wire [N:0] next = a[N] ? sh + m : sh - m;
    always @(posedge clk)
        if (start) begin
            a <= 0;
            q <= dividend;
            sc <= N;
        end else if (sc != 0) begin
            a <= next;
            q <= {q[N-2:0], ~next[N]};     // q0 = 1 if A >= 0
            sc <= sc - 1'b1;
        end else if (a[N])
            a <= a + m;                    // final correction of remainder
endmodule

module tb;
    reg clk = 0, start = 1;
    wire [4:0] a;
    wire [3:0] q, sc;
    nonrestoring u (clk, start, 4'd11, 4'd3, a, q, sc);
    always #5 clk = ~clk;
    initial begin
        @(posedge clk);
        #1 start = 0;
        $display("initial      A=%b Q=%b", a, q);
        while (sc != 0) begin
            $write("%s ", a[4] ? "A<0:  +M" : "A>=0: -M");
            @(posedge clk);
            #1 $display("A=%b Q=%b", a, q);
        end
        @(posedge clk);
        #1 $display("quotient = %0d, remainder = %0d", q, a);
        $finish;
    end
endmodule

Each line shows the operation chosen and the A and Q that result, as in the non-restoring table:

OUTPUT
initial      A=00000 Q=1011
A>=0: -M A=11110 Q=0110
A<0:  +M A=11111 Q=1100
A<0:  +M A=00010 Q=1001
A>=0: -M A=00010 Q=0011
quotient = 3, remainder = 2
RestoringNon-restoring
After a failed subtractionadds M back (restore)no restore; next step adds M
Operations per step1 or 2 (subtract, maybe add)exactly 1 (add or subtract)
Final correctionnoneadd M if the remainder is negative
Speedslowerfaster

3. Floating-Point Representation (Review)

A floating-point number is m × re: a signed mantissa (fraction) m and an exponent e (lesson 2 covers IEEE 754). The mantissa is normalized when its most significant digit is non-zero: in binary, 0.1xxx (Mano's fraction form) or 1.xxx (IEEE 754).

Arithmetic on floating-point numbers is done on the two parts separately: an exponent unit adds, subtracts and compares exponents, and the mantissa unit is an ordinary fixed-point unit. Exponents are often stored biased (a constant added so they are never negative), which makes them easy to compare.

4. Floating-Point Addition and Subtraction

Steps of floating-point addition and subtractionSteps of floating-point addition and subtraction

The mantissas can only be added when the exponents are equal, so the smaller number is shifted right first:

  1. Check for zeros. If one operand is 0 the result is the other.
  2. Align the mantissas. Shift the mantissa with the smaller exponent right, adding 1 to its exponent each time, until the exponents are equal.
  3. Add or subtract the mantissas (signed-magnitude rules, lesson 37).
  4. Normalize. If the sum overflowed (1x.xx), shift right and add 1 to the exponent. If it has leading 0s (after a subtraction), shift left and subtract 1 from the exponent until it is normalized. Check for exponent overflow or underflow.
SolvedExample 3 · Decimal addition with alignment (Mano)

Add .5372400 × 102 and .1580000 × 10-1.

  1. Exponents differ by 2 - (-1) = 3, so shift the second mantissa right 3 places: .0001580 × 102.
  2. Add: .5372400 + .0001580 = .5373980.
  3. The result is already normalized.
Answer.5373980 × 102
SolvedExample 4 · Binary addition and subtraction

Using 4-bit fraction mantissas:

(a) (0.1101 × 23) + (0.1000 × 21), that is 6.5 + 1:

  1. Align: exponent difference 2, so 0.1000 × 21 = 0.0010 × 23.
  2. Add: 0.1101 + 0.0010 = 0.1111, so the result is 0.1111 × 23 = 7.5. Normalized.

(b) (0.1010 × 22) - (0.1001 × 22), that is 2.5 − 2.25:

  1. Exponents equal; subtract: 0.1010 - 0.1001 = 0.0001.
  2. Normalize: three leading 0s, so shift left 3 and subtract 3 from the exponent: 0.1000 × 2-1 = 0.25.
Answer(a) 0.1111 × 23 = 7.5; (b) 0.1000 × 2-1 = 0.25

5. Floating-Point Multiplication and Division

No alignment is needed for multiplication or division:

  • Multiply: add the exponents, multiply the mantissas, set the sign to As oplus Bs, then normalize. With biased exponents, the bias is counted twice in the sum, so subtract it once: e = e1 + e2 - bias.
  • Divide: subtract the exponents, divide the mantissas, then normalize. With biased exponents add the bias back: e = e1 - e2 + bias. To avoid divide overflow, the dividend mantissa is first made smaller than the divisor mantissa (shift it right and add 1 to its exponent).
SolvedExample 5 · Binary floating-point multiply and divide

(a) (0.1100 × 22) × (0.1010 × 21), that is 3 × 1.25:

  1. Exponent: 2 + 1 = 3.
  2. Mantissas: 0.1100 × 0.1010 = 0.01111000.
  3. Normalize: one leading 0, so shift left 1 and subtract 1: 0.1111 × 22 = 3.75.

(b) (0.1100 × 23) ÷ (0.1000 × 21), that is 6 ÷ 1:

  1. The dividend mantissa 0.1100 ≥ divisor mantissa 0.1000, so shift it right: 0.0110 × 24.
  2. Exponent: 4 − 1 = 3. Mantissas: 0.0110 ÷ 0.1000 = 0.1100.
  3. Result 0.1100 × 23 = 6. Normalized.

(c) In IEEE 754 single precision, 2.0 and 3.0 have biased exponents 128 and 128 (bias 127). The product's exponent is 128 + 128 − 127 = 129, that is 22, and 1.0 × 1.12 = 1.12, so the product is 1.12 × 22 = 6.

Answer(a) 0.1111 × 22; (b) 0.1100 × 23; (c) biased exponent 129
OperationExponentMantissaBeforeAfter
Add / subtractlarger exponent keptadd or subtractalign (shift smaller right)normalize
Multiplye1 + e2 (− bias)multiplycheck for zeronormalize
Dividee1 - e2 (+ bias)dividedividend mantissa < divisor mantissanormalize
💡 Tip

Key points to remember

  • Restoring division: shl EAQ, A + B' + 1; E = 1 keeps it with Qn = 1; E = 0 restores A.
  • Divide overflow when A ≥ B at the start; it sets DVF.
  • Non-restoring: A ≥ 0 subtract, A < 0 add; one operation per step; correct a negative remainder at the end.
  • Floating add/subtract needs alignment; multiply and divide do not. All four end with normalization.
  • Biased exponents: subtract the bias after adding, add it after subtracting.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Draw the flowchart for division of signed-magnitude numbers. Divide 448 by 17 showing each step. (10 Marks)
  • Write: the flowchart, the overflow check, and the E, A, Q, SC table.
  • Q2. What is divide overflow? How is it detected? (5 Marks)
  • Q3. Compare restoring and non-restoring division. Divide 11 by 3 using non-restoring division. (10 Marks)
  • Q4. Explain the steps of floating-point addition and subtraction with an example. (5 Marks)
  • Q5. How are floating-point multiplication and division performed? Why is the bias subtracted in multiplication? (5 Marks)