VFIRST.M

RISC-V VFIRST.M Instruction Details

Instruction ManualR-type

Find lowest-indexed set bit in vs2 mask, writing index to scalar rd (-1 if none).

Instruction Syntax

vfirst.m rd, vs2, vm
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
VVector OperationsMask Operations

Instruction Behavior

VFIRST.M finds the lowest-numbered active set bit in mask source vs2 and writes that element index to integer register rd; if none is found, it writes -1.

Quick Understanding & Search Notes

VFIRST.M is an RVV mask-to-scalar instruction: the result is written to integer register rd, not a vector register.

The source is viewed as a mask register; participation is still controlled by vl and the optional execution mask.
The operation applies only to active elements within vl; inactive and tail elements follow the current vma/vta policy.
Except for dedicated mask forms, vm=0 uses v0 as the execution mask and vm=1 is unmasked.
Floating-point operations follow the vector FP rules: normal FP operations use frm rounding and set FP exception flags; fixed-point vxrm does not control them.

Common Usage Scenarios

Conditional Loop

Understand this scenario with real code like «vsetvli t0, a0, e32, m1, ta, ma vmseq.vv v0, v8, v9 vfirst.m a0, v0».

Mask Traversal

Understand this scenario with real code like «vsetvli t0, a0, e32, m1, ta, ma vmseq.vv v0, v8, v9 vfirst.m a0, v0».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is R-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

Result in rd (x register), not vector. vs2 is mask source (not limited to v0). Returns -1 if none found.

FAQ

Where is the result written?

It is written to scalar integer register rd. VCPOP.M writes a count; VFIRST.M writes the first set element index or -1.