Thanks a lot fvm, for such an enlightening post.
@amilcar : Not that I couldn't understand how an overflow would occur in the case of multiplying two most negative numbers; what confused me was the term "two sign bits".
But, now I
completely understand what FvM had to say. Even I cross-checked my logic, and here's my elaborate proof:
Consider this - All we need is to multiply 2 numbers A & B (which are 16-bit signed).
We
assume that (
we have checked that) whatever range of values A & B will take (fractional or integer) - the integer-part of our "
result_axb" is small enough to be represented by less than or equal to 15-bits.
(i.e.
-32768 <= result_axb <= 32767)
Which means, that we have already checked that no overflow would occur with our result_AxB (
'overflow' w.r.t. 16-bits signed representation )
now, in 32-bit signed representation:
Any positive no. less than +32767: has all it
17 msb's [31:15] = 0 (= sign bit)
Any negative no. greater than -32768: has all it
17 msb's [31:15] = 1 ( = sign bit)
I am attempting to generate a 32-bit signed result:
mult_out32 = k*l*m ( refer my 2nd post: A*B = K*L*(M/N) ). This product (K*L*M)
may exceed the 16-bit signed range.
But, since
i'm sure that later on, dividing it by N (i.e. stripping-off its P LSBs) would bring the result back in the 16-bit signed range
... and in this resulting form,
all its [31-p:15] msbs would be equal ) ...
therefore, before stripping-off, these same bits are at the position [31:P+15], and hence these are
necessarily equal !
(We can collectively call all these equal bits as 'The sign bit' !)
So, summing up -
I can rest assured that, iff I've checked the 16-bit overflow condition at the start - then
mult_out32[31] &
mult_out32[30:p+15] are all equal !
Moreover,
result_axb = {mult_32out[31], mult_32out[p + 14 : p]};
will give me the correct 16-bit signed result, in the end !
Hence proved ! :-)
p.s.: Thanks again FvM, for helping me arrive at this proof; and also giving an Overflow-condition-checker, in case we wish to make a more generic module !