Page 227 - ARM 64 Bit Assembly Language
P. 227
Integer mathematics 215
48 struct bigint_struct {
49 chunk *blks; /* pointer to array of bit chunks */
50 int size; /* number of chunks in the array */
51 };
52
53 /* Private function prototypes */
54 static bigint bigint_adc(bigint l, bigint r, chunk carry);
55 static bigint bigint_shift_left_chunk(bigint l, int chunks);
56 static bigint bigint_shift_right_chunk(bigint l, int chunks);
57 static bigint bigint_trim(bigint b);
58 static bigint bigint_mul_uint(bigint l, chunk r);
59 static bigint bigint_extend(bigint b, int nchunks);
60 static unsigned bigint_smallmod(bigint b, chunk num);
61
62 /******************************************************************/
63 /* Initialization, conversion, and copy functions */
64 /******************************************************************/
65
66 /* convert string to bigint */
67 bigint bigint_from_str(char *s) {
68 bigint d;
69 bigint power;
70 bigint ten;
71 bigint tmp;
72 bigint currprod;
73 int i, negative = 0;
74 d = bigint_from_int(0);
75 ten = bigint_from_int(10);
76 power = bigint_from_int(1);
77 if (*s == ’-’) {
78 negative = 1;
79 s++;
80 }
81 for (i = strlen(s)-1; i >= 0; i--) {
82 if (!isdigit(s[i])) {
83 fprintf(stderr,"Cannot convert string to bigint\n");
84 exit(1);
85 }
86 tmp = bigint_from_int(s[i]-’0’);
87 currprod = bigint_mul(tmp,power);
88 bigint_free(tmp);
89 tmp = bigint_adc(currprod,d,0);
90 bigint_free(d);
91 d = tmp;
92 bigint_free(currprod);
93 if (i > 0) {
94 tmp = bigint_mul(power,ten);
95 bigint_free(power);
96 power = tmp;