snezpseudo

snez

pseudo
RISC-V Assembly Reference

lyszt.net covers snez and other RISC-V instructions in more detail.

Description

snez tests whether a register holds a value other than zero, the inverse of seqz. The name means set if not equal to zero. When rs is non-zero, the form below writes 1 into rd, and when rs is zero it writes 0.

snez rd, rs

snez is a pseudo-instruction, like seqz, and the assembler expands it into a real instruction. That instruction asks whether zero is less than the value.

sltu rd, zero, rs

This holds for any value except zero. The assembler performs the translation when you write snez.

A computation often produces a value whose only useful property is whether it is zero, such as a count of remaining items, the difference between two things or a group of flags. snez reduces that value to 1 when it is non-zero and 0 when it is zero, the standard form for a function that returns a yes or no answer.

The RISC-V manual defines snez as an assembler pseudoinstruction for sltu with x0 as the first source. It sets rd to 1 if rs2 is not equal to zero and to 0 otherwise.

snez works with XOR to test inequality. XOR the two values, which yields a non-zero result only when they differ, then apply snez to obtain a 1 when they are different. Along with seqz, this produces a true or false result for equal and for not equal, which you can combine with and and or into larger conditions without a jump.

Expands to

sltu rd, zero, rs

The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.

Example

li t0, 5
snez t1, t0       # t1 = 1