-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibvint.c
513 lines (412 loc) · 10.7 KB
/
libvint.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "libvint.h"
vint vint_new(void)
{
vint v = NULL;
v = calloc(1, sizeof(vbyte));
if (v != NULL)
{
v[0] |= END_MARKER;
}
return v;
}
vint vint_from_uint(uint64_t value)
{
const int input_bits = sizeof(value) * 8;
int num_bits = 0;
vint v = vint_new();
int num_vbytes = 1;
int idx_vbit = 0;
int idx_vbyte = 0;
bool bit = false;
if (v == NULL)
{
return NULL;
}
for (int idx = 0; idx < input_bits; idx++)
{
bit = value & (1UL << idx);
idx_vbyte = idx / BITS_PER_VBYTE;
idx_vbit = idx % BITS_PER_VBYTE;
if (bit)
{
/* Not enough vbytes. */
if ((idx_vbyte + 1) > num_vbytes)
{
//++num_vbytes;
num_vbytes = idx_vbyte + 1;
vint_resize(&v, num_vbytes);
}
v[idx_vbyte] |= (1UL << idx_vbit);
}
}
return v;
}
vint vint_from_str(char * value)
{
size_t nchars = 0;
vint v = NULL;
size_t i = 0;
char num[4] = "\0";
int count = 0;
if ((value == NULL) or true)
{
return NULL;
}
nchars = strlen(value);
for (i = nchars; i > 0; i--)
{
//putchar(value[i - 1]);
num[count] = value[i - 1];
++count;
if (count >= 3)
{
count = 0;
}
}
}
int vint_get_size(vint vnum)
{
int nbytes = 0;
if (vnum == NULL)
{
return 0;
}
while ( !(vnum[nbytes] & END_MARKER) )
{
++nbytes;
};
return nbytes + 1;
}
bool vint_erase(vint vnum)
{
if (vnum != NULL)
{
free(vnum);
return true;
}
return false;
}
vint vint_add(vint vnum_a, vint vnum_b)
{
const int len_a = vint_get_size(vnum_a);
const int len_b = vint_get_size(vnum_b);
const int total_vbytes = (len_a > len_b) ? len_a : len_b;
vint result = vint_new();
int result_length = /*1*/total_vbytes;
int idx_vbyte = 0;
bool carry = false;
if (result == NULL)
{
return NULL;
}
vint_resize(&result, result_length);
carry = false;
for (idx_vbyte = 0; idx_vbyte < total_vbytes; idx_vbyte++)
{
int vbyte_a = 0;
int vbyte_b = 0;
if (idx_vbyte < len_a)
{
vbyte_a = vnum_a[idx_vbyte] & ~END_MARKER;
}
if (idx_vbyte < len_b)
{
vbyte_b = vnum_b[idx_vbyte] & ~END_MARKER;
}
for (int nbit = 0; nbit < BITS_PER_VBYTE; nbit++)
{
bool a = vbyte_a & (1 << nbit);
bool b = vbyte_b & (1 << nbit);
if ((a or b or carry) and ((idx_vbyte + 1) > result_length))
{
++result_length;
vint_resize(&result, result_length);
}
if (not carry)
{
if (a and b)
{
carry = true;
}
else if ((a and not b) or (not a and b))
{
result[idx_vbyte] |= (1 << nbit);
}
}
else
{
if (a and b)
{
result[idx_vbyte] |= (1 << nbit);
}
else if ((a and not b) or (not a and b))
{
carry = true;
}
else if (not a and not b)
{
result[idx_vbyte] |= (1 << nbit);
carry = false;
}
}
}
}
if (carry)
{
++result_length;
vint_resize(&result, result_length);
result[result_length - 1] |= 1;
result[result_length - 1] |= END_MARKER;
}
else
{
result[result_length - 1] |= END_MARKER;
}
return result;
}
void vint_bitshift(vint * vnum, int amount)
{
bool is_ovf = false;
bool is_end = false;
const bool is_right_shift = (amount < 0);
const bool is_left_shift = (amount > 0);
int n_vbytes = 0;
int idx_vbyte = 0;
int current_length = 0;
vbyte current_vbyte = 0;
int step = 0;
/* Invalid input or nothing to do. */
if ((vnum == NULL) or (*vnum == NULL) or (amount == 0))
{
return;
}
n_vbytes = vint_get_size(*vnum);
current_length = n_vbytes;
/* Where to begin. */
if (is_right_shift)
{
idx_vbyte = 0;
amount = -amount;
step = -1;
}
if (is_left_shift)
{
idx_vbyte = n_vbytes - 1;
step = 1;
}
while (amount > 0)
{
current_vbyte = (*vnum)[idx_vbyte];
is_end = current_vbyte & END_MARKER;
if (is_right_shift)
{
is_ovf = current_vbyte & 1;
current_vbyte >>= 1;
current_vbyte &= ~(END_MARKER >> 1);
}
if (is_left_shift)
{
current_vbyte <<= 1;
is_ovf = current_vbyte & END_MARKER;
}
if (is_ovf and is_end)
{
(*vnum)[idx_vbyte] = current_vbyte & ~END_MARKER;
(*vnum)[idx_vbyte] |= END_MARKER;
current_vbyte = (*vnum)[idx_vbyte];
/* Right shifts only need resizing if the end marker is the
only 1-bit on the vbyte. */
if (is_left_shift or \
(is_right_shift and (current_vbyte == END_MARKER) and \
(current_length > 1)))
{
current_length += step;
vint_resize(vnum, current_length);
}
/* Placing overflow bits.*/
if (is_left_shift)
{
(*vnum)[current_length - 1] |= 1;
}
if (is_right_shift and (idx_vbyte > 1))
{
(*vnum)[current_length - 1] |= (END_MARKER >> 1);
}
/* Making sure the final vbyte is always terminated. */
(*vnum)[current_length - 1] |= END_MARKER;
}
else if (is_ovf and not is_end)
{
(*vnum)[idx_vbyte] = current_vbyte & ~END_MARKER;
/* Placing overflow bits. */
if (is_left_shift)
{
(*vnum)[idx_vbyte + step] |= 1;
}
if (is_right_shift and (idx_vbyte > 0))
{
(*vnum)[idx_vbyte + step] |= (END_MARKER >> 1);
}
}
else if (not is_ovf and is_end)
{
/*current_vbyte &= ~(END_MARKER >> 1);*/
(*vnum)[idx_vbyte] = current_vbyte & ~END_MARKER;
(*vnum)[idx_vbyte] |= END_MARKER;
}
else if (not is_ovf and not is_end)
{
(*vnum)[idx_vbyte] = current_vbyte;
(*vnum)[idx_vbyte] &= ~END_MARKER;
}
idx_vbyte -= step;
if (is_right_shift)
{
if (idx_vbyte >= current_length)
{
idx_vbyte = 0;
--amount;
}
}
if (is_left_shift)
{
if (idx_vbyte < 0)
{
idx_vbyte = current_length - 1;
--amount;
}
}
}
}
int vint_deepcopy(vint * from, vint * to)
{
int idx = 0;
int len = 0;
bool is_end = false;
if ((from == NULL) or (*from == NULL) or (to == NULL))
{
return 0;
}
len = vint_get_size(*from);
vint_resize(to, len);
idx = 0;
do
{
(*to)[idx] = (*from)[idx];
if ((*from)[idx] & END_MARKER)
{
is_end = true;
}
++idx;
} while (not is_end);
return idx;
}
/* Resizes *vnum so that it will become req_vbytes long. */
void vint_resize(vint * vnum, int req_vbytes)
{
vint new = NULL;
int vbytes = 0;
int num_to_copy = 0;
if ((vnum == NULL) or (*vnum == NULL) or (req_vbytes < 1))
{
return;
}
vbytes = vint_get_size(*vnum);
/* Nothing to do... */
if (vbytes == req_vbytes)
{
return;
}
new = calloc(req_vbytes, sizeof(vbyte));
if (new == NULL)
{
return;
}
num_to_copy = vbytes;
/* If shrinking, must only copy up to new length. */
if (req_vbytes < vbytes)
{
num_to_copy = req_vbytes;
}
/* Copying all data vbytes. */
for (int idx = 0; idx < num_to_copy; idx++)
{
new[idx] = (*vnum)[idx] & ~END_MARKER;
}
/* Marking the end. */
new[req_vbytes - 1] |= END_MARKER;
free(*vnum);
*vnum = new;
}
void vint_print_bits(vint vnum)
{
int vbytes = 0;
int vbits = 0;
bool bit = false;
if (vnum == NULL)
{
return;
}
vbits = sizeof(vnum[0]) * 8;
vbytes = vint_get_size(vnum);
printf("MSB ");
for (int idx_vbyte = vbytes - 1; idx_vbyte >= 0; idx_vbyte--)
{
for (int idx_bit = vbits - 1; idx_bit >= 0; idx_bit--)
{
bit = vnum[idx_vbyte] & (1 << idx_bit);
printf("%d", bit);
}
printf(" ");
}
printf("LSB");
}
vint vint_multiply(vint vnum_a, vint vnum_b)
{
vint result = NULL;
vint tmp = NULL;
vint sum = NULL;
vint * multiplicand = NULL;
vint * multiplier = NULL;
int len_multiplicand = 0;
int len_multiplier = 0;
int n_vbyte = 0;
int n_bit = 0;
int shifts = 0;
if ((vnum_a == NULL) or (vnum_b == NULL))
{
return NULL;
}
len_multiplicand = vint_get_size(vnum_a);
len_multiplier = vint_get_size(vnum_b);
multiplicand = &vnum_a;
multiplier = &vnum_b;
if (len_multiplicand < len_multiplier)
{
multiplicand = &vnum_b;
multiplier = &vnum_a;
int tmp_len = len_multiplicand;
len_multiplicand = len_multiplier;
len_multiplier = tmp_len;
}
/* Partial sums. */
result = vint_new();
for (n_vbyte = 0; n_vbyte < len_multiplier; n_vbyte++)
{
for (n_bit = 0; n_bit < BITS_PER_VBYTE; n_bit++)
{
if ( ((*multiplier)[n_vbyte] & (1 << n_bit)) )
{
tmp = vint_new();
vint_deepcopy(multiplicand, &tmp);
vint_bitshift(&tmp, shifts);
sum = vint_add(tmp, result);
vint_deepcopy(&sum, &result);
vint_erase(tmp);
vint_erase(sum);
}
++shifts;
}
}
return result;
}