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

C8 · Lesson 20 · Multiplication, division and fixed point

Arithmetic with whole numbers that stands in for fractions

A sensor reading arrives as a 12-bit number from 0 to 4095, and the display needs millivolts. A digital filter needs to multiply each sample by 0.7. Many small processors have no floating-point hardware, and the ones that do often avoid it in interrupt handlers, so this arithmetic is done with integers: multiply, then shift. RISC-V's M extension adds the multiply and divide instructions, and using them well means knowing how wide a product gets, which way division rounds, and how to keep track of a binary point that the hardware knows nothing about.

  • The program scales a 12-bit ADC reading to millivolts, multiplies 0.5 by 0.75 in a 16-bit fraction format, and divides −7 by 2 two different ways.
  • Step it. a1 ends at 2199 mV: 2730 × 3300 / 4096 with the fraction dropped. a2 ends at 12288, which is 0.375 × 32768.
  • Compare a3 and a5: div gives −3, rounding toward zero, while shifting right by one gives −4, rounding down.

Predict

Two 32-bit numbers are multiplied. How many bits can the full product need?

Name it

Multiply and divide

InstructionResult
mul rd, a, bthe low 32 bits of a × b (the same for signed and unsigned)
mulh, mulhu, mulhsuthe high 32 bits, for signed × signed, unsigned × unsigned, signed × unsigned
div, divuthe quotient, rounded toward zero
rem, remuthe remainder, with the sign of the dividend

so that a=b⋅div(a,b)+rem(a,b) always holds: −7 = 2 × (−3) + (−1). Division by zero doesn't trap: the quotient is all ones and the remainder is the dividend, and the program has to check for zero itself if it matters. Dividing is slow on most processors (often 10 to 40 cycles against one for a multiply), so compilers turn division by a constant into a multiply and a shift.

Shifting instead. srai x, x, k divides by 2k rounding down (toward −∞), so for negative numbers it differs from div whenever there's a remainder. slli multiplies by 2k.

Fixed point

A fixed-point number is an integer with an agreed binary point. In Qm.n format there are n fraction bits, and the stored integer k means

x=k2n

The common Q15 format packs a signed fraction into 16 bits: k=round(x⋅215), covering −1 to 1−2−15 in steps of 2−15≈3.05×10−5.

  • Adding two numbers with the same n is plain integer addition.
  • Multiplying adds the fraction bits: Q15 × Q15 has 30 fraction bits, so shift right by 15 to get back to Q15. The 16-bit inputs make a product that fits easily in 32 bits.
  • Rounding: adding 2k−1 before shifting right by k rounds to nearest instead of down.

Scaling a reading

An N-bit converter with reference Vref maps code c to c⋅Vref/2N. In millivolts that's one multiply and one shift, as long as c⋅Vref fits in 32 bits.

Worked example

Represent 0.7 in Q15 and multiply it by 0.5.

Show the solution
  1. Represent 0.7. 0.7×32768=22937.6 which rounds to 22938.
  2. Represent 0.5. 0.5 is 16384.
  3. Multiply. The product has 30 fraction bits: 22938×16384=375,816,192
  4. Shift back to Q15. Shifting right by 15 gives 11469: 1146932768=0.350006
  5. The error. The 6 millionths come from rounding 0.7.

Answer 0.7 is 22938 in Q15, and the product is 11469, which is 0.350006.

Worked example

A 12-bit ADC with a 3.3 V reference reads 2730. Convert it to millivolts with integer arithmetic.

Show the solution
  1. Multiply first. 2730×3300=9,009,000 which fits in 32 bits easily (the largest possible, 4095×3300, is about 13.5 million).
  2. Divide by 4096. Shifting right by 12 divides by 4096: 2199 mV.
  3. Check. The exact value is 2199.46 mV, so dropping the fraction costs less than one millivolt.

Answer 2199 mV.

Worked example

Divide a 16-bit unsigned x by 10 without a divide instruction.

Show the solution
  1. Scale the reciprocal. 21910=52428.8 rounded up to 52429 (0xCCCD).
  2. Multiply and shift. x10≈(x×52429)≫19
  3. Try x = 1234. The product is 64,697,386, and shifting by 19 gives 123.
  4. Check the error. The rounding error is 0.2/219 per unit of x, too small to change any result for x<216; a check over all 65,536 values confirms it.
  5. Where it's used. Compilers do exactly this for division by a constant, because a multiply takes a few cycles and a divide dozens.

Answer multiply by 52429 and shift right by 19, which is exact for every 16-bit x; for 1234 it gives 123.

Check

What integer represents 0.25 in Q15?

Check

Two Q15 numbers, 16384 and 6554, are multiplied with mul and the product shifted right by 15 (rounding down). What integer results?

Check

A 12-bit ADC with a 2500 mV reference reads 1534. What does mul by 2500 followed by srli by 12 give, in millivolts?

Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.

Check

What does div give for −23 ÷ 2?

Check

What does rem give for −23 and 2?

Check

Compute the dot product of the three-word vectors p and q (the sum of p[i] × q[i]) and leave it in a0.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

a0 holds a 12-bit ADC code; the reference is 3300 mV. Put the reading in millivolts in a1, rounded down.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

Why do compilers replace x / 8 for a signed int with more than a single srai x, x, 3?

Check

Multiply 0.45 by 0.25 in Q15.

(a)

What integer stores 0.45?

(b)

What Q15 integer does the product, shifted right 15, give?

Check

A fixed-point number with n fraction bits stores the value x as the integer X=x2n.

Step 1

What integer stores x?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does multiplying two Q15 numbers need a shift afterward, while adding them doesn't?

Practice

Keep practicing

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