-
Notifications
You must be signed in to change notification settings - Fork 0
/
psr_apmath.c
248 lines (178 loc) · 4.39 KB
/
psr_apmath.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Arbitrary precision add/sub multiply/divider
#include <stdio.h>
#include <assert.h>
#include "apmath.h"
/*
APC *apnew(int size)
APC *apcopy(APC *inapc)
APC *apdestroy(APC *desapc)
int apadd(APC *answer, APC *add1, APC *add2)
answer = add1 + add2
int apsub(APC *anwser, APC *sub1, APC *sub2)
answer = sub1 - sub2
int apmul(APC *product, APC *mul1, APC *mul2)
product = mul1 * mul2;
int abdiv(APC *div, APC *rem, APC *quot, APC *div)
div = quot / div; (if div=NULL, not returned)
rem = quot % div; (if rem=NULL, not returned)
int ab2csignext(apc *num, int numtot, int numsign)
Sign extend number to make 2s compliment
int ab2unsign(apc *num, int numtot, int *numsign)
Remove sign extension and set the sign
int abcmp(apc *num1, apc *num2, int ignsign)
+1,0,-1 if num1 > num2, ignore sign in calg if ignsign set
int abeq(apc *num1, apc *num2, int ignsign)
True if num1 == num2, ignore sign in calg if ignsign set
*/
/*
How do we do this again?
Addition (base 10)
111
+ 1234 + 6789
+ 4321 + 9876
------ ------
+ 5555 16665
2
+ 2315 + 1234
- 2222 - 4567
------ ------
+ 0093
same sign = a + b sign is same
diff sign max - min sign is max
special case: zero is positive
Multiplication
011
112
122
122
1234
* 4567
------
8638
7404o
6170oo
4936ooo
-------
5635678
Division
00536
_______
23 ) 12345
115oo
-------
845
69o
-------
155
138
-------
17 <-- Remainder
Signs easy on mult or div
+ * + = +, + / + = +
+ * - = -, - * + = -, + / - = -, - / + = -
- * - = +, - / - = +
So, we do this exactly the same way, but base 256 instead of 10
*/
// AP Constructor
APC *apnew(int nsize)
{
char *innew;
APC *newapc;
int i;
newapc = (APC *) malloc(sizeof(APC));
assert(newapc);
newapc->bytesize = nsize;
newapc->signb = 0;
newapc->extend = 0;
newapc->vals = (unsigned char *)
malloc(sizeof (unsigned char) * nsize);
assert(newapc->vals);
for (i=0; i<nsize; i++)
newapc->vals[i] = 0;
return(newapc);
}
// AP Copier
APC *apcopy(APC *inapc)
{
char *innew;
APC *newapc;
int i,nsize;
newapc = (APC *) malloc(sizeof(APC));
assert(newapc);
nsize = newapc->bytesize = inapc->bytesize;
newapc->signb = inapc->signb;
newapc->extend = inapc->extend;
newapc->vals = (unsigned char *)
malloc(sizeof (unsigned char) * nsize);
assert(newapc->vals);
for (i=0; i<nsize; i++)
newapc->vals[i] = inapc->vals[i];
return(newapc);
}
// AP Destructor
void apdestroy(APC *desapc)
{
if (desapc==NULL) return;
assert(desapc->vals); // Should never be NULL
free(desapc->vals);
free(desapc);
}
// Check for equality
int abeq(APC *num1, APC *num2, int ignsign)
{
int i,m,m1,m2;
assert(num1->extend==0 && num2->extend==0); // Never call with 2c
if (!ignsign)
if (num1->signb != num2->signb) return(0);
m1 = num1->bytesize;
m2 = num2->bytesize;
m = max(m1, m2);
for (i=0; i<m; i++) {
if (i>=m1 && num2->vals[i]) return(0);
if (i>=m2 && num1->vals[i]) return(0);
}
return(1);
}
// Check for magnitude
//
// if ignsign
// if abeq(num1,num2) then return 0
// if num1 > num2 then return 1 else return -1
// else
// if num1 is +, num2 is -, return 1
// if num2 is -, num2 is +, return -1
// if abeq(num1,num2) return 0
// if both pos and num1 > num2 then return 1 else -1
// if both neg and num2 < num1 then return 1 else -1
int abcmp(APC *num1, APC *num2, int ignsign)
{
int i,m,m1,m2,big;
assert(num1->extend==0 && num2->extend==0); // Never call with 2c
if (!ignsign) {
if (num1->signb==0 && num2->signb==1) return(1);
else return(-1);
if (num1->signb==1 && num2->signb==0) return(-1);
else return(1);
}
i = abeq(num1,num2,ignsign);
if (i) return(0);
m1 = num1->bytesize;
m2 = num2->bytesize;
m = max(m1, m2);
big = 1;
for (i=0; i<m; i++) {
if (i>=m1 && num2->vals[i]) {
big = 0;
break;
}
if (i>=m2 && num1->vals[i]) {
big = 1;
break;
}
}
if (!ignsign && (num1->signb && num2->signb)) big = !big;
if (big)
return(1);
else
return(-1);
}