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:
divgives −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?
- An n-bit number times an m-bit number needs up to n + m bits.
mulkeeps the low 32 bits of the product;mulh(signed) andmulhu(unsigned) return the high 32, so a pair of instructions gets the whole thing.
Name it
Multiply and divide
| Instruction | Result |
|---|---|
mul rd, a, b | the low 32 bits of a × b (the same for signed and unsigned) |
mulh, mulhu, mulhsu | the high 32 bits, for signed × signed, unsigned × unsigned, signed × unsigned |
div, divu | the quotient, rounded toward zero |
rem, remu | the remainder, with the sign of the dividend |
so that 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 rounding down (toward −∞), so for negative numbers it differs from div whenever there's a remainder. slli multiplies by .
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
The common Q15 format packs a signed fraction into 16 bits: , covering −1 to in steps of .
- 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 before shifting right by k rounds to nearest instead of down.
Scaling a reading
An N-bit converter with reference maps code c to . In millivolts that's one multiply and one shift, as long as fits in 32 bits.
Worked example
Represent 0.7 in Q15 and multiply it by 0.5.
Show the solution
- Represent 0.7. which rounds to 22938.
- Represent 0.5. 0.5 is 16384.
- Multiply. The product has 30 fraction bits:
- Shift back to Q15. Shifting right by 15 gives 11469:
- 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
- Multiply first. which fits in 32 bits easily (the largest possible, , is about 13.5 million).
- Divide by 4096. Shifting right by 12 divides by 4096: 2199 mV.
- 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
- Scale the reciprocal. rounded up to 52429 (0xCCCD).
- Multiply and shift.
- Try x = 1234. The product is 64,697,386, and shifting by 19 gives 123.
- Check the error. The rounding error is per unit of x, too small to change any result for ; a check over all 65,536 values confirms it.
- 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 .
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°.
Step 2
What's the integer product XY?
Step 3
What is XY shifted right by n, the stored form of xy?
Explain why
Why does multiplying two Q15 numbers need a shift afterward, while adding them doesn't?
A Q15 integer is the real value times . Adding two of them gives , which is already the Q15 form of the sum. Multiplying gives : the scale factors multiply too, so the product has 30 fraction bits. Shifting right by 15 divides by and puts it back on the Q15 scale, throwing away the lowest 15 bits of precision, which is why rounding before the shift matters.
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.