-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmath.c
165 lines (136 loc) · 3.66 KB
/
bitmath.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "program_counter.h"
#include "bitmath.h"
long long signed_byte_value(int bit_array[], int size)
{
int is_signed = bit_array[size-1];
long long pow = 1;
long long output = 0;
for(int i=0; i<size-1; i++)
{
output += bit_array[i]*pow;
pow*=2;
}
if(is_signed)
output*=-1;
return output;
}
int* create_byte_value(long value, int out_size)
{
int is_negative = value < 0 ? 1 : 0;
if(is_negative)
value*=-1;
int *byte_array = malloc(sizeof(int)*out_size);
int offset=1;
for(int i=0; i<out_size; i++)
{
byte_array[i] = (value & offset) >> i;
offset*=2;
}
byte_array[out_size-1] = is_negative;
return byte_array;
}
void byte_value_to_string(int value[],char* output, int size)
{
for(int i=0; i<size; i++)
output[i] = (char) (value[i]+48);
}
void byte_value_from_string(char* input, int* output,int length)
{
for(int i=0; i<length; i++)
output[i] = (input[i] == '0') ? 0 : 1;
}
void add_accumulator(struct program_counter *pc, long add_val)
{
long init_val = signed_byte_value(pc->accumulator,ACCUMULATOR_SIZE);
long new_val = init_val + add_val;
memcpy(pc->accumulator,create_byte_value(new_val,ACCUMULATOR_SIZE),ACCUMULATOR_SIZE);
}
// returns remainder
int add_bit_array(int value_a[], int value_b[],int* output, int size)
{
if(output != NULL)
free(output);
output = malloc(sizeof(int)*size);
int carry = 0;
for(int i=0; i<size; i++)
{
if(value_a[i]+value_b[i] == 0)
{
if(carry > 0)
{
output[i] = 1;
carry--;
}
else
output[i] = 0;
}
else if(value_a[i] == value_b[i] && value_a[i] == 1)
{
if(carry == 1)
{
output[i] = 1;
}
else
output[i] =0;
carry = 1;
}
else if(value_a[i] != value_b[i])
{
if(carry == 1)
{
output[i] = 0;
}
else
output[i] = 1;
}
}
if(carry)
{
return 1;
}
return 0;
}
void and_bit_array(int value_a[], int value_b[], int *output, int size)
{
for(int i=0; i<size; i++)
if(value_a[i] == value_b[i] && value_a[i] == 1)
output[i] = 1;
else
output[i] = 0;
}
void shift_bit_array(int* array, int size, int shift_amount, int shift_left)
{
if(shift_left)
for(int i=size-1; i>=0; i--)
{
if(i-shift_amount >=0)
array[i] = array[i-shift_amount];
else
array[i] = 0;
}
else
for(int i=0; i < size; i++)
if((i + shift_amount) < size)
array[i] = array[i+shift_amount];
else
array[i] = 0;
}
int* combine_multiplier_accumulator(struct program_counter *pc)
{
int *temp_multiple_val = (int*) malloc((ACCUMULATOR_SIZE+MULTIPLIER_QUOTIENT_SIZE)*sizeof(int));
for(int i=0; i<ACCUMULATOR_SIZE; i++)
temp_multiple_val[i] = pc->accumulator[i];
for(int i=0; i<MULTIPLIER_QUOTIENT_SIZE; i++)
temp_multiple_val[ACCUMULATOR_SIZE + i] = pc->multiplier_quotient[i];
return temp_multiple_val;
}
long long compute_multiplier_accumulator(struct program_counter *pc)
{
int *combined = combine_multiplier_accumulator(pc);
long long output = signed_byte_value(combined,ACCUMULATOR_SIZE + MULTIPLIER_QUOTIENT_SIZE);
free(combined);
return output;
}