C721 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 21 · Division in hardware

Long division one bit at a time, dividing by constants without dividing, and the trouble with negative numbers

Lesson 20 built multipliers that finish in a few gate delays. Division is harder: each quotient bit depends on whether the previous subtraction went negative, so the work is inherently sequential, and a 32-bit divide takes tens of clock cycles even in fast processors. Hardware designers have three answers: do it one bit per cycle with a small circuit, avoid it by multiplying with a precomputed reciprocal when the divisor is a constant, and shift when it's a power of two. The last is where a classic bug lives: shifting a negative number right doesn't round the way division does.

  • Long division in binary, one bit per step. The remainder doubles and takes in the next dividend bit; if it's at least the divisor, the divisor is subtracted and the quotient bit is 1.
  • Drag the dividend and divisor and step through. The number of steps is the number of dividend bits, whatever the values.
  • The curve is the remainder after each step for the chosen values, which always stays below the divisor.

Predict

A C program computes −7 / 2 with integers. A compiler that "optimizes" it into an arithmetic right shift by one bit would get what?

Name it

Restoring division

To divide an n-bit N by D, keep a remainder R, starting at 0. For each bit of N from the top:

  1. R←2R+ (next bit of N)
  2. If R≥D: R←R−D and the quotient bit is 1; otherwise it's 0.

After n steps the quotient bits form Q=⌊N/D⌋ and R=N−QD. The hardware is a subtractor, a comparator (the subtractor's borrow out), a shift register and a counter: one quotient bit per cycle. It's called restoring because the simplest form subtracts first and adds D back when the result went negative.

Non-restoring division skips the add-back: after a negative result it adds D on the next step instead of subtracting, which saves time per step. Fast processors use SRT division, which produces two or more quotient bits per cycle from a small lookup table, and even then a 64-bit divide costs tens of cycles, ten or more times a multiply.

Dividing by a constant

If D is known in advance, multiply by a scaled reciprocal and shift:

⌊xD⌋=⌊x⋅M2s⌋,M=⌈2sD⌉

which is exact for every n-bit x when s is large enough: roughly n+log2⁡D. For D = 10 and 16-bit x, M = 52 429 and s = 19. Compilers do this automatically for division by constants; it turns a 30-cycle divide into a 3-cycle multiply.

Powers of two and negative numbers

Dividing by 2k is a right shift. For unsigned numbers it's exact. For signed numbers, an arithmetic shift rounds toward −∞, but C, Java and most hardware divide instructions truncate toward zero, with a remainder that takes the dividend's sign: −7/2=−3 remainder −1. Adding 2k−1 to a negative dividend before the shift gives the truncated result.

Worked example

Divide 13 (1101) by 3 (0011) with the restoring algorithm.

Show the solution
  1. Shift in one bit per step. Each step doubles the partial remainder R, adds the next bit of 1101, and subtracts 3 when it can:
StepR←2R + bitR≥3?R afterQuotient bit
10 + 1 = 1no10
22 + 1 = 3yes01
30 + 0 = 0no00
40 + 1 = 1no10
  1. Read the result. The quotient is 0100 = 4 and the remainder 1: 13=4×3+1
  2. Count the steps. Four steps for four bits, regardless of the values.

Answer the quotient is 4 (0100) and the remainder 1.

Worked example

Show that ⌊x/10⌋=⌊52429x/219⌋ for 16-bit x, by bounding the error.

Show the solution
  1. Write the multiplier as a fraction. 52429219=0.1000003815 so 52429x/219=x/10+ϵx with ϵ=3.815×10−7.
  2. Bound the excess. For x<65536, the excess ϵx is under 0.025.
  3. Compare with the fractions. The fractional part of x/10 is a multiple of 0.1, at most 0.9, so adding less than 0.025 can never push it past the next integer: the floors agree.
  4. Check. A program that checks all 65 536 values confirms it.
  5. Why it's useful. On a microcontroller without a divide instruction, that's one 16 × 16 multiply and a shift instead of a slow software loop.

Answer the error ϵx stays under 0.025, too small to carry a fraction of at most 0.9 past the next integer, so the two floors agree for every 16-bit x.

Worked example

A 32-bit restoring divider in a 500 MHz design takes one cycle per quotient bit plus two cycles of setup. How long is a divide, and how does that compare with a multiplier that finishes in 3 cycles? What would radix-4 SRT do?

Show the solution
  1. Divide time. 32+2=34 cycles at 2 ns: t=34×2ns=68ns
  2. Compare with the multiplier. Its 3 cycles take 6 ns, so the divide is more than ten times slower.
  3. Radix-4 SRT. It produces two quotient bits per cycle, so about 16+2=18 cycles, 36 ns.
  4. What compilers do. That's why compilers replace division by constants with multiplication, and why code in tight loops is written to multiply by reciprocals.

Answer 68 ns, more than ten times the multiplier's 6 ns; radix-4 SRT would take about 36 ns.

Check

A restoring divider computes 104 ÷ 4. What quotient does it produce?

Check

What remainder does the divider leave for 125 ÷ 7?

Check

A restoring divider produces one quotient bit per step. How many steps does a 16-bit by 16-bit division take, not counting setup?

Check

In C, what is −13 / 2 for signed integers?

Check

A compiler replaces y = x / 8 with y = x >> 3 for a signed int x. When does the result differ from C's division?

Check

Write divide(N, Dv) returning (q, r) by the restoring algorithm for n-bit unsigned N (12 bits), counting the steps in a global list steps (append 1 per quotient bit). Then find the smallest shift s (search from n upward) such that M = ceil(2**s / D) gives (x*M) >> s == x // D for every n-bit x, with D = 3, and set s_min and M_min.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why is division so much slower than multiplication in hardware, even though both are built from adders?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.