bge
lyszt.net covers bge and other RISC-V instructions in more detail.
Encoding format
Description
bge pairs with blt and reverses its condition. The letters stand for branch if greater than or equal. When rs1 is at least rs2, the instruction below jumps to the labeled position. Otherwise execution continues with the next instruction.
bge rs1, rs2, label
For any two values, exactly one of blt and bge takes the branch, so the two cover all cases. The comparison is described under blt, and branches in general under beq.
bge commonly appears at the top of a loop as the exit test. The line below leaves the loop once the counter t1 reaches its limit t2.
bge t1, t2, done
The loop body follows, and an unconditional jump at its end returns to the top. Testing before the body handles zero repetitions, since a body whose condition already holds never runs.
The or equal part decides behavior at the boundary. bge branches when the two values are equal, and off-by-one errors in loops usually trace back to this boundary. A loop that counts from 0 up to n exits when the counter reaches n.
According to the RISC-V manual, bge and bgeu take the branch when rs1 is greater than or equal to rs2, comparing signed and unsigned values respectively. A branch offset is a 12-bit value measured in multiples of 2 bytes.
As with blt, the comparison is signed, so negative values are allowed. bgeu is the unsigned form, used for addresses and sizes. A common use is checking that a value is non-negative, as in the line below.
bge t0, zero, ok
Example
bge t1, t2, done # leave the loop once i >= n
