109
AT94KAL Series FPSLIC
Rev. 1138G–FPSLI–11/03
16-bit x 16-bit = 16-bit
Operation
This operation is valid for both unsigned and signed numbers, even though only the unsigned
multiply instruction (MUL) is needed, see Figure 61. A mathematical explanation is given:
When A and B are positive numbers, or at least one of them is zero, the algorithm is clearly
correct, provided that the product C = A • B is less than 2
16
if the product is to be used as an
unsigned number, or less than 2
15
if the product is to be used as a signed number.
When both factors are negative, the two’s complement notation is used:
A = 2
16
- |A| and B = 2
16
- |B|:
C = A • B = (2
16
- |A|) • (2
16
- |B|) = |A • B| + 2
32
- 2
16
• (|A| + |B|)
Here we are only concerned with the 16 LSBs; the last part of this sum will be discarded and
we will get the (correct) result C = |A • B|.
Figure 61.
16-bit Multiplication, 16-bit Result
When one factor is negative and one factor is positive, for example, A is negative and B is
positive:
C = A • B = (2
16
- |A|) • |B| = (2
16
• |B|) - |A • B| = (2
16
- |A • B|) + 2
16
• (|B| - 1)
The MSBs will be discarded and the correct two’s complement notation result will be
C = 2
16
- |A • B|.
The product must be in the range 0
≤
C
≤
2
16
- 1 if unsigned numbers are used, and in the
range -2
15
≤
C
≤
2
15
- 1 if signed numbers are used.
When doing integer multiplication in C language, this is how it is done. The algorithm can be
expanded to do 32-bit multiplication with 32-bit result.
AH
AL
BH
BL
X
AL * BL
AL * BH
AH * BL
+
+
=
CL
CH
1
2
3