RRON
RRON

Reputation: 1115

Why mulh instruction in riscv32 gives 0?

Multiplier = 0xffffffff
Multiplicand = 0xffffffff
Result = 0xFFFFFFFE 00000001 (multiplier * multiplicand)

mulh instruction is expected to give the upper XLEN bits of the result as per RISC-Vs pec. But when I run mulh t0, a0, a1; with multiplier in a0 and multiplicand in a1, the result in t0 is 0. Can someone explain why? I run this in RV32IMAC (FE310 core).

assembly and C code below:

mulh_asm:
    mulh t0, a0, a1;
    addi a0, x0, 0; // **Break point here to watch t0 value**
ret;

mrest[0] = mulh_asm(0xffffffff,  0xffffffff);

Upvotes: 0

Views: 433

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26646

the result in t0 is 0. Can someone explain why?

mulh is for signed × signed, so you're doing -1 × -1!  The result for which is +1, and in 64 bits, that is 63-bits of zeros followed by a 1 bit — so naturally the proper answer for the upper 32 bits (of 64-bit +1) is all zeros.

[Desired] Result = 0xFFFFFFFE 00000001 (multiplier * multiplicand)

Try mulhu, which is for unsigned × unsigned, and this will give you the results you're expecting.

Upvotes: 2

Related Questions