Truncated match.
PICList
Thread
'Math with a PIC'
1995\01\07@000318
by
crocontroller discussion list
|
Martin Kirk (spam_OUTmlkTakeThisOuT
asu.edu) wrote:
>Here is my situation. The input data is 8-bit, two's complement
>data. I need to multiply two inputs together then multiply the result
>by 8 and still get the result to fit in 16 bits.
>
>As I see it the max 8-bit value can be FFh (-1). In order to
>make the multiply work for the 16-bit math I need to set all upper 8
>bits (I think) so that my multiplier data is FFFFh. This will cause an
>overflow if I have two such values to multiply so I was going to limit
>one of my inputs to 5 bits (ie, max value = 1Fh). But if I do this how
>do I use this 5-bit representation of -1 in the 16-bit adder???
Martin:
The two's-complement representation was designed to solve exactly your
problem. If you're using narrow numbers (with a small number of
digits) and want to translate them to wider numbers (with more digits),
all you have to do is "sign-extend" your numbers. That is, replicate
the sign bit (the narrow representation's leftmost bit) to the left,
filling all the "extra" bits in the wider representation.
In your case, you want to extend 5-bit numbers in the range [-16 - 15]
to 16-bit numbers. Here... Take a look at the 5-bit and 16-bit
representations of some numbers in that range:
Decimal 5-Bit Binary 16-Bit Binary
------- ------------ -------------
0 00000 00000000 00000000
1 00001 00000000 00000001
10 01010 00000000 00001010
15 01111 00000000 00001111
-1 11111 11111111 11111111
-10 10110 11111111 11110110
-15 10000 11111111 11110000
As you can see, bit-positions 5-15 in the 16-bit numbers contain the
same value (0 for positive numbers, 1 for negative) that's in bit 4.
[If you're REAL unfamiliar with binary notation, I guess I should tell
you that the bits are numbered from right to left, starting with bit
number 0. If you're not, I apologize for talking down to you.]
If you were writing your own math routines or were more comfortable with
binary math, I'd recommend that you write your multiply routine to use
the 5-bit value directly; the resulting code would be shorter and would
execute more quickly. However, since you're using pre-packaged
routines, just extend the sign bit into the upper bits before you do the
multiplication, and you'll be fine.
-Andy
--
Andrew Warren - .....fastfwdKILLspam
@spam@ix.netcom.com
Fast Forward Engineering, Vista, California
1995\01\09@075617
by
crocontroller discussion list
|
> Here is my situation. The input data is 8-bit, two's complement
> data. I need to multiply two inputs together then multiply the result by
> 8 and still get the result to fit in 16 bits.
>
> As I see it the max 8-bit value can be FFh (-1). In order to
> make the multiply work for the 16-bit math I need to set all upper 8 bits
> (I think) so that my multiplier data is FFFFh. This will cause an
> overflow if I have two such values to multiply so I was going to limit
> one of my inputs to 5 bits (ie, max value = 1Fh). But if I do this how
> do I use this 5-bit representation of -1 in the 16-bit adder???
>
Hi Martin,
I'm not sure that I fully understand what you are trying to do. If you
convert your data from two's complement, you can simply multiply the
two values and then left shift the 16-bit result three times for the 8*a*b
result.
If you leave the data in 2's comp. format, when you multiply you will
get the following:
a+a2=h'100' or a2=h'100'-a these are your 2's comp values
b+b2=h'100' or b2=h'100'-b
Now multiplying a2*b2
a2*b2 = h'10000' - h'100'*a - h'100'*b + a*b
For a 16-bit result, the h'10000' overflows. To get your desired value
of a*b you must correct the a2*b2 result by the following
a*b = a2*b2 + h'100'*a + h'100'*b - h'10000'
This simply means to add a and b to the high byte of the 2 byte a2*b2 result,
but your data is still in 2's comp. So you'll need to substitute
a*b = a2*b2 + h'100'*(h'100'-a2) + h'100'*(h'100'-b2) - h'10000'
or
a*b = a2*b2 - h'100'*a2 - h'100'*b2 + h'10000'
So, you can get your a*b from you a2*b2 by subtracting a2 and b2 from the
high byte of a2*b2. You may want to use the 16-bit subtraction with a2
and b2 as the high byte.
Now that you have a*b, just left shift three times for 8*a*b.
I hope this is correct.
Good luck,
Derrick Early
early
KILLspamfinite.nrl.navy.mil
More... (looser matching)
- Last day of these posts
- In 1995
, 1996 only
- Today
- New search...