How can you multiply two unsigned 23-bit numbers if you only have memory locations that are 16-bits?
I am not performing these operations using x86 instructions, so information related to x86 will be ignored.
I assume you mean you can only multiply two 16-bit numbers at a time.
First I'm going to say that a
and b
are the numbers we want to multiply. They are between 17 and 32 bits in size (which covers your 23). However big they are they're zero padded out to an unsigned 32-bit size.
r = a * b
can be re-written as
r = (al + (ah << 16)) * (bl + (bh << 16))
where al
and ah
are the low and high 16-bit parts of a
. Shifting by 16 is the same as multiplying by 2^16. We can then expand this equation.
r = (al + (ah * 2^16)) * (bl + (bh * 2^16))
r = al * bl + al * (bh * 2^16) + (ah * 2^16) * bl + (ah * 2^16) * (bh * 2^16)
r = al * bl + al * bh * 2^16 + ah * bl * 2^16 + ah * bh * 2^32
r = al * bl + (al * bh + ah * bl) << 16 + (ah * bh) << 2^32
So you end up with 4 multiplies (al*bl
, al*bh
, ah*bl
& ah*bh
) and you shift the results and add them together. Bear in mind that each 16-bit multiply produces a 32-bit result, so the whole thing will go into 64 bits. In your case, because you know a
& b
aren't bigger than 2^23 the whole thing will fit in 46 bits.
The other thing is that the summation will need to be done in 16-bit chunks, with the carry bits looked after.
Hope that helps.