RISC-V zext.h: Zbb/Zbkb Instruction and Equivalent Shift Sequence
Zbb/Zbkb instruction; equivalent shift sequenceThe zext.h instruction zero-extends the low 16 bits to XLEN. It is provided by the Zbb extension and also by the Zbkb extension via RV32 PACK or RV64 PACKW with rs2=x0. The Assembly Manual lists an equivalent SLLI/SRLI shift sequence.
Single Instruction and Equivalent Shift Sequence
The zero extension performed by zext.h has a single-instruction form in Zbb/Zbkb and can also be performed by the Assembly Manual's two-shift sequence. The single instruction has different RV32 and RV64 encodings.
Official Semantics Checklist
Availability And Extension Conditions
- The Zbb extension provides zext.h with different RV32 and RV64 encodings.
- The Zbkb extension also provides zext.h: PACK with rs2=x0 on RV32, or PACKW with rs2=x0 on RV64.
- The target ISA includes the Zbb or Zbkb extension.
- Zero-extends the low 16 bits.
How The Equivalent Shift Sequence Works
Official References And Reading Order
The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture Section 30.9.50 gives the Zbb zext.h encodings and semantics; Sections 30.9.26 and 30.9.28 specify that Zbkb RV32 PACK and RV64 PACKW with rs2=x0 are also zext.h. RISC-V Assembly Programmer's Manual Table 5 lists an equivalent SLLI/SRLI sequence.
When To Think Of It First
Pitfalls / Common Confusions
FAQ
Is zext.h a pseudo-instruction with Zbb?
No. The Unprivileged ISA defines zext.h as a real Zbb instruction and gives RV32 and RV64 encodings.
Is a two-shift sequence required without Zbb?
No. Zbkb RV32 PACK and RV64 PACKW with rs2=x0 are also zext.h. Check whether the target ISA provides a single-instruction form.
What is the equivalent shift sequence?
The RISC-V Assembly Programmer's Manual lists slli rd, rs, XLEN - 16 followed by srli rd, rd, XLEN - 16 to zero-extend the source's low 16 bits to XLEN.