C8 · Lesson 3 · Overflow, comparisons and big numbers
Carries, overflow and comparisons without a flags register
Many processors keep a flags register: after every addition, hardware records whether it carried, overflowed or came out negative, and the next instruction can test those bits. RISC-V has no flags at all. An add just wraps around modulo and says nothing. So how does a RISC-V program add two 64-bit numbers, notice that a signed sum has overflowed, or tell whether 0xFFFFFFFF is the largest number or −1?
- This program adds two 64-bit numbers held as pairs of 32-bit words: = 0x1_FFFFFFF0 in a1:a0 and = 0x2_00000020 in a3:a2.
- Step it. The low words' sum wraps to 0x10, and
sltunotices: a sum smaller than one of its addends means a carry. - The high words add with that carry, giving 4: the full sum is 0x4_00000010.
Predict
t0 holds 0xFFFFFFFF and t1 holds 1. What do slt t2, t0, t1 and sltu t3, t0, t1 leave?
t2 = 1 and t3 = 0. The same 32 bits are −1 to slt, which compares them as signed numbers, and 4,294,967,295 to sltu, which compares them as unsigned. The register doesn't know which it holds; the instruction decides.
Name it
The same bits, two meanings
32 bits hold 0 to as an unsigned number, or to in two's complement. add, sub and the loads and stores don't care which: the bits come out the same either way. Only comparisons, right shifts, widening loads and division need to know, so they come in pairs: slt/sltu, blt/bltu, bge/bgeu, sra/srl, lb/lbu, div/divu.
Carries and borrows
An unsigned sum wrapped exactly when (unsigned), so
is one sltu. A subtraction borrows when . These are what let a 32-bit machine do multiword arithmetic: add the low words, compute the carry, add the high words and the carry. Four instructions for a 64-bit add, and the same pattern extends to any length.
Signed overflow
A signed sum overflows when both operands have the same sign and the result's sign differs: two large positives can't add to a negative. The RISC-V manual's check for add t0, t1, t2 is
add t0, t1, t2
slti t3, t2, 0 # is the second operand negative?
slt t4, t0, t1 # is the sum below the first operand?
bne t3, t4, overflow # they must agree unless it overflowed
Adding a negative number must make the result smaller, and adding a non-negative one mustn't; overflow is exactly when that fails. When one operand is a known positive constant, a single blt does.
Wide products
mul gives the low 32 bits of a product. mulhu gives the high 32 bits of the unsigned 64-bit product, and mulh those of the signed one, so a pair of instructions gets the whole product.
The bounds check in one branch
To test for a signed index i, compare unsigned: bgeu a0, t0, out. A negative i looks like a huge unsigned number, so it fails the same test as one that's too large.
Worked example
Add 0x7FFFFFFF and 1 as signed numbers. What does add give, and how does the check catch the overflow?
Show the solution
- The sum.
addgives 0x80000000, which as a signed number is −2,147,483,648: two positives produced a negative. - The first test.
sltion the second operand (1) gives 0. - The second test.
sltasks whether the sum (−2,147,483,648) is below the first operand (2,147,483,647): yes, 1. - The branch. They disagree, so the branch goes to the overflow handler.
- As unsigned numbers. The same addition is perfectly fine: 2,147,483,647 + 1 = 2,147,483,648, and
sltufinds no carry.
Answer add gives 0x80000000, which is −2,147,483,648; slti gives 0 and slt gives 1, and because they disagree the branch goes to the overflow handler.
Worked example
Subtract the 64-bit numbers 0x00000003_00000005 − 0x00000001_00000009 with 32-bit instructions.
Show the solution
- Low words first. 5 − 9 wraps to 0xFFFFFFFC, and
sltufinds 5 < 9, a borrow of 1. - High words, less the borrow.
- Join the halves. The result is 0x00000001_FFFFFFFC.
- Check in decimal. which is 0x1_FFFFFFFC.
Answer 0x00000001_FFFFFFFC, which is 8,589,934,588.
Worked example
a0 holds a signed index that must satisfy 0 ≤ i < 100. Check it with one branch.
Show the solution
- Compare as unsigned.
li t0, 100thenbgeu a0, t0, out. - A valid index. Any i from 0 to 99 is below 100 as an unsigned number, so it falls through.
- Too large. An index of 100 or more is caught as usual.
- Negative. A negative one such as −3 reads as 0xFFFFFFFD = 4,294,967,293 unsigned, far above 100, so it's caught too.
- Where it's used. Compilers use this trick for array bounds and for
switchjump tables.
Answer li t0, 100 and then bgeu a0, t0, out: the one unsigned branch catches both an index of 100 or more and a negative one.
Check
a1:a0 and a3:a2 hold two 64-bit numbers (high word first). Put their sum in a5:a4.
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
Two unsigned 32-bit numbers, 3 × 10⁹ and 1.5 × 10⁹, are added.
(a)
What does add leave, as an unsigned number?
(b)
What carry does sltu find (1 or 0)?
Check
An n-bit register, n = 12.
(a)
What's the most negative two's-complement value?
(b)
What's the largest unsigned value?
Check
a0 holds a signed number and a1 a positive one. Put their sum in a2, saturated: if the addition overflows, a2 gets the largest signed value, 0x7FFFFFFF, instead.
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 −100 and t0 holds 10.
(a)
What unsigned value does bgeu see in a0?
(b)
Does bgeu a0, t0, out branch (1 for yes)?
Check
A 64-bit number is stored as a high word h and a low word l; a second as m and k.
Step 1
What value does the first represent?
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 carry out of the low words' sum?
Step 3
What's the high word of the sum, before it wraps?
Check
Which pair of instructions gives the full 64-bit product of two unsigned 32-bit numbers?
Check
What does mulhu give for 2596069104 × 3?
Explain why
Why do RISC-V's add and sub need no signed and unsigned versions, when its comparisons do?
Two's complement was chosen so that adding and subtracting the bit patterns gives the right bits whether you read them as signed or unsigned: both are arithmetic modulo , and only the meaning of the top bit differs. Comparison is different, because the order isn't the same: 0xFFFFFFFF is the largest unsigned number but −1 as a signed one, so "less than" gives different answers. The same goes for anything that widens or shrinks a number (right shifts, byte loads, division), which has to know whether to copy the sign bit.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.