Chris Low-level systems developer
← Back to all posts

The Conditional Move RISC-V Added as Two Instructions

ISA

matters at ISA because the verified properties are instruction semantics and encodings: czero.eqz means zero-unless and czero.nez means zero-if, objdump round-trips the pair as 0ec5d533 and 0ec5f533 one funct3 bit apart, and the spec's own sentences fix the shape. The refusal of predication and flag-based moves is the ISA's design philosophy stated as architecture, and the post verifies the contract's text.

MICROARCH

not at Microarch because the post's only implementation story, cores fusing a short forward branch over a single ALU op, arrives explicitly as a debated report rather than a verified property: the spec sentence quoted is about what implementations do not need, no core's behavior was measured, and the verified facts stay on the contract side.

FIRMWARE

not at Firmware because nothing in the post runs at firmware's privilege level or uses its services: the verified facts are assembler input, disassembler output, compiler output, and spec text, and czero needs no trap, no delegation, and no machine-mode enable gate.

KERNEL

not at Kernel because no kernel mechanism enters the argument: the post verifies instruction encodings, compiler output, and spec sentences, and the select lowering it shows is a compiler's code-generation rule, not a privilege or scheduling contract.

ALGORITHM

not at Algorithm because the lowering is spelled in Zicond instructions: cond ? x : y as OR of czero.eqz and czero.nez exists only where the extension does, so it is an ISA-specific idiom rather than a portable computational mechanism, and the motivating lab's checks judge a C construction, not this lowering.

Pick x when the condition holds and y when it does not, with no branch. Base RISC-V has no instruction for that. An earlier letter built a branchless binary search on the ISA anyway: each iteration's comparison produces a 0/1 integer, and the search window is steered with integer arithmetic alone, no data-dependent branch in the loop body. It works, and it is honest code. But the shape shows an instruction is missing.

A manual railway switch beside the tracks
The railroad switch: pick a path, branch never taken.

The missing instruction was deliberate, and the rejected designs are worth naming, because the new instructions only make sense against them. ARM-style predication, where nearly every instruction carries a condition, was refused as decode cost paid on every fetch. The x86 conditional-move family was refused because it needs a condition-flags register, and the ISA pointedly does not have one. The Zicond spec's own architecture comment says the quiet part out loud: defining more comparisons does not offer a benefit, given the lack of immediates or an additional register operand to compare against. Minimalism is structural here, not decoration.

The addition, when it came, is two instructions. czero.eqz rd, rs1, rs2 puts zero in rd if rs2 equals zero, otherwise it moves rs1 into rd. czero.nez is the mirror: zero in rd if rs2 is nonzero, otherwise move rs1. Zero this, unless. The spec gives the reason for this shape and no other: a simple solution that provides most of the benefit and all of the flexibility one would desire for conditional arithmetic and conditional select, while staying true to the RISC-V design philosophy. Branchless sequences come out in typically two instructions, and the spec names the price of admission: no instruction fusion, no special provisions during decoding, and no other microarchitectural provisions.

The canonical lowering is worth seeing once. cond ? x : y becomes three lines:

czero.eqz t0, x, cond
czero.nez t1, y, cond
or        rd, t0, t1

Each line glossed: the first yields x when cond is nonzero and 0 otherwise. The second yields y when cond is zero and 0 otherwise. The OR keeps exactly the surviving side. That is the rule a Go compiler proposal uses for RVA23 targets. No branch, no flags: three instructions where the branchy version needed a label.

The toolchain knows the name already. This run assembled both mnemonics with binutils 2.42 and the disassembler round-tripped them: czero.eqz a0,a1,a2 is 0ec5d533, czero.nez is 0ec5f533, one funct3 bit apart. But gcc 13.2.0, handed -march=rv64imac_zicsr_zicond, accepts the extension string and still emits beq plus mv for the textbook gate (c ? x : 0) and for the two-way select. The assembler knows the word. The compiler has not learned to reach for it.

Honest boundaries, as always. Zicond is an extension, not base: the ratified RVA23 profile made it mandatory for application processors, but portable code still cannot assume it. This note assembled and disassembled the two instructions and never executed them, so its claims are about encodings and semantics, not about silicon. The one implementation story in the neighborhood, cores fusing a short forward branch over a single ALU op in hardware, arrives as a debated forum thread rather than a measured fact, and the note treats it that way.

The thread back to the branchless search: for years x86 shipped a conditional move the RISC-V base ISA left unnamed. It has a name now, and the two spellings differ in one funct3 bit. One sentence to keep: RISC-V left the conditional move out until it could add it in two instructions, and the addition is spelled 'zero this, unless.'

Related work: crash triage is $250/symptom, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app