Webcentive RISC-V Reference

div

Signed division

Extension
M
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
div xd, xs1, xs2
Length
32-bit

Encoding

31252420191514121176210
0000001rs2rs1100rd0110011

Match 0x02004033, mask 0xfe00707f. Source: div rd rs1 rs2 31..25=1 14..12=4 6..2=0x0C 1..0=3 in riscv-opcodes/extensions/rv_m.

FieldBitsriscv-opcodes name
rs224:20rs2
rs119:15rs1
rd11:7rd

Description

Divide xs1 by xs2, and store the result in xd. The remainder is discarded.

Division by zero will put -1 into xd.

Division resulting in signed overflow (when most negative number is divided by -1) will put the most negative number into xd;

Description from riscv-unified-db, reformatted.

Operation

if (implemented?(ExtensionName::M) && MISA_CSR_IMPLEMENTED && (CSR[misa].M == 1'b0)) {
  reserved_instruction($encoding);
}

XReg src1 = X[xs1];
XReg src2 = X[xs2];

# smallest signed value
XReg signed_min = (xlen() == 32) ? $signed({1'b1, {31{1'b0}}}) : {1'b1, {63{1'b0}}};

if (src2 == 0) {
  # division by zero. Since RISC-V does not have arithmetic exceptions, the result is defined
  # to be -1
  X[xd] = {MXLEN{1'b1}};

} else if ((src1 == signed_min) && (src2 == {MXLEN{1'b1}})) {
  # signed overflow. Since RISC-V does not have arithmetic exceptions, the result is defined
  # to be the most negative number (-2^(MXLEN-1))
  X[xd] = signed_min;

} else {
  # no special case, just divide
  X[xd] = $signed(src1) / $signed(src2);
}

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages