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

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 232 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: x = 0x1_FFFFFFF0 in a1:a0 and y = 0x2_00000020 in a3:a2.
  • Step it. The low words' sum wraps to 0x10, and sltu notices: 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?

Name it

The same bits, two meanings

32 bits hold 0 to 232−1 as an unsigned number, or −231 to 231−1 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 s=a+b wrapped exactly when s<a (unsigned), so

carry=(s<ua)

is one sltu. A subtraction a−b borrows when a<ub. 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 0≤i<n 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
  1. The sum. add gives 0x80000000, which as a signed number is −2,147,483,648: two positives produced a negative.
  2. The first test. slti on the second operand (1) gives 0.
  3. The second test. slt asks whether the sum (−2,147,483,648) is below the first operand (2,147,483,647): yes, 1.
  4. The branch. They disagree, so the branch goes to the overflow handler.
  5. As unsigned numbers. The same addition is perfectly fine: 2,147,483,647 + 1 = 2,147,483,648, and sltu finds 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
  1. Low words first. 5 − 9 wraps to 0xFFFFFFFC, and sltu finds 5 < 9, a borrow of 1.
  2. High words, less the borrow. 3−1−1=1
  3. Join the halves. The result is 0x00000001_FFFFFFFC.
  4. Check in decimal. 12,884,901,893−4,294,967,305=8,589,934,588 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
  1. Compare as unsigned. li t0, 100 then bgeu a0, t0, out.
  2. A valid index. Any i from 0 to 99 is below 100 as an unsigned number, so it falls through.
  3. Too large. An index of 100 or more is caught as usual.
  4. Negative. A negative one such as −3 reads as 0xFFFFFFFD = 4,294,967,293 unsigned, far above 100, so it's caught too.
  5. Where it's used. Compilers use this trick for array bounds and for switch jump 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°.

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?

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.