-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooth-algorithm.c
More file actions
142 lines (115 loc) · 2.54 KB
/
booth-algorithm.c
File metadata and controls
142 lines (115 loc) · 2.54 KB
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
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int A[SIZE] = {0, 0, 0, 0};
int Q[SIZE] = {0, 1, 0, 1};
int M[SIZE] = {0, 1, 1, 1};
int M_comp[SIZE] = {1, 0, 0, 1};
int Qo = 0;
int count = 4;
int *right_shift(int A[], int Q[], int Qo)
{
int *shift_array = (int *)malloc(((SIZE * 2) + 1) * sizeof(int));
static int i = 0;
int j = 0;
for (i = 0; i < SIZE; i++)
{
shift_array[i] = A[i];
}
for (i = SIZE; i < (SIZE * 2); i++)
{
shift_array[i] = Q[j];
j++;
}
shift_array[i] = Qo;
while (i > 0)
{
shift_array[i] = shift_array[i - 1];
i--;
}
return shift_array;
}
int *sum(int A[], int M[])
{
int i, temp1, temp2, carry = 0, i_mediate_sum;
int *result = (int *)malloc(SIZE * sizeof(int));
for (i = SIZE - 1; i >= 0; i--)
{
temp1 = A[i];
temp2 = M[i];
i_mediate_sum = (temp1 + temp2 + carry) % 2;
carry = (temp1 + temp2 + carry) / 2;
result[i] = i_mediate_sum;
}
return result;
}
int *compare(int A[], int Q[], int M[], int M_comp[], int Qo)
{
int LSB_Q = Q[SIZE - 1];
int check = LSB_Q * 10 + Qo;
int *temp_acc = (int *)malloc(SIZE * sizeof(int));
if (check == 10)
{
temp_acc = sum(A, M_comp);
}
else if (check == 1)
{
temp_acc = sum(A, M);
}
else
return A;
return temp_acc;
}
int main()
{
int *temp_acc;
while(count > 0)
{
static int j = 0;
temp_acc = compare(A, Q, M, M_comp, Qo);
for (int i = 0; i < SIZE; i++)
{
A[i] = temp_acc[i];
}
printf("\n");
printf("A: ");
for (int i = 0; i < SIZE; i++)
{
printf("%d", A[i]);
}
printf(" Q: ");
for (int i = 0; i < SIZE; i++)
{
printf("%d", Q[i]);
}
printf(" Qo: %d", Qo);
printf("\n");
free(temp_acc);
temp_acc = right_shift(A, Q, Qo);
for (j = 0; j < (SIZE); j++)
{
A[j] = temp_acc[j];
}
int i = 0;
for (j = SIZE; j < (SIZE * 2); j++)
{
Q[i] = temp_acc[j];
i++;
}
Qo = temp_acc[j];
printf("A: ");
for (int i = 0; i < SIZE; i++)
{
printf("%d", A[i]);
}
printf(" Q: ");
for (int i = 0; i < SIZE; i++)
{
printf("%d", Q[i]);
}
printf(" Qo: %d", Qo);
printf("\n");
count--;
free(temp_acc);
}
}