booth乘法算法仅用于乘2个负数(-3 * -4)
还是一个正数(-3 * 4)
?当我用booth算法乘2个正数时,我得到了一个错误的结果。
例子:5*4
A=101000 0 // binary of 5 is 101
S=011000 0 // 2's complement of 5 is 011
P= 000 100 0 // binary of 4 is 100
X=3 number of bits in m
Y=3 number of bits in r
M=5
-m =m的2's补
R=4
在@ ruakh回答后更新
5*4= 20
M= 0101 is 5
R= 0100 is 4
A=0101万0
S= 1010 0000
P= 0000 0100 0
here 1 is the carry generated
向右移动1位: 110010000离开LSB : 11001000 (不等于20)
发布于 2011-11-19 04:23:31
你没有足够的空间来处理你的招牌。5不是101
,而是0101
:它必须从0
开始,因为以1
开头的值是负值。101
实际上是- 3 :它是011
的两个补充,也就是3。类似地,4不是100
,而是0100
;100
是-4。所以当你把101
乘以100
的时候,你实际上是把-3乘以-4,这就是为什么你得到12。
发布于 2011-11-19 06:26:39
Booth的算法是针对有符号整数的,也就是说,每个整数可以是正整数,也可以是负整数,也可以是零整数。
下面是一个示例C程序,它说明了将两个8位有符号整数(2的补码)相乘并得到16位有符号乘积的实现和中间结果:
#include <stdio.h>
#include <limits.h>
#include <string.h>
typedef signed char int8;
typedef short int16;
char* Num2BaseStr(unsigned long long num, unsigned base, int maxDigits)
{
static char s[sizeof(num) * CHAR_BIT + 1];
char* p = &s[sizeof(s) - 1];
memset(s, 0, sizeof(s));
do
{
*--p = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[num % base];
num /= base;
} while ((num != 0) || (p > s));
// Keep at most maxDigits digits if requested
if ((maxDigits >= 0) && (&s[sizeof(s) - 1] - p > maxDigits))
{
p = &s[sizeof(s) - 1] - maxDigits;
}
// Skip leading zeroes otherwise
else
{
while (*p == '0') p++;
}
return p;
}
int16 BoothMul(int8 a, int8 b)
{
int16 result = 0;
int16 bb = b;
int f0 = 0, f1;
int i = 8;
printf("a = %sb (%d)\n", Num2BaseStr(a, 2, 8), a);
printf("b = %sb (%d)\n", Num2BaseStr(b, 2, 8), b);
printf("\n");
while (i--)
{
f1 = a & 1;
a >>= 1;
printf(" %sb\n", Num2BaseStr(result, 2, 16));
printf("(%d%d) ", f1, f0);
if (!f1 && f0)
{
printf("+ %sb\n", Num2BaseStr(bb, 2, 16));
result += bb;
}
else if (f1 && !f0)
{
printf("- %sb\n", Num2BaseStr(bb, 2, 16));
result -= bb;
}
else
{
printf("no +/-\n");
}
printf("\n");
bb <<= 1;
f0 = f1;
}
printf("a * b = %sb (%d)\n", Num2BaseStr(result, 2, 16), result);
return result;
}
int main(void)
{
const int8 testData[][2] =
{
{ 4, 5 },
{ 4, -5 },
{ -4, 5 },
{ -4, -5 },
{ 5, 4 },
{ 5, -4 },
{ -5, 4 },
{ -5, -4 },
};
int i;
for (i = 0; i < sizeof(testData)/sizeof(testData[0]); i++)
printf("%d * %d = %d\n\n",
testData[i][0],
testData[i][1],
BoothMul(testData[i][0], testData[i][1]));
return 0;
}
输出:
a = 00000100b (4)
b = 00000101b (5)
0000000000000000b
(00) no +/-
0000000000000000b
(00) no +/-
0000000000000000b
(10) - 0000000000010100b
1111111111101100b
(01) + 0000000000101000b
0000000000010100b
(00) no +/-
0000000000010100b
(00) no +/-
0000000000010100b
(00) no +/-
0000000000010100b
(00) no +/-
a * b = 0000000000010100b (20)
4 * 5 = 20
a = 00000100b (4)
b = 11111011b (-5)
0000000000000000b
(00) no +/-
0000000000000000b
(00) no +/-
0000000000000000b
(10) - 1111111111101100b
0000000000010100b
(01) + 1111111111011000b
1111111111101100b
(00) no +/-
1111111111101100b
(00) no +/-
1111111111101100b
(00) no +/-
1111111111101100b
(00) no +/-
a * b = 1111111111101100b (-20)
4 * -5 = -20
a = 11111100b (-4)
b = 00000101b (5)
0000000000000000b
(00) no +/-
0000000000000000b
(00) no +/-
0000000000000000b
(10) - 0000000000010100b
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
a * b = 1111111111101100b (-20)
-4 * 5 = -20
a = 11111100b (-4)
b = 11111011b (-5)
0000000000000000b
(00) no +/-
0000000000000000b
(00) no +/-
0000000000000000b
(10) - 1111111111101100b
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
a * b = 0000000000010100b (20)
-4 * -5 = 20
a = 00000101b (5)
b = 00000100b (4)
0000000000000000b
(10) - 0000000000000100b
1111111111111100b
(01) + 0000000000001000b
0000000000000100b
(10) - 0000000000010000b
1111111111110100b
(01) + 0000000000100000b
0000000000010100b
(00) no +/-
0000000000010100b
(00) no +/-
0000000000010100b
(00) no +/-
0000000000010100b
(00) no +/-
a * b = 0000000000010100b (20)
5 * 4 = 20
a = 00000101b (5)
b = 11111100b (-4)
0000000000000000b
(10) - 1111111111111100b
0000000000000100b
(01) + 1111111111111000b
1111111111111100b
(10) - 1111111111110000b
0000000000001100b
(01) + 1111111111100000b
1111111111101100b
(00) no +/-
1111111111101100b
(00) no +/-
1111111111101100b
(00) no +/-
1111111111101100b
(00) no +/-
a * b = 1111111111101100b (-20)
5 * -4 = -20
a = 11111011b (-5)
b = 00000100b (4)
0000000000000000b
(10) - 0000000000000100b
1111111111111100b
(11) no +/-
1111111111111100b
(01) + 0000000000010000b
0000000000001100b
(10) - 0000000000100000b
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
1111111111101100b
(11) no +/-
a * b = 1111111111101100b (-20)
-5 * 4 = -20
a = 11111011b (-5)
b = 11111100b (-4)
0000000000000000b
(10) - 1111111111111100b
0000000000000100b
(11) no +/-
0000000000000100b
(01) + 1111111111110000b
1111111111110100b
(10) - 1111111111100000b
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
0000000000010100b
(11) no +/-
a * b = 0000000000010100b (20)
-5 * -4 = 20
发布于 2011-11-25 20:48:59
5*4 =20
m=5,r=4,x=y=4
m=0101 , r=0100 , -m=1011 ,x=y=4
A=0101 0000 0
S=1011 0000 0
P=0000 0100 0
1. P=0000 0100 0 //last two bits are 00 so simply shift P
P=0000 0010 0
2. P=0000 0010 0 //last two bits are 00 so simply shift P
P=0000 0001 0
3. P=0000 0001 0 //last two bits are 10 so perform P = P+S
P=1011 0001 0
P=1101 1000 1 // after shifting P
4. P=1101 1000 1 //last two bits are 01 so perform P = P+A
P=0010 1000 1
P=0001 0100 0 // after shifting P
5. now remove LSB
the product is P=00010100(20)
https://stackoverflow.com/questions/8191824
复制相似问题