1

Recently, I study the instruction set of riscv32 and face a order as "mulh" which tends to multiply two 32-bits signed integers and store the high 32 value into the register.

And here comes the problem, there are two integer src1 and src2, I want to implement these instruction in C language by using shift operation but failed. So I came to another method which divide src1 and src2 as A(the high 16 bits)B(the low 16 bits) but here appears an another question, how can I know the correct carry number from low 32-bits to high 32-bits, which I came here for help.

Ecthelion
  • 13
  • 3

1 Answers1

0

You are computing

$$(2^{16}A_1+B_1)(2^{16}A_2+B_2)$$

which is

$$2^{32}A_1A_2+2^{16}(A_1B_2+A_2B_1)+B_1B_2.$$

If we decompose

$$A_1B_2+A_2B_1=2^{16}A_3+B_3$$

where $A_3$ is at most $17$ bits and $B_3$ is $16$ bits, this turns to

$$2^{32}A_1A_2+2^{32}A_3+2^{16}B_3+B_1B_2.$$

So the carry from the least significant $32$ bits is the carry out of $2^{16}B_3+B_1B_2$.