Chris Low-level systems developer
← Back to all posts

The Add That Sign-Extends the Word

ISA

The claim is the unprivileged specification's addiw rule: it adds the sign-extended immediate to the low word, keeps only the low 32 bits of the sum, then sign-extends the result to 64 bits. With immediate 0 the addition does nothing, so the instruction is a pure sign extension, and the run shows it directly: the 0x80000001 word gives 0xffffffff80000001, the 0xffffffff word gives 0xffffffffffffffff, and dirty upper bits never survive.

MICROARCH

No claim depends on how any core implements word arithmetic. QEMU serves as a functional executor only; no pipeline or execution-unit behavior is asserted or measured, and the honest limit states the result is emulator-observed, not silicon-proved.

FIRMWARE

No firmware service takes part in the claim. The binary runs in M-mode on bare QEMU with no SBI or firmware calls; the behavior under test is the addiw instruction's own word semantic.

KERNEL

No kernel is involved at any step. The test binary runs on bare QEMU with no operating system; no system call or kernel data structure appears in the argument.

ALGORITHM

The claim is one fixed instruction semantic exercised as five fixed inputs with register readback, not a computation proved against an independent oracle. The five results are shown as the consequence the addiw rule forces, not as a verified algorithm.

A 32-bit value living in a 64-bit register can carry junk upstairs. Take 0x0000000180000001. Its low word is 0x80000001, which as a signed 32-bit number means negative 2,147,483,647. But the register as a whole has bit 32 set too, so as a 64-bit number it is a large positive value, not that negative number at all. The word and the register disagree, and any code that trusts the register gets the wrong sign.

Measured on QEMU 8.2.2, bare metal in M-mode. The program ran addiw with immediate 0 and addi with immediate 0 on five inputs. The addiw column matched the sign extension of the low word every time, and the addi column matched the input every time. Five addiw checks, zero mismatches, two runs identical. [1]

The witness, the one instruction and three of its answers:

addiw t0, s0, 0   # sext.w: keep the low word, sign-extend it
# s0 = 0x0000000080000001  -> t0 = 0xffffffff80000001
# s0 = 0x000000007fffffff  -> t0 = 0x000000007fffffff
# s0 = 0x00000000ffffffff  -> t0 = 0xffffffffffffffff
Blueprint-style diagram of the addiw sign extension in three panels: HAND IT 0x80000001 shows addiw t0, s0, 0 with s0 equal to 0x0000000080000001 giving t0 equal to 0xffffffff80000001; HAND IT 0x7fffffff shows s0 equal to 0x000000007fffffff giving t0 unchanged; HAND IT LOW ALL ONES shows s0 equal to 0x00000000ffffffff giving t0 equal to 0xffffffffffffffff because the low word means -1
0x80000001 gained ones upstairs, 0x7fffffff stayed put, and 0xffffffff became -1: addiw with 0 is sext.w, five checks, zero mismatches.
Diagram: hand-built by Chris Dillinger for this post

Why addiw and not addi. The addi instruction works on all 64 bits, so adding 0 changes nothing, and the junk upstairs survives. The addiw instruction works on 32 bits: it adds the immediate to the low word, keeps only the low 32 bits of that sum, then sign-extends the result back to 64 bits. With an immediate of 0, the addition does nothing, and what is left is pure narrowing plus sign extension. The 0 is just the excuse; the narrowing is the trick.

The other two inputs close the loop. The dirty-upper-bits input 0x0000000180000001 also came out 0xffffffff80000001, because addiw never looks above bit 31. And the already extended 0xffffffff80000001 came out unchanged, so running sext.w twice is harmless: the second pass is a no-op. [1]

The disassembly gives the idiom its short name. The loop test is one instruction, and objdump prints it as sext.w a0, s0 at 0x80000222: sext.w is the assembler's name for exactly addiw rd, rs, 0. Writing sext.w in your own assembly says what you mean, and the reader never has to wonder why you are adding zero. The addi side of the loop prints as mv, the assembler's name for addi with immediate 0, which is why that column never moved. [1]

The bench habit: whenever a value reaches you as a 32-bit int but sits in a 64-bit register, and you cannot prove the upper bits are clean, one sext.w settles it. After that instruction the register and the word agree, and a comparison, a sign test, or a widening conversion reads the number the word actually names.

One sentence to carry away: addiw with 0 is not an addition at all, it is a sign extension wearing an addition's clothes.

Sources

  1. 01
    src/addiw-sextw: module PROOF.md and the QEMU runChris Dillinger

    The QEMU 8.2.2 run behind the post: addiw with immediate 0 and addi with immediate 0 for five inputs on bare metal in M-mode, the addiw column matching the sign extension of the low word in all five cases and the addi column matching the input in all five. 5 checks, 0 mismatches, two runs identical. The disassembly confirms the loop test as sext.w a0, s0 at 0x80000222, the assembler's name for addiw a0, s0, 0, and the addi side as mv, the assembler's name for addi with immediate 0.

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app