0

The RISC-V reader states that mulh rd, rs1, rs2 "multiplies x[rs1] by x[rs2], treating the values as two'cplement numbers, and writes the upper half of the product to x[rd]"

So I am trying to multiply two signed 64 bit numbers and get a signed 128 bit result:

 mulh t0, a0, a1
 mul t1, a0, a1

And I'd expect that t0 would hold the upper 64 bits and t1 the lower 64 bits, but if a0= 0x7fffffffffffffff (ie MAX-INT) and a1 = 2, I get:

t0 = 0, t1=0xfffffffffffffffe as though I'd carried out an unsigned multiplication. And not t0 = 1 , t1= 0x7fffffffffffffff which appears to me to be the correct answer for two's complement arithmetic.

Now - in writing this out, I can see why I get t1=0xffffffffffffff, but I still don't get why I get t0=1, which would allow me to implement some logic to correct the sign bit in t1.

What have I got wrong here? I used to have rather complex bit-by-bit long multiplication algorithm for this, but this seemed so much simpler, but it (obviously) doesn't seem to work.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
  • 2
    `t0:t1` (concatenation of the two registers) is to be interpreted as the 128-bit full product of `a0` and `a1`. Sign information is going to be in `t0`. Here, `t0<63>` = `(a0*a1)<127>` is 0, so the product is positive. – njuffa Apr 04 '22 at 20:45
  • Thanks - get this now - could you turn that into answer and I will accept it? – adrianmcmenamin Apr 04 '22 at 20:56

1 Answers1

2

The 128-bit answer to 2 x 0x7FFFFFFFFFFFFFFF is

0x0000000000000000FFFFFFFFFFFFFFE
  |------ t0 ----||----- t1 ----|
  |
  |
  +-- the sign bit is the uppermost bit of this hex digit

Because you're multiplying two positive numbers, the answer is the same bit pattern whether you choose signed or unsigned multiplication.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53