-
Notifications
You must be signed in to change notification settings - Fork 0
/
booth_algo.c
168 lines (129 loc) · 2.77 KB
/
booth_algo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// C code to implement Booth's algorithm
// for bit-level fast multiplication
#include <stdio.h>
#include <stdlib.h>
// function to perform adding in the accumulator
void add(int ac[], int x[], int qrn)
{
int i, c = 0;
for (i = 0; i < qrn; i++) {
// updating accumulator with A = A + BR
ac[i] = ac[i] + x[i] + c;
if (ac[i] > 1) {
ac[i] = ac[i] % 2;
c = 1;
}
else
c = 0;
}
}
// function to find the number's complement
void complement(int a[], int n)
{
int i;
int x[8] = {0};
x[0] = 1;
for (i = 0; i < n; i++) {
a[i] = (a[i] + 1) % 2;
}
add(a, x, n);
}
// function to perform right shift
void rightShift(int ac[], int qr[], int qn, int qrn)
{
int temp, i;
temp = ac[0];
qn = qr[0];
printf("\t\t Right Shift\t");
for (i = 0; i < qrn - 1; i++) {
ac[i] = ac[i + 1];
qr[i] = qr[i + 1];
}
qr[qrn - 1] = temp;
}
// function to display operations
void display(int ac[], int qr[], int qrn)
{
int i;
// accumulator content
for (i = qrn - 1; i >= 0; i--) {
printf("%i", ac[i]);
printf("\t"); }
// multiplier content
for (i = qrn - 1; i >= 0; i--)
printf("%i", qr[i]);
}
// Function to implement booth's algo
void boothAlgorithm(int br[], int qr[], int mt[], int qrn, int sc)
{
int qn = 0, ac[10] = { 0 };
int temp = 0;
printf("qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n");
printf("\t\t\tinitial\t\t");
display(ac, qr, qrn);
printf("\t\t %i", sc);
printf("\n");
while (sc != 0) {
printf("%i", qr[0]);
printf("\t%i", qn);
// SECOND CONDITION
if ((qn + qr[0]) == 1)
{
if (temp == 0) {
// subtract BR from accumulator
add(ac, mt, qrn);
printf("\t\tA = A - BR\t");
for (int i = qrn - 1; i >= 0; i--)
printf("%i", ac[i]);
temp = 1;
}
// THIRD CONDITION
else if (temp == 1)
{
// add BR to accumulator
add(ac, br, qrn);
printf("\t\tA = A + BR\t");
for (int i = qrn - 1; i >= 0; i--)
printf("%i",ac[i]);
temp = 0;
}
printf("\n\t");
rightShift(ac, qr, qn, qrn);
}
// FIRST CONDITION
else if (qn - qr[0] == 0)
rightShift(ac, qr, qn, qrn);
display(ac, qr, qrn);
printf("\t");
// decrement counter
sc--;
printf("\t%i", sc);
printf("\n");
}
}
// driver code
int main(int argc, char** arg)
{
int mt[10], sc;
int brn, qrn;
// Number of multiplicand bit
brn = 4;
// multiplicand
int br[] = { 0, 1, 1, 0 };
// copy multiplier to temp array mt[]
for (int i = brn - 1; i >= 0; i--)
mt[i] = br[i];
reverse(br, br + brn);
complement(mt, brn);
// No. of multiplier bit
qrn = 4;
// sequence counter
sc = qrn;
// multiplier
int qr[] = { 1, 0, 1, 0 };
reverse(qr, qr + qrn);
boothAlgorithm(br, qr, mt, qrn, sc);
printf("Result = ");
for (int i = qrn - 1; i >= 0; i--)
printf("%i", qr[i]);
}