-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbig_integer.c
460 lines (383 loc) · 11.3 KB
/
big_integer.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
/*
** big_integer.c
** Description: "Arbitrary"-precision integer
** Author: Andre Azevedo <http://github.com/andreazevedo>
**/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include "macros.h"
#include "big_integer.h"
/*#define UINT_NUM_BITS (sizeof(unsigned int) * 8)*/
const int UINT_NUM_BITS = (sizeof(unsigned int) * 8);
/* PRIVATE FUNCTIONS DECLARATIONS */
BigIntegerData big_integer_empty_data( );
BigIntegerData big_integer_create_data( const unsigned int bits[], const int length );
BigInteger big_integer_create_internal( const char sign, const BigIntegerData data );
void big_integer_normalize( BigIntegerData *pBigIntData );
void big_integer_normalize_from( BigIntegerData *pBigIntData, const int from );
void big_integer_clear_trash_data( BigIntegerData *pBigIntData );
void big_integer_report_overflow();
int big_integer_compare_data( const BigIntegerData *pLeft, const BigIntegerData *pRight );
int big_integer_compare_data_uint( const BigIntegerData *pBigIntData, unsigned int value );
BigIntegerData big_integer_add_data( const BigIntegerData left, const BigIntegerData right );
BigIntegerData big_integer_subtract_data( const BigIntegerData left, const BigIntegerData right );
void big_integer_increment_data( BigIntegerData *pBigIntData, const unsigned int value );
void big_integer_decrement_data( BigIntegerData *pBigIntData, const unsigned int value );
/* PRIVATE FUNCTIONS IMPLEMENTATION */
BigIntegerData big_integer_empty_data( )
{
BigIntegerData bigIntData;
bigIntData.length = 0;
big_integer_clear_trash_data( &bigIntData );
return bigIntData;
};
BigIntegerData big_integer_create_data( const unsigned int bits[], const int length )
{
if ( length > BIG_INTEGER_DATA_MAX_SIZE )
{
big_integer_report_overflow();
abort();
exit( EXIT_FAILURE );
}
BigIntegerData bigIntData;
if (bits && length > 0)
memcpy( bigIntData.bits, bits, sizeof(unsigned int) * length );
bigIntData.length = length;
big_integer_clear_trash_data( &bigIntData );
return bigIntData;
};
BigInteger big_integer_create_internal( const char sign, const BigIntegerData data )
{
BigInteger bigInt;
bigInt.sign = sign;
bigInt.data = data;
return bigInt;
};
void big_integer_normalize( BigIntegerData *pBigIntData )
{
big_integer_normalize_from( pBigIntData, BIG_INTEGER_DATA_MAX_SIZE-1 );
};
void big_integer_normalize_from( BigIntegerData *pBigIntData, const int from )
{
int i;
for ( i = from; i >= 0; --i )
{
if ( pBigIntData->bits[i] != 0 )
{
pBigIntData->length = i + 1;
break;
}
}
};
void big_integer_clear_trash_data( BigIntegerData *pBigIntData )
{
int i;
for ( i = pBigIntData->length; i < BIG_INTEGER_DATA_MAX_SIZE; ++i )
pBigIntData->bits[i] = 0;
};
void big_integer_report_overflow()
{
fprintf(stderr, "BigInteger reported overflow!\n");
};
int big_integer_compare_data( const BigIntegerData *pLeft, const BigIntegerData *pRight )
{
/* if the lengths are different */
if ( pLeft->length > pRight->length )
return 1;
if ( pLeft->length < pRight->length )
return -1;
int length = pLeft->length;
int i;
for ( i = (length-1); i >= 0; --i)
{
if ( pLeft->bits[i] > pRight->bits[i] )
return 1;
if ( pLeft->bits[i] < pRight->bits[i] )
return -1;
}
return 0;
};
int big_integer_compare_data_uint( const BigIntegerData *pBigIntData, unsigned int value )
{
if ( pBigIntData->length == 0 )
return -1;
if ( pBigIntData->length > 1 )
return 1;
if ( pBigIntData->bits[0] > value )
return 1;
else if ( pBigIntData->bits[0] < value )
return -1;
return 0;
};
BigIntegerData big_integer_add_data( const BigIntegerData left, const BigIntegerData right )
{
int uIntNumBits = UINT_NUM_BITS;
BigIntegerData result = big_integer_empty_data( );
int len = MAX( left.length, right.length );
unsigned long long sum = 0;
int i;
for ( i = 0; i < len; ++i )
{
sum += (unsigned long long) left.bits[i] + right.bits[i];
result.bits[i] = (unsigned int) sum;
sum >>= uIntNumBits;
}
if ( sum > 0 )
{
result.bits[i] = (unsigned int) sum;
i++;
}
result.length = i;
return result;
};
/* left > right always */
BigIntegerData big_integer_subtract_data( const BigIntegerData left, const BigIntegerData right )
{
BigIntegerData result = big_integer_empty_data( );
int len = MAX( left.length, right.length );
unsigned long long borrow = 0;
int i;
for ( i = 0; i < len; ++i )
{
/* what happens here is that, if left is less than right, borrow will become
"negative" (not really because it is unsigned), and the bit pattern for that is
the 1's complement (complementing it to get to 0), which is exactly the remainder
of this term in the subtraction. */
borrow = (unsigned long long) left.bits[i] - right.bits[i] - borrow;
result.bits[i] = (unsigned int) borrow;
/* here we just want the first 1 after removing the lower order term */
borrow = (borrow >> UINT_NUM_BITS) & 1;
}
big_integer_normalize_from( &result, i );
return result;
};
void big_integer_increment_data( BigIntegerData *pBigIntData, const unsigned int value )
{
unsigned long long carry = value;
int i = 0;
while ( carry > 0 )
{
carry += (unsigned long long) pBigIntData->bits[i];
pBigIntData->bits[i] = (unsigned int) carry;
carry >>= UINT_NUM_BITS;
++i;
}
if ( i > pBigIntData->length )
pBigIntData->length = i;
};
/* pBigIntData > value */
void big_integer_decrement_data( BigIntegerData *pBigIntData, const unsigned int value )
{
unsigned long long borrow = value;
int i = 0;
while ( borrow > 0 )
{
borrow = (unsigned long long) pBigIntData->bits[i] - borrow;
pBigIntData->bits[i] = (unsigned int) borrow;
borrow = (borrow >> UINT_NUM_BITS) & 1;
++i;
}
big_integer_normalize_from( pBigIntData, i );
};
/* PUBLIC FUNCTIONS IMPLEMENTATION */
BigInteger big_integer_create( long long value )
{
BigInteger bigInt;
int numBits = UINT_NUM_BITS;
if ( value == 0 )
{
bigInt.sign = 0;
bigInt.data.bits[0] = 0;
bigInt.data.length = 1;
}
else
{
unsigned long long uValue;
if ( value < 0 )
{
bigInt.sign = -1;
uValue = (unsigned long long) -value;
}
else
{
bigInt.sign = 1;
uValue = (unsigned long long) value;
}
bigInt.data.length = 0;
while ( uValue > 0 )
{
bigInt.data.bits[bigInt.data.length++] = (unsigned int) uValue;
uValue >>= numBits;
}
}
big_integer_clear_trash_data( &bigInt.data );
return bigInt;
};
int big_integer_to_int( const BigInteger bigInt )
{
if ( bigInt.sign == 0 )
return 0;
/* overflow check */
if ( bigInt.data.length > 1 ||
(bigInt.sign == 1 && bigInt.data.bits[0] > INT_MAX) ||
(bigInt.sign == -1 && -(bigInt.data.bits[0]) < INT_MIN) )
{
big_integer_report_overflow();
abort();
exit( EXIT_FAILURE );
}
if ( bigInt.sign == -1 )
return -(int)bigInt.data.bits[0];
return (int)bigInt.data.bits[0];
};
long long big_integer_to_long_long( const BigInteger bigInt )
{
if ( bigInt.sign == 0 )
return 0;
int uIntNumBits = UINT_NUM_BITS;
int maxLength = sizeof(long long) / sizeof(unsigned int);
if ( bigInt.data.length > maxLength )
{
big_integer_report_overflow();
abort();
exit( EXIT_FAILURE );
}
unsigned long long result = 0;
int i = 0;
for ( i = 0; i < bigInt.data.length; ++i )
{
result |= ((unsigned long long)bigInt.data.bits[i]) << (uIntNumBits * i);
}
if ( bigInt.sign == -1 )
return -(long long)result;
return result;
};
int big_integer_compare( const BigInteger left, const BigInteger right )
{
/* if one is positive and the other negative */
if ( left.sign > right.sign )
return 1;
if ( left.sign < right.sign )
return -1;
/* if they have the same sign */
char sign = left.sign;
return sign * big_integer_compare_data( &left.data, &right.data );
};
BigInteger big_integer_add( const BigInteger left, const BigInteger right )
{
if ( left.sign == 0 )
return right;
if ( right.sign == 0 )
return left;
if ( left.sign == right.sign )
return big_integer_create_internal( left.sign, big_integer_add_data( left.data, right.data ));
/* compare the MOD of the numbers */
int compRes = big_integer_compare_data( &left.data, &right.data );
if ( compRes == 0 )
return big_integer_create( 0 );
if ( compRes > 0 ) /* left > right */
return big_integer_create_internal( left.sign, big_integer_subtract_data( left.data, right.data ));
return big_integer_create_internal( right.sign, big_integer_subtract_data( right.data, left.data ));
};
BigInteger big_integer_subtract( const BigInteger left, const BigInteger right )
{
if ( left.sign == 0 )
return big_integer_create_internal( -right.sign, right.data );
if ( right.sign == 0 )
return left;
if ( left.sign != right.sign )
return big_integer_create_internal( left.sign, big_integer_add_data(left.data, right.data) );
/* compare the MOD of the numbers */
int compRes = big_integer_compare_data( &left.data, &right.data );
if ( compRes == 0 )
return big_integer_create( 0 );
if ( compRes > 0 ) /* left > right */
return big_integer_create_internal( left.sign, big_integer_subtract_data(left.data, right.data) );
return big_integer_create_internal( -right.sign, big_integer_subtract_data(right.data, left.data) );
};
void big_integer_increment( BigInteger *bigInt, const unsigned int value )
{
if ( bigInt->sign >= 0 ) /* bigInt >= 0 */
{
if ( bigInt->sign == 0 && value > 0 )
bigInt->sign = 1;
big_integer_increment_data( &bigInt->data, value );
}
else /* bigInt < 0 */
{
int compRes = big_integer_compare_data_uint( &bigInt->data, value );
if ( compRes == 0 ) /* |bigInt| == |value| */
{
bigInt->sign = 0;
bigInt->data.length = 0;
big_integer_clear_trash_data( &bigInt->data );
}
else if ( compRes > 0 ) /* |bigInt| > |value| */
{
big_integer_decrement_data( &bigInt->data, value );
}
else /* |bigInt| < |value| */
{
#ifdef DEBUG
/* |bigInt| < |value| implies that bigInt has length 1
because value, if expressed as a BigInteger, would have length 1. */
assert( bigInt->data.length == 1 );
#endif
bigInt->sign = 1;
bigInt->data.bits[0] = value - bigInt->data.bits[0];
}
}
};
void big_integer_decrement( BigInteger *bigInt, const unsigned int value )
{
if ( bigInt->sign <= 0 )/* bigInt <= 0 */
{
if ( bigInt->sign == 0 && value > 0 )
bigInt->sign = -1;
big_integer_increment_data( &bigInt->data, value );
}
else /* bigInt > 0 */
{
int compRes = big_integer_compare_data_uint( &bigInt->data, value );
if ( compRes == 0 ) /* |bigInt| == |value| */
{
bigInt->sign = 0;
bigInt->data.length = 0;
big_integer_clear_trash_data( &bigInt->data );
}
else if ( compRes > 0 ) /* |bigInt| > |value| */
{
big_integer_decrement_data( &bigInt->data, value );
}
else /* |bigInt| < |value| */
{
#ifdef DEBUG
/* |bigInt| < |value| implies that bigInt has length 1 or less
because value, if expressed as a BigInteger, would have length 1. */
assert( bigInt->data.length == 1 );
#endif
bigInt->sign = -1;
bigInt->data.bits[0] = value - bigInt->data.bits[0];
}
}
};
#ifdef DEBUG
void big_integer_dump( const BigInteger bigInt )
{
printf("BigInteger:\n");
printf("Sign: %d\n", (int)bigInt.sign);
printf("Data: { ");
if ( bigInt.data.length > 0 )
{
int i;
for ( i = 0; i < (bigInt.data.length - 1); i++ )
printf("%u, ", bigInt.data.bits[i]);
printf("%u ", bigInt.data.bits[bigInt.data.length-1]);
}
printf("}\n");
printf("Length: %d\n", bigInt.data.length);
}
#endif