16 bit signed multiply.
Archiver
Posts: 46,084
I am relativly new to programming basic stamps. Does anyone know of a
routine for signed 16-bit by signed 16-bit multiply that yields a
signed 32-bit resuslt? Thank you!
routine for signed 16-bit by signed 16-bit multiply that yields a
signed 32-bit resuslt? Thank you!
Comments
http://www.emesystems.com/BS2math6.htm
allways keep this website in mind for questions about any aspect
of basic stamps.
regards
adrian
(sign in bit 15) to get a signed 32 bit result (sign in bit 31).
In one method to do this, the first step is to extend the sign to 32
bits, and then just multiply them together:
X0 = -5 ' low word of X
Y0 = -2 ' low word of Y
X1 = -X0.bit15 ' high word of X
Y1 = -Y0.bit15 ' high word of Y
Z0 = X0 * Y0 ' low word of 32 bit signed product
Z1 = X1 ** Y1 ' high word of 32 bit signed product
Another method is to multiply together the absolute values of the
numbers, and then extend the sign:
Z0 = ABS X0 * ABS Y0
Z1 = ABS X1 ** ABS Y1
IF X0.bit15 ^ Y0.bit15 = 1 THEN ' conditional negation
Z0=~Z0+1 ' ~ is the NOT operator
IF Z0=0 THEN Z1=~Z1+1 ' conditional carry
ENDIF
There is more info about this on the page that Adrian kindly mentioned.
-- Tracy
>have a look at
>
>http://www.emesystems.com/BS2math6.htm
>
>allways keep this website in mind for questions about any aspect
>of basic stamps.
>
>regards
>adrian
>
>>I am relativly new to programming basic stamps. Does anyone know of a
>>routine for signed 16-bit by signed 16-bit multiply that yields a
>>signed 32-bit resuslt? Thank you!
first method wrong. It should be,
X1 = -X0.bit15 ' extend sign to high word of x1:x0
Y1 = -Y0.bit15 ' extend sign to high word of y1:y0
Z0 = X0 * Y0 ' low word of 32 bit signed product
Z1 = (X0 ** Y0) + (X1 * Y0) + (Y1 * X0) ' high word of 32 bit
signed product
' Z1:Z0 contains the product, sign is bit 31
A third way to do the multiply, is bit by bit shift and add as you
would do it by hand in school:
X1 = -X0.bit15 ' extend sign to high word of x1:x0
Y1 = -Y0.bit15 ' extend sign to high word of y1:y0
FOR i=0 to 15 ' bit by bit multiply, low word
IF X0.bit0(i)=1 THEN ' add conditional
Z0=Z0+Y0
Z1=Z1+Y1
IF Z0<Y0 THEN Z1=Z1+1 ' carry
ENDIF
Y1 = Y1 << 1 | Y0.bit15 ' Y1:Y0 * 2
Y0 = Y0 << 1
NEXT
FOR i=0 to 15 ' continue to the high word
IF X1.bit0(i)=1 THEN
' Y0=0 by this point
Z1=Z1+Y1 ' drop carry beyond 32 bits
ENDIF
Y1 = Y1 << 1
NEXT
' Z1:Z0 contains the product, sign is bit 31
>The trick here is that you want to multiply two signed 16 bit numbers
>(sign in bit 15) to get a signed 32 bit result (sign in bit 31).
>
>In one method to do this, the first step is to extend the sign to 32
>bits, and then just multiply them together:
> X0 = -5 ' low word of X
> Y0 = -2 ' low word of Y
> X1 = -X0.bit15 ' high word of X
> Y1 = -Y0.bit15 ' high word of Y
> Z0 = X0 * Y0 ' low word of 32 bit signed product
> Z1 = X1 ** Y1 ' high word of 32 bit signed product
>
>Another method is to multiply together the absolute values of the
>numbers, and then extend the sign:
> Z0 = ABS X0 * ABS Y0
> Z1 = ABS X1 ** ABS Y1
> IF X0.bit15 ^ Y0.bit15 = 1 THEN ' conditional negation
> Z0=~Z0+1 ' ~ is the NOT operator
> IF Z0=0 THEN Z1=~Z1+1 ' conditional carry
> ENDIF
>
>There is more info about this on the page that Adrian kindly mentioned.
>
> -- Tracy
>
>
>
>>have a look at
>>
>>http://www.emesystems.com/BS2math6.htm
>>
>>allways keep this website in mind for questions about any aspect
>>of basic stamps.
>>
>>regards
>>adrian
>>
>
>>>I am relativly new to programming basic stamps. Does anyone know of a
>>>routine for signed 16-bit by signed 16-bit multiply that yields a
>>>signed 32-bit resuslt? Thank you!
>
>To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
>from the same email address that you subscribed. Text in the
>Subject and Body of the message will be ignored.
>
>
>Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/