Loading lessons...
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 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.
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).
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).
| Step | Operation | E | A | Q | SC |
|---|---|---|---|---|---|
| initial | 01110 | 00000 | 5 | ||
| 1 | shl EAQ | 0 | 11100 | 00000 | |
| add overline{B} + 1 | 1 | 01011 | |||
| E = 1: Qn ← 1 | 1 | 01011 | 00001 | 4 | |
| 2 | shl EAQ | 0 | 10110 | 00010 | |
| add overline{B} + 1 | 1 | 00101 | |||
| E = 1: Qn ← 1 | 1 | 00101 | 00011 | 3 | |
| 3 | shl EAQ | 0 | 01010 | 00110 | |
| add overline{B} + 1 | 0 | 11001 | |||
| E = 0: restore (add B), Qn = 0 | 1 | 01010 | 00110 | 2 | |
| 4 | shl EAQ | 0 | 10100 | 01100 | |
| add overline{B} + 1 | 1 | 00011 | |||
| E = 1: Qn ← 1 | 1 | 00011 | 01101 | 1 | |
| 5 | shl EAQ | 0 | 00110 | 11010 | |
| add overline{B} + 1 | 0 | 10101 | |||
| E = 0: restore, Qn = 0 | 1 | 00110 | 11010 | 0 |
Check: 17 × 26 + 6 = 448.
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:
// 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
endmoduleThe A and Q columns match the table after each "Qn" decision:
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 = 62. 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):
- Clear A, then repeat the next two steps n times.
- Shift A Q left one place. If A was ≥q 0, do A ← A - M; otherwise do A ← A + M.
- Look at the new A: if it is ≥q 0, set q0 = 1, otherwise set q0 = 0.
- At the end, if A < 0, add M to A once to correct the remainder.
Q = 1011, M = 00011, −M = 11101 (A has 5 bits).
Restoring:
| Step | shl A Q | A − M | Result | A | Q |
|---|---|---|---|---|---|
| 1 | 00001 011_ | 11110 (< 0) | restore, q0 = 0 | 00001 | 0110 |
| 2 | 00010 110_ | 11111 (< 0) | restore, q0 = 0 | 00010 | 1100 |
| 3 | 00101 100_ | 00010 (≥ 0) | q0 = 1 | 00010 | 1001 |
| 4 | 00101 001_ | 00010 (≥ 0) | q0 = 1 | 00010 | 0011 |
Non-restoring:
| Step | shl A Q | Operation | New A | q0 | Q |
|---|---|---|---|---|---|
| 1 | 00001 011_ | A ≥ 0: − M | 11110 | 0 | 0110 |
| 2 | 11100 110_ | A < 0: + M | 11111 | 0 | 1100 |
| 3 | 11111 100_ | A < 0: + M | 00010 | 1 | 1001 |
| 4 | 00101 001_ | A ≥ 0: − M | 00010 | 1 | 0011 |
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.
The non-restoring divider checks Example 2, choosing +M or −M from the sign of A:
// 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
endmoduleEach line shows the operation chosen and the A and Q that result, as in the non-restoring table:
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| Restoring | Non-restoring | |
|---|---|---|
| After a failed subtraction | adds M back (restore) | no restore; next step adds M |
| Operations per step | 1 or 2 (subtract, maybe add) | exactly 1 (add or subtract) |
| Final correction | none | add M if the remainder is negative |
| Speed | slower | faster |
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 subtraction
The mantissas can only be added when the exponents are equal, so the smaller number is shifted right first:
- Check for zeros. If one operand is 0 the result is the other.
- Align the mantissas. Shift the mantissa with the smaller exponent right, adding 1 to its exponent each time, until the exponents are equal.
- Add or subtract the mantissas (signed-magnitude rules, lesson 37).
- 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.
Add .5372400 × 102 and .1580000 × 10-1.
- Exponents differ by 2 - (-1) = 3, so shift the second mantissa right 3 places: .0001580 × 102.
- Add: .5372400 + .0001580 = .5373980.
- The result is already normalized.
Using 4-bit fraction mantissas:
(a) (0.1101 × 23) + (0.1000 × 21), that is 6.5 + 1:
- Align: exponent difference 2, so 0.1000 × 21 = 0.0010 × 23.
- 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:
- Exponents equal; subtract: 0.1010 - 0.1001 = 0.0001.
- Normalize: three leading 0s, so shift left 3 and subtract 3 from the exponent: 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).
(a) (0.1100 × 22) × (0.1010 × 21), that is 3 × 1.25:
- Exponent: 2 + 1 = 3.
- Mantissas: 0.1100 × 0.1010 = 0.01111000.
- 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:
- The dividend mantissa 0.1100 ≥ divisor mantissa 0.1000, so shift it right: 0.0110 × 24.
- Exponent: 4 − 1 = 3. Mantissas: 0.0110 ÷ 0.1000 = 0.1100.
- 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.
| Operation | Exponent | Mantissa | Before | After |
|---|---|---|---|---|
| Add / subtract | larger exponent kept | add or subtract | align (shift smaller right) | normalize |
| Multiply | e1 + e2 (− bias) | multiply | check for zero | normalize |
| Divide | e1 - e2 (+ bias) | divide | dividend mantissa < divisor mantissa | normalize |
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.
📝 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)