The Mask That Won't Fit in Twelve Bits
The claim is the I-type immediate rule: the immediate is 12 bits, sign-extended, so its range is -2048 to 2047 and the largest positive mask it can spell is 2047, or 0x7FF. The run shows it directly: the assembler refuses andi with 0xfff and with 0x800, andi with 2047 loses bit 11 on 0x12345abc, 0xfff, and 0xffff, and the shift pair matches a loaded 4095 constant on all four inputs.
No claim depends on how any core implements immediate AND. 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 andi immediate encoding itself.
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 four fixed inputs with register readback, not a computation proved against an independent oracle. The four results are shown as the consequence the immediate rule forces, not as a verified algorithm.
You have a value in a register and you want its low 12 bits, a packed field, a page offset, a tag. The line that writes itself is andi t0, s0, 0xfff: AND the register with twelve ones, keep the field, throw the rest away. It reads exactly like the mask it means. It does not assemble.
Measured on QEMU 8.2.2, bare metal in M-mode. The program ran three ways of masking for four inputs: andi with 2047, the shift pair slli 52 then srli 52, and and against 4095 loaded with li. The shift pair and the loaded constant agreed on every input. The andi column agreed only where bit 11 was clear, and lost bit 11 everywhere else. Four inputs, zero mismatches between the two true 0xFFF masks, two runs identical. [1]
The witness, the refused line and the two that work:
andi t0, s0, 0xfff # illegal operands: 4095 is out of range
andi t0, s0, 2047 # keeps the low 11 bits, no more
slli t0, s0, 52 # the compiler's dodge for x & 0xFFF
srli t0, t0, 52 # low 12 bits back down, zero-filled
# v = 0x12345abc: andi 2047 -> 0x2bc, shift pair -> 0xabc
Why the assembler says no. The immediate in an I-type instruction is 12 bits, and it is sign-extended, so it runs from -2048 to 2047. A mask is a positive number, so the largest mask you can write as an andi immediate is 2047, which is 0x7FF, eleven ones. 0xFFF is 4095, one past the top of the range, and 0x800 is 2048, also past it, so the assembler refuses both. The sign extension is not a detail here; it is the whole limit, because bit 11 of the immediate is the sign bit, not a mask bit you can set freely.
The numbers show the cost of settling for 2047. For 0x12345abc the andi column gave 0x2bc and the true mask gave 0xabc: the field's top bit, bit 11, silently dropped. For 0xfff the columns gave 0x7ff and 0xfff, and for 0xffff the same split. For 0x1000 all three gave 0, because bit 12 is outside both masks. Settling for the smaller mask does not error; it quietly reads a narrower field. [1]
The compiler already knows the dodge. Given x & 0xFFFUL in C at -O2, GCC 13.2.0 for rv64imac did not emit an andi at all. It emitted slli a0, a0, 52 then srli a0, a0, 52: shift left to throw away everything above bit 11, shift right to bring the twelve bits back down, zero-filled. [1]
The bench habit: when a mask in hand-written assembly needs bit 11 or higher, do not reach for andi. Write the shift pair, or load the constant and use and. If the assembler accepts your andi mask, it fits; if it refuses it, the refusal is the specification telling you the range, not a quirk to work around with a different spelling of the same immediate.
One sentence to carry away: an andi immediate is a signed 12-bit number, so the widest low mask it can spell is 0x7FF, and 0xFFF has to be built, not written.
Diagram: hand-built by Chris Dillinger for this post
Sources
- 01src/andi-mask12: module PROOF.md and the QEMU runChris Dillinger
The QEMU 8.2.2 run behind the post: andi with 2047, the slli 52 and srli 52 pair, and and against a loaded 4095 for four inputs on bare metal in M-mode, the shift pair and the loaded constant agreeing on all four and the andi column losing bit 11 where it was set. 6 checks, 0 mismatches, two runs identical. The assembler rejection log and the GCC 13.2.0 shift-pair output for x and 0xFFFUL are in the same module's bench-logs.