The Comparison That Tests for Zero
The claim is the unprivileged specification's sltiu rule: it writes 1 when rs1 is smaller than the sign-extended immediate under an unsigned comparison, else 0. With the immediate 1, only rs1 equal to zero qualifies, and the run shows it directly: 0 gave 1, and 1, 2, 5, and all ones gave 0.
No claim depends on how any core implements set-less-than-immediate. 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.
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 sltiu instruction's own comparison semantic.
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.
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 sltiu rule forces, not as a verified algorithm.
Testing for zero usually means a branch: compare, then jump. RISC-V has a branchless way. The instruction sltiu compares its input against an immediate as an unsigned number and writes 1 when the input is smaller, else 0. Compare against 1, and only one unsigned value qualifies: zero itself.
Measured on QEMU 8.2.2, bare metal in M-mode. The program ran sltiu against 1 for five inputs. Zero returned 1. One, two, and five returned 0. All ones, the 64-bit value 0xffffffffffffffff, returned 0. Five checks, zero mismatches, two runs identical. [1]
The witness, the zero test itself:
sltiu a2, a1, 1 # a2 = 1 exactly when a1 == 0
# a1 = 0 -> a2 = 1
# a1 = 5 -> a2 = 0
# a1 = 0xffffffffffffffff -> a2 = 0
Diagram: hand-built by Chris Dillinger for this post
The all-ones result is the point of the unsigned compare. Read as a signed number, all ones is negative one, and negative one is below 1. But sltiu reads it unsigned, where it is the largest possible value, far above 1, so it returns 0. The signed twin, slti, would have answered 1 for that input. Picking the wrong twin turns a zero test into a test for zero or negative. [1]
The disassembly gives the idiom its short name. The loop kept the test as one instruction, and objdump prints it as seqz a0, s0 at 0x80000218: seqz is the assembler's name for exactly sltiu rd, rs, 1. Writing seqz in your own assembly says the same thing, and says it more plainly. [1]
The bench habit: when you need a 0-or-1 flag that says whether a value is zero, and you do not want a branch, sltiu against 1 is the one instruction. The flag it writes can feed an add, a mask, or a count without ever jumping.
One sentence to carry away: sltiu against 1 is a zero test, because zero is the only unsigned value below 1.
Sources
- 01src/sltiu-zero-test: module PROOF.md and the QEMU runChris Dillinger
The QEMU 8.2.2 run behind the post: sltiu against 1 for five inputs on bare metal in M-mode, 0 giving 1 and 1, 2, 5, and 0xffffffffffffffff giving 0. 5 checks, 0 mismatches, two runs identical. The disassembly confirms the loop test as seqz a0, s0 at 0x80000218, the assembler's name for sltiu a0, s0, 1.