/
fib.c
494 lines (397 loc) · 12.5 KB
/
fib.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
/*--------------------------------------------------------------------*/
/* fib.c */
/* Author: Bob Dondero */
/*--------------------------------------------------------------------*/
#include "bigint.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#ifndef S_SPLINT_S
#include <sys/resource.h>
#endif
/*--------------------------------------------------------------------*/
#ifndef S_SPLINT_S
/* Set the process's "CPU time" resource limit. After the CPU
time limit expires, the OS will send a SIGKILL signal to the
process. */
static void setCpuTimeLimit(void)
{
enum {CPU_TIME_LIMIT_IN_SECONDS = 300};
struct rlimit sRlimit;
sRlimit.rlim_cur = CPU_TIME_LIMIT_IN_SECONDS;
sRlimit.rlim_max = CPU_TIME_LIMIT_IN_SECONDS;
setrlimit(RLIMIT_CPU, &sRlimit);
}
#endif
/*--------------------------------------------------------------------*/
/* Create and return a new BigInt object initialized to ul. Exit
with status EXIT_FAILURE if insufficient memory is available. */
static BigInt_T createBigInt(unsigned long ul)
{
BigInt_T oBigInt = BigInt_new(ul);
if (oBigInt == NULL)
{
fprintf(stderr, "Memory allocation error\n");
exit(EXIT_FAILURE);
}
return oBigInt;
}
/*--------------------------------------------------------------------*/
/* Compute and write fib(iNum) to stdout. Write the CPU time
consumed to stderr. */
static void computeFib(int iNum)
{
int i;
BigInt_T oFirstPrev;
BigInt_T oSecondPrev;
BigInt_T oCurrent;
BigInt_T oTemp;
if (iNum < 2)
{
BigInt_T oBigInt = createBigInt((unsigned long)iNum);
printf("Fibonacci number %d:\n", iNum);
BigInt_writeHex(stdout, oBigInt);
putchar('\n');
BigInt_free(oBigInt);
return;
}
oSecondPrev = createBigInt(0);
oFirstPrev = createBigInt(1);
oTemp = createBigInt(0);
oCurrent = NULL;
for (i = 2; i <= iNum; i++)
{
oCurrent = oTemp;
if (! BigInt_add(oFirstPrev, oSecondPrev, oCurrent))
{
fprintf(stderr, "Addition overflow\n");
exit(EXIT_FAILURE);
}
oTemp = oSecondPrev;
oSecondPrev = oFirstPrev;
oFirstPrev = oCurrent;
}
printf("Fibonacci number %d:\n", iNum);
BigInt_writeHex(stdout, oCurrent);
putchar('\n');
BigInt_free(oTemp);
BigInt_free(oFirstPrev);
BigInt_free(oSecondPrev);
}
/*--------------------------------------------------------------------*/
/* Boundary test the BigInt_add function. */
static void boundaryTest(void)
{
{ /* 0 + 0 */
BigInt_T oFirst = createBigInt(0);
BigInt_T oSecond = createBigInt(0);
BigInt_T oSum = createBigInt(0);
printf("Boundary test 1: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* Clear of sum required. */
BigInt_T oFirst = createBigInt(0);
BigInt_T oSecond = createBigInt(0);
BigInt_T oSum = createBigInt(0);
BigInt_assignFromHexStr(oSum, "100000000");
/* This addition should clear element 1 of oSum. */
printf("Boundary test 2a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_assignFromHexStr(oFirst, "100000000");
BigInt_assignFromHexStr(oSecond, "0");
/* This addition will fail if element 1 of oSum wasn't cleared. */
printf("Boundary test 2b: ");
fflush(stdout);
if (BigInt_add(oFirst, oSum, oSecond))
BigInt_writeHex(stdout, oSecond);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* fffffffffffffffe + 0, 0 + fffffffffffffffe */
BigInt_T oFirst = createBigInt(ULONG_MAX - 1);
BigInt_T oSecond = createBigInt(0);
BigInt_T oSum = createBigInt(0);
printf("Boundary test 3a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 3b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* fffffffffffffffe + 1, 1 + fffffffffffffffe */
BigInt_T oFirst = createBigInt(ULONG_MAX - 1);
BigInt_T oSecond = createBigInt(1);
BigInt_T oSum = createBigInt(0);
printf("Boundary test 4a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 4b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* ffffffffffffffff + 0, 0 + ffffffffffffffff */
BigInt_T oFirst = createBigInt(ULONG_MAX);
BigInt_T oSecond = createBigInt(0);
BigInt_T oSum = createBigInt(0);
printf("Boundary test 5a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 5b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* ffffffffffffffff + 1, 1 + ffffffffffffffff */
BigInt_T oFirst = createBigInt(ULONG_MAX);
BigInt_T oSecond = createBigInt(1);
BigInt_T oSum = createBigInt(0);
printf("Boundary test 6a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 6b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* ffffffffffffffff0000000000000001 + ffffffffffffffff,
ffffffffffffffff + ffffffffffffffff0000000000000001 */
BigInt_T oFirst = createBigInt(0);
BigInt_T oSecond = createBigInt(ULONG_MAX);
BigInt_T oSum = createBigInt(0);
BigInt_assignFromHexStr(oFirst,
"ffffffffffffffff0000000000000001");
printf("Boundary test 7a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 7b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHex(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* largest possible + 0, 0 + largest possible */
BigInt_T oFirst = createBigInt(0);
BigInt_T oSecond = createBigInt(0);
BigInt_T oSum = createBigInt(0);
BigInt_largest(oFirst);
printf("Boundary test 8a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHexAbbrev(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 8b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHexAbbrev(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* largest possible + 1, 1 + largest possible */
BigInt_T oFirst = createBigInt(0);
BigInt_T oSecond = createBigInt(1);
BigInt_T oSum = createBigInt(0);
BigInt_largest(oFirst);
printf("Boundary test 9a: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHexAbbrev(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
printf("Boundary test 9b: ");
fflush(stdout);
if (BigInt_add(oSecond, oFirst, oSum))
BigInt_writeHexAbbrev(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
{ /* largest possible + largest possible */
BigInt_T oFirst = createBigInt(0);
BigInt_T oSecond = createBigInt(0);
BigInt_T oSum = createBigInt(0);
BigInt_largest(oFirst);
BigInt_largest(oSecond);
printf("Boundary test 10: ");
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHexAbbrev(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
}
/*--------------------------------------------------------------------*/
/* Stress test the BigInt_add function. */
static void stressTest(void)
{
/* The number of stress test additions to perform. */
enum {STRESS_TEST_COUNT = 20}; /* Arbitrary */
BigInt_T oFirst;
BigInt_T oSecond;
BigInt_T oSum;
int i;
/* Create three BigInt objects. */
oFirst = createBigInt(0);
oSecond = createBigInt(0);
oSum = createBigInt(0);
/* Perform stress tests. */
for (i = 0; i < STRESS_TEST_COUNT; i++)
{
/* Assign random numbers to the three BigInt objects. */
BigInt_random(oFirst);
BigInt_random(oSecond);
BigInt_random(oSum);
/* Compute and write a sum. */
printf("Stress test %da: ", i);
fflush(stdout);
if (BigInt_add(oFirst, oSecond, oSum))
BigInt_writeHexAbbrev(stdout, oSum);
else
printf("Addition overflow");
putchar('\n');
/* Compute and write a sum using the previous sum. */
BigInt_random(oFirst);
BigInt_random(oSecond);
printf("Stress test %db: ", i);
fflush(stdout);
if (BigInt_add(oFirst, oSum, oSecond))
BigInt_writeHexAbbrev(stdout, oSecond);
else
printf("Addition overflow");
putchar('\n');
}
/* Free the BigInt objects. */
BigInt_free(oSum);
BigInt_free(oSecond);
BigInt_free(oFirst);
}
/*--------------------------------------------------------------------*/
/* First write fibonacci number n to stdout, where n is provided as
argv[1]. n is expressed in decimal notation. The fibonacci number
is expressed in hexadecimal notation. Write the CPU time consumed
to stderr.
Then boundary test and stress test the BigInt_add() function by
performing some computations and writing the results to stdout.
As always, argc is the argument count. Return 0 iff successful. */
int main(int argc, char *argv[])
{
int iNum;
clock_t iInitialClock;
clock_t iFinalClock;
/* Validate the command line argument, and assign it to iNum. */
if (argc != 2)
{
fprintf(stderr, "Usage: %s nonneginteger\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[1], "%d", &iNum) != 1)
{
fprintf(stderr, "Argument must be an integer\n");
exit(EXIT_FAILURE);
}
if (iNum < 0)
{
fprintf(stderr, "Argument must be non-negative\n");
exit(EXIT_FAILURE);
}
#ifndef S_SPLINT_S
setCpuTimeLimit();
#endif
printf("------------------------------------------------\n");
/* Compute and write fibonacci number iNum. */
iInitialClock = clock();
computeFib(iNum);
iFinalClock = clock();
fprintf(stderr, "CPU time: %f seconds\n",
((double)(iFinalClock - iInitialClock)) / CLOCKS_PER_SEC);
printf("------------------------------------------------\n");
/* Perform boundary tests. */
boundaryTest();
printf("------------------------------------------------\n");
/* Perform stress tests. */
stressTest();
printf("------------------------------------------------\n");
return 0;
}